{"id":15355811,"url":"https://github.com/fibo/matrix-multiplication","last_synced_at":"2026-05-24T07:30:18.189Z","repository":{"id":57292535,"uuid":"55605308","full_name":"fibo/matrix-multiplication","owner":"fibo","description":"implements row by column multiplication","archived":false,"fork":false,"pushed_at":"2020-01-24T01:13:22.000Z","size":33,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-03T09:03:46.397Z","etag":null,"topics":["matrices","matrix-multiplication","square-matrices"],"latest_commit_sha":null,"homepage":"http://g14n.info/matrix-multiplication","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/fibo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["fibo"],"patreon":"fibo"}},"created_at":"2016-04-06T12:49:38.000Z","updated_at":"2020-01-24T01:13:25.000Z","dependencies_parsed_at":"2022-08-27T12:20:56.684Z","dependency_job_id":null,"html_url":"https://github.com/fibo/matrix-multiplication","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fmatrix-multiplication","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fmatrix-multiplication/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fmatrix-multiplication/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fmatrix-multiplication/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fibo","download_url":"https://codeload.github.com/fibo/matrix-multiplication/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240136987,"owners_count":19753645,"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":["matrices","matrix-multiplication","square-matrices"],"created_at":"2024-10-01T12:25:42.801Z","updated_at":"2026-05-24T07:30:18.125Z","avatar_url":"https://github.com/fibo.png","language":"JavaScript","funding_links":["https://github.com/sponsors/fibo","https://patreon.com/fibo"],"categories":[],"sub_categories":[],"readme":"# matrix-multiplication\n\n\u003e implements row by column multiplication\n\n[Installation](#installation) |\n[API](#api) |\n[Examples](#examples) |\n[License](#license)\n\n[![NPM version](https://badge.fury.io/js/matrix-multiplication.svg)](http://badge.fury.io/js/matrix-multiplication)\n[![Build Status](https://travis-ci.org/fibo/matrix-multiplication.svg?branch=master)](https://travis-ci.org/fibo/matrix-multiplication?branch=master)\n[![Dependency Status](https://gemnasium.com/fibo/matrix-multiplication.svg)](https://gemnasium.com/fibo/matrix-multiplication)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n## Installation\n\nWith [npm](https://npmjs.org/) do\n\n```bash\nnpm install matrix-multiplication\n```\n\n## API\n\nOptional custom operators are supported.\n\n### `var operator = matrixMultiplication([customOperator])`\n\n* **@param** `{Object}` **[customOperator]**\n* **@param** `{Function}` **[customOperator.addition]** defaults to common `+`\n* **@param** `{Function}` **[customOperator.multiplication]** defaults to common `*`\n* **@returns** `{Function}` **operator**\n\n### `var mul = operator(middle, leftMatrix, rightMatrix)`\n\nThe only requirement needed to multiply row by column an **a x b** matrix by\nan **c x d** matrix is that `b = c`, i.e. the middle indexes are equal.\nActually two compatible matrices are **n x m** and **m x l**, let's call **m** the **middle**.\n\n* **@param** `{Number}` **middle**\n* **@returns** `{Function}` **mul**\n\n### `var matrix = mul(leftMatrix, rightMatrix)`\n\nFinally we have the matrix multiplication function. Remember that is **not** a\ncommutative operator.\n\n* **@param** `{Array}` **leftMatrix**\n* **@param** `{Array}` **rightMatrix**\n* **@returns** `{Array}` **matrix**\n\n### `matrixMultiplication.error`\n\nAn object exposing the following error messages:\n\n* [leftMatrixNotCompatible](#leftmatrixnotcompatible)\n* [rightMatrixNotCompatible](#rightmatrixnotcompatible)\n\n## Examples\n\nAll code in the examples below is intended to be contained into a [single file](https://github.com/fibo/matrix-multiplication/blob/master/test.js).\n\nSquare matrices **2x2**\n\n```javascript\nvar matrixMultiplication = require('matrix-multiplication')\n\nvar mul = matrixMultiplication()(2)\n\nvar leftMatrix = [2, 3,\n                  1, 1]\n\nvar rightMatrix = [0, 1,\n                  -1, 0]\n\nmul(leftMatrix, rightMatrix) // [-3, 2,\n                             //  -1, 1]\n```\n\nActually, any pair of matrices with *middle* = 2 can be multiplied with the same `mul`\nfunction, try with a **3x2** by **2x4**\n\n```javascript\nvar matrix3x2 = [2, 3,\n                 1, 1,\n                 1, 1]\n\nvar matrix2x4 = [0, 1, 1, 1,\n                -1, 0, 2, 3]\n\nmul(matrix3x2, matrix2x4) // [-3, 2, 8, 11,\n                          //  -1, 1, 3, 4,\n                          //  -1, 1, 3, 4])\n```\n\nMatrices are checked for compatibility, for instances the following snippets will throw.\n\n### leftMatrixNotCompatible\n\nSince *mul* was defined as a multiplication with *middle* index 2, left matrix is\nnot compatible cause it has 3 columns.\n\n```javascript\nmul([1, 2, 3,\n     4, 5, 6,\n     7, 8, 9], [1, 2,\n                3, 4])\n```\n\n### rightMatrixNotCompatible\n\nSince *mul* was defined as a multiplication with *middle* index 2, right matrix is\nnot compatible cause it has 3 rows.\n\n```javascript\nmul([1, 2,\n     3, 4], [1, 2, 3,\n             4, 5, 6,\n             7, 8, 9])\n```\n\nYou can also multiply over a custom field, just provide a *customOperator* object with\nan *addition* and a *multiplication* function.\nThe following example shows multiplication of two square matrices of booleans.\n\n\u003e Matrices of strings are left as an excercise to the reader.\n\n```javascript\nfunction booleanAdd (a, b) { return a || b }\nfunction booleanMul (a, b) { return a \u0026\u0026 b }\n\nvar customOperators = {\n  addition: booleanAdd,\n  multiplication: booleanMul\n}\n\nvar mulB = matrixMultiplication(customOperators)(3)\n\nvar y = true\nvar n = false\n\nvar matrix = [n, y, n,\n              y, n, y,\n              n, y, n]\n\nvar identity = [y, n, n\n                n, y, n,\n                n, n, y]\n\nmulB(matrix, identity) // [n, y, n,\n                       //  y, n, y,\n                       //  n, y, n]\n```\n\n## License\n\n[MIT](http://g14n.info/mit-license/)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibo%2Fmatrix-multiplication","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffibo%2Fmatrix-multiplication","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibo%2Fmatrix-multiplication/lists"}