vectrix.matrices - nphyx/vectrix GitHub Wiki
vectrix/matrices
Description
Require the module:
const matrices = require("vectrix.matrices.js");
Create a 2x2 matrix using create(rows, columns, values)
:
let mat = matrices.create(2,2,[0,1, 2,3]);
Add two matrices using a.plus(b)
:
let first = matrices.create(2,2,[1,2, 3,4]);
let second = matrices.create(2,2,[3,4, 5,6]);
let sum = first.plus(second);
Subtract two matrices with a.minus(b)
:
let diff = second.minus(first);
Get the dot product of two matrices via a.dot(b)
:
let prod = first.dot(second);
Dot can also multiply a matrix by a scalar:
let scalarProd = first.dot(3);
All matrix and vector methods produce a new object from their operands, creating and returning a new object as a result.
sum.toArray(); // [4,6,8,10]
diff.toArray(); // [2,2,2,2]
product.toArray(); // [13,16,29,26]
first.toArray(); // [1,2,3,4]
second.toArray(); // [3,4,5,6]
scalarProd; // [3,6,9,12]
This means matrix operations are composable in an intuitive left-to-right fashion:
first.sub(second).dot(diff).toArray(); // [8,8,8,8]
But keep in mind that you must follow matrix operation rules! Operating on two incompatible matrices returns undefined:
let third = matrices.create(1,2,[0,1]);
first.add(third); // undefined
It turned out to be useful to get a single row or column from a matrix, so you can
do that too using mat.row(N)
and mat.col(N)
:
first.row(0); // matrix(2,1,[1,2])
first.col(1); // matrix(1,2,[2,4])
#Functions
flatten
mixed
flatten(a) ⇨ Flattens an array. Used for flattening arguments passed to factories.
Parameters
name | type | description |
---|---|---|
a | mixed |
an array, array-like, or object that can be flattened |
Returns: mixed
flat version of input
plus
matrix
plus(a, b, out) ⇨ Add two matrices together.
Example:
plus(matrix, anotherMatrix); // function
matrix.plus(anotherMatrix); // method
Parameters
name | type | description |
---|---|---|
a | matrix |
first matrix |
b | matrix |
second matrix |
out | matrix |
out value (optional) |
Returns: matrix
mut_plus
matrix
mut_plus(a, b, out) ⇨ Mutating version of plus.
Parameters
name | type | description |
---|---|---|
a | matrix |
first matrix |
b | matrix |
second matrix |
out | matrix |
out value (optional) |
Returns: matrix
mut_minus
matrix
mut_minus(a, b, out) ⇨ Mutating version of minus.
Parameters
name | type | description |
---|---|---|
a | matrix |
first matrix |
b | matrix |
second matrix |
out | matrix |
out value (optional) |
Returns: matrix
toArray
array
toArray(a) ⇨ Get the basic array representation of a matrix.
Example:
toArray(matrix); // function
matrix.toArray(); // method
Parameters
name | type | description |
---|---|---|
a | matrix |
Get the basic array representation of a matrix. |
Returns: array
values as flat array
create
matrix
create(rows, cols, values, buffer, offset) ⇨ Factory for creating generic matrices.
Parameters
name | type | description |
---|---|---|
rows | int |
matrix rows |
cols | int |
matrix columns |
values | mixed |
(optional) matrix values as an array-like object |
buffer | ArrayBuffer |
(optional) pre-supplied ArrayBuffer |
offset | int |
(optional) offset for buffer |
Returns: matrix