Matrix<T>
A two-dimensional table of elements of type T, stored in row-major order.
Create with Matrix.new(). Access elements with get / set; iterate rows with for row in mat.
Kind: object
Static Methods
new
Matrix.new(
rows: int = 0,
columns: int = 0,
initial_value: T = na
): const Matrix<T>Creates a new matrix with the specified number of rows and columns, initializing all elements to the given initial value.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
rows | int | 0 | The number of rows in the matrix. |
columns | int | 0 | The number of columns in the matrix. |
initial_value | T | na | The initial value for all elements. |
Returns: const Matrix<T>
Methods
add_col
Matrix.add_col(id: Matrix<T>, column: int = na, values: Array<T> = na)Adds a new column to the specified matrix at the given column index, filling it with the provided values or na if no values are given.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to which the column will be added. | |
column | int | na | The index at which the column will be inserted. When omitted, last column position. |
values | Array<T> | na | An array of values to fill the column, or na to fill with na values. |
add_row
Matrix.add_row(id: Matrix<T>, row: int = na, values: Array<T> = na)Adds a new row to the specified matrix at the given row index, filling it with the provided values or na if no values are given.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to which the row will be added. | |
row | int | na | The index at which the row will be inserted. When omitted, last row position. |
values | Array<T> | na | An array of values to fill the row, or na to fill with na values. |
avg
Calculates and returns the average of all elements in the specified matrix.
If the matrix is empty or contains no values, it returns na.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to calculate the average from. |
Returns: float
col
Matrix.col(id: Matrix<T>, column: int): Array<T>Retrieves all elements from the specified column of the matrix and returns them as an array.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to retrieve the column from. | |
column | int | The column index to retrieve. |
Returns: Array<T>
columns
Matrix.columns(id: Matrix<T>): intReturns the number of columns in the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to get the column count from. |
Returns: int
concat
Matrix.concat(id1: Matrix<T>, id2: Matrix<T>)Concatenates two matrices of the same type to the matrix and returns id.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id1 | Matrix<T> | The first matrix to concatenate. | |
id2 | Matrix<T> | The second matrix to concatenate. |
copy
Matrix.copy(id: Matrix<T>)Creates a copy of the given matrix and returns the new matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to copy. |
det
Calculates and returns the determinant of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to calculate the determinant from. |
Returns: int
diff
Computes the element-wise difference between two matrices of the same dimensions and returns a new matrix containing the results.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id1 | Matrix<int> | The first integer matrix. | |
id2 | Matrix<int> | The second integer matrix. |
Returns: Matrix<int>
eigenvalues
Calculates and returns the eigenvalues of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer square matrix to calculate eigenvalues from. |
Returns: Matrix<float>
eigenvectors
Calculates and returns the eigenvectors of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer square matrix to calculate eigenvectors from. |
Returns: Matrix<float>
elements_count
Matrix.elements_count(id: Matrix<T>): intReturns the total number of elements in the specified matrix by multiplying its number of rows by its number of columns.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to count the elements of. |
Returns: int
fill
Matrix.fill(
id: Matrix<T>,
value: T,
from_row: int = 0,
to_row: int = id.rows(),
from_column: int = 0,
to_column: int = id.columns()
)Fills a specified submatrix of the given matrix with a specified value.
The submatrix is defined by the row and column ranges provided as parameters.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to fill. | |
value | T | The value to fill the submatrix with. | |
from_row | int | 0 | The starting row index. |
to_row | int | id.rows() | The ending row index. When omitted, denotes matrix row count. |
from_column | int | 0 | The starting column index. |
to_column | int | id.columns() | The ending column index. When omitted, denotes matrix column count. |
get
Matrix.get(id: Matrix<T>, row: int, column: int): TRetrieves the value at the specified row and column indices from the given matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to retrieve the value from. | |
row | int | The row index of the element. | |
column | int | The column index of the element. |
Returns: T
inv
Computes and returns the inverse of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer square matrix to invert. |
Returns: Matrix<float>
is_antidiagonal
Checks if the given matrix is an antidiagonal matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to check. |
Returns: bool
is_antisymmetric
Checks if the given matrix is an antisymmetric matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to check. |
Returns: bool
is_binary
Checks if the given matrix is a binary matrix (containing only 0 and 1), returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to check. |
Returns: bool
is_diagonal
Checks if the given matrix is a diagonal matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to check. |
Returns: bool
is_identity
Checks if the given matrix is an identity matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to check. |
Returns: bool
is_square
Matrix.is_square(id: Matrix<T>): boolChecks if the given matrix is a square matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to check. |
Returns: bool
is_stochastic
Checks if the given matrix is a stochastic matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<float> | The float matrix to check. |
Returns: bool
is_symmetric
Checks if the given matrix is a symmetric matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to check. |
Returns: bool
is_triangular
Checks if the given matrix is a triangular matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to check. |
Returns: bool
is_zero
Checks if the given matrix is a zero matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to check. |
Returns: bool
kron
Computes the Kronecker product of two matrices and returns the resulting matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id1 | Matrix<int> | The first integer matrix. | |
id2 | Matrix<int> | The second integer matrix. |
Returns: Matrix<int>
max
Calculates and returns the maximum value among all elements in the specified matrix.
If the matrix is empty or contains no values, it returns na.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to find the maximum from. |
Returns: int
median
Calculates and returns the median of all elements in the specified matrix.
If the matrix is empty or contains no values, it returns na.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to find the median from. |
Returns: float
min
Calculates and returns the minimum value among all elements in the specified matrix.
If the matrix is empty or contains no values, it returns na.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to find the minimum from. |
Returns: int
mode
The function returns the mode of an matrix's elements.
If there are several values with the same frequency, it returns the smallest value.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to find the mode from. |
Returns: int — The most frequently occurring value, or the smallest among tied values.
multi
Multiplies two matrices and returns the resulting matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id1 | Matrix<int> | The first integer matrix. | |
id2 | Matrix<int> | The second integer matrix. |
Returns: Matrix<int>
pinv
Computes and returns the Moore-Penrose pseudoinverse of the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to compute the pseudoinverse from. |
Returns: Matrix<float>
pow
Raises a square matrix to the specified non-negative integer power and returns the resulting matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer square matrix to raise to a power. | |
power | int | The exponent (non-negative integer). |
Returns: Matrix<int>
rank
Calculates and returns the rank of the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to calculate the rank of. |
Returns: int
remove_col
Matrix.remove_col(id: Matrix<T>, column: int = na): Array<T>Removes the specified column from the given matrix and returns the removed column as an array.
If no column index is provided, it removes the last column by default.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix from which the column will be removed. | |
column | int | na | The index of the column to remove. When omitted, last column. |
Returns: Array<T> — An array containing the elements of the removed column.
remove_row
Matrix.remove_row(id: Matrix<T>, row: int = na): Array<T>Removes the specified row from the given matrix and returns the removed row as an array.
If no row index is provided, it removes the last row by default.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix from which the row will be removed. | |
row | int | na | The index of the row to remove. When omitted, last row. |
Returns: Array<T> — An array containing the elements of the removed row.
reshape
Matrix.reshape(id: Matrix<T>, rows: int, columns: int)Reshapes the given matrix to the specified number of rows and columns.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to reshape. | |
rows | int | The new number of rows. | |
columns | int | The new number of columns. |
reverse
Matrix.reverse(id: Matrix<T>)Reverses the order of elements in the specified matrix in place.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to reverse. |
row
Matrix.row(id: Matrix<T>, row: int): Array<T>Retrieves all elements from the specified row of the matrix and returns them as an array.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to retrieve the row from. | |
row | int | The row index to retrieve. |
Returns: Array<T>
rows
Matrix.rows(id: Matrix<T>): intReturns the number of rows in the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to get the row count from. |
Returns: int
set
Matrix.set(id: Matrix<T>, row: int, column: int, value: T)Sets the value at the specified row and column indices in the given matrix to the provided value.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to modify. | |
row | int | The row index of the element. | |
column | int | The column index of the element. | |
value | T | The value to set at the specified position. |
sort
Sorts the elements of the given matrix in ascending or descending order.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer matrix to sort. | |
order | any | SortOrder.Ascending | The sort order (ascending or descending). |
subMatrix
Matrix.subMatrix(
id: Matrix<T>,
from_row: int = 0,
to_row: int = id.rows(),
from_column: int = 0,
to_column: int = id.columns()
): Matrix<T>Extracts a submatrix from the given matrix based on the specified row and column ranges and returns the new submatrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to extract the submatrix from. | |
from_row | int | 0 | The starting row index. |
to_row | int | id.rows() | The ending row index. When omitted, denotes matrix row count. |
from_column | int | 0 | The starting column index. |
to_column | int | id.columns() | The ending column index. When omitted, denotes matrix column count. |
Returns: Matrix<T>
sum
Computes the element-wise sum of two matrices of the same dimensions and returns a new matrix containing the results.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id1 | Matrix<int> | The first integer matrix. | |
id2 | Matrix<int> | The second integer matrix. |
Returns: Matrix<int>
swap_columns
Matrix.swap_columns(id: Matrix<T>, column1: int, column2: int)Swaps two columns in the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix containing the columns to swap. | |
column1 | int | The index of the first column. | |
column2 | int | The index of the second column. |
swap_rows
Matrix.swap_rows(id: Matrix<T>, row1: int, row2: int)Swaps two rows in the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix containing the rows to swap. | |
row1 | int | The index of the first row. | |
row2 | int | The index of the second row. |
trace
Calculates and returns the trace of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<int> | The integer square matrix to calculate the trace of. |
Returns: int
transpose
Matrix.transpose(id: Matrix<T>): Matrix<T>Transposes the given matrix and returns the new transposed matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | Matrix<T> | The matrix to transpose. |
Returns: Matrix<T>