A simple complex-numbers library for browsers and Node.js.
This library was created as solution for this RosettaCode task. It has been moved to its own repo as a way to learn TDD with Travis-CI.
Complex.js can be used in both the browser and Node.js:
Install complex.js using npm:
npm i @iamsquare/complex.js
Since this library is compiled from Typescript, type definition files are provided by default. No additional @types installation required!
Just clone this repo and build the library:
git clone --depth=0 https://github.com/iamsquare/complex.js
cd complex.js/
npm i
npm run prod
npm run prod
will run Jest and build the library only if it passes all tests. To build without testing:
npm run build
Polyfills are not included (read Usage to learn more).
Just import the Complex class and the operations/functions you want to use in your Javascript (ES5/ES6/ES7) or Typescript project:
ES6/ES7/Typescript
import { Complex, add, log, pow, asinh, ...} from '@iamsquare/complex.js';
ES5
var ComplexJS = require('@iamsquare/complex.js');
var Complex = ComplexJS.Complex; // This line assigns the Complex constructor to the Complex variable.
var add = ComplexJS.add; // This line assigns the add operation to the add variable.
...
Note: for ES5 you will need to polyfill the following methods and properties when necessary:
To keep the build as little as possible - and to let old tech die - these polyfills are NOT included in the bundle. You almost surely use Babel in your workflow anyway, so it's useless to polyfill the library beforehand (you can find a guide on how to include built-ins here).
The library documentation can be found here.
const z: Complex = new Complex(1, -1); // Numeric arguments
const w: Complex = new Complex({ x: 1, y: -3 }); // Cartesian argument
const k: Complex = new Complex({ r: 1, p: Math.PI / 2 }); // Polar argument
const zz: Complex = new Complex(z); // Complex argument
const a: Complex = add(z, w);
console.log(a); // => Complex {re: 2, im: -4}
const s: Complex = subtract(z, w);
console.log(s); // => Complex {re: 0, im: 2}
const m: Complex = multiply(z, w);
console.log(m); // => Complex {re: -2, im: -4}
const d: Complex = divide(z, w);
console.log(d); // => Complex {re: 0.39999999999999997, im: 0.2}
These are just the four basic operations. Check the documentation to know more.
The code in this project is licensed under MIT License.
Polar coordinate type definition
Gets the argument of a Complex number.
Calculates z / w using a modified Smith's Method.
An helper function that checks if an object is a Cartesian coordinate.
Returns true when a Complex number is ∞.
Returns true when a Complex number is NaN.
An helper function that checks if an object is a Polar coordinate.
Returns true when a Complex number real part is zero.
Returns true when a Complex number imaginary part is zero.
Returns true when a Complex number real and imaginary part are zero.
Calculates the principal value of Ln(z).
Calculates the modulus of a Complex number.
Calculates the modulus squared of a Complex number.
Calculates the principal value of the square-root of a Complex number.
Generated using TypeDoc
Cartesian Coordinate type definition