lacuna.sparse.csr#
Sparse CSR matrix implementation backed by a Rust core.
This module exposes a minimal, NumPy-friendly CSR matrix class with high-performance kernels delegated to a PyO3-bound Rust extension.
Notes
Index arrays (indptr, indices) are stored as int64 and validated (when check=True) against structural invariants expected by the Rust core.
Data is stored as float64 in v0.1.
Most computational methods call into the Rust core when available; if the core is not available, a RuntimeError is raised.
Classes
|
Compressed Sparse Row (CSR) matrix. |
- class lacuna.sparse.csr.CSR(indptr, indices, data, shape, dtype=<class 'numpy.float64'>, check=True)[source]#
Bases:
SparseMatrixCompressed Sparse Row (CSR) matrix.
- Parameters:
indptr (array-like of int64, shape (n_rows + 1,)) – Row pointer array. Must be non-decreasing, start at 0, end at nnz.
indices (array-like of int64, shape (nnz,)) – Column indices for each non-zero. Must be strictly increasing within each row.
data (array-like of float64, shape (nnz,)) – Non-zero values.
dtype (numpy.dtype, optional (default: np.float64)) – Data dtype (v0.1 supports float64 only).
check (bool, optional (default: True)) – When True, validate structural invariants using the Rust core.
Notes
The constructor may raise if the structure is invalid when check=True.
Examples
Construct a small CSR and run basic ops:
>>> import numpy as np >>> from lacuna.sparse import CSR >>> indptr = np.array([0, 2, 3]) # 2 rows, 3 nnz >>> indices = np.array([0, 2, 1]) >>> data = np.array([1.0, 3.0, 2.0]) >>> a = CSR(indptr, indices, data, shape=(2, 3)) >>> a.nnz 3 >>> (a @ np.array([1.0, 0.0, 1.0])).tolist() # SpMV [4.0, 2.0] >>> a.sum() 6.0
- property T#
Transpose of the matrix (CSR).
- Returns:
Transposed matrix as a new CSR instance.
- Return type:
- Raises:
RuntimeError – If the native core is not available.
- astype(dtype)[source]#
Return a copy converted to the given dtype.
- Parameters:
dtype (numpy.dtype or str) – Target dtype. v0.1 supports float64 only.
- Returns:
A new CSR with data cast to the requested dtype.
- Return type:
- Raises:
NotImplementedError – If dtype is not float64 in v0.1.
- eliminate_zeros()[source]#
Remove exact zeros from the matrix structure.
- Returns:
New CSR with all zero entries removed.
- Return type:
- Raises:
RuntimeError – If the native core is not available.
- classmethod from_arrays(indptr, indices, data, shape, check=True)[source]#
Construct a CSR from raw arrays.
- Parameters:
indptr – See CSR.__init__.
indices – See CSR.__init__.
data – See CSR.__init__.
shape – See CSR.__init__.
check – See CSR.__init__.
- Returns:
New CSR instance.
- Return type:
- property nnz#
Number of stored non-zero entries (int).
- prune(eps)[source]#
Remove entries with absolute value <= eps.
- Parameters:
eps (float) – Threshold for pruning.
- Returns:
New CSR with pruned entries.
- Return type:
- Raises:
RuntimeError – If the native core is not available.
- sum(axis=None)[source]#
Sum elements along the given axis using the Rust core.
- Parameters:
axis ({None, 0, 1}, optional) – None for total sum (scalar), 0 for column sums (length = ncols), 1 for row sums (length = nrows).
- Returns:
Scalar for total sum, 1D array for axis-specific sums.
- Return type:
- Raises:
RuntimeError – If the native core is not available.
ValueError – If axis is not one of {None, 0, 1}.