npm's personal messenger

# npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

## matrix-multiplication

0.5.2 • Public • Published

# matrix-multiplication

implements row by column multiplication

With npm do

## API

Optional custom operators are supported.

### `var operator = matrixMultiplication([customOperator])`

• @param `{Object}` [customOperator]
• @param `{Function}` [customOperator.addition] defaults to common `+`
• @param `{Function}` [customOperator.multiplication] defaults to common `*`
• @returns `{Function}` operator

### `var mul = operator(middle, leftMatrix, rightMatrix)`

The only requirement needed to multiply row by column an a x b matrix by an c x d matrix is that `b = c`, i.e. the middle indexes are equal. Actually two compatible matrices are n x m and m x l, let's call m the middle.

• @param `{Number}` middle
• @returns `{Function}` mul

### `var matrix = mul(leftMatrix, rightMatrix)`

Finally we have the matrix multiplication function. Remember that is not a commutative operator.

• @param `{Array}` leftMatrix
• @param `{Array}` rightMatrix
• @returns `{Array}` matrix

### `matrixMultiplication.error`

An object exposing the following error messages:

## Examples

All code in the examples below is intended to be contained into a single file.

Square matrices 2x2

Actually, any pair of matrices with middle = 2 can be multiplied with the same `mul` function, try with a 3x2 by 2x4

Matrices are checked for compatibility, for instances the following snippets will throw.

### leftMatrixNotCompatible

Since mul was defined as a multiplication with middle index 2, left matrix is not compatible cause it has 3 columns.

### rightMatrixNotCompatible

Since mul was defined as a multiplication with middle index 2, right matrix is not compatible cause it has 3 rows.

You can also multiply over a custom field, just provide a customOperator object with an addition and a multiplication function. The following example shows multiplication of two square matrices of booleans.

Matrices of strings are left as an excercise to the reader.

MIT

## Keywords

### install

`npm i matrix-multiplication`

21

0.5.2

MIT

g14n.info

github