Immutable-Complex
This is a fork of the original Complex
library of Arian Stolwijk. Thanks for the initial work!
It changes one fundamental thing and some details:
Fundamental Change
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.
Changes in Details
-
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 c['='](d)
New aliases are:
** => pow
* => multiply
/ => divide
+ => add
- => subtract
= => equals
Used like this:
var c = new Complex(1,1);
c.add === c['+'];
var cc = c['+'](c)
, cc2 = c.add(c)
;
cc['='](cc2);
About:
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.
![Screenshot](https://github.com/graphicore/Complex/raw/master/wiki-complex.png)
Node
You can get this package with NPM:
npm install ComplexImmutable
var Complex = require('Complex');
console.log(new Complex(3, 4).abs());
Browser
Complex can be built for the browser with wrapup
or other tools that can generate browser JS from Node packages.
Testing
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
API Documentation
Complex constructor:
var z = new Complex(real, imag);
Arguments:
- real (number) the real part of the number
- imag (number) the imaginary part of the number
Function: Complex.from
A in line function like Number.from.
var z = Complex.from(real[, imag]);
Arguments:
- real (number) the real part of the number
- imag (number, optional) the imaginary part of the number
Or
- real (string) a string representation of the number, for example
1+4i
Examples:
var z = Complex.from(2, 4);
var z = Complex.from(5);
var z = Complex.from('2+5i');
Function: Complex.fromPolar
Creates a complex instance from a polar representation: r*e^(phi*i) = r (cos(phi) + i sin(phi))
var z = Complex.fromPolar(r, phi);
Arguments:
- r (number) the radius/magnitude of the number
- phi (number) the angle/phase of the number
Constant: Complex.i
A instance of the imaginary unit i
var i = Complex.i;
Constant: Complex.one
A instance for the real number 1
var one = Complex.one;
Method: fromRect
Sets the real and imaginary properties a and b from a + bi
myComplex.fromRect(real, imag);
Arguments:
- real (number) the real part of the number
- imag (number) the imaginary part of the number
Method: fromPolar
Sets the a and b in a + bi
from a polar representation.
myComplex.fromPolar(r, phi);
Arguments:
- r (number) the radius/magnitude of the number
- phi (number) the angle/phase of the number
Method: toPrecision
Sets the precision of the numbers. Similar to Number.prototype.toPrecision. Useful befor printing the number with the toString method.
myComplex.toPrecision(k);
Arguments:
- k (number) An integer specifying the number of significant digits
Method: toFixed
Formats a number using fixed-point notation. Similar to Number.prototype.toFixed. Useful before printing the number with the toString method.
myComplex.toFixed(k);
Arguments:
- k (number) The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0
Method: magnitude
Calculates the magnitude of the complex number
myComplex.magnitude();
Alias:
Method: angle
Calculates the angle with respect to the real axis, in radians.
myComplex.angle();
Aliases
Method: conjugate
Calculates the conjugate of the complex number (multiplies the imaginary part with -1)
myComplex.conjugate();
Method: negate
Negates the number (multiplies both the real and imaginary part with -1)
myComplex.negate();
Method: multiply
Multiplies the number with a real or complex number
myComplex.multiply(z);
Arguments:
- z (number, complex) the number to multiply with
Alias:
Method: divide
Divides the number by a real or complex number
myComplex.divide(z);
Arguments:
- z (number, complex) the number to divide by
Alias:
Method: add
Adds a real or complex number
myComplex.add(z);
Arguments:
- z (number, complex) the number to add
Alias:
Method: subtract
Subtracts a real or complex number
myComplex.subtract(z);
Arguments:
- z (number, complex) the number to subtract
Alias:
Method: pow
Returns the base to the exponent
myComplex.pow(z);
Arguments:
- z (number, complex) the exponent
Alias:
Method: sqrt
Returns the square root
myComplex.sqrt();
Method: log
Returns the natural logarithm (base E
)
myComplex.log([k]);
Arguments:
- k (number) the actual answer has a multiplicity (
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
Method: exp
Calculates the e^z
where the base is E
and the exponential the complex number.
myComplex.exp();
Method: sin
Calculates the sine of the complex number
myComplex.sin();
Method: cos
Calculates the cosine of the complex number
myComplex.cos();
Method: tan
Calculates the tangent of the complex number
myComplex.tan();
Method: sinh
Calculates the hyperbolic sine of the complex number
myComplex.sinh();
Method: cosh
Calculates the hyperbolic cosine of the complex number
myComplex.cosh();
Method: tanh
Calculates the hyperbolic tangent of the complex number
myComplex.tanh();
Method: clone
Returns a new Complex instance with the same real and imaginary properties
myComplex.clone();
Method: toString
Returns a string representation of the complex number
myComplex.toString();
Examples:
new Complex(1, 2).toString();
new Complex(0, 1).toString();
new Complex(4, 0).toString();
new Complex(1, 1).toString();
'my Complex Number is: ' + (new Complex(3, 5));
Method: Equals
Checks if the real and imaginary components are equal to the passed in compelex components.
myComplex.equals(z);
Arguments:
- z (number, complex) the complex number to compare with
Alias:
Examples:
new Complex(1, 4).equals(new Complex(1, 4));
new Complex(1, 4).equals(new Complex(1, 3));
MIT License
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.