Skip to content

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

navi
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

NameTypeDefaultDescription
rowsint0The number of rows in the matrix.
columnsint0The number of columns in the matrix.
initial_valueTnaThe initial value for all elements.

Returns: const Matrix<T>

Methods

add_col

navi
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

NameTypeDefaultDescription
idMatrix<T>The matrix to which the column will be added.
columnintnaThe index at which the column will be inserted. When omitted, last column position.
valuesArray<T>naAn array of values to fill the column, or na to fill with na values.

add_row

navi
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

NameTypeDefaultDescription
idMatrix<T>The matrix to which the row will be added.
rowintnaThe index at which the row will be inserted. When omitted, last row position.
valuesArray<T>naAn 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to calculate the average from.

Returns: float

Calculates and returns the average of all elements in the specified matrix.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float matrix to calculate the average from.

Returns: float


col

navi
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

NameTypeDefaultDescription
idMatrix<T>The matrix to retrieve the column from.
columnintThe column index to retrieve.

Returns: Array<T>


columns

navi
Matrix.columns(id: Matrix<T>): int

Returns the number of columns in the specified matrix.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix to get the column count from.

Returns: int


concat

navi
Matrix.concat(id1: Matrix<T>, id2: Matrix<T>)

Concatenates two matrices of the same type to the matrix and returns id.

Parameters

NameTypeDefaultDescription
id1Matrix<T>The first matrix to concatenate.
id2Matrix<T>The second matrix to concatenate.

copy

navi
Matrix.copy(id: Matrix<T>)

Creates a copy of the given matrix and returns the new matrix.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix to copy.

det

Calculates and returns the determinant of the specified square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<int>The integer matrix to calculate the determinant from.

Returns: int

Calculates and returns the determinant of the specified square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float matrix to calculate the determinant from.

Returns: float


diff

Computes the element-wise difference between two matrices of the same dimensions and returns a new matrix containing the results.

Parameters

NameTypeDefaultDescription
id1Matrix<int>The first integer matrix.
id2Matrix<int>The second integer matrix.

Returns: Matrix<int>

Computes and returns the element-wise difference between two matrices of the same dimensions.

Parameters

NameTypeDefaultDescription
id1Matrix<float>The first float matrix.
id2Matrix<float>The second float matrix.

Returns: Matrix<float>

Computes and returns the element-wise difference between two matrices of the same dimensions.

Parameters

NameTypeDefaultDescription
id1Matrix<int>The first integer matrix.
id2Matrix<float>The second float matrix.

Returns: Matrix<float>

Computes and returns the element-wise difference between two matrices of the same dimensions.

Parameters

NameTypeDefaultDescription
id1Matrix<float>The first float matrix.
id2Matrix<int>The second integer matrix.

Returns: Matrix<float>


eigenvalues

Calculates and returns the eigenvalues of the specified square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<int>The integer square matrix to calculate eigenvalues from.

Returns: Matrix<float>

Returns the eigenvalues of the specified square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float square matrix to calculate eigenvalues from.

Returns: Matrix<float>


eigenvectors

Calculates and returns the eigenvectors of the specified square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<int>The integer square matrix to calculate eigenvectors from.

Returns: Matrix<float>

Returns the eigenvectors of the specified square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float square matrix to calculate eigenvectors from.

Returns: Matrix<float>


elements_count

navi
Matrix.elements_count(id: Matrix<T>): int

Returns the total number of elements in the specified matrix by multiplying its number of rows by its number of columns.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix to count the elements of.

Returns: int


fill

navi
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

NameTypeDefaultDescription
idMatrix<T>The matrix to fill.
valueTThe value to fill the submatrix with.
from_rowint0The starting row index.
to_rowintid.rows()The ending row index. When omitted, denotes matrix row count.
from_columnint0The starting column index.
to_columnintid.columns()The ending column index. When omitted, denotes matrix column count.

get

navi
Matrix.get(id: Matrix<T>, row: int, column: int): T

Retrieves the value at the specified row and column indices from the given matrix.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix to retrieve the value from.
rowintThe row index of the element.
columnintThe column index of the element.

Returns: T


inv

Computes and returns the inverse of the specified square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<int>The integer square matrix to invert.

Returns: Matrix<float>

Computes the inverse of the specified square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to check.

Returns: bool

Checks if the given matrix is an antidiagonal matrix, returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to check.

Returns: bool

Checks if the given matrix is an antisymmetric matrix, returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to check.

Returns: bool

Checks if the given matrix is a binary matrix (containing only 0 and 1), returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to check.

Returns: bool

Checks if the given matrix is a diagonal matrix, returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to check.

Returns: bool

Checks if the given matrix is an identity matrix, returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float matrix to check.

Returns: bool


is_square

navi
Matrix.is_square(id: Matrix<T>): bool

Checks if the given matrix is a square matrix, returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<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

NameTypeDefaultDescription
idMatrix<float>The float matrix to check.

Returns: bool

Checks if the given matrix is a stochastic matrix, returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<int>The integer 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to check.

Returns: bool

Checks if the given matrix is a symmetric matrix, returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to check.

Returns: bool

Checks if the given matrix is a triangular matrix, returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to check.

Returns: bool

Checks if the given matrix is a zero matrix, returning true if it is and false otherwise.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float matrix to check.

Returns: bool


kron

Computes the Kronecker product of two matrices and returns the resulting matrix.

Parameters

NameTypeDefaultDescription
id1Matrix<int>The first integer matrix.
id2Matrix<int>The second integer matrix.

Returns: Matrix<int>

Computes the Kronecker product of two matrices and returns the resulting matrix.

Parameters

NameTypeDefaultDescription
id1Matrix<float>The first float matrix.
id2Matrix<float>The second float matrix.

Returns: Matrix<float>


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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to find the maximum from.

Returns: int

Returns the maximum value among all elements.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float matrix to find the maximum from.

Returns: float


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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to find the median from.

Returns: float

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

NameTypeDefaultDescription
idMatrix<float>The float 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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to find the minimum from.

Returns: int

Returns the minimum value among all elements.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float matrix to find the minimum from.

Returns: float


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

NameTypeDefaultDescription
idMatrix<int>The integer matrix to find the mode from.

Returns: int — The most frequently occurring value, or the smallest among tied values.

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

NameTypeDefaultDescription
idMatrix<float>The float matrix to find the mode from.

Returns: float — The most frequently occurring value, or the smallest among tied values.


multi

Multiplies two matrices and returns the resulting matrix.

Parameters

NameTypeDefaultDescription
id1Matrix<int>The first integer matrix.
id2Matrix<int>The second integer matrix.

Returns: Matrix<int>

Matrix multiplication.

Parameters

NameTypeDefaultDescription
id1Matrix<float>The first float matrix.
id2Matrix<float>The second float matrix.

Returns: Matrix<float>

Matrix multiplication.

Parameters

NameTypeDefaultDescription
id1Matrix<int>The first integer matrix.
id2Matrix<float>The second float matrix.

Returns: Matrix<float>

Matrix multiplication.

Parameters

NameTypeDefaultDescription
id1Matrix<float>The first float matrix.
id2Matrix<int>The second integer matrix.

Returns: Matrix<float>


pinv

Computes and returns the Moore-Penrose pseudoinverse of the specified matrix.

Parameters

NameTypeDefaultDescription
idMatrix<int>The integer matrix to compute the pseudoinverse from.

Returns: Matrix<float>

Computes the Moore-Penrose pseudoinverse.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float 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

NameTypeDefaultDescription
idMatrix<int>The integer square matrix to raise to a power.
powerintThe exponent (non-negative integer).

Returns: Matrix<int>

Raises a square matrix to the specified non-negative integer power and returns the resulting matrix.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float square matrix to raise to a power.
powerintThe exponent (non-negative integer).

Returns: Matrix<float>


rank

Calculates and returns the rank of the specified matrix.

Parameters

NameTypeDefaultDescription
idMatrix<int>The integer matrix to calculate the rank of.

Returns: int

Returns the rank of the matrix.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float matrix to calculate the rank of.

Returns: int


remove_col

navi
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

NameTypeDefaultDescription
idMatrix<T>The matrix from which the column will be removed.
columnintnaThe index of the column to remove. When omitted, last column.

Returns: Array<T> — An array containing the elements of the removed column.


remove_row

navi
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

NameTypeDefaultDescription
idMatrix<T>The matrix from which the row will be removed.
rowintnaThe index of the row to remove. When omitted, last row.

Returns: Array<T> — An array containing the elements of the removed row.


reshape

navi
Matrix.reshape(id: Matrix<T>, rows: int, columns: int)

Reshapes the given matrix to the specified number of rows and columns.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix to reshape.
rowsintThe new number of rows.
columnsintThe new number of columns.

reverse

navi
Matrix.reverse(id: Matrix<T>)

Reverses the order of elements in the specified matrix in place.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix to reverse.

row

navi
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

NameTypeDefaultDescription
idMatrix<T>The matrix to retrieve the row from.
rowintThe row index to retrieve.

Returns: Array<T>


rows

navi
Matrix.rows(id: Matrix<T>): int

Returns the number of rows in the specified matrix.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix to get the row count from.

Returns: int


set

navi
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

NameTypeDefaultDescription
idMatrix<T>The matrix to modify.
rowintThe row index of the element.
columnintThe column index of the element.
valueTThe value to set at the specified position.

sort

Sorts the elements of the given matrix in ascending or descending order.

Parameters

NameTypeDefaultDescription
idMatrix<int>The integer matrix to sort.
orderanySortOrder.AscendingThe sort order (ascending or descending).

Sorts matrix elements.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float matrix to sort.
orderanySortOrder.AscendingThe sort order (ascending or descending).

Sorts matrix elements.

Parameters

NameTypeDefaultDescription
idMatrix<string>The string matrix to sort.
orderanySortOrder.AscendingThe sort order (ascending or descending).

subMatrix

navi
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

NameTypeDefaultDescription
idMatrix<T>The matrix to extract the submatrix from.
from_rowint0The starting row index.
to_rowintid.rows()The ending row index. When omitted, denotes matrix row count.
from_columnint0The starting column index.
to_columnintid.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

NameTypeDefaultDescription
id1Matrix<int>The first integer matrix.
id2Matrix<int>The second integer matrix.

Returns: Matrix<int>

Computes the element-wise sum of two matrices of the same dimensions and returns a new matrix containing the results.

Parameters

NameTypeDefaultDescription
id1Matrix<float>The first float matrix.
id2Matrix<float>The second float matrix.

Returns: Matrix<float>

Computes the element-wise sum of two matrices of the same dimensions and returns a new matrix containing the results.

Parameters

NameTypeDefaultDescription
id1Matrix<int>The first integer matrix.
id2Matrix<float>The second float matrix.

Returns: Matrix<float>

Computes the element-wise sum of two matrices of the same dimensions and returns a new matrix containing the results.

Parameters

NameTypeDefaultDescription
id1Matrix<float>The first float matrix.
id2Matrix<int>The second integer matrix.

Returns: Matrix<float>


swap_columns

navi
Matrix.swap_columns(id: Matrix<T>, column1: int, column2: int)

Swaps two columns in the specified matrix.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix containing the columns to swap.
column1intThe index of the first column.
column2intThe index of the second column.

swap_rows

navi
Matrix.swap_rows(id: Matrix<T>, row1: int, row2: int)

Swaps two rows in the specified matrix.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix containing the rows to swap.
row1intThe index of the first row.
row2intThe index of the second row.

trace

Calculates and returns the trace of the specified square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<int>The integer square matrix to calculate the trace of.

Returns: int

Returns the trace of the square matrix.

Parameters

NameTypeDefaultDescription
idMatrix<float>The float square matrix to calculate the trace of.

Returns: float


transpose

navi
Matrix.transpose(id: Matrix<T>): Matrix<T>

Transposes the given matrix and returns the new transposed matrix.

Parameters

NameTypeDefaultDescription
idMatrix<T>The matrix to transpose.

Returns: Matrix<T>

Released under the MIT License.