Skip to content

matrix

Functions

new

pine
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

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

pine
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

NameTypeDefaultDescription
matmatrix<T>The matrix to which the column will be added.
columnintmat.columns()The 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

pine
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

NameTypeDefaultDescription
matmatrix<T>The matrix to which the row will be added.
rowintmat.rows()The 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

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

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

Returns: float

This function calculates and returns the average of all elements in the specified matrix.

Parameters

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

Returns: float


col

pine
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

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

Returns: array<T>


columns

pine
matrix.columns(matrix<T> mat) → int

This function returns the number of columns in the specified matrix.

Parameters

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

Returns: int


concat

pine
matrix.concat(matrix<T> mat1, matrix<T> mat2)

This function concatenates two matrices of the same type to mat1 and returns mat1.

Parameters

NameTypeDefaultDescription
mat1matrix<T>The first matrix to concatenate.
mat2matrix<T>The second matrix to concatenate.

copy

pine
matrix.copy(matrix<T> mat)

This function creates a copy of the given matrix and returns the new matrix.

Parameters

NameTypeDefaultDescription
matmatrix<T>The matrix to copy.

det

This function calculates and returns the determinant of the specified square matrix.

Parameters

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

Returns: int

This function calculates and returns the determinant of the specified square matrix.

Parameters

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

Returns: float


diff

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

Parameters

NameTypeDefaultDescription
mat1matrix<int>The first integer matrix.
mat2matrix<int>The second integer matrix.

Returns: matrix<int>

Element-wise difference.

Parameters

NameTypeDefaultDescription
mat1matrix<float>The first float matrix.
mat2matrix<float>The second float matrix.

Returns: matrix<float>

Element-wise difference.

Parameters

NameTypeDefaultDescription
mat1matrix<int>The first integer matrix.
mat2matrix<float>The second float matrix.

Returns: matrix<float>

Element-wise difference.

Parameters

NameTypeDefaultDescription
mat1matrix<float>The first float matrix.
mat2matrix<int>The second integer matrix.

Returns: matrix<float>


eigenvalues

This function calculates and returns the eigenvalues of the specified square matrix.

Parameters

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

Returns: matrix<float>

Returns the eigenvalues of the specified square matrix.

Parameters

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

Returns: matrix<float>


eigenvectors

This function calculates and returns the eigenvectors of the specified square matrix.

Parameters

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

Returns: matrix<float>

Returns the eigenvectors of the specified square matrix.

Parameters

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

Returns: matrix<float>


elements_count

pine
matrix.elements_count(matrix<T> mat) → int

This function returns the total number of elements in the specified matrix by multiplying its number of rows by its number of columns.

Parameters

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

Returns: int


fill

pine
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

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

get

pine
matrix.get(matrix<T> mat, int row, int column) → T

This function retrieves the value at the specified row and column indices from the given matrix.

Parameters

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

Returns: T


inv

This function computes and returns the inverse of the specified square matrix.

Parameters

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

Returns: matrix<float>

Computes the inverse of the specified square matrix.

Parameters

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

NameTypeDefaultDescription
matmatrix<int>The integer matrix to check.

Returns: bool

Checks if the matrix is antidiagonal.

Parameters

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

NameTypeDefaultDescription
matmatrix<int>The integer matrix to check.

Returns: bool

Checks if the matrix is antisymmetric.

Parameters

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

NameTypeDefaultDescription
matmatrix<int>The integer matrix to check.

Returns: bool

Checks if the matrix is binary.

Parameters

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

NameTypeDefaultDescription
matmatrix<int>The integer matrix to check.

Returns: bool

Checks if the matrix is diagonal.

Parameters

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

NameTypeDefaultDescription
matmatrix<int>The integer matrix to check.

Returns: bool

Checks if the matrix is identity.

Parameters

NameTypeDefaultDescription
matmatrix<float>The float matrix to check.

Returns: bool


is_square

pine
matrix.is_square(matrix<T> mat) → bool

This function checks if the given matrix is a square matrix, returning true if it is and false otherwise.

Parameters

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

NameTypeDefaultDescription
matmatrix<float>The float matrix to check.

Returns: bool

Checks if the matrix is stochastic.

Parameters

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

NameTypeDefaultDescription
matmatrix<int>The integer matrix to check.

Returns: bool

Checks if the matrix is symmetric.

Parameters

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

NameTypeDefaultDescription
matmatrix<int>The integer matrix to check.

Returns: bool

Checks if the matrix is triangular.

Parameters

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

NameTypeDefaultDescription
matmatrix<int>The integer matrix to check.

Returns: bool

Checks if the matrix is zero.

Parameters

NameTypeDefaultDescription
matmatrix<float>The float matrix to check.

Returns: bool


kron

This function computes the Kronecker product of two matrices and returns the resulting matrix.

Parameters

NameTypeDefaultDescription
mat1matrix<int>The first integer matrix.
mat2matrix<int>The second integer matrix.

Returns: matrix<int>

Kronecker product.

Parameters

NameTypeDefaultDescription
mat1matrix<float>The first float matrix.
mat2matrix<float>The second float matrix.

Returns: matrix<float>


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

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

Returns: int

Returns the maximum value among all elements.

Parameters

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

Returns: float


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

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

Returns: float

Returns the median of all elements.

Parameters

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

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

Returns: int

Returns the minimum value among all elements.

Parameters

NameTypeDefaultDescription
matmatrix<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
matmatrix<int>The integer matrix to find the mode from.

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

Returns the mode of the matrix elements.

Parameters

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

Returns: float


multi

This function multiplies two matrices and returns the resulting matrix.

Parameters

NameTypeDefaultDescription
mat1matrix<int>The first integer matrix.
mat2matrix<int>The second integer matrix.

Returns: matrix<int>

Matrix multiplication.

Parameters

NameTypeDefaultDescription
mat1matrix<float>The first float matrix.
mat2matrix<float>The second float matrix.

Returns: matrix<float>

Matrix multiplication.

Parameters

NameTypeDefaultDescription
mat1matrix<int>The first integer matrix.
mat2matrix<float>The second float matrix.

Returns: matrix<float>

Matrix multiplication.

Parameters

NameTypeDefaultDescription
mat1matrix<float>The first float matrix.
mat2matrix<int>The second integer matrix.

Returns: matrix<float>


pinv

This function computes and returns the Moore-Penrose pseudoinverse of the specified matrix.

Parameters

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

Returns: matrix<float>

Computes the Moore-Penrose pseudoinverse.

Parameters

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

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

Returns: matrix<int>

Raises matrix to power.

Parameters

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

Returns: matrix<float>


rank

This function calculates and returns the rank of the specified matrix.

Parameters

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

Returns: int

Returns the rank of the matrix.

Parameters

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

Returns: int


remove_col

pine
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

NameTypeDefaultDescription
matmatrix<T>The matrix from which the column will be removed.
columnintmatrix.columns() - 1The index of the column to remove. When omitted, last column.

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


remove_row

pine
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

NameTypeDefaultDescription
matmatrix<T>The matrix from which the row will be removed.
rowintmatrix.rows() - 1The index of the row to remove. When omitted, last row.

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


reshape

pine
matrix.reshape(matrix<T> mat, int rows, int columns)

This function reshapes the given matrix to the specified number of rows and columns.

Parameters

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

reverse

pine
matrix.reverse(matrix<T> mat)

This function reverses the order of elements in the specified matrix in place.

Parameters

NameTypeDefaultDescription
matmatrix<T>The matrix to reverse.

row

pine
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

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

Returns: array<T>


rows

pine
matrix.rows(matrix<T> mat) → int

This function returns the number of rows in the specified matrix.

Parameters

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

Returns: int


set

pine
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

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

This function sorts the elements of the given matrix in ascending or descending order.

Parameters

NameTypeDefaultDescription
matmatrix<int>The integer matrix to sort.
orderanyorder.ascendingThe sort order (ascending or descending).

Sorts matrix elements.

Parameters

NameTypeDefaultDescription
matmatrix<float>The float matrix to sort.
orderanyorder.ascendingThe sort order (ascending or descending).

Sorts matrix elements.

Parameters

NameTypeDefaultDescription
matmatrix<string>The string matrix to sort.
orderanyorder.ascendingThe sort order (ascending or descending).

submatrix

pine
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

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

NameTypeDefaultDescription
mat1matrix<int>The first integer matrix.
mat2matrix<int>The second integer matrix.

Returns: matrix<int>

Element-wise sum.

Parameters

NameTypeDefaultDescription
mat1matrix<float>The first float matrix.
mat2matrix<float>The second float matrix.

Returns: matrix<float>

Element-wise sum.

Parameters

NameTypeDefaultDescription
mat1matrix<int>The first integer matrix.
mat2matrix<float>The second float matrix.

Returns: matrix<float>

Element-wise sum.

Parameters

NameTypeDefaultDescription
mat1matrix<float>The first float matrix.
mat2matrix<int>The second integer matrix.

Returns: matrix<float>


swap_columns

pine
matrix.swap_columns(matrix<T> mat, int column1, int column2)

This function swaps two columns in the specified matrix.

Parameters

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

swap_rows

pine
matrix.swap_rows(matrix<T> mat, int row1, int row2)

This function swaps two rows in the specified matrix.

Parameters

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

trace

This function calculates and returns the trace of the specified square matrix.

Parameters

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

Returns: int

Returns the trace of the square matrix.

Parameters

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

Returns: float


transpose

pine
matrix.transpose(matrix<T> mat) → matrix<T>

This function transposes the given matrix and returns the new transposed matrix.

Parameters

NameTypeDefaultDescription
matmatrix<T>The matrix to transpose.

Returns: matrix<T>

基于 MIT 许可证发布。