Skip to content

CSRMatrix

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

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

Compressed Sparse Row (CSR) 2D matrix format.


def __init__(
out self,
rows: Int,
cols: Int,
var data: List[Scalar[Self.dtype]],
var indices: List[Int],
var indptr: List[Int],
)
Argument Type Description
rows Int —
cols Int —

Method Description
CSRMatrix.empty() Create an empty sparse matrix of the given dimensions.
CSRMatrix.from_scipy() Construct from a scipy.sparse matrix.
CSRMatrix.to_scipy() Convert to a scipy.sparse matrix.
CSRMatrix.from_dense() Construct a sparse matrix from a dense Matrix.
CSRMatrix.to_dense() Convert the sparse matrix to a dense Matrix.
CSRMatrix.to_csc() Convert to Compressed Sparse Column (CSCMatrix) format.
CSRMatrix.num_rows() Return the number of rows.
CSRMatrix.num_cols() Return the number of columns.
CSRMatrix.num_elements() Return the total number of elements (rows * cols).
CSRMatrix.shape() Return the (rows, cols) dimensions tuple.
CSRMatrix.cast() Promotes or converts the CSRMatrix data elements to target_dtype.
CSRMatrix.nnz() Return the number of stored non-zero entries.
CSRMatrix.dot_vec() Compute matrix-vector product.
CSRMatrix.dot_dense() Compute sparse-dense matrix multiplication.
CSRMatrix.dot_sparse() Compute sparse-sparse matrix multiplication.
CSRMatrix.write_to() Format a human-readable string representation to a writer.

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

Create an empty sparse matrix of the given dimensions.

Parameter Type Description
rows Int —
cols Int —

Returns: Self


def from_scipy(sp_arr: PythonObject) -> Self

Construct from a scipy.sparse matrix.

Parameter Type Description
sp_arr PythonObject —

Returns: Self


def to_scipy(self) -> PythonObject

Convert to a scipy.sparse matrix.

Returns: PythonObject


def from_dense(dense: Matrix[Self.dtype]) -> Self

Construct a sparse matrix from a dense Matrix.

Parameter Type Description
dense Matrix[Self.dtype] —

Returns: Self


def to_dense(self) -> Matrix[Self.dtype]

Convert the sparse matrix to a dense Matrix.

Returns: Matrix[Self.dtype]


def to_csc(self) -> CSCMatrix[Self.dtype]

Convert to Compressed Sparse Column (CSCMatrix) format.

Returns: CSCMatrix[Self.dtype]


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 cast[target_dtype: DType](self) -> CSRMatrix[target_dtype]

Promotes or converts the CSRMatrix data elements to target_dtype.

Returns: CSRMatrix[target_dtype]


def nnz(self) -> Int

Return the number of stored non-zero entries.

Returns: Int


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 dot_dense(self, other: Matrix[Self.dtype]) -> Matrix[Self.dtype]

Compute sparse-dense matrix multiplication.

Parameter Type Description
other Matrix[Self.dtype] —

Returns: Matrix[Self.dtype]


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

Compute sparse-sparse matrix multiplication.

Parameter Type Description
other Self —

Returns: Self


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

Format a human-readable string representation to a writer.