@rayyamhk/matrix
Version:
A professional, comprehensive and high-performance library for you to manipulate matrices.
621 lines (510 loc) • 10.7 kB
Markdown
and high-performance library for you to manipulate matrices.
- 6 Categories: [Decompositions](
- Professional
- Comprehensive
- High-performance
- Matrix properties are cached
- Easy to use
- 3000+ Test cases
```
npm install --save @rayyamhk/matrix
```
```javascript
const Matrix = require('@rayyamhk/matrix');
const A = new Matrix([
[ ],
[ ],
]);
const B = new Matrix([
[ ],
[ ],
]);
const Sum = Matrix.add(A, B);
const [Q, R] = Matrix.QR(Sum);
const det = Sum.det();
const eigenvalues = Sum.eigenvalues();
```
```
npm install
npm run build
```
It creates a production version in `/lib`
```
npm install
npm run test
```
It runs all tests in `/src/tests`
You can find the documentation in the following link:
[ ](https://rayyamhk.github.io/Matrix.js/Matrix.html)
```javascript
new Matrix([]); // 0x0 matrix
new Matrix([
[ ],
]); // 1x4 matrix
new Matrix([
[ ],
[ ],
[ ],
]); // 3x1 matrix
new Matrix([
[ ],
[ ],
[ ],
]); // 3x3 matrix
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const [P, L, U] = Matrix.LU(A, false);
// P is [[0, 1], [1, 0]], L is [[1, 0], [2/3, 1]], U is [[6, 3], [0, 1]] and A = PLU.
const [P, LU] = Matrix.LU(A, true);
// P is [ 1, 0 ], LU = [[6, 3], [2/3, 1]]
// Note: P is an permutation array, L and U can be extracted from LU.
```
```javascript
const A = new Matrix([
[ ],
[ ],
[-4, 24, -41],
]);
const [Q, R] = Matrix.QR(A);
// Q is [[-0.8571, 0.3943, 0.3314], [-0.4286, -0.9029, -0.0343], [0.2857, -0.1714, 0.9429]],
// R is [[-14, -21, 14], [0, -175, 70], [0, 0, -35]],
// and A = QR
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const y = new Matrix([
[ ],
[ ],
]);
try {
const x = Matrix.backward(A, y); // [[-1], [1]]
} catch (e) {
console.log(e.message);
}
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const y = new Matrix([
[ ],
[ ],
]);
try {
const x = Matrix.forward(A, y); // [[1], [2]]
} catch (e) {
console.log(e.message);
}
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const y = new Matrix([
[ ],
[ ],
]);
try {
const x = Matrix.solve(A, y); // [[1], [2]]
} catch (e) {
console.log(e.message);
}
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const B = new Matrix([
[ ],
[ ],
]);
const Sum = Matrix.add(A, B); // [[6, 8], [10, 12]]
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
try {
const inv = Matrix.inverse(A); // [[-2, 1], [1.5, -0.5]]
} catch (e) {
console.log(e.message);
}
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const B = new Matrix([
[-1, -2],
[ ],
[-5, -6],
]);
const Product = Matrix.multiply(A, B); // [[-10, -12], [-19, -24]]
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const Result = Matrix.pow(A, 10); // [[1024, 0], [0, 1024]]
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const B = new Matrix([
[ ],
[ ],
]);
const Diff = Matrix.subtract(A, B); // [[-3, -1], [1, 3]]
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const T = Matrix.transpose(A); // [[1, 4], [2, 5], [3, 6]]
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
A.cond(1); // 64
A.cond(2); // 32.844126527227147
A.cond(Infinity); // 42.4999,
A.cond('F'); // 34.117851306578174
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
[ ],
]);
A.det(); // -376
```
Note that eigenvalues are instance of Complex. For more details, please check the documentation [here](https://rayyamhk.github.io/Complex.js/)
```javascript
const A = new Matrix([
[ ],
[ ],
[-6, -2, 15, -6],
[ ],
]);
const eigenvalues = A.eigenvalues();
eigenvalues.forEach((eigenvalue) => {
console.log(eigenvalue.toString()); // Instance method of Complex
});
// Result: '10.7046681565572', '-12.9152701010176', '15.1053009722302 + 14.3131819845827i', '15.1053009722302 - 14.3131819845827i'
```
```javascript
const A = new Matrix([
[ ],
[-8, 0, 2, 9, 4],
[ ],
]);
A.norm(1); // 17
A.norm(2); // 15.849881886952135
A.norm(Infinity); // 27
A.norm('F'); // 21.447610589527216
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
A.nullity(); // 1
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
A.rank(); // 2
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const [row, col] = A.size(); // 2, 4
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
A.trace(); // 15
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
const B = new Matrix([
[ ],
[ ],
[ ],
]);
A.isDiagonal(); // true
B.isDiagonal(); // false
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
A.isLowerTriangular(); // true
```
```javascript
const Reflection = new Matrix([
[ ],
[ ],
]);
Reflection.isOrthongonal(); // true
```
```javascript
const A = new Matrix([
[ ],
[-2, 2, -4, 5],
[-3, 4, 100, 10],
[-4, -5, -10, 5],
]);
A.isSkewSymmetric(); // true
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
A.isSquare(); // true
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
A.isSymmetric(); // true
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
A.isUpperTriangular(); // true
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
Matrix.clone(A); // [[1, 2], [3, 4]]
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
Matrix.column(A, 0); // [[1], [3], [5]]
Matrix.column(A, 1); // [[2], [4], [6]]
```
```javascript
Matrix.diag([1, 2, 3]); // [[1, 0, 0], [0, 2, 0], [0, 0, 3]]
const values = [
new Matrix([
[ ],
[ ],
]),
new Matrix([
[ ],
[ ],
])
];
Matrix.diag(values); // [[1, 2, 0, 0], [3, 4, 0, 0], [0, 0, 5, 6], [0, 0, 7, 8]]
```
```javascript
Matrix.elementwise(A, (entry) => entry * 2); // element-wise multiplication
Matrix.elementwise(A, (entry) => entry ** 2); // element-wise power
Matrix.elementwise(A, (entry) => entry - 10); // element-wise subtraction
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
A.entry(0, 0); // 1
A.entry(0, 1); // 2
A.entry(1, 0); // 3
A.entry(1, 1); // 4
```
```javascript
const matrix = new Matrix([
[ ],
[ ],
[ ],
]);
const myArray = matrix.flatten(); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
```
```javascript
const myArray = [1, 2, 3, 4, 5, 6, 7, 8];
const matrix = Matrix.fromArray(myArray, 2, 4); // [[1, 2, 3, 4], [5, 6, 7, 8]]
```
```javascript
Matrix.generate(3, 3, () => 0); // 3 x 3 zero matrix
Matrix.generate(3, 3, (i, j) => 1 / (i + j + 1)); // 3 x 3 Hilbert matrix
Matrix.generate(3, 3, (i, j) => i >= j ? 1 : 0); // 3 x 3 lower triangular matrix
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
Matrix.getDiag(A); // [1, 6]
```
```javascript
Matrix.getRandomMatrix(3, 4, -10, 10, 2); // 3 x 4 matrix which entries are bounded by -10 and 10 and has 2 decimal places
```
```javascript
Matrix.identity(2); // 2 x 2 identity matrix
Matrix.identity(10); // 10 x 10 identity matrix
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
const B = new Matrix([
[ ],
[ ],
]);
Matrix.isEqual(A, B); // true
const C = new Matrix([
[ ],
[ ],
]);
Matrix.isEqual(A, C); // false
```
```javascript
const A = new Matrix([
[ ],
[ ],
]);
Matrix.row(A, 0); // [[1, 2, 3]]
Matrix.row(A, 1); // [[4, 5, 6]]
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
Matrix.submatrix(A, 0, 1); // [[2]], row 0 & column 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
```
```javascript
const A = new Matrix([
[ ],
[ ],
[ ],
]);
A.toString(); // '1 2 3\n4 5 6\n7 8 9'
// 1 2 3
// 4 5 6
// 7 8 9
```
```javascript
Matrix.zero(3, 4); // 3 x 4 zero matrix
Matrix.zero(10, 1); // 10 x 1 zero matrix
```
You are welcome to contribute by:
- Reporting bugs
- Fixing bugs
- Adding new features
- Improving performance
- Improving code style of this library
MIT
A professional, comprehensive