const { Matrix } = require('ml-matrix');var A = new Matrix([ [1, 1], [2, 2],]);var B = new Matrix([ [3, 3], [1, 1],]);var C = new Matrix([ [3, 3], [1, 1],]);
// remember: A = Matrix [[1, 1], [-1, -1], rows: 2, columns: 2]var numberRows = A.rows; // A has 2 rowsvar numberCols = A.columns; // A has 2 columnsvar firstValue = A.get(0, 0); // get(rows, columns)var numberElements = A.size; // 2 * 2 = 4 elementsvar isRow = A.isRowVector(); // false because A has more than 1 rowvar isColumn = A.isColumnVector(); // false because A has more than 1 columnvar isSquare = A.isSquare(); // true, because A is 2 * 2 matrixvar isSym = A.isSymmetric();
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.
var A = new Matrix([ [2, 3, 5], [4, 1, 6], [1, 3, 0],]);var inverseA = inverse(A);var B = A.mmul(inverseA); // B = A * inverse(A), so B ~= Identity// if A is singular, you can use SVD :var A = new Matrix([ [1, 2, 3], [4, 5, 6
// if you want the pseudo-inverse of a matrix :var A = new Matrix([ [1, 2], [3, 4], [5, 6],]);var pseudoInverseA = A.pseudoInverse();var B = A.mmul(pseudoInverseA).mmul(A); // with pseudo inverse, A*pseudo-inverse(A)*A ~= A. It's the case here
Least square
Least 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 :var A = new Matrix([ [3, 1], [4.25, 1], [5.5, 1], [8, 1],]);var B = Matrix.columnVector([4.5, 4.25, 5.5, 5.5]);var x = solve(A, B);var error = Matrix.sub(B, A.mmul(x)); // The error enables to evaluate the solution x found.
// If A is non singular :var A = new Matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9],]);var B = Matrix.columnVector([8, 20, 32]);var x = 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.var error = Matrix.sub(B, A.mmul(x)); // The error enables to evaluate the solution x found.
Decompositions
QR Decomposition
var A = new Matrix([ [2, 3, 5], [4, 1, 6], [1, 3, 0],]);var QR = new QrDecomposition(A);var Q = QR.orthogonalMatrix;var R = 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 triangular
LU Decomposition
var A = new Matrix([ [2, 3, 5], [4, 1, 6], [1, 3, 0],]);var LU = new LuDecomposition(A);var L = LU.lowerTriangularMatrix;var U = LU.upperTriangularMatrix;var P = 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.
Cholesky Decomposition
var A = new Matrix([ [2, 3, 5], [4, 1, 6], [1, 3, 0],]);var cholesky = new CholeskyDecomposition(A);var L = cholesky.lowerTriangularMatrix;
Eigenvalues & eigenvectors
var A = new Matrix([ [2, 3, 5], [4, 1, 6], [1, 3, 0],]);var e = new EigenvalueDecomposition(A);var real = e.realEigenvalues;var imaginary = e.imaginaryEigenvalues;var vectors = e.eigenvectorMatrix;
Linear dependencies
var A = new Matrix([ [2, 0, 0, 1], [0, 1, 6, 0], [0, 3, 0, 1], [0, 0, 1, 0], [0, 1, 2, 0],]);var dependencies = 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.