# functional-matrix

0.1.0 • Public • Published

# Functional Matrix

An easy-to-use library for working with two-dimensional arrays and matrices. Most functional and array methods are implemented here, including `each`, `map`, `reduce`, `zipWith`, `every`, `push`, `pop`, `slice`, and `concat`. Standard numerical methods, such as `multiply` and `determinant`, are also included, along with some common examples, like the identity and 2d-rotation matrices.

Functional Matrix is fully tested and throws clear error messages to aid in your debugging.

## Method List

### Class methods

• new Matrix(height, width, value)
• new Matrix(height, width, fillFunction(row, col))
• new Matrix(arrayOfArrays)
• .identity(size)
• .rotation(radians) - 2d rotation matrix for given angle
• .rotationRadians(radians) - alias of `rotation`
• .rotationDegrees(degrees)
• .vector(array) - a matrix with one row
• .vectorHorizontal(array) - alias of `rector`
• .vectorVertical(array) - like vector, but transposed

### Basics

• .to2dArray()
• .toString()
• .copy()
• .clone() - alias of `copy`
• .equals(matrix)
• .equalsRow(rowIdx, array) - tests if row is equal to given array
• .equalsRow(rowIdx, matrix) - tests if row is equal to row in other matrix
• .equalsCol(colIdx, array)
• .equalsCol(colIdx, matrix)
• .size() - returns an object {rows: x, cols: y};
• .rows() - row count
• .cols() - col count
• .withinBounds(rowIdx, colIdx) - boolean
• .get(rowIdx, colIdx)
• .getRow(rowIdx) - returns a copy of the indicated row
• .getCol(colIdx)
• .set(rowIdx, colIdx, newValue)
• .setRow(rowIdx, newValue) - all entries are set to newValue
• .setRow(rowIdx, arrayOfNewValues) - entries set to values in array
• .setRow(rowIdx, function(colIdx)) - entries are calculated by given function
• .setCol(colIdx, newValue)
• .setCol(colIdx, arrayOfNewValues)
• .setCol(colIdx, function(rowIdx))
• .fill(fillFunction(rowIdx, colIdx)) - sets all values
• .clear() - sets all values to undefined

### Functional Methods

All functional methods by default iterate from left to right, top to bottom. Vertical versions are also provided, though. All methods are NOT in-place and return copies.

• .map(iterator(value, rowIdx, colIdx, matrix))
• .reduce(iterator(acc, value, rowIdx, colIdx, matrix))
• .reduceHorizontal - alias of `reduce`
• .reduceVertical
• .reduceRows(iterator(acc, value, row), initial) - passes each row to iterator, returns an array of results
• .reduceCols(iterator(acc, value, col), initial) - initial can be a value or an array of values, one per column
• .each(iterator(value, rowIdx, colIdx, matrix))
• .eachHorizontal - alias of `each`
• .eachVertical
• .eachRow(iterator(row, rowIdx, matrix)) - passes each row array to the iterator
• .eachCol(iterator(col, colIdx, matrix))
• .every(iterator(value, rowIdx, colIdx))
• .some(iterator(value, rowIdx, colIdx))
• .zipWith(otherMatrix, iterator(thisElem, otherElem, i, j, thisMatrix, otherMatrix)) - like `map` but with two sources of the same size
• .pluck(property) - maps to named property of each element
• .invoke(method) - maps to result of invoking named method of each element
• .sample() - returns random element
• .sample(n) - returns array of n random elements
• .shuffle() - shuffles elements but maintains size

coming soon

• Support for currying and partial application!
• Right/bottom versions of each/reduce

### Numerical methods

All methods are NOT in-place and return copies.

• .plus(number)
• .plus(matrix)
• .add - alias of `plus`
• .minus(number)
• .minus(matrix)
• .subtract - alias of `minus`
• .times(number)
• .times(matrix)
• .multiply - alias of `times`
• .mod(number)
• .round(digit) - rounds all values to the given precision
• .determinant()
• .rowMultiply(fromIdx, toIdx, multiple) - add multiple of fromRow to toRow

coming soon

• .inverse()
• .upperTriangular()
• .solveSystem(values)
• .eigenvalues()

### Transforming methods

These work the same as the familiar array methods, except they take/return arrays. Push/pop/shift/unshift are in-place, to match arrays. All other methods return new matrices.

• .slice(startRow, startCol, endRow, endCol) - negative indices behave like array slice
• .submatrix() - alias of `slice`
• .pushRow(newRow) - returns new row count
• .pushCol(newCol)
• .unshiftRow(newRow)
• .unshiftCol(newCol)
• .popRow()
• .popCol()
• .shiftRow()
• .shiftCol()
• .concat(matrix)
• .concatHorizontal - alias of `concat`
• .concatVertical(matrix)
• .minor(rowIdx, colIdx) - the matrix after removing the given row and column
• .transpose()
• .swapRows(rowIdx1, rowIdx2)
• .swapCols(colIdx1, colIdx2)
• .rotateCW()
• .rotateCCW()
• .flipHorizontal()
• .flipVertical()

### Query methods

• .contains(elem)
• .indexOf(elem) - finds the first index; returns {row: x, col: y} or null
• .indexesOf(elem) - returns array of all matches
• .count(elem)
• .replace(elem, newElem) - returns copy

coming soon

• Support for functions and regexes in .replace

## Package Sidebar

### Install

`npm i functional-matrix`

### Repository

github.com/peterkhayes/functional-matrix

### Homepage

github.com/peterkhayes/functional-matrix

0

0.1.0