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 MatrixDense 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.
Parameters (Compile-Time)
Section titled “Parameters (Compile-Time)”| Parameter | Description |
|---|---|
dtype |
Numerical data type of matrix elements. Default DType.float64. |
Constructors
Section titled “Constructors”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 (). |
cols |
Int |
Number of columns (). |
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 (). |
cols |
Int |
Number of columns (). |
data |
— | Flat row-major element buffer of length rows * cols. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
rows |
Number of matrix rows (). |
cols |
Number of matrix columns (). |
data |
Flat 1D buffer of matrix elements in row-major order. |
Methods Overview
Section titled “Methods Overview”| 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 submatrix containing only the specified column indices in order. |
Matrix.select_rows() |
Extracts an 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. |
Method Details
Section titled “Method Details”Matrix.zeros()
Section titled “Matrix.zeros()”def zeros(rows: Int, cols: Int) -> SelfCreate a zero-filled Matrix of shape (rows, cols).
| Parameter | Type | Description |
|---|---|---|
rows |
Int |
— |
cols |
Int |
— |
Returns: Self
Matrix.ones()
Section titled “Matrix.ones()”def ones(rows: Int, cols: Int) -> SelfCreate a one-filled Matrix of shape (rows, cols).
| Parameter | Type | Description |
|---|---|---|
rows |
Int |
— |
cols |
Int |
— |
Returns: Self
Matrix.eye()
Section titled “Matrix.eye()”def eye(n: Int) -> SelfCreate an identity Matrix of shape (n, n).
| Parameter | Type | Description |
|---|---|---|
n |
Int |
— |
Returns: Self
Matrix.from_numpy()
Section titled “Matrix.from_numpy()”def from_numpy(np_arr: PythonObject) -> SelfConstruct from a 2D NumPy ndarray.
| Parameter | Type | Description |
|---|---|---|
np_arr |
PythonObject |
— |
Returns: Self
Matrix.to_numpy()
Section titled “Matrix.to_numpy()”def to_numpy(self) -> PythonObjectConvert to a 2D NumPy ndarray.
Returns: PythonObject
Matrix.num_rows()
Section titled “Matrix.num_rows()”def num_rows(self) -> IntReturn the number of rows.
Returns: Int
Matrix.num_cols()
Section titled “Matrix.num_cols()”def num_cols(self) -> IntReturn the number of columns.
Returns: Int
Matrix.num_elements()
Section titled “Matrix.num_elements()”def num_elements(self) -> IntReturn the total number of elements (rows * cols).
Returns: Int
Matrix.shape()
Section titled “Matrix.shape()”def shape(self) -> Tuple[Int, Int]Return the (rows, cols) dimensions tuple.
Returns: Tuple[Int, Int]
Matrix.view()
Section titled “Matrix.view()”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)]
Matrix.slice_rows()
Section titled “Matrix.slice_rows()”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)]
Matrix.slice_cols()
Section titled “Matrix.slice_cols()”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)]
Matrix.slice_2d()
Section titled “Matrix.slice_2d()”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)]
Matrix.row()
Section titled “Matrix.row()”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]]
Matrix.col()
Section titled “Matrix.col()”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]]
Matrix.cast()
Section titled “Matrix.cast()”def cast[target_dtype: DType](self) -> Matrix[target_dtype]Promotes or converts the Matrix elements to target_dtype.
Returns: Matrix[target_dtype]
Matrix.transpose()
Section titled “Matrix.transpose()”def transpose(self) -> SelfReturn the transposed matrix.
Returns: Self
Matrix.select_columns()
Section titled “Matrix.select_columns()”def select_columns(self, indices: List[Int]) -> SelfExtracts an 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)).
Matrix.select_rows()
Section titled “Matrix.select_rows()”def select_rows(self, indices: List[Int]) -> SelfExtracts an 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).
Matrix.dot()
Section titled “Matrix.dot()”def dot(self, other: Self) -> SelfCompute matrix multiplication with another matrix.
| Parameter | Type | Description |
|---|---|---|
other |
Self |
— |
Returns: Self
Matrix.dot_vec()
Section titled “Matrix.dot_vec()”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]]
Matrix.mean_along_axis_0()
Section titled “Matrix.mean_along_axis_0()”def mean_along_axis_0(self) -> List[Scalar[Self.dtype]]Compute the column-wise arithmetic mean.
Returns: List[Scalar[Self.dtype]]
Matrix.std_along_axis_0()
Section titled “Matrix.std_along_axis_0()”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]]
Matrix.write_to()
Section titled “Matrix.write_to()”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.”Example
Section titled “Example”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)