A JavaScript complex number class that allows you to do most of the core mathematical operations with complex numbers, including addition, subtraction, multiplication, and getting the modulus of a complex number.
Since forking this project from Jan Hartigan, I have made a few changes. I have divided the project into two objects, ComplexNumber and ComplexMath.
/* The real part of the complex number * * @type Number */ real: 0, /* The imaginary part of the complex number * * @type Number */ imaginary: 0,
A complex number is made up of two parts: a real and an imaginary parts. In the complex number 3 + 5i, for example, the real part is 3 and the imaginary part 5.
Creating a ComplexNumber is done by passing in two parameters: 1) real part and 2) imaginary part:
var complex = new ComplexNumber(3, 5); complex.toString(); //returns "3 + 5i"
The ComplexNumber object holds both the complex number, itself, as well as methods that act as unary operations on that complex number.
Returns the modulus of a complex number. The modulus is defined as the square root of the real part squared plus the imaginary part squared. This basically turns the complex number into a purely real number.
Note that the modulus is the first part of a complex number expressed as polar coordinates (it's the r in (r, theta)).
Example usage:
var complex = new ComplexNumber(3, 4); complex.mod(); //returns 5 (3^2=9 and 4^2=16. 9 + 16 = 25. Square root of 25 is 5.
By default, this code works on complex numbers in rectangular form. If you want to get the polar form (i.e., r theta), then use the mod function to get the modulus and this function to get the argument.
Returns the string representation of a complex number (e.g. "3 + 5i"). If the imaginary part is negative, then a minus sign will be placed in front of it instead of a plus sign.
Example usage:
var complex = new ComplexNumber(3, 4); complex.toString(); //returns "3 + 4i"
Prints the complex number in polar coordinates rather than the default rectangular form. Note that the angle measurement is in radians.
This function returns the conjugate of a complex number. I.e., for a complex number (a + bi), ComplexNumber.conjugate will return (a - bi).
These methods are "binary" in the sense that they take two arguments. These mathematical operations are add()
, sub()
, mult()
, div()
. I moved these methods into a different object so as to make math operations on complex numbers look more natural.
Adds two complex numbers together by adding the real parts and the complex parts. In the source code, I call the first argument the addend and the second argument the summand even though the order of the arguments doesn't matter.
Example usage:
var complex1 = new ComplexNumber(3, 5); var complex2 = new ComplexNumber(1, 2); ComplexMath.add(complex1, complex2); //returns "4 + 7i"
Subtracts a complex number from another by finding the difference between the real parts and the complex parts. The order of the arguments matters, in this case. The first argument is the minuend and the second argument is the subtrahend.
Example usage:
var complex1 = new ComplexNumber(3, 5); var complex2 = new ComplexNumber(1, 2); ComplexMath.sub(complex1, complex2); //returns "2 + 3i"
Multiplies a complex number with another. Given complex numbers A and B, the result of their multiplication is: [(realA * realB) - (imaginaryA * imaginaryB)] + [(realA * imaginaryB) + (imaginaryA * realB)]*i. In the source code, I call the first argument the multiplicand and the second argument the multiplier even though the order won't make any difference in the result.
Example usage:
var complex1 = new ComplexNumber(3, 5); var complex2 = new ComplexNumber(1, 2); ComplexMath.mult(complex1, complex2); //returns "-7 + 11i"
Divides two complex numbers via the following formula:
a + bi ac + bd bc - ad ------ = ------- + -------*i c + di c^2+d^2 c^2+d^2
where (a + bi) is the dividend and (c + di) is the divisor.
The power function takes two arguments. The first argument must be the name of a complex number. The second argument is the power to which that complex number will be raised. This function uses De Moivre's formula, so the power must be an integer.