Matrix.js
A professional, comprehensive and high-performance library for you to manipulate matrices.
Features
Install
npm install --save @rayyamhk/matrix
How to use
const Matrix = require('@rayyamhk/matrix');
const A = new Matrix([
[1, 2],
[3, 4],
]);
const B = new Matrix([
[2, 3],
[4, 5],
]);
const Sum = Matrix.add(A, B);
const [Q, R] = Matrix.QR(Sum);
const det = Sum.det();
const eigenvalues = Sum.eigenvalues();
Build
npm install
npm run build
It creates a production version in /lib
Test
npm install
npm run test
It runs all tests in /src/tests
API
You can find the documentation in the following link:
https://rayyamhk.github.io/Matrix.js/Matrix.html
Examples
constructor(A)
new Matrix([]);
new Matrix([
[1, 2, 3, 4],
]);
new Matrix([
[1],
[2],
[3],
]);
new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
Decompositions
LU(A, optimized)
const A = new Matrix([
[4, 3],
[6, 3],
]);
const [P, L, U] = Matrix.LU(A, false);
const [P, LU] = Matrix.LU(A, true);
QR(A)
const A = new Matrix([
[12, -51, 4],
[6, 167, -68],
[-4, 24, -41],
]);
const [Q, R] = Matrix.QR(A);
Linear-Equations
backward(U, y)
const A = new Matrix([
[1, 2],
[0, 3],
]);
const y = new Matrix([
[1],
[3],
]);
try {
const x = Matrix.backward(A, y);
} catch (e) {
console.log(e.message);
}
forward(L, y)
const A = new Matrix([
[1, 0],
[2, 3],
]);
const y = new Matrix([
[1],
[8],
]);
try {
const x = Matrix.forward(A, y);
} catch (e) {
console.log(e.message);
}
solve(A, y)
const A = new Matrix([
[1, 2],
[3, 4],
]);
const y = new Matrix([
[5],
[11],
]);
try {
const x = Matrix.solve(A, y);
} catch (e) {
console.log(e.message);
}
Operations
add(A, B)
const A = new Matrix([
[1, 2],
[3, 4],
]);
const B = new Matrix([
[5, 6],
[7, 8],
]);
const Sum = Matrix.add(A, B);
inverse(A)
const A = new Matrix([
[1, 2],
[3, 4],
]);
try {
const inv = Matrix.inverse(A);
} catch (e) {
console.log(e.message);
}
multiply(A, B)
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);
const B = new Matrix([
[-1, -2],
[3, 4],
[-5, -6],
]);
const Product = Matrix.multiply(A, B);
pow(A, n)
const A = new Matrix([
[2, 0],
[0, 2],
]);
const Result = Matrix.pow(A, 10);
subtract(A, B)
const A = new Matrix([
[1, 2],
[3, 4],
]);
const B = new Matrix([
[4, 3],
[2, 1],
]);
const Diff = Matrix.subtract(A, B);
transpose(A)
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);
const T = Matrix.transpose(A);
Properties
cond(p = 2)
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
[1, 2, 7],
]);
A.cond(1);
A.cond(2);
A.cond(Infinity);
A.cond('F');
det()
const A = new Matrix([
[1, 3, 5, 9],
[1, 3, 1, 7],
[4, 3, 9, 7],
[5, 2, 0, 9],
]);
A.det();
eigenvalues()
const A = new Matrix([
[13, -12, 6, -9],
[1, -11, -13, 0],
[-6, -2, 15, -6],
[14, -8, 1, 11],
]);
const eigenvalues = A.eigenvalues();
eigenvalues.forEach((eigenvalue) => {
console.log(eigenvalue.toString());
});
norm(p)
const A = new Matrix([
[1, 7, -5, 2, -7],
[-8, 0, 2, 9, 4],
[3, 4, 9, 6, 5],
]);
A.norm(1);
A.norm(2);
A.norm(Infinity);
A.norm('F');
nullity()
const A = new Matrix([
[0, 1, 2],
[1, 2, 1],
[2, 7, 8],
]);
A.nullity();
rank()
const A = new Matrix([
[0, 1, 2],
[1, 2, 1],
[2, 7, 8],
]);
A.rank();
size()
const A = new Matrix([
[0, 1, 2, 3],
[4, 5, 6, 7],
]);
const [row, col] = A.size();
trace()
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
A.trace();
Structure
isDiagonal(digit = 8)
const A = new Matrix([
[1, 0, 0],
[0, 5, 0],
[0, 0, -3],
]);
const B = new Matrix([
[1, 0, 0.1],
[0, 5, 0],
[0, 0, -3],
]);
A.isDiagonal();
B.isDiagonal();
isLowerTriangular(digit = 8)
const A = new Matrix([
[6, 0, 0, 0],
[1, -5, 0, 0],
[2, 30, 1, 0],
]);
A.isLowerTriangular();
isOrthogonal(digit = 8)
const Reflection = new Matrix([
[1, 0],
[0, -1],
]);
Reflection.isOrthongonal();
isSkewSymmetric(digit = 8)
const A = new Matrix([
[1, 2, 3, 4],
[-2, 2, -4, 5],
[-3, 4, 100, 10],
[-4, -5, -10, 5],
]);
A.isSkewSymmetric();
isSquare()
const A = new Matrix([
[1, 2],
[3, 4],
]);
A.isSquare();
isSymmetric(digit = 8)
const A = new Matrix([
[1, 4, 3],
[4, 5, 4],
[3, 4, 5],
]);
A.isSymmetric();
isUpperTriangular(digit = 8)
const A = new Matrix([
[6, 0, 1, 5],
[0, -5, 4, 7],
[0, 0, 1, 2],
]);
A.isUpperTriangular();
Utilities
clone(A)
const A = new Matrix([
[1, 2],
[3, 4],
]);
Matrix.clone(A);
column(A, index)
const A = new Matrix([
[1, 2],
[3, 4],
[5, 6],
]);
Matrix.column(A, 0);
Matrix.column(A, 1);
diag(values)
Matrix.diag([1, 2, 3]);
const values = [
new Matrix([
[1, 2],
[3, 4],
]),
new Matrix([
[5, 6],
[7, 8],
])
];
Matrix.diag(values);
elementwise(A, cb)
Matrix.elementwise(A, (entry) => entry * 2);
Matrix.elementwise(A, (entry) => entry ** 2);
Matrix.elementwise(A, (entry) => entry - 10);
entry(row, col)
const A = new Matrix([
[1, 2],
[3, 4],
]);
A.entry(0, 0);
A.entry(0, 1);
A.entry(1, 0);
A.entry(1, 1);
generate(row, col, cb)
Matrix.generate(3, 3, () => 0);
Matrix.generate(3, 3, (i, j) => 1 / (i + j + 1));
Matrix.generate(3, 3, (i, j) => i >= j ? 1 : 0);
getDiag(A)
const A = new Matrix([
[1, 2, 3, 4],
[5, 6, 7, 8],
]);
Matrix.getDiag(A);
getRandomMatrix(row, col, min = 0, max = 1, toFixed = 0)
Matrix.getRandomMatrix(3, 4, -10, 10, 2);
identity(size)
Matrix.identity(2);
Matrix.identity(10);
isEqual(A, B, digit = 5)
const A = new Matrix([
[1, 2],
[3, 4],
]);
const B = new Matrix([
[1, 2],
[3, 4 + 10e-10],
]);
Matrix.isEqual(A, B);
const C = new Matrix([
[1, 2],
[3, 4 + 10e-2],
]);
Matrix.isEqual(A, C);
row(A, index)
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);
Matrix.row(A, 0);
Matrix.row(A, 1);
submatrix(A, rowsExp, colsExp)
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
Matrix.submatrix(A, 0, 1);
Matrix.submatrix(A, '0:1', 1); [[1], [4]], row 0 + row 1 & column 1
Matrix.submatrix(A, '0:1', '0:1'); [[1, 2], [4, 5]], row 0 + row 1 & column 0 + column 1
Matrix.submatrix(A, ':', '1:2'); [[2, 3], [5, 6], [8,9]], all rows && column 1 + column 2
Matrix.submatrix(A, ':', ':'); same with A
toString()
const A = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
A.toString();
zero(row, col)
Matrix.zero(3, 4);
Matrix.zero(10, 1);
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