Skip to content

Instantly share code, notes, and snippets.

@shovon
Created December 8, 2014 20:05
/*
* Represents a complex number.
*
* @param real is a number representing the real component of the complex number
* @param imaginary is a number representing the imaginary component of the
* complex number
*/
module.exports = Complex;
function Complex(real, imaginary) {
this._real = real;
this._imag = imaginary;
}
/*
* @returns a number representing the real component of the complex number
* represented by this instance
*/
Complex.prototype.getReal = function () {
return this._real;
};
/*
* @returns a number representing the imaginary component of the complex number
* represented by this instance
*/
Complex.prototype.getImaginary = function () {
return this._imag;
}
/*
* Gets the modulus of the complex number. The modulus is √(real^2 + imag^2).
* Another word for the modulus is "amplitude"
*
* @returns a number representing the modulus of the complex number represented
* by this instance
*/
Complex.prototype.getModulus = function () {
return Math.sqrt(this._real*this._real + this._imag*this._imag);
};
/*
* Gets the argument of the complex number. The argument is the angle formed by
* the real and imaginary pair, where real represents the horizontal magnitude,
* and the imaginary component represents the vertical magnitude. Another word
* for argument is "phase"
*
* @returns a number representing the argument of the complex number represented
* by this instance
*/
Complex.prototype.getArgument = function () {
return Math.atan2(this._imag, this._real);
};
/*
* Gets the conjugate of the complex number. The conjugate is simply the
* original complex number, but with the coefficient of the imaginary component
* negated
*
* @returns an instance of the Complex class, representing the conjugate of this
* instance
*/
Complex.prototype.getConjugate = function () {
return new Complex(this._real, -this._imag);
};
/*
* Gets the sum of the complex number (represented by this instance) and another
* complex number. The left summand is represented by this instance, where as
* the right summand is represented by the passed-in parameter.
*
* @param summand an instance of the Complex class representing the right
* summand
* @returns an instance of the Complex class, reprsenting the sum of the complex
* number (represented by this instance) and another complex number
*/
Complex.prototype.add = function (summand) {
return Complex.add(this, summand);
};
/*
* Gets the difference between this complex number and another complex number.
* The minuend is represented by this instance, where as the subtrahend is
* represented by the passed-in parameter.
*
* @param num an instance of the Complex class representing the subtrahend
* @returns an instance of the Complex class, representing the difference
* between the complex number (represented by this instance) and another
* complex number
*/
Complex.prototype.subtract = function (subtrahend) {
return Complex.subtract(this, subtrahend);
};
/*
* Gets the product of this complex number and another complex number. The left
* multiplicand is represented by this instance, where as the right multiplicand
* is represented by the passed-in parameter
*
* @param multiplicand an instance of the Complex class representing the right
* multiplicand
* @returns an instance of the Complex class, representing the product between
* this instance (representing the left multiplicand) and a right multiplicand
*/
Complex.prototype.multiply = function (multiplicand) {
return Complex.multiply(this, multiplicand);
};
/*
* Gets the quotient of this complex number. The dividend is represented by this
* instance, where as the divisor is represented by the passed-in parameter
*
* @param divisor an instance of the Complex class representing the divisor
* @returns an instance of the Complex class, representing the quotiont of this
* instance (representing the dividend) and the divisor
*/
Complex.prototype.divide = function (divisor) {
return Complex.divide(this, num);
}
/*
* @returns a string representation of the complex number represented by this
* instance of the Complex class.
*/
Complex.prototype.toString = function () {
if (this._real === 0 && this._imag !== 0) {
return this._imag + 'i';
} else if (this._imag === 0 && this._real !== 0) {
return this._real.toString();
} else if (this._imag === 0 && this._real === 0) {
return 0;
}
return (
this._real.toString() +
(this._imag < 0 ? ' - ' + this._imag : ' + ' + this._imag) +
'i'
);
};
/*
* Gets the sum of two complex numbers
*
* @param leftSummand an instance of the Complex class, representing the left
* summand
* @param rightSummand an instance of the Complex class, representing the right
* summand
* @returns an instance of the Complex number representing the sum of the left
* and right summand
*/
Complex.add = function (leftSummand, rightSummand) {
return new Complex(
leftSummand.getReal() + rightSummand.getReal(),
leftSummand.getImaginary() + rightSummand.getImaginary()
);
};
/*
* Gets the difference of two complex numbers.
*
* @param minuend an instance of the Complex class, representing the minuend
* @param subtrahend an instance of the Complex class, representing the
* subtrahend
* @returns an instance of the Complex class representing the difference of the
* minuend and subtrahend
*/
Complex.subtract = function (minuend, subtrahend) {
return new Complex(
minuend.getReal() - subtrahend.getReal(),
minuend.getImaginary() - subtrahend.getImaginary()
);
};
/*
* Gets the product of two complex numbers.
*
* @param leftMultiplicand an instance of the Complex class, representing the
* left multplicand
* @param rightMultplicand an instance of the Complex class, representing the
* right multplicand
* @returns an instance of the Complex class, representing the product of the
* left and right multplicand
*/
Complex.multiply = function (leftMultiplicand, rightMultiplicand) {
return new Complex(
leftMultiplicand.getReal()*rightMultiplicand.getReal() -
leftMultiplicand.getImaginary()*rightMultiplicand.getImaginary(),
leftMultiplicand.getReal()*rightMultiplicand.getImaginary() +
rightMultiplicand.getReal()*leftMultiplicand.getImaginary()
)
};
/*
* Gets the quotient of two complex numbers.
*
* @param dividend an instance of the Complex class, representing the dividend
* @param divisor an instance of the Complex class, representing the divisor
* @returns an instance of the Complex class, representing the quotient of the
* dividend and divisor
*/
Complex.divide = function (dividend, divisor) {
var nominator = dividend.multiply(b.getConjugate())
var denominator =
Math.pow(divisor.getReal(),2 ) + Math.pow(divisor.getImaginary(), 2);
return new Complex(
nominator.getReal() / denominator,
nominator.getImaginary() / denominator
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment