Matrix manipulation and computation library.
$ npm install ml-matrix
import{Matrix}from'ml-matrix';constmatrix=Matrix.ones(5,5);const{ Matrix }=require('ml-matrix');constmatrix=Matrix.ones(5,5);const{ Matrix }=require('ml-matrix');varA=newMatrix([[1,1],[2,2],]);varB=newMatrix([[3,3],[1,1],]);varC=newMatrix([[3,3],[1,1],]);constaddition=Matrix.add(A,B);// addition = Matrix [[4, 4], [3, 3], rows: 2, columns: 2]constsubtraction=Matrix.sub(A,B);// subtraction = Matrix [[-2, -2], [1, 1], rows: 2, columns: 2]constmultiplication=A.mmul(B);// multiplication = Matrix [[4, 4], [8, 8], rows: 2, columns: 2]constmulByNumber=Matrix.mul(A,10);// mulByNumber = Matrix [[10, 10], [20, 20], rows: 2, columns: 2]constdivByNumber=Matrix.div(A,10);// divByNumber = Matrix [[0.1, 0.1], [0.2, 0.2], rows: 2, columns: 2]constmodulo=Matrix.mod(B,2);// modulo = Matrix [[1, 1], [1, 1], rows: 2, columns: 2]constmaxMatrix=Matrix.max(A,B);// max = Matrix [[3, 3], [2, 2], rows: 2, columns: 2]constminMatrix=Matrix.min(A,B);// max = Matrix [[1, 1], [1, 1], rows: 2, columns: 2]C.add(A);// => C = C + AC.sub(A);// => C = C - AC.mul(10);// => C = 10 * CC.div(10);// => C = C / 10C.mod(2);// => C = C % 2// Standard Math operations: (abs, cos, round, etc.)varA=newMatrix([[1,1],[-1,-1],]);varexponential=Matrix.exp(A);// exponential = Matrix [[Math.exp(1), Math.exp(1)], [Math.exp(-1), Math.exp(-1)], rows: 2, columns: 2].varcosinus=Matrix.cos(A);// cosinus = Matrix [[Math.cos(1), Math.cos(1)], [Math.cos(-1), Math.cos(-1)], rows: 2, columns: 2].varabsolute=Matrix.abs(A);// absolute = Matrix [[1, 1], [1, 1], rows: 2, columns: 2].// Note: you can do it inplace too as A.abs()Available Methods:
abs,acos,acosh,asin,asinh,atan,atanh,cbrt,ceil,clz32,cos,cosh,exp,expm1,floor,fround,log,log1p,log10,log2,round,sign,sin,sinh,sqrt,tan,tanh,trunc// remember: A = Matrix [[1, 1], [-1, -1], rows: 2, columns: 2]varnumberRows=A.rows;// A has 2 rowsvarnumberCols=A.columns;// A has 2 columnsvarfirstValue=A.get(0,0);// get(rows, columns)varnumberElements=A.size;// 2 * 2 = 4 elementsvarisRow=A.isRowVector();// false because A has more than 1 rowvarisColumn=A.isColumnVector();// false because A has more than 1 columnvarisSquare=A.isSquare();// true, because A is 2 * 2 matrixvarisSym=A.isSymmetric();// false, because A is not symmetricA.set(1,0,10);// A = Matrix [[1, 1], [10, -1], rows: 2, columns: 2]. We have changed the second row and the first columnvardiag=A.diag();// diag = [1, -1] (values in the diagonal)varm=A.mean();// m = 2.75varproduct=A.prod();// product = -10 (product of all values of the matrix)varnorm=A.norm();// norm = 10.14889156509222 (Frobenius norm of the matrix)vartranspose=A.transpose();// transpose = Matrix [[1, 10], [1, -1], rows: 2, columns: 2]varM=newMatrix([[1,2,3],[4,5,6],]);varsumOf=(vector)=>vector.reduce((total,value)=>total+value,0);varrowSums=M.applyAlongAxis(sumOf,'row');// rowSums = [6, 15]varcolumnSums=M.applyAlongAxis(sumOf,'column');// columnSums = [5, 7, 9]The callback receives each row or column as a plain array along with its index, so any reduction can be expressed with it.
varz=Matrix.zeros(3,2);// z = Matrix [[0, 0], [0, 0], [0, 0], rows: 3, columns: 2]varz=Matrix.ones(2,3);// z = Matrix [[1, 1, 1], [1, 1, 1], rows: 2, columns: 3]varz=Matrix.eye(3,4);// z = Matrix [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], rows: 3, columns: 4]. there are 1 only in the diagonalvarM=newMatrix([[1,2],[3,4],]);varstacked=M.concat([[5,6]]);// stacked = Matrix [[1, 2], [3, 4], [5, 6], rows: 3, columns: 2]varwidened=M.concat(Matrix.columnVector([5,6]),'column');// widened = Matrix [[1, 2, 5], [3, 4, 6], rows: 2, columns: 3]Concatenating by row needs the same number of columns on both sides, concatenating by column needs the same number of rows. The two operands are left untouched.
const{
Matrix,
inverse,
solve,
linearDependencies,
QrDecomposition,
LuDecomposition,
CholeskyDecomposition,
EigenvalueDecomposition,}=require('ml-matrix');varA=newMatrix([[2,3,5],[4,1,6],[1,3,0],]);varinverseA=inverse(A);varB=A.mmul(inverseA);// B = A * inverse(A), so B ~= Identity// if A is singular, you can use SVD :varA=newMatrix([[1,2,3],[4,5,6],[7,8,9],]);// A is singular, so the standard computation of inverse won't work (you can test if you don't trust me^^)varinverseA=inverse(A,(useSVD=true));// inverseA is only an approximation of the inverse, by using the Singular Values DecompositionvarB=A.mmul(inverseA);// B = A * inverse(A), but inverse(A) is only an approximation, so B doesn't really be identity.// if you want the pseudo-inverse of a matrix :varA=newMatrix([[1,2],[3,4],[5,6],]);varpseudoInverseA=A.pseudoInverse();varB=A.mmul(pseudoInverseA).mmul(A);// with pseudo inverse, A*pseudo-inverse(A)*A ~= A. It's the case hereLeast square is the following problem: We search for x, such that A.x = B (A, x and B are matrix or vectors).
Below, how to solve least square with our function
// If A is non singular :varA=newMatrix([[3,1],[4.25,1],[5.5,1],[8,1],]);varB=Matrix.columnVector([4.5,4.25,5.5,5.5]);varx=solve(A,B);varerror=Matrix.sub(B,A.mmul(x));// The error enables to evaluate the solution x found.// If A is non singular :varA=newMatrix([[1,2,3],[4,5,6],[7,8,9],]);varB=Matrix.columnVector([8,20,32]);varx=solve(A,B,(useSVD=true));// there are many solutions. x can be [1, 2, 1].transpose(), or [1.33, 1.33, 1.33].transpose(), etc.varerror=Matrix.sub(B,A.mmul(x));// The error enables to evaluate the solution x found.varA=newMatrix([[2,3,5],[4,1,6],[1,3,0],]);varQR=newQrDecomposition(A);varQ=QR.orthogonalMatrix;varR=QR.upperTriangularMatrix;// So you have the QR decomposition. If you multiply Q by R, you'll see that A = Q.R, with Q orthogonal and R upper triangularvarA=newMatrix([[2,3,5],[4,1,6],[1,3,0],]);varLU=newLuDecomposition(A);varL=LU.lowerTriangularMatrix;varU=LU.upperTriangularMatrix;varP=LU.pivotPermutationVector;// So you have the LU decomposition. P includes the permutation of the matrix. Here P = [1, 2, 0], i.e the first row of LU is the second row of A, the second row of LU is the third row of A and the third row of LU is the first row of A.varA=newMatrix([[2,3,5],[4,1,6],[1,3,0],]);varcholesky=newCholeskyDecomposition(A);varL=cholesky.lowerTriangularMatrix;varA=newMatrix([[2,3,5],[4,1,6],[1,3,0],]);vare=newEigenvalueDecomposition(A);varreal=e.realEigenvalues;varimaginary=e.imaginaryEigenvalues;varvectors=e.eigenvectorMatrix;varA=newMatrix([[2,0,0,1],[0,1,6,0],[0,3,0,1],[0,0,1,0],[0,1,2,0],]);vardependencies=linearDependencies(A);// dependencies is a matrix with the dependencies of the rows. When we look row by row, we see that the first row is [0, 0, 0, 0, 0], so it means that the first row is independent, and the second row is [ 0, 0, 0, 4, 1 ], i.e the second row = 4 times the 4th row + the 5th row.