Skip to content

iamsquare/complex.js

Repository files navigation

Logo

NPM Travis (.org) branch GitHub issues GitHub License NPM

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.

Getting started

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!

Building

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).

Usage

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:

  • core-js/modules/es6.math.sinh
  • core-js/modules/es6.math.cosh
  • core-js/modules/es6.math.tanh
  • core-js/modules/es6.math.hypot
  • core-js/modules/es6.math.sign
  • core-js/modules/es6.number.epsilon
  • core-js/modules/es6.number.is-nan
  • core-js/modules/es6.number.is-finite
  • core-js/modules/es6.number.is-integer

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).

Documentation

The library documentation can be found here.

Examples

Declaration

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

Addition

const a: Complex = add(z, w);
console.log(a); // => Complex {re: 2, im: -4}

Subtraction

const s: Complex = subtract(z, w);
console.log(s); // => Complex {re: 0, im: 2}

Multiplication

const m: Complex = multiply(z, w);
console.log(m); // => Complex {re: -2, im: -4}

Division

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.

TODO

  • Support for trig functions.
  • Support for hyperbolic functions.
  • Support for powers.
  • Support for nth-roots (n√z).
  • Refactor tests.
  • Refactor the Complex class.

Built With

Licensing

The code in this project is licensed under MIT License.