matrmath
v0.1.3
Published
A library for performing common matrix operations.
Downloads
10
Maintainers
Readme
matrmath
A library for performing matrix operations.
Installation
npm install matrmath
Usage
Utilize the functions in this library for performing matrix operations.
For example, compute the diagonal difference of a square (n x n) matrix:
const mtr = require("matrmath");
let diff = mtr.diagonalDiff([
[2, 5, 3],
[4, 6, 1],
[7, 8, 9]
]);
console.log(diff);
// |(2+6+9) - (3+6+7)| = |17-16| = 1
Or compute a matrix in reduced row echelon form:
const mtr = require("matrmath");
let reduced = mtr.rowReduce([
[5, 8, 2],
[3, 5, 3],
[6, 4, 9]
]);
console.log(reduced);
// [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]
Config
rowReduce(M)
Transform a matrix in-place and return the reduced row-echelon form of a matrix. Row replacement is achieved through Gaussian Elimination.
M
-number[][]
- A 2-dimensional array.square
-boolean
- Optional boolean parameter to specify if the matrix is square (ie 3x3, 4x4 etc).
diagonalDiff(M)
Return the diagonal difference of a square. The diagonal difference is the absolute value of the left diagonal minus the right diagonal in a square matrix.
M
-number[][] | number[]
- A 2-dimensional array, or optionally 1D array.is2D
-boolean
- Optional boolean parameter to specify if the matrix is 1D instead of 2D.debug
-boolean
- Optional boolean parameter to output thelib
object with items used in computations.
isSquare(M)
Check whether a 2D or 1D matrix is square. That is the number of rows equals the number of columns in a n x m
layout where n = rows and m = columns and n must equal m.
M
-number[][] | number[]
- A 2D or 1D array, or optionally 1D array.is2D
-boolean
- Optional boolean parameter to specify if the matrix is 1D instead of 2D.
det(M)
Calculate the determinant of a matrix.
M
-number[][]
- A 2-dimensional array.
isIndependent(M)
Calculate whether a square matrix is linearly independent. If the determinant of a matrix is not equal to 0, its linearly independent.
M
-number[][]
- A 2-dimensional array.
isDependent(M)
Calculate whether a square matrix is linearly dependent. Meaning its determinant equals 0.
M
-number[][]
- A 2-dimensional array.
Tests
npm run test
Todo
- [ ] Add a utility for transforming points in R^n to R^n. In other words, transform a set of points in some space (R2) to points in (R3). Have a initial input matrix and a transformation matrix as parameters to output the points after being multiplied by the transformation matrix.
Resources
- Reduced Row Echelon Form (JavaScript) - code example by rosettacode.
- Determinant - JavaScript - code example by rosettacode.
- substrack/rref - library for row reducing based on rosettacode example.