Skip to content

Matrix

Module: strata.core • Kind: struct • Traits: ArrayLike, Copyable, Movable, Writable
Source: strata/core/matrix.mojo

struct Matrix[dtype: DType = DType.float64](ArrayLike, Copyable, Movable, Writable)
from strata.core import Matrix

Dense 2D row-major matrix container with striding and view support.

Provides contiguous buffer allocation, SIMD-compatible row-major layout, slicing, element-wise arithmetic, and BLAS/LAPACK interop.


Parameter Description
dtype Numerical data type of matrix elements. Default DType.float64.

def __init__(out self, rows: Int, cols: Int, fill: Scalar[Self.dtype] = 0)

Initialize a Matrix of shape (rows, cols) filled with a constant value.

Argument Type Description
rows Int Number of rows (N>=0N >= 0).
cols Int Number of columns (D>=0D >= 0).
fill Scalar[Self.dtype] Constant scalar value to fill the buffer. Default 0.
def __init__(
out self,
rows: Int,
cols: Int,
var data: List[Scalar[Self.dtype]],
)

Initialize a Matrix from an existing 1D buffer.

Argument Type Description
rows Int Number of rows (N>=0N >= 0).
cols Int Number of columns (D>=0D >= 0).
data — Flat row-major element buffer of length rows * cols.

Attribute Description
rows Number of matrix rows (NN).
cols Number of matrix columns (DD).
data Flat 1D buffer of matrix elements in row-major order.

Method Description
Matrix.zeros() Create a zero-filled Matrix of shape (rows, cols).
Matrix.ones() Create a one-filled Matrix of shape (rows, cols).
Matrix.eye() Create an identity Matrix of shape (n, n).
Matrix.from_numpy() Construct from a 2D NumPy ndarray.
Matrix.to_numpy() Convert to a 2D NumPy ndarray.
Matrix.num_rows() Return the number of rows.
Matrix.num_cols() Return the number of columns.
Matrix.num_elements() Return the total number of elements (rows * cols).
Matrix.shape() Return the (rows, cols) dimensions tuple.
Matrix.view() Return a non-owning MatrixView over the matrix buffer.
Matrix.slice_rows() Return a non-owning view of a row range.
Matrix.slice_cols() Return a non-owning view of a column range.
Matrix.slice_2d() Return a non-owning 2D sub-matrix view.
Matrix.row() Return a non-owning view of a single row.
Matrix.col() Return a non-owning view of a single column.
Matrix.cast() Promotes or converts the Matrix elements to target_dtype.
Matrix.transpose() Return the transposed matrix.
Matrix.select_columns() Extracts an N×KN \times K submatrix containing only the specified column indices in order.
Matrix.select_rows() Extracts an M×DM \times D submatrix containing only the specified row indices in order.
Matrix.dot() Compute matrix multiplication with another matrix.
Matrix.dot_vec() Compute matrix-vector product.
Matrix.mean_along_axis_0() Compute the column-wise arithmetic mean.
Matrix.std_along_axis_0() Compute the column-wise standard deviation.
Matrix.write_to() Format a human-readable string representation to a writer.

def zeros(rows: Int, cols: Int) -> Self

Create a zero-filled Matrix of shape (rows, cols).

Parameter Type Description
rows Int —
cols Int —

Returns: Self


def ones(rows: Int, cols: Int) -> Self

Create a one-filled Matrix of shape (rows, cols).

Parameter Type Description
rows Int —
cols Int —

Returns: Self


def eye(n: Int) -> Self

Create an identity Matrix of shape (n, n).

Parameter Type Description
n Int —

Returns: Self


def from_numpy(np_arr: PythonObject) -> Self

Construct from a 2D NumPy ndarray.

Parameter Type Description
np_arr PythonObject —

Returns: Self


def to_numpy(self) -> PythonObject

Convert to a 2D NumPy ndarray.

Returns: PythonObject


def num_rows(self) -> Int

Return the number of rows.

Returns: Int


def num_cols(self) -> Int

Return the number of columns.

Returns: Int


def num_elements(self) -> Int

Return the total number of elements (rows * cols).

Returns: Int


def shape(self) -> Tuple[Int, Int]

Return the (rows, cols) dimensions tuple.

Returns: Tuple[Int, Int]


def view(ref self) -> MatrixView[Self.dtype, origin_of(self.data)]

Return a non-owning MatrixView over the matrix buffer.

Returns: MatrixView[Self.dtype, origin_of(self.data)]


def slice_rows(
ref self,
start_row: Int,
end_row: Int,
) -> MatrixView[Self.dtype, origin_of(self.data)]

Return a non-owning view of a row range.

Parameter Type Description
start_row Int —
end_row Int —

Returns: MatrixView[Self.dtype, origin_of(self.data)]


def slice_cols(
ref self,
start_col: Int,
end_col: Int,
) -> MatrixView[Self.dtype, origin_of(self.data)]

Return a non-owning view of a column range.

Parameter Type Description
start_col Int —
end_col Int —

Returns: MatrixView[Self.dtype, origin_of(self.data)]


def slice_2d(
ref self,
start_row: Int,
end_row: Int,
start_col: Int,
end_col: Int,
) -> MatrixView[Self.dtype, origin_of(self.data)]

Return a non-owning 2D sub-matrix view.

Parameter Type Description
start_row Int —
end_row Int —
start_col Int —
end_col Int —

Returns: MatrixView[Self.dtype, origin_of(self.data)]


def row(self, r: Int) -> List[Scalar[Self.dtype]]

Return a non-owning view of a single row.

Parameter Type Description
r Int —

Returns: List[Scalar[Self.dtype]]


def col(self, c: Int) -> List[Scalar[Self.dtype]]

Return a non-owning view of a single column.

Parameter Type Description
c Int —

Returns: List[Scalar[Self.dtype]]


def cast[target_dtype: DType](self) -> Matrix[target_dtype]

Promotes or converts the Matrix elements to target_dtype.

Returns: Matrix[target_dtype]


def transpose(self) -> Self

Return the transposed matrix.

Returns: Self


def select_columns(self, indices: List[Int]) -> Self

Extracts an N×KN \times K submatrix containing only the specified column indices in order.

Parameter Type Description
indices List[Int] List of column indices to extract.

Returns: Self — Matrix[dtype]: Submatrix of shape (self.rows, len(indices)).


def select_rows(self, indices: List[Int]) -> Self

Extracts an M×DM \times D submatrix containing only the specified row indices in order.

Parameter Type Description
indices List[Int] List of row indices to extract.

Returns: Self — Matrix[dtype]: Submatrix of shape (len(indices), self.cols).


def dot(self, other: Self) -> Self

Compute matrix multiplication with another matrix.

Parameter Type Description
other Self —

Returns: Self


def dot_vec(self, vec: List[Scalar[Self.dtype]]) -> List[Scalar[Self.dtype]]

Compute matrix-vector product.

Parameter Type Description
vec List[Scalar[Self.dtype]] —

Returns: List[Scalar[Self.dtype]]


def mean_along_axis_0(self) -> List[Scalar[Self.dtype]]

Compute the column-wise arithmetic mean.

Returns: List[Scalar[Self.dtype]]


def std_along_axis_0(
self,
means: List[Scalar[Self.dtype]],
) -> List[Scalar[Self.dtype]]

Compute the column-wise standard deviation.

Parameter Type Description
means List[Scalar[Self.dtype]] —

Returns: List[Scalar[Self.dtype]]


def write_to(self, mut writer: Some[Writer])

Format a human-readable string representation to a writer.

Section titled “Format a human-readable string representation to a writer.”
from strata.core import Matrix
var A = Matrix[DType.float64](2, 3, fill=1.0)
var B = Matrix[DType.float64].eye(3)
var C = A.dot(B)