Module ID: shellfish/core/matrix
This module provides the Shellfish matrix toolbox for linear algebra, which is useful when working with coordinates in a 2D or 3D space.
Examples
Importing in JavaScript
shRequire("shellfish/core/matrix", mat =>
{
...
});
Importing in Shui
require "shellfish/core/matrix" as mat;
Object {
...
}
Methods
-
matrix.add (a, b)matrix.Matrix static
-
Returns the result of the addition of two matrices or of a matrix with a scalar.
Name Type Description amatrix.Matrix The matrix.
bmatrix.Matrix | number A matrix or a scalar.
Throws:
Throws an error if the two matrices are of different dimensions.
Returns:
Type Description matrix.Matrix The resulting matrix. -
Returns the determinant of the given matrix. The determinant is only defined for square matrices.
Name Type Description mmatrix.Matrix The matrix.
Throws:
Throws an error if the matrix is not square.
Returns:
Type Description number The determinant. -
Returns the dot product of two column-vectors. Both vectors must have the same dimensions.
Name Type Description umatrix.Matrix The first vector.
vmatrix.Matrix The second vector.
Throws:
Throws an error if the two vectors are no column-vectors or are of different dimensions.
Returns:
Type Description number The dot product. -
matrix.elementWise (m, n, op)matrix.Matrix static
-
Performs an element-wise operator with a scalar on a matrix.
The element-wise operators
PLUS,MINUS, andMULTIPLYare available as predefined functions in thematrixmodule.Name Type Description mmatrix.Matrix The matrix.
nnumber The scalar.
opfunction The element-wise operator.
Returns:
Type Description matrix.Matrix The resulting matrix. -
Produces a flat representation of a matrix in row-major order.
Name Type Description mmatrix.Matrix The matrix to flatten.
Returns:
Type Description Array.<number> The flat representation. -
matrix.fromArray (arr, cols)matrix.Matrix static
-
Creates a matrix from an array, given in row-major order.
Name Type Description arrArray.<number> The array of values in row-major order.
colsnumber The amount of columns.
Returns:
Type Description matrix.Matrix The matrix. -
matrix.identityM (dim)matrix.Matrix static
-
Creates the identity matrix for the given dimension.
Name Type Description dimnumber The dimension of the matrix.
Returns:
Type Description matrix.Matrix The identity matrix. -
matrix.inv (m)matrix.Matrix static
-
Returns the inverse of the given matrix. Only square matrices with a determinant != 0 have an inverse.
Name Type Description mmatrix.Matrix The matrix.
Throws:
Throws an error if the matrix has no inverse.
Returns:
Type Description matrix.Matrix The inverse of the matrix. -
Returns the length of the given column-vector.
Name Type Description vmatrix.Matrix The vector.
Throws:
Throws an error if the matrix is not a vector.
Returns:
Type Description number The length of the vector. -
matrix.minor (m, row, col)matrix.Matrix static
-
Returns the minor of a matrix at the given row and column.
Name Type Description mmatrix.Matrix The matrix.
rownumber The row for computing the minor.
colnumber The column for computing the minor.
Returns:
Type Description matrix.Matrix The minor. -
matrix.mul (a, b)matrix.Matrix static
-
Returns the result of the mulitplication of two matrices, or of a matrix with a scalar.
Name Type Description amatrix.Matrix The first matrix.
bmatrix.Matrix | number The second matrix or a scalar.
Throws:
Throws an error if the two matrices are of different dimensions.
Returns:
Type Description matrix.Matrix The resulting matrix. -
matrix.perspectiveM (distance)matrix.Matrix static
-
Creates a 3D to 2D perspective projection matrix.
Name Type Description distancenumber The viewer's distance to the screen.
Returns:
Type Description matrix.Matrix The perspective projection matrix. -
matrix.rotationM (u, v, angle)matrix.Matrix static
-
Creates the rotation matrix for rotating around two orthogonal vectors.
Name Type Description umatrix.Matrix The first vector.
vmatrix.Matrix The second vector.
anglenumber The rotation angle in degrees.
Returns:
Type Description matrix.Matrix The rotation matrix. -
matrix.rotationMByQuaternion (u, v, angle)matrix.Matrix static
-
Creates the rotation matrix for rotating by a quaternion (a four-dimensional complex number expressing a rotation in 3D space). Quaternion-rotations do not induce a gimbal-lock.
Name Type Description umatrix.Matrix The first vector.
vmatrix.Matrix The second vector.
anglenumber The rotation angle in degrees.
Returns:
Type Description matrix.Matrix The rotation matrix. -
matrix.scalingM (vec)matrix.Matrix static
-
Creates the scaling matrix for scaling by the given vector.
Name Type Description vecmatrix.Matrix The scaling vector.
Returns:
Type Description matrix.Matrix The scaling matrix. -
Returns the shape as
{ rows, cols }of the given matrix.Name Type Description mmatrix.Matrix The matrix.
Returns:
Type Description object - The object describing the shape.
-
matrix.sub (a, b)matrix.Matrix static
-
Returns the result of the subtraction of two matrices or of a matrix with a scalar.
Name Type Description amatrix.Matrix The matrix.
bmatrix.Matrix | number A matrix or a scalar.
Returns:
Type Description matrix.Matrix The resulting matrix. -
matrix.t (m)matrix.Matrix static
-
Returns the transpose of the given matrix. Transposing a column-vector creates a row-vector and vice versa.
Name Type Description mmatrix.Matrix The matrix to transpose.
Returns:
Type Description matrix.Matrix The transpose of the matrix. -
matrix.translationM (vec)matrix.Matrix static
-
Creates the translation matrix for the given translation vector. The dimension of the translation matrix is one higher than of the vector.
Name Type Description vecmatrix.Matrix The translation vector.
Returns:
Type Description matrix.Matrix The translation matrix. -
matrix.vec (values)matrix.Matrix static
-
Creates a column-vector of the given values.
Name Type Description valuesnumber repeatable The values.
Returns:
Type Description matrix.Matrix The vector (a (n x 1) matrix).
Type Definitions
-
Type for a (n x m) matrix. A vector is a special form of the matrix with one column or one row.
The matrix is defined in row-major order:
| 1 2 3 | | 4 5 6 | ~ [1, 2, 3, 4, 5, 6, 7, 8, 9] | 7 8 9 |