It changes one fundamental thing and some details:
The Complex Objects are now immutable, in the sense that when applying mathematical operations on an instance of Complex doesn't change its value, but it returns a new instance with the new value. I need this for my math.
Consequently, the finalize method was removed.
I changed the name of the imaginary part from "im" to "imag", because it is more verbose and because it reminds me of the API for complex numbers in Python.
I added an operator like API as aliases for existing functions. You can
access them via the brackets
New aliases are:
** => pow
* => multiply
/ => divide
+ => add
- => subtract
= => equals
Used like this:
var c = 11;cadd === c'+'; // true// thus:var cc = c'+'ccc2 = c;cc'='cc2; // true
You can get this package with NPM:
npm install ComplexImmutable
var Complex = ;console; // 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 imag;
A in line function like Number.from.
var z = Complex;
var z = Complex;var z = Complex;var z = Complex;
Creates a complex instance from a polar representation:
r*e^(phi*i) = r (cos(phi) + i sin(phi))
var z = Complex;
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.
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 2; // 1+2i0 1; // i4 0; // 41 1; // 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 4; // true1 4; // false
Copyright (c) 2014 Arian Stolwijk, Lasse Fister
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.