math-js
Yet another JS math library
Installation
npm install @rgsoft/math
Tests
npm run test
Complex Numbers
The complex number library has some methods for performing calculations on
complex numbers. To create a complex number of the form a + bi
:
const cpx = new Complex(a, b);
📝 Every complex number is inmutable; both, the real and the imaginary
parts are readonly.
Magnitude
const c = new Complex(5, 12);
console.log(c.mag);
Conjugate
const c = new Complex(2, 7);
console.log(`${c.conjugate()}`);
Addition
Scalar Addition
const c1 = new Complex(5, 12);
const c2 = c1.add(4);
console.log(`${c2}`);
Complex Addition
const c1 = new Complex(4, -8);
const c2 = new Complex(-3, 6);
const c3 = c1.add(c2);
console.log(`${c3}`);
Substraction
Scalar Substraction
const c1 = new Complex(9, 1);
const c2 = c1.sub(10);
console.log(`${c2}`);
Complex Substraction
const c1 = new Complex(4, -8);
const c2 = new Complex(-3, 6);
const c3 = c1.add(c2);
console.log(`${c3}`);
Multiplication
Scalar Multiplication
const c1 = new Complex(4, -1);
const c2 = c1.mult(-5);
console.log(`${c2}`);
Complex Multiplication
const c1 = new Complex(3, -1);
const c2 = new Complex(-2, 1);
const c3 = c2.mult(c2);
console.log(`${c3}`);
Division
Scalar Division
const c1 = new Complex(4, -1);
const c2 = c1.div(-2);
console.log(`${c2}`);
Complex Division
let c1 = new Complex(2, 1);
let c2 = new Complex(-1, -1);
let c3 = c1.div(c2);
console.log(`${c3}`);
Square Root
let c = new Complex(4, 0);
c = c.sqrt();
console.log(`${c}`);
Vectors
Instantiation:
const vector = new Vector(x, y);
Angle
const v = new Vector(1, 1);
console.log(v.angle);
Magnitude
const v = new Vector(1, 1);
console.log(v.mag);
Normalization
const v = new Vector(4, 4);
v.nomalize();
console.log(v.mag);
Scalar Multiplication
const v = new Vector(0, 3);
v.mult(4);
console.log(v.mag);
console.log(v.y);
Addition
const v = new Vector(-2, 3);
v.add(new Vector(3, -5));
console.log(v.x);
console.log(v.y);
Substraction
const v = new Vector(-2, 3);
v.sub(new Vector(3, -5));
console.log(v.x);
console.log(v.y);
Distance to Another Vector
const v = new Vector(7, 2);
console.log(v.dist(new Vector(3, -1)));
Angle to Another Vector
const v1 = new Vector(0, 1);
const v2 = new Vector(1, 0);
console.log(v1.angleTo(v2));
Create Vector from Angle
let v = Vector.fromAngle(Math.PI * 0.5);
console.log(v.x);
console.log(v.y);
Equality with Other Vector
const v1 = new Vector(7, 2);
const v2 = new Vector(7, 2);
console.log(v1.equals(v2));
Copy Vector
const v = new Vector(7, 2);
const cv = v.copy();
Modulo
The mod
function calculates the modular remainder of a number n
modulo m
.
The main diference between this function and the remainder operator %
is that
the mod
function applies the modular arithmetic strictly. For example
console.log(-1 % 4);
will output -1
. Whereas using the mod
:
console.log(mod(-1, 4));
we should get 3
.
The mod
function will fail if it receives a negative modulo or any non-integer
number.
Greatest Common Divisor
The gcd
function calculates greatest common divisor between to positive
integers
console.log(gcd(18, 4));
will ouput 2
.
Least Common Multiple
The lcm
function calculates least common multple between to positive integers
console.log(lcm(18, 4));
will ouput 36
.
Is a Number Prime
The prime
checks if a positive integer is prime or not
console.log(prime(17));
will ouput true
.
Prime Factorization
The factors
function gets the prime factors with their respective exponents
console.log(factors(48));
will ouput [ [2, 4], [3, 1] ]
, where each element of the array is another
array with two numbers:
- The prime factor
- The exponent of the factor
In the example, this means that 48
is 2^4 + 3^1
.
Number Totient
The totient
function gets the totient
from a positive integer, that is, the number of prime numbers from 1 to n-1
for a given integer n
.
console.log(totient(100));
will ouput 40
.
Collatz Sequence
The collatz
function gets the collatz
sequence from a positive integer.
console.log(collatz(5));
will ouput [ 5, 16, 8, 4, 2, 1 ]
.
Optionally, it accepts a second parameter that limits the maximum length of the
resulting sequence.
console.log(collatz(5, 5));
will ouput [ 5, 16, 8, 4, 2 ]
.
Digital Roots
The digitalRoots
function gets the digital roots from a positive integer, that
is, the sum of all its digits.
console.log(digitalRoots(19));
will ouput 1
.
Converting to Other Bases
The toBase
function gets the string representation of a given positive integer
in a certain base.
console.log(toBase(255, 16), toBase(255, 2));
will ouput FF 11111111
.
The inverse process is achieved with the fromBase
function.
console.log(fromBase('FF', 16), fromBase('11111111', 2));
outputs 255 255
.