matrix
Functions
new
matrix.new(
int rows = 0,
int columns = 0,
T initial_value = na
) → const matrix<T>This function 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(matrix<T> mat, int column = mat.columns(), array<T> values = na)This function 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 |
|---|---|---|---|
mat | matrix<T> | The matrix to which the column will be added. | |
column | int | mat.columns() | 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(matrix<T> mat, int row = mat.rows(), array<T> values = na)This function 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 |
|---|---|---|---|
mat | matrix<T> | The matrix to which the row will be added. | |
row | int | mat.rows() | 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
This function 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 |
|---|---|---|---|
mat | matrix<int> | The integer matrix to calculate the average from. |
Returns: float
col
matrix.col(matrix<T> mat, int column) → array<T>This function retrieves all elements from the specified column of the matrix and returns them as an array.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<T> | The matrix to retrieve the column from. | |
column | int | The column index to retrieve. |
Returns: array<T>
columns
matrix.columns(matrix<T> mat) → intThis function returns the number of columns in the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<T> | The matrix to get the column count from. |
Returns: int
concat
matrix.concat(matrix<T> mat1, matrix<T> mat2)This function concatenates two matrices of the same type to mat1 and returns mat1.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat1 | matrix<T> | The first matrix to concatenate. | |
mat2 | matrix<T> | The second matrix to concatenate. |
copy
matrix.copy(matrix<T> mat)This function creates a copy of the given matrix and returns the new matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<T> | The matrix to copy. |
det
This function calculates and returns the determinant of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to calculate the determinant from. |
Returns: int
diff
This function 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 |
|---|---|---|---|
mat1 | matrix<int> | The first integer matrix. | |
mat2 | matrix<int> | The second integer matrix. |
Returns: matrix<int>
eigenvalues
This function calculates and returns the eigenvalues of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer square matrix to calculate eigenvalues from. |
Returns: matrix<float>
eigenvectors
This function calculates and returns the eigenvectors of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer square matrix to calculate eigenvectors from. |
Returns: matrix<float>
elements_count
matrix.elements_count(matrix<T> mat) → intThis function returns 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 |
|---|---|---|---|
mat | matrix<T> | The matrix to count the elements of. |
Returns: int
fill
matrix.fill(
matrix<T> mat,
T value,
int from_row = 0,
int to_row = mat.rows(),
int from_column = 0,
int to_column = mat.columns()
)This function 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 |
|---|---|---|---|
mat | 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 | mat.rows() | The ending row index. When omitted, denotes matrix row count. |
from_column | int | 0 | The starting column index. |
to_column | int | mat.columns() | The ending column index. When omitted, denotes matrix column count. |
get
matrix.get(matrix<T> mat, int row, int column) → TThis function retrieves the value at the specified row and column indices from the given matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | 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
This function computes and returns the inverse of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer square matrix to invert. |
Returns: matrix<float>
is_antidiagonal
This function checks if the given matrix is an antidiagonal matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to check. |
Returns: bool
is_antisymmetric
This function checks if the given matrix is an antisymmetric matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to check. |
Returns: bool
is_binary
This function 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 |
|---|---|---|---|
mat | matrix<int> | The integer matrix to check. |
Returns: bool
is_diagonal
This function checks if the given matrix is a diagonal matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to check. |
Returns: bool
is_identity
This function checks if the given matrix is an identity matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to check. |
Returns: bool
is_square
matrix.is_square(matrix<T> mat) → boolThis function checks if the given matrix is a square matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<T> | The matrix to check. |
Returns: bool
is_stochastic
This function checks if the given matrix is a stochastic matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<float> | The float matrix to check. |
Returns: bool
is_symmetric
This function checks if the given matrix is a symmetric matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to check. |
Returns: bool
is_triangular
This function checks if the given matrix is a triangular matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to check. |
Returns: bool
is_zero
This function checks if the given matrix is a zero matrix, returning true if it is and false otherwise.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to check. |
Returns: bool
kron
This function computes the Kronecker product of two matrices and returns the resulting matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat1 | matrix<int> | The first integer matrix. | |
mat2 | matrix<int> | The second integer matrix. |
Returns: matrix<int>
max
This function 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 |
|---|---|---|---|
mat | matrix<int> | The integer matrix to find the maximum from. |
Returns: int
median
This function 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 |
|---|---|---|---|
mat | matrix<int> | The integer matrix to find the median from. |
Returns: float
min
This function 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 |
|---|---|---|---|
mat | 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 |
|---|---|---|---|
mat | matrix<int> | The integer matrix to find the mode from. |
Returns: int — The most frequently occurring value, or the smallest among tied values.
multi
This function multiplies two matrices and returns the resulting matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat1 | matrix<int> | The first integer matrix. | |
mat2 | matrix<int> | The second integer matrix. |
Returns: matrix<int>
pinv
This function computes and returns the Moore-Penrose pseudoinverse of the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to compute the pseudoinverse from. |
Returns: matrix<float>
pow
This function raises a square matrix to the specified non-negative integer power and returns the resulting matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer square matrix to raise to a power. | |
power | int | The exponent (non-negative integer). |
Returns: matrix<int>
rank
This function calculates and returns the rank of the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to calculate the rank of. |
Returns: int
remove_col
matrix.remove_col(matrix<T> mat, int column = matrix.columns() - 1) → array<T>This function 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 |
|---|---|---|---|
mat | matrix<T> | The matrix from which the column will be removed. | |
column | int | matrix.columns() - 1 | 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(matrix<T> mat, int row = matrix.rows() - 1) → array<T>This function 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 |
|---|---|---|---|
mat | matrix<T> | The matrix from which the row will be removed. | |
row | int | matrix.rows() - 1 | 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(matrix<T> mat, int rows, int columns)This function reshapes the given matrix to the specified number of rows and columns.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<T> | The matrix to reshape. | |
rows | int | The new number of rows. | |
columns | int | The new number of columns. |
reverse
matrix.reverse(matrix<T> mat)This function reverses the order of elements in the specified matrix in place.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<T> | The matrix to reverse. |
row
matrix.row(matrix<T> mat, int row) → array<T>This function retrieves all elements from the specified row of the matrix and returns them as an array.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<T> | The matrix to retrieve the row from. | |
row | int | The row index to retrieve. |
Returns: array<T>
rows
matrix.rows(matrix<T> mat) → intThis function returns the number of rows in the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<T> | The matrix to get the row count from. |
Returns: int
set
matrix.set(matrix<T> mat, int row, int column, T value)This function sets the value at the specified row and column indices in the given matrix to the provided value.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | 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
This function sorts the elements of the given matrix in ascending or descending order.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer matrix to sort. | |
order | any | order.ascending | The sort order (ascending or descending). |
submatrix
matrix.submatrix(
matrix<T> mat,
int from_row = 0,
int to_row = mat.rows(),
int from_column = 0,
int to_column = mat.columns()
) → matrix<T>This function 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 |
|---|---|---|---|
mat | matrix<T> | The matrix to extract the submatrix from. | |
from_row | int | 0 | The starting row index. |
to_row | int | mat.rows() | The ending row index. When omitted, denotes matrix row count. |
from_column | int | 0 | The starting column index. |
to_column | int | mat.columns() | The ending column index. When omitted, denotes matrix column count. |
Returns: matrix<T>
sum
This function 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 |
|---|---|---|---|
mat1 | matrix<int> | The first integer matrix. | |
mat2 | matrix<int> | The second integer matrix. |
Returns: matrix<int>
swap_columns
matrix.swap_columns(matrix<T> mat, int column1, int column2)This function swaps two columns in the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | 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(matrix<T> mat, int row1, int row2)This function swaps two rows in the specified matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | 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
This function calculates and returns the trace of the specified square matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<int> | The integer square matrix to calculate the trace of. |
Returns: int
transpose
matrix.transpose(matrix<T> mat) → matrix<T>This function transposes the given matrix and returns the new transposed matrix.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
mat | matrix<T> | The matrix to transpose. |
Returns: matrix<T>