{"id":26145758,"url":"https://github.com/gladchinda/light-matrix","last_synced_at":"2025-07-01T06:33:04.375Z","repository":{"id":58234831,"uuid":"129084126","full_name":"gladchinda/light-matrix","owner":"gladchinda","description":"Lightweight JavaScript library for basic matrix computations.","archived":false,"fork":false,"pushed_at":"2018-04-17T21:23:44.000Z","size":158,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-14T04:16:09.154Z","etag":null,"topics":["bower","computations","javascript","library","matrix","nodejs"],"latest_commit_sha":null,"homepage":"","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/gladchinda.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-11T11:37:36.000Z","updated_at":"2020-09-15T08:39:51.000Z","dependencies_parsed_at":"2022-08-31T09:21:15.556Z","dependency_job_id":null,"html_url":"https://github.com/gladchinda/light-matrix","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/gladchinda/light-matrix","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gladchinda%2Flight-matrix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gladchinda%2Flight-matrix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gladchinda%2Flight-matrix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gladchinda%2Flight-matrix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gladchinda","download_url":"https://codeload.github.com/gladchinda/light-matrix/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gladchinda%2Flight-matrix/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262913621,"owners_count":23383746,"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":["bower","computations","javascript","library","matrix","nodejs"],"created_at":"2025-03-11T04:54:55.025Z","updated_at":"2025-07-01T06:33:04.326Z","avatar_url":"https://github.com/gladchinda.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lightmatrix\n\n\u003e Lightweight JavaScript library for basic matrix computations.\n\n1. [Installation and Usage](#installation-and-usage)\n\t- [Node Usage](#node-usage)\n\t- [Browser Usage](#browser-usage)\n2. [API Reference](#api-reference)\n\t- [Matrix Specification](#matrix-specification)\n\t- [`lightmatrix` Methods](#lightmatrix-methods)\n3. [License](#license)\n\n\u003cbr/\u003e\n\n## Installation and Usage\n\n### Node Usage\n\nYou can install the package in Node using either `npm` or `yarn`. Simply run any of the following command on your terminal. For `npm`, run the following command.\n\n```shell\nnpm install lightmatrix --save\n```\n\nIf you are using `yarn`, run the following command instead.\n\n```shell\nyarn add lightmatrix\n```\n\nYou can also install the package with `bower` using the following command.\n\n```shell\nbower install lightmatrix\n```\n\nIf you installed the package using `npm` or `yarn`, you can then require it in your code as follows.\n\n```js\nconst lightmatrix = require('lightmatrix');\n\nconst matrix = [\n  [1, 2],\n  [3, 4]\n];\n\nconsole.log(lightmatrix.determinant(matrix)); // -2\n```\n\nNote that installing the package with `bower` will place the package files in the `bower_components` directory of your project.\n\n### Browser Usage\n\nOn the browser, you can use the package by adding the following `\u003cscript\u003e` tag to your code.\n\n```html\n\u003cscript src=\"https://unpkg.com/lightmatrix/dist/lightmatrix.js\"\u003e\u003c/script\u003e\n```\n\n**Minified**  \nAt the moment, the minified version of the package is approximately (~3KB).\n\n```html\n\u003cscript src=\"https://unpkg.com/lightmatrix/dist/lightmatrix.min.js\"\u003e\u003c/script\u003e\n```\n\nThe package adds the namespace: `lightmatrix` to the global `window` object when used in the browser and all its methods can be accessed via that namespace. Hence, you can do the following somewhere else in your script:\n\n```js\nif (lightmatrix in window) {\n\n  var matrix = [\n    [1, 2],\n    [3, 4]\n  ];\n\n  console.log(lightmatrix.determinant(matrix)); // -2\n\n}\n```\n\n## API Reference\n\n### Matrix Specification\n\nA _matrix_ is specified using the array literal notation. The `matrix` is an **array of rows**, whereas each `row` is an **array of column elements**. A `column element` must be a `numeric` or `number` value.\n\nHere is a valid 2x3 matrix (2 rows, 3 cols):\n\n```js\nvar matrix = [\n  [1, 2, 3], // Row 1 (3 column elements)\n  [4, 5, 6] // Row 2 (3 column elements)\n];\n```\n\n### `lightmatrix` Methods\n\nHere are the available methods of the `lightmatrix` module:\n\n- [ok](#lightmatrixokmatrix)\n- [dimension](#lightmatrixdimensionmatrix)\n- [unit](#lightmatrixunitdimension)\n- [equal](#lightmatrixequalmatrixa-matrixb)\n- [sum](#lightmatrixsummatrixa-matrixb)\n- [product](#lightmatrixproductmatrixorscalara-matrixorscalarb)\n- [transpose](#lightmatrixtransposematrix)\n- [determinant](#lightmatrixdeterminantmatrix)\n- [minors](#lightmatrixminorsmatrix)\n- [cofactors](#lightmatrixcofactorsmatrix)\n- [adjoint](#lightmatrixadjointmatrix)\n- [inverse](#lightmatrixinversematrix)\n\n#### `lightmatrix.ok(matrix)`\n\n**Alias:** `lightmatrix.valid`\n\nTests the input `matrix` argument to verify it is a valid matrix. It returns `true` if it is a valid matrix, otherwise it returns `false`.\n\n```js\nvar matrix = [\n  [1, 2],\n  [3, 4]\n];\n\nconsole.log(lightmatrix.ok(matrix)); // true\nconsole.log(lightmatrix.valid([1, 2, 3])); // false\nconsole.log(lightmatrix.valid(5)); // false\n```\n\n#### `lightmatrix.dimension(matrix)`\n\n**Alias:** `lightmatrix.order`\n\nReturns the order of the `matrix` argument as an array of the format `[rows, cols]`. However, if the `matrix` argument is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid matrix specification.'` is thrown.\n\n```js\nvar matrix = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nconsole.log(lightmatrix.dimension(matrix)); // [2, 3]\n\nconsole.log(lightmatrix.order([1, 2, 3])); // throws Error\nconsole.log(lightmatrix.order(5)); // throws Error\n```\n\n#### `lightmatrix.unit(dimension)`\n\n**Alias:** `lightmatrix.identity`\n\nReturns a new _identity or unit matrix_ matrix of the specified dimension. If the `dimension` argument is `0` or any `non-numeric` value, then a 2x2 identity matrix is returned by default.\n\n```js\nconsole.log(lightmatrix.unit(3)); // [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]\nconsole.log(lightmatrix.identity(0)); // [ [1, 0], [0, 1] ]\n```\n\n#### `lightmatrix.equal(matrixA, matrixB)`\n\n**Alias:** `lightmatrix.same`\n\nCompares the two input matrix arguments `matrixA` and `matrixB` to verify they are of the same order and contain the same elements at all positions. It returns `true` these conditions are met, otherwise it returns `false`. However, if any of the `matrix` arguments is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid matrix specification.'` is thrown.\n\n```js\nvar matrixA = [\n  [1, 2],\n  [4, 5]\n];\n\nvar matrixB = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nconsole.log(lightmatrix.equal(matrixA, matrixB)); // false\nconsole.log(lightmatrix.same(matrixB, matrixB)); // true\n```\n\n#### `lightmatrix.sum(matrixA, matrixB)`\n\n**Alias:** `lightmatrix.add`\n\nAdds the two input matrix arguments `matrixA` and `matrixB` and returns the resulting matrix provided that the input matrices are of the same order. However, if the input matrices are of different orders, an `'Error: Matrices not compatible.'` is thrown. If any of the `matrix` arguments is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid matrix specification.'` is thrown.\n\n```js\nvar matrixA = [\n  [1, 2],\n  [4, 5]\n];\n\nvar matrixB = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nconsole.log(lightmatrix.sum(matrixA, matrixB)); // throws Error\nconsole.log(lightmatrix.add(matrixA, matrixA)); // [ [2, 4], [8, 10] ]\n```\n\n#### `lightmatrix.product(matrixOrScalarA, matrixOrScalarB)`\n\n**Alias:** `lightmatrix.multiply`\n\nMultiplies the two input matrix/scalar arguments `matrixOrScalarA` and `matrixOrScalarB`.\n\nIf _two scalars_ are supplied, then the arithmetic product of the scalars is returned. If _one scalar_ and _one matrix_ are supplied, then the resulting scalar product matrix is returned. However, if _two matrices_ are supplied, it returns the resulting product matrix provided that the matrix product `matrixOrScalarA * matrixOrScalarB` is possible, otherwise an `'Error: Matrices not compatible.'` is thrown.\n\n\u003e **Note:** The scalar is required to strictly be of the `number` type.\n\nIf any of the `matrix` arguments is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid arguments supplied.'` is thrown.\n\n```js\nvar matrixA = [\n  [1, 2],\n  [4, 5]\n];\n\nvar matrixB = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nvar matrixC = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9]\n];\n\nconsole.log(lightmatrix.multiply(-5, 10)); // -50\nconsole.log(lightmatrix.multiply(-5, matrixA)); // [ [-5, -10], [-20, -25] ]\nconsole.log(lightmatrix.product(matrixA, matrixB)); // [ [9, 12, 15], [24, 33, 42] ]\nconsole.log(lightmatrix.product(matrixA, matrixC)); // throws Error\n```\n\n#### `lightmatrix.transpose(matrix)`\n\nTransposes the input `matrix` argument and returns the transposed matrix provided that the input matrix is valid. However, if the `matrix` argument is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid matrix specification.'` is thrown.\n\n```js\nvar matrix = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nconsole.log(lightmatrix.transpose(matrix)); // [ [1, 4], [2, 5], [3, 6] ]\nconsole.log(lightmatrix.transpose([1, 2, 3])); // throws Error\n```\n\n#### `lightmatrix.determinant(matrix)`\n\nComputes and returns the _determinant_ of the `matrix` argument provided that the input matrix is valid and is a square matrix.\n\n\u003e **Note:** The matrix must be a _square matrix_ to be able to compute its determinant otherwise an `'Error: Square matrix required.'` will be thrown.\n\n\u003e **Note:** The determinant is `0` for a _singular matrix_ which means the _inverse_ of such a matrix does not exist.\n\nIf the `matrix` argument is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid matrix specification.'` is thrown.\n\n```js\nvar matrixA = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9]\n];\n\nvar matrixB = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nconsole.log(lightmatrix.determinant(matrixA)); // 0 (singular matrix)\nconsole.log(lightmatrix.determinant(matrixB)); // throws Error (non-square matrix)\n```\n\n#### `lightmatrix.minors(matrix)`\n\nComputes the _minor_ of each element of the `matrix` argument and returns a matrix of the minors provided that the input matrix is valid and is a square matrix.\n\n\u003e **Note:** The matrix must be a _square matrix_ to be able to compute its minors otherwise an `'Error: Square matrix required.'` will be thrown.\n\nif the `matrix` argument is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid matrix specification.'` is thrown.\n\n```js\nvar matrixA = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9]\n];\n\nvar matrixB = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nconsole.log(lightmatrix.minors(matrixA)); // [ [-3, -6, -3], [-6, -12, -6], [-3, -6, -3] ]\nconsole.log(lightmatrix.minors(matrixB)); // throws Error (non-square matrix)\n```\n\n#### `lightmatrix.cofactors(matrix)`\n\nComputes the _cofactor_ of each element of the `matrix` argument and returns a matrix of the cofactors provided that the input matrix is valid and is a square matrix.\n\n\u003e **Note:** The matrix must be a _square matrix_ to be able to compute its cofactors otherwise an `'Error: Square matrix required.'` will be thrown.\n\nif the `matrix` argument is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid matrix specification.'` is thrown.\n\n```js\nvar matrixA = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9]\n];\n\nvar matrixB = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nconsole.log(lightmatrix.cofactors(matrixA)); // [ [-3, 6, -3], [6, -12, 6], [-3, 6, -3] ]\nconsole.log(lightmatrix.cofactors(matrixB)); // throws Error (non-square matrix)\n```\n\n#### `lightmatrix.adjoint(matrix)`\n\nComputes and returns the _adjoint_ (transpose of the cofactors matrix) of the `matrix` argument provided that the input matrix is valid and is a square matrix.\n\n\u003e **Note:** The matrix must be a _square matrix_ to be able to compute its adjoint otherwise an `'Error: Square matrix required.'` will be thrown.\n\nif the `matrix` argument is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid matrix specification.'` is thrown.\n\n```js\nvar matrixA = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9]\n];\n\nvar matrixB = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nconsole.log(lightmatrix.adjoint(matrixA)); // [ [-3, 6, -3], [6, -12, 6], [-3, 6, -3] ]\nconsole.log(lightmatrix.adjoint(matrixB)); // throws Error (non-square matrix)\n```\n\n#### `lightmatrix.inverse(matrix)`\n\nComputes and returns the _inverse_ (scalar product of the adjoint matrix and the reciprocal of the determinant) of the `matrix` argument provided that the input matrix is valid and is a square matrix.\n\n\u003e **Note:** The matrix must be a _square matrix_ to be able to compute its inverse otherwise an `'Error: Square matrix required.'` will be thrown.\n\n\u003e **Note:** The inverse cannot be computed for a _singular matrix_ since its determinant is `0`. Attempting to compute the inverse throws an `'Error: Cannot compute inverse of singular matrix.'`.\n\nif the `matrix` argument is an `array` but an invalid matrix, or a `non-array`, an `'Error: Invalid matrix specification.'` is thrown.\n\n```js\nvar matrixA = [\n  [-8, 10],\n  [-4, 4]\n];\n\nvar matrixB = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9]\n];\n\nvar matrixC = [\n  [1, 2, 3],\n  [4, 5, 6]\n];\n\nconsole.log(lightmatrix.inverse(matrixA)); // [ [0.5, -1.25], [0.5, -1] ]\nconsole.log(lightmatrix.inverse(matrixB)); // throws Error (singular matrix)\nconsole.log(lightmatrix.inverse(matrixC)); // throws Error (non-square matrix)\n```\n\n## License\n\nThe `lightmatrix` package is covered by the **MIT License** (please read carefully).\n\n___\n\n```plain\n\nMIT License\n\nCopyright (c) 2018 Glad Chinda\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgladchinda%2Flight-matrix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgladchinda%2Flight-matrix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgladchinda%2Flight-matrix/lists"}