Matt is available on NPM.
npm install matt
Matt is pretty intuitive to use if already familiar with common matrix operations and transforms. It looks like this, required in Node.js:
var Matt = ;var Matrix = MattMatrix;var assert = ;// list-style (1D Array)var A = 3 31 2 34 5 67 8 9;// table-style (2D Array)var B =1 2 34 5 67 8 9;;;assert;
Also see Seth, my other mathematical DSL for Set Theory.
Matt exposes one core
Matrix ES6 written and ready class with tons of matrix methods. Methods include
All methods are tested and throw appropriate errors when the operation is impossible. For example,
determinant will throw when called on a non-square matrix.
Matrices are immutable. All methods that mutate a matrix will return a new matrix. This means variables will not be overwritten with new data and operations can be chained and composed more functionally.
The method signatures of the Matrix class are listed.
constructor(rows Number, cols Number, elements [Any]) Matrixget(row Number, col Number) Anyset(row Number, col Number, value Any) MatrixgetRow(row Number) [Any]setRow(row Number, elements [Any]) MatrixgetColumn(col Number) [Any]setColumn(col Number, elements [Any]) MatrixgetDiagonal(<offset Number = 0>) [Any]getRightDiagonal(<offset Number = 0>) [Any]trace() NumberrightTrace() Numberadd(matrix Matrix, <reduce Function(elementA Any, elementB Any) Any>) Matrixsubtract(matrix Matrix, <reduce Function(elementA Any, elementB Any) Any>) Matrixmultiply(matrix Matrix, <reduce Function(elementA Any, elementB Any) Any>) MatrixjoinHorizontal(matrix Matrix) MatrixjoinVertical(matrix Matrix) Matrixclone() Matrixmap(fn Function(element Any, row Number, col Number, matrix Matrix)) Matrixfmap(fn Function(element Any, row Number, col Number, matrix Matrix)) MatrixforEach(fn Function(element Any, row Number, col Number, matrix Matrix)) voidreduce(fn Function(acc Any, value Any, row Number, col Number, matrix Matrix), <memo Any = M(0,0)>) Matrixscale(num Number) Matrixtranspose() Matrixidentity() Matrixsubmatrix(topLeftRow Number, topLeftCol Number, bottomRightRow Number, bottomRightCol Number) Matrixminor(row Number, col Number) Matrixcofactor(row Number, col Number) NumbercofactorMatrix() Matrixinvert() Matrixdeterminant() NumberisSquare() booleanequals(matrix Matrix) booleantoArray() [Any]toTable() [[Any]]toString() String
For complete documentation, please refer to the tests as they double as documentation quite well. In the
matt directory run:
npm install && npm run build && npm test
Matt was designed to be ready to embrace these changes. Unlike most matrix implementations, Matt deals with one-dimensional instead of two-dimensional arrays. This decision was made for multiple reasons.
Since V8 and others optimize for array methods, Matt uses built-in functions, initializes arrays where possible, and runs for loops wherever possible to gain those speed boosts. Arrays also enjoy faster lookup than tables (2D arrays) while being less messy.
Typing. It is faster and easier to write
2, 2, [1, 2, 3, 4]than
[[1,2],[3,4]]; more with larger and nested matrices. It is also better for the new ES6 destructuring syntax we all will be using soon enough.
The places where performance is worst is the same as all other implementations: determinants and inversions when
N * N matrices) is large and anything when
N is very, very, very large.
If you are a professional linear algebraist or even an amateur like me, if there is a bug please open an issue. If there is a feature this DSL should have, more common operations or methods, please point me to them or be hardcore and send me the pull request.
Contributions are incredibly welcome as long as they are standardly applicable and pass the tests (or break bad ones). Tests are written in Mocha and assertions are done with the Node.js core
# generating sourcenpm run build# running testsnpm test