Complex is a additional Type to deal with Complex Numbers in JavaScript. It provides several methods to add, multiply numbers as well as calculate the magnitude and angle in the complex plane.
Just include the file Complex.js into your page or require it in your ssjs script.
Build via Packager, requires MooTools Core to be registered to Packager already (ty cpojer)
./packager register /path/to/Complex ./packager build Complex/* > Complex
var z = new Complex(real[, im]);
Or
var z = new Complex(2, 4); var z = new Complex('2+5i');
A in line function like Number.from.
var z = Complex.from(real, im);
The same as the Complex constructor.
Creates a complex instance from a polar representation: r*e^(phi*i) = r (cos(phi) + i sin(phi))
var z = Complex.fromPolar(r, phi);
A instance of the imaginary unit i
var i = Complex.i;
A instance for the real number 1
var one = Complex.one;
Sets the real and imaginary properties a and b from a + bi
myComplex.fromRect(real, im);
Sets the a and b in a + bi from a polar representation.
myComplex.fromPolar(r, phi);
Sets the precision of the numbers. Similar to Number.prototype.toPrecision. Useful befor printing the number with the toString method.
myComplex.toPrecision(k);
Formats a number using fixed-point notation. Similar to Number.prototype.toFixed. Useful before printing the number with the toString method.
myComplex.toFixed(k);
Finalizes the instance. The number will not change and any other method call will return a new instance. Very useful when a complex instance should stay constant. For example the Complex.i variable is a finalized instance.
myComplex.finalize();
Calculates the magnitude of the complex number
myComplex.maginude();
Calculates the angle with the real axis.
myComplex.angle();
Calculates the conjungate of the complex number (multiplies the imaginary part with -1)
myComplex.conjungate();
Negates the number (multiplies both the real and imaginary part with -1)
myComplex.negate();
Multiplies the number with a real or complex number
myComplex.multiply(z);
Devides the number by a real or complex number
myComplex.devide(z);
Adds a real or complex number
myComplex.add(z);
Subtracts a real or complex number
myComplex.subtract(z);
Returns the base to the exponent
myComplex.pow(z);
Returns the square root
myComplex.sqrt();
Returns the natural logarithm (base E)
myComplex.log([k]);
Calculates the e^z where the base is E and the exponential the complex number.
myComplex.exp();
Calculates the sine of the complex number
myComplex.sin();
Calculates the cosine of the complex number
myComplex.cos();
Calculates the tangent of the complex number
myComplex.tan();
Calculates the hyperbolic sine of the complex number
myComplex.sinh();
Calculates the hyperbolic cosine of the complex number
myComplex.cosh();
Calculates the hyperbolic tangent of the complex number
myComplex.tanh();
Returns a new Complex instance with the same real and imaginary properties
myComplex.clone();
Returns a string representation of the complex number
myComplex.toString();
new Complex(1, 2).toString(); // 1+2i new Complex(0, 1).toString(); // i new Complex(4, 0).toString(); // 4 new Complex(1, 1).toString(); // 1+i 'my Complex Number is: ' + (new Complex(3, 5)); // 'my Complex Number is: 3+5i
Checks if the real and imaginary components are equal to the passed in compelex components.
myComplex.equals(z);
new Complex(1, 4).equals(new Complex(1, 4)); // true new Complex(1, 3).equals(new Complex(1, 3)); // false
Each method of the Complex object is added to the Number object, so it is easy to use them together.
(3).add(new Complex(2, 4)).toString(); // 5+4i
Returns a Complex instance where the real component is the value of the number.
myNumber.toComplex();
(3).toComplex().add(new Complex(3, 4)).toString(); // 6+4i
Number.from accepts Complex instances. It will return the real component.
Number.from(new Complex(3, 4)); // 3
The following functions are added to the Math object:
String implements the following methods:
Parses a string like 3+5i into a Complex instance.
myString.toComplex();
'4+2i'.toComplex(); // a complex instance with the real component = 4 and the imaginary component = 2
A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.
Found a bug in this plugin? Please report it this repository's Github Issues.
blog comments powered by Disqus