Complex.js
A lightweight and easy-to-use library for you to manipulate complex numbers
Features
- Lightweight
- Comprehensive
- Easy to use
- No dependencies
- 1000+ Test cases
- No INFINITY!!
Install
npm install --save @rayyamhk/complex
How to use
const Complex = require('@rayyamhk/complex');
const num1 = new Complex(3, 4);
const num2 = new Complex(-2);
const sum = Complex.add(num1, num2);
console.log(sum.toString());
Build
npm install
npm run build
It creates a production version in /build
Test
npm install
npm run test
It runs all tests in /lib/tests
API
You can find the documentation in the following link:
https://rayyamhk.github.io/Complex.js/Complex.html
Examples
constructor(re, im)
new Complex();
new Complex(3);
new Complex(Infinity);
new Complex('3');
new Complex(3, 4);
new Complex(3, Infinity);
Instance methods
getReal()
new Complex(3, 4).getReal();
new Complex(0, 1).getReal();
getImaginary()
new Complex(3, 4).getImaginary();
Complex.ZERO.getImaginary();
getModulus()
new Complex(3, 4).getModulus();
Complex.ZERO.getModulus();
getArgument()
new Complex(3, 3).getArgument();
Complex.ZERO.getArgument();
toString()
new Complex(3, 4).toString();
new Complex(3.1415).toString();
Complex.NaN.toString();
Static methods
isNaN(num)
Complex.isNaN(new Complex(3));
Complex.isNaN(new Complex(3, 4));
Complex.isNaN(new Complex(Infinity));
Complex.isNaN(Complex.NaN);
isEqual(num1, num2, digit = 15)
const num1 = new Complex(3, 4);
const num2 = new Complex(3 + 4e-16, 4);
const num3 = new Complex(3 + 4e-16, 4 + 6e-16);
Complex.isEqual(num1, num2);
Complex.isEqual(num1, num3);
Complex.isEqual(Complex.NaN, new Complex(1 / 0));
4 basic operations
const num1 = new Complex(3, 4);
const num2 = new Complex(-1, 2);
Complex.add(num1, num2);
Complex.subtract(num1, num2);
Complex.multiply(num1, num2);
Complex.divide(num1, num2);
Complex.divide(num1, Complex.ZERO);
conjugate(num)
Complex.conjugate(new Complex(3, 4));
Complex.conjugate(new Complex(3, -4));
Complex.conjugate(new Complex(-3, 4));
Complex.conjugate(new Complex(3));
Complex.conjugate(Complex.NaN);
inverse(num)
Complex.inverse(new Complex(3, 4));
Complex.inverse(Complex.ZERO);
pow(base, exponent)
Complex.pow(z, 2);
Complex.pow(z, 1.234);
Complex.pow(z, 0);
Complex.pow(z, -2);
Complex.pow(z, 1 / 4);
exp(num)
Complex.exp(Complex.ZERO);
Complex.exp(new Complex(3, 4));
log(num)
Complex.log(Complex.E);
Complex.log(Complex.ZERO);
6 trigonometric functions
Complex.sin(num);
Complex.cos(num);
Complex.tan(num);
Complex.tan(new Complex(Math.PI / 2));
Complex.csc(num);
Complex.csc(Complex.ZERO);
Complex.sec(num);
Complex.sec(new Complex(Math.PI / 2));
Complex.cot(num);
Complex.cot(Complex.PI);
6 inverse of trigonometric functions
Complex.asin(num);
Complex.acos(num);
Complex.atan(num);
Complex.acsc(num);
Complex.asec(num);
Complex.acot(num);
How to contribute
You are welcome to contribute by:
- Reporting bugs
- Fixing bugs
- Adding new features
- Improving performance
- Improving code style of this library
License
MIT