Do calculations with Complex numbers
You can get this package with NPM:
npm install Complex
var Complex = require'Complex';console.log3 4abs; // 5
Complex can be built for the browser with wrapup or other tools that can generate browser JS from Node packages.
Testing is done with Mocha and Expect.js:
# install dependencies npm install # run the tests in node ./node_modules/.bin/mocha test/Complex.js
or testing in the browser:
# install dependencies npm install # run a small node server node ./test/server.js # run tests google-chrome http://localhost:3000
var z = real im;
A in line function like Number.from.
var z = Complexfromreal im;
var z = Complexfrom2 4;var z = Complexfrom5;var z = Complexfrom'2+5i';
Creates a complex instance from a polar representation:
r*e^(phi*i) = r (cos(phi) + i sin(phi))
var z = ComplexfromPolarr phi;
A instance of the imaginary unit
var i = Complexi;
A instance for the real number
var one = Complexone;
Sets the real and imaginary properties a and b from
a + bi
Sets the a and b in
a + bi from a polar representation.
Sets the precision of the numbers. Similar to Number.prototype.toPrecision. Useful befor printing the number with the toString method.
Formats a number using fixed-point notation. Similar to Number.prototype.toFixed. Useful before printing the number with the toString method.
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.
Calculates the magnitude of the complex number
Calculates the angle with respect to the real axis, in radians.
Calculates the conjugate of the complex number (multiplies the imaginary part with -1)
Negates the number (multiplies both the real and imaginary part with -1)
Multiplies the number with a real or complex number
Divides the number by a real or complex number
Adds a real or complex number
Subtracts a real or complex number
Returns the base to the exponent
Returns the square root
Returns the natural logarithm (base
ln(z) = ln|z| + arg(z)) where arg(z) can return the same for different angles (every 2*pi), with this argument you can define which answer is required
e^z where the base is
E and the exponential the complex number.
Calculates the sine of the complex number
Calculates the cosine of the complex number
Calculates the tangent of the complex number
Calculates the hyperbolic sine of the complex number
Calculates the hyperbolic cosine of the complex number
Calculates the hyperbolic tangent of the complex number
Returns a new Complex instance with the same real and imaginary properties
Returns a string representation of the complex number
1 2toString; // 1+2i0 1toString; // i4 0toString; // 41 1toString; // 1+i'my Complex Number is: ' + 3 5; // 'my Complex Number is: 3+5i
Checks if the real and imaginary components are equal to the passed in compelex components.
1 4equals1 4; // true1 3equals1 3; // false