Creates a new matrix with the specified number of rows and columns.
The number of rows in the matrix.
The number of columns in the matrix.
Optional
value: number | ((row, col) => number) | "identity"The initial value of the matrix. Can be a number, a function that returns a number, or the string identity
.
If a number is provided, all elements of the matrix will be set to that number.
If a function is provided, it will be called for each element of the matrix to determine its initial value.
If identity
is provided, the matrix will be initialized as an identity matrix.
An error if the number of rows or columns is less than or equal to 1.
Protected
_dataThe number of columns in the matrix.
The number of rows in the matrix.
Returns an array containing the diagonal elements of the matrix. If the matrix is not square, the diagonal is truncated to the smaller dimension.
An array containing the diagonal elements of the matrix.
Returns a boolean indicating whether the matrix has room for more elements.
True if the matrix has room for more elements, false otherwise.
Returns a boolean indicating whether the matrix is empty or not.
True if the matrix is empty, false otherwise.
Returns a boolean indicating whether the matrix is full or not.
True if the matrix is full, false otherwise.
Returns a copy of the matrix data as a two-dimensional array.
A copy of the matrix data.
Returns an array containing the mean value of each column in the matrix.
An array containing the mean value of each column in the matrix.
Returns an array containing the mean value of each row in the matrix.
An array containing the mean value of each row in the matrix.
Returns the number of empty spaces in the matrix.
The number of empty spaces in the matrix.
Returns the sum of all elements in the matrix.
The sum of all elements in the matrix.
Concatenates two matrices either horizontally or vertically.
The matrix to concatenate with.
The type of concatenation to perform. Can be 'horizontal' or 'vertical'. Defaults to 'horizontal'.
A new matrix that is the result of the concatenation.
An error if the matrices do not have the same number of rows (for horizontal concatenation) or columns (for vertical concatenation).
Returns the dot product of the current matrix and the passed matrix.
The matrix to multiply with the current matrix.
A new matrix that is the result of the dot product.
An error if the number of columns of the current matrix is different from the number of rows of the passed matrix.
Returns an array containing the elements of the specified column in the matrix.
The index of the column to retrieve.
An array containing the elements of the specified column.
An error if the passed index exceeds the total number of columns in the matrix.
Applies a binary operation to each element of the current matrix and another matrix.
The matrix to operate with.
The binary operation to apply to each element.
A new matrix with the result of the operation.
If the number of columns of the current matrix is different from the number of rows of the passed matrix.
Sets the values of a given column in the matrix.
The index of the column to set.
An array of values to set in the column.
The updated matrix.
An error if the passed index exceeds the total number of columns in the matrix.
An error if the passed values exceed the total number of columns in the matrix.
Sets the values of a row in the matrix.
The index of the row to set.
The values to set for the row.
The updated matrix.
If the passed index exceeds the total number of rows in the matrix or if the passed values exceed the total number of rows in the matrix.
Returns a new matrix that is a submatrix of the current matrix with the specified row and column removed.
The row to remove.
The column to remove.
A new matrix that is a submatrix of the current matrix with the specified row and column removed.
Updates the value at the specified row and column index using the provided update function.
The row index of the value to update.
The column index of the value to update.
The update function that takes the old value as input and returns the new value.
The new value after the update.
Static
fromCreates a new matrix instance from the given parameters.
The array to use for the new matrix instance.
A new matrix instance.
An error if the array is empty or not all the columns of the 2D array have the same length.
A matrix data structure.