numerical.ts is a comprehensive TypeScript library for linear algebra and various mathematical operations, including vector and matrix operations, eigenvalue calculations, optimization algorithms, parsing mathematical statements, complex numbers, and more
npm install numericalts
number, bigint, or custom numerical types. The use of generics provides versatility and adaptability to different use cases and data structures.
bash
npm install numericalts
`
For a full list of currently implemented methods and types please see here
$3
- Number
- Bigint
- Complex number
These three Numerical interfaces have been implemented meaning you don't have to create them from scratch. Furthermore you can create matrixes without giving the constuctor the Numerical class
`ts
import { Matrix } from 'numericalts';
const A: Matrix = new Matrix([[1,2],[3,4]])
const B: Matrix = new Matrix([[1n,2n],[3n,4n]])
const C: Matrix = new Matrix([
[{real: 1, imaginary: 1},{real: 2, imaginary: 2}],
[{real: 3, imaginary: 3},{real: 4, imaginary: 4}]
])
`
$3
`ts
import { Matrix } from 'numericalts';
const A: Matrix = new Matrix([[1, 2], [3, 4]]);
const B: Matrix = new Matrix([[5, 6, 7, 8], 2, 2]);
const stringA: Matrix
//A = new Matrix([["1","2"],["3","4"]]) throws a typeerror while new Matrix([["1","2"],["3","4"]]) does not
// Matrix multiplication
const C: Matrix = A.multiply(B);
console.log(C.toArray()); // [[19, 22], [43, 50]]
// Matrix exponentiation
const D: Matrix = A.pow(2);
console.log(D.toArray()); // [[7, 10], [15, 22]]
// Matrix multiplication using Strassen's algorithm
const E: Matrix = A.strassenMultiply(B);
console.log(E.toArray()); // [[19, 22], [43, 50]]
// QR decomposition
const { Q, R } = A.QRDecomposition();
console.log(Q.toArray()); // [[-0.31622776601683794, -0.9486832980505138], [-0.9486832980505138, 0.31622776601683794]]
console.log(R.toArray()); // [[-3.1622776601683795, -4.427188724235731], [0, 0.6324555320336759]]
`
$3
Notice a complex number Numerical class have since been added
This section demonstrates how to create a custom class that implements the Numerical interface and how to use it with the Matrix class.
`ts
// Import The Matrix class and the Numerical interface
import { Matrix, Numerical} from 'numericalts';
// Skeleton for a custom Complex class
class Complex implements Numerical{
zeroValue: new Complex(0,0);
oneValue: new Complex(1,0);
constructor(realValue: number, imagValue: number){/Logic here/}
public zeroValue(): Complex {/Logic here/}
public oneValue(): Complex {/Logic here/}
public add(x: Complex, y: Complex): Complex {/Logic here/}
public subtract(x: Complex, y: Complex): Complex {/Logic here/}
public multiply(x: Complex, y: Complex): Complex {/Logic here/}
public divide(x: Complex, y: Complex): Complex {/Logic here/}
public sqrt(x: Complex): Complex {/Logic here/}
public fromIntegral(n: number): Complex {/Logic here/}
public toIntegral(x: Complex): number {/Logic here/}
public toString(x:Complex):string {/Logic here/}
public signOperator(x: Complex): number {/Logic here/}
}
// Now that the custom class has been implemented just use the Matrix class as normal
const c1: Complex = new Complex(1, 1);
const c2: Complex = new Complex(2, 2);
const c3: Complex = new Complex(3, 3);
const c4: Complex = new Complex(4, 4);
const complexMatrix: Matrix = new Matrix([[c1, c2], [c3, c4]], {numerical: new Complex(0, 0)});
// Matrix multiplication
const A: Matrix = complexMatrix.multiply(complexMatrix);
console.log(C.toString());
/*
* "14i 20i"
* "30i 44i"
*/
// Matrix addition
const B: Matrix = complexMatrix.add(complexMatrix);
console.log(B.toString());
/*
* "2+2i 4+4i"
* "6+6i 8+8i"
*/
`
$3
`ts
import { math } from 'numericalts';
const num: number = 3.14159;
// Get the fractional part of a number
const frac: number = math.fracPart(num);
console.log(frac); // 0.14159
// Calculate the greatest common divisor (GCD) of two numbers
const gcd: number = math.GCD(24, 36);
console.log(gcd); // 12
// Calculate the least common divisor (LCD) of two numbers
const lcd: number = math.LCD(24, 36);
console.log(lcd); // 72
// Round a number to a specified number of decimal places
const rounded: number = math.toFixedNumber(num, 2);
console.log(rounded); // 3.14
// Count the number of decimal places in a number
const decimalPlaces: number = math.countDecimals(num);
console.log(decimalPlaces); // 5
// Calculate the dot product of two vectors
const vector1: number[] = [1, 2, 3];
const vector2: number[] = [4, 5, 6];
const dotProduct: number = math.dot(vector1, vector2);
console.log(dotProduct); // 32
// Normalize a vector
const normalizedVector: number[] = math.normalize(vector1);
console.log(normalizedVector); // [0.2672612419124244, 0.5345224838248488, 0.8017837257372732]
``