{"id":13671645,"url":"https://github.com/mljs/matrix","last_synced_at":"2025-05-14T05:00:28.032Z","repository":{"id":22179388,"uuid":"25511318","full_name":"mljs/matrix","owner":"mljs","description":"Matrix manipulation and computation library","archived":false,"fork":false,"pushed_at":"2025-03-11T08:25:25.000Z","size":5349,"stargazers_count":366,"open_issues_count":20,"forks_count":54,"subscribers_count":15,"default_branch":"main","last_synced_at":"2025-04-13T21:27:53.197Z","etag":null,"topics":["javascript","machine-learning","matrix","ml"],"latest_commit_sha":null,"homepage":"https://mljs.github.io/matrix/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mljs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-10-21T08:33:17.000Z","updated_at":"2025-04-08T06:02:58.000Z","dependencies_parsed_at":"2024-01-14T17:03:50.600Z","dependency_job_id":"7a4a3238-b54e-4716-bd4b-2541da1a9347","html_url":"https://github.com/mljs/matrix","commit_stats":{"total_commits":376,"total_committers":34,"mean_commits":"11.058823529411764","dds":"0.43882978723404253","last_synced_commit":"2b98189295e297a6c3a48f76ca05ca0641d14e2b"},"previous_names":[],"tags_count":73,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fmatrix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fmatrix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fmatrix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fmatrix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mljs","download_url":"https://codeload.github.com/mljs/matrix/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254076329,"owners_count":22010596,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","machine-learning","matrix","ml"],"created_at":"2024-08-02T09:01:15.324Z","updated_at":"2025-05-14T05:00:27.673Z","avatar_url":"https://github.com/mljs.png","language":"JavaScript","readme":"# ml-matrix\n\nMatrix manipulation and computation library.\n\n\u003ch3 align=\"center\"\u003e\n\n  \u003ca href=\"https://www.zakodium.com\"\u003e\n    \u003cimg src=\"https://www.zakodium.com/brand/zakodium-logo-white.svg\" width=\"50\" alt=\"Zakodium logo\" /\u003e\n  \u003c/a\u003e\n\n  \u003cp\u003e\n    Maintained by \u003ca href=\"https://www.zakodium.com\"\u003eZakodium\u003c/a\u003e\n  \u003c/p\u003e\n\n[![NPM version][npm-image]][npm-url]\n[![build status][ci-image]][ci-url]\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.5644534.svg)](https://doi.org/10.5281/zenodo.5644534)\n[![npm download][download-image]][download-url]\n\n\u003c/h3\u003e\n\n## Installation\n\n`$ npm install ml-matrix`\n\n## Usage\n\n### As an ES module\n\n```js\nimport { Matrix } from 'ml-matrix';\n\nconst matrix = Matrix.ones(5, 5);\n```\n\n### As a CommonJS module\n\n```js\nconst { Matrix } = require('ml-matrix');\n\nconst matrix = Matrix.ones(5, 5);\n```\n\n## [API Documentation](https://mljs.github.io/matrix/)\n\n## Examples\n\n### Standard operations\n\n```js\nconst { Matrix } = require('ml-matrix');\n\nvar A = new Matrix([\n  [1, 1],\n  [2, 2],\n]);\n\nvar B = new Matrix([\n  [3, 3],\n  [1, 1],\n]);\n\nvar C = new Matrix([\n  [3, 3],\n  [1, 1],\n]);\n```\n\n#### Operations\n```js\nconst addition       = Matrix.add(A, B);   // addition       = Matrix [[4, 4], [3, 3], rows: 2, columns: 2]\nconst subtraction    = Matrix.sub(A, B);   // subtraction    = Matrix [[-2, -2], [1, 1], rows: 2, columns: 2]\nconst multiplication = A.mmul(B);          // multiplication = Matrix [[4, 4], [8, 8], rows: 2, columns: 2]\nconst mulByNumber    = Matrix.mul(A, 10);  // mulByNumber    = Matrix [[10, 10], [20, 20], rows: 2, columns: 2]\nconst divByNumber    = Matrix.div(A, 10);  // divByNumber    = Matrix [[0.1, 0.1], [0.2, 0.2], rows: 2, columns: 2]\nconst modulo         = Matrix.mod(B, 2);   // modulo         = Matrix [[1, 1], [1, 1], rows: 2, columns: 2]\nconst maxMatrix      = Matrix.max(A, B);   // max            = Matrix [[3, 3], [2, 2], rows: 2, columns: 2]\nconst minMatrix      = Matrix.min(A, B);   // max            = Matrix [[1, 1], [1, 1], rows: 2, columns: 2]\n```\n\n#### Inplace Operations\n```js\nC.add(A);   // =\u003e C = C + A\nC.sub(A);   // =\u003e C = C - A\nC.mul(10);  // =\u003e C = 10 * C\nC.div(10);  // =\u003e C = C / 10\nC.mod(2);   // =\u003e C = C % 2\n```\n\n#### Math Operations\n```js\n// Standard Math operations: (abs, cos, round, etc.)\nvar A = new Matrix([\n  [ 1,  1],\n  [-1, -1],\n]);\n\nvar exponential = Matrix.exp(A);  // exponential = Matrix [[Math.exp(1), Math.exp(1)], [Math.exp(-1), Math.exp(-1)], rows: 2, columns: 2].\nvar cosinus     = Matrix.cos(A);  // cosinus     = Matrix [[Math.cos(1), Math.cos(1)], [Math.cos(-1), Math.cos(-1)], rows: 2, columns: 2].\nvar absolute    = Matrix.abs(A);  // absolute    = Matrix [[1, 1], [1, 1], rows: 2, columns: 2].\n// Note: you can do it inplace too as A.abs()\n```\nAvailable Methods:\n```js\nabs, 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\n```\n#### Manipulation of the matrix\n```js\n// remember: A = Matrix [[1, 1], [-1, -1], rows: 2, columns: 2]\n\nvar numberRows     = A.rows;             // A has 2 rows\nvar numberCols     = A.columns;          // A has 2 columns\nvar firstValue     = A.get(0, 0);        // get(rows, columns)\nvar numberElements = A.size;             // 2 * 2 = 4 elements\nvar isRow          = A.isRowVector();    // false because A has more than 1 row\nvar isColumn       = A.isColumnVector(); // false because A has more than 1 column\nvar isSquare       = A.isSquare();       // true, because A is 2 * 2 matrix\nvar isSym          = A.isSymmetric();    // false, because A is not symmetric\nA.set(1, 0, 10);                         // A = Matrix [[1, 1], [10, -1], rows: 2, columns: 2]. We have changed the second row and the first column\nvar diag           = A.diag();           // diag = [1, -1] (values in the diagonal)\nvar m              = A.mean();           // m = 2.75\nvar product        = A.prod();           // product = -10 (product of all values of the matrix)\nvar norm           = A.norm();           // norm = 10.14889156509222 (Frobenius norm of the matrix)\nvar transpose      = A.transpose();      // transpose = Matrix [[1, 10], [1, -1], rows: 2, columns: 2]\n```\n\n#### Instantiation of matrix\n```js\nvar z = Matrix.zeros(3, 2); // z = Matrix [[0, 0], [0, 0], [0, 0], rows: 3, columns: 2]\nvar z = Matrix.ones(2, 3);  // z = Matrix [[1, 1, 1], [1, 1, 1], rows: 2, columns: 3]\nvar z = 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 diagonal\n```\n\n### Maths\n```js\nconst {\n  Matrix,\n  inverse,\n  solve,\n  linearDependencies,\n  QrDecomposition,\n  LuDecomposition,\n  CholeskyDecomposition,\n  EigenvalueDecomposition,\n} = require('ml-matrix');\n```\n#### Inverse and Pseudo-inverse\n```js\nvar A = new Matrix([\n  [2, 3, 5],\n  [4, 1, 6],\n  [1, 3, 0],\n]);\n\nvar inverseA = inverse(A);\nvar B = A.mmul(inverseA); // B = A * inverse(A), so B ~= Identity\n\n\n// if A is singular, you can use SVD :\nvar A = new Matrix([\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n]); \n// A is singular, so the standard computation of inverse won't work (you can test if you don't trust me^^)\n\nvar inverseA = inverse(A, (useSVD = true)); // inverseA is only an approximation of the inverse, by using the Singular Values Decomposition\nvar B = A.mmul(inverseA); // B = A * inverse(A), but inverse(A) is only an approximation, so B doesn't really be identity.\n```\n```js\n// if you want the pseudo-inverse of a matrix :\nvar A = new Matrix([\n  [1, 2],\n  [3, 4],\n  [5, 6],\n]);\n\nvar pseudoInverseA = A.pseudoInverse();\nvar B = A.mmul(pseudoInverseA).mmul(A); // with pseudo inverse, A*pseudo-inverse(A)*A ~= A. It's the case here\n```\n#### Least square\nLeast square is the following problem: We search for `x`, such that `A.x = B` (`A`, `x` and `B` are matrix or vectors).\nBelow, how to solve least square with our function\n```js\n// If A is non singular :\nvar A = new Matrix([\n  [3,    1],\n  [4.25, 1],\n  [5.5,  1],\n  [8,    1],\n]);\n\nvar B = Matrix.columnVector([4.5, 4.25, 5.5, 5.5]);\nvar x = solve(A, B);\nvar error = Matrix.sub(B, A.mmul(x)); // The error enables to evaluate the solution x found.\n```\n```js\n// If A is non singular :\nvar A = new Matrix([\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n]);\n\nvar B = Matrix.columnVector([8, 20, 32]);\nvar 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.\nvar error = Matrix.sub(B, A.mmul(x)); // The error enables to evaluate the solution x found.\n```\n#### Decompositions\n\n##### QR Decomposition\n```js\nvar A = new Matrix([\n  [2, 3, 5],\n  [4, 1, 6],\n  [1, 3, 0],\n]);\n\nvar QR = new QrDecomposition(A);\nvar Q = QR.orthogonalMatrix;\nvar R = QR.upperTriangularMatrix;\n// 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\n```\n##### LU Decomposition\n```js\nvar A = new Matrix([\n  [2, 3, 5],\n  [4, 1, 6],\n  [1, 3, 0],\n]);\n\nvar LU = new LuDecomposition(A);\nvar L = LU.lowerTriangularMatrix;\nvar U = LU.upperTriangularMatrix;\nvar P = LU.pivotPermutationVector;\n// 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.\n```\n##### Cholesky Decomposition\n```js\nvar A = new Matrix([\n  [2, 3, 5],\n  [4, 1, 6],\n  [1, 3, 0],\n]);\n\nvar cholesky = new CholeskyDecomposition(A);\nvar L = cholesky.lowerTriangularMatrix;\n```\n##### Eigenvalues \u0026 eigenvectors\n```js\nvar A = new Matrix([\n  [2, 3, 5],\n  [4, 1, 6],\n  [1, 3, 0],\n]);\n\nvar e = new EigenvalueDecomposition(A);\nvar real = e.realEigenvalues;\nvar imaginary = e.imaginaryEigenvalues;\nvar vectors = e.eigenvectorMatrix;\n```\n#### Linear dependencies\n```js\nvar A = new Matrix([\n  [2, 0, 0, 1],\n  [0, 1, 6, 0],\n  [0, 3, 0, 1],\n  [0, 0, 1, 0],\n  [0, 1, 2, 0],\n]);\n\nvar dependencies = linearDependencies(A);\n// 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.\n```\n\n## License\n\n[MIT](./LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/ml-matrix.svg\n[npm-url]: https://npmjs.org/package/ml-matrix\n[ci-image]: https://github.com/mljs/matrix/workflows/Node.js%20CI/badge.svg?branch=main\n[ci-url]: https://github.com/mljs/matrix/actions?query=workflow%3A%22Node.js+CI%22\n[download-image]: https://img.shields.io/npm/dm/ml-matrix.svg\n[download-url]: https://npmjs.org/package/ml-matrix\n","funding_links":[],"categories":["Math and processing","JavaScript"],"sub_categories":["Pure math"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fmatrix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmljs%2Fmatrix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fmatrix/lists"}