SparseTensor¶
SparseTensor is the single-process sparse matrix. It stores
a matrix in COO form (val, row, col plus a shape), optionally
with a leading batch dimension, and carries the full operation vocabulary:
linear and nonlinear solves, eigendecomposition, SVD, matrix–vector products,
scalar/structural queries, graph analysis and visualization. Every solve is
differentiable through torch.autograd and dispatches to the backend best
suited to the device (SciPy / PyTorch-native on CPU, cuDSS / STRUMPACK on GPU).
For the matrix sharded across processes, see DSparseTensor. For the per-operation reference, see Operations.
Construction¶
A SparseTensor can be built directly from COO triplets or
through several convenience constructors:
import torch
from torch_sla import SparseTensor
# Direct COO: values, row indices, col indices, shape
A = SparseTensor(val, row, col, (n, n))
# From a dense matrix (drops the zeros)
A = SparseTensor.from_dense(dense)
# From an explicit list of (row, col, value) entries
A = SparseTensor.from_coo_list(entries, shape=(n, n))
# Structured constructors
I = SparseTensor.eye(n) # identity
D = SparseTensor.diag(values) # diagonal
T = SparseTensor.tridiagonal(n, ...) # tridiagonal band
Conversions back out:
dense = A.to_dense() # torch.Tensor
crow, col, val = A.to_csr() # CSR triplet
t = A.to_torch_sparse() # torch.sparse_coo_tensor
A leading batch dimension (shape [B, n, n]) makes every operation act on a
stack of same-pattern matrices in one call; see solve_batch. For
matrices with different patterns use
SparseTensorList.
Operation catalog¶
Each operation links to its detailed entry in Operations and to its API object. Operations marked (grad) propagate gradients via the adjoint method.
Linear solves¶
Operation |
API |
Description |
|---|---|---|
solve (grad) |
Solve \(Ax = b\); auto-selects a direct or iterative backend. |
|
Many right-hand sides / value sets sharing one sparsity pattern. |
||
Cache an LU factorization for repeated solves. |
Nonlinear¶
Operation |
API |
Description |
|---|---|---|
nonlinear_solve (grad) |
Newton / Picard / Anderson solve of \(F(u, \theta) = 0\). |
Eigen / spectral¶
Matrix–vector¶
Operation |
API |
Description |
|---|---|---|
Sparse matrix–vector / matrix–matrix product (SpMV). |
Scalar / structural¶
Graph¶
Operation |
API |
Description |
|---|---|---|
Label connected components of the adjacency pattern. |
Visualization¶
I/O and reductions¶
Beyond the headline operations, SparseTensor also offers
save/load (safetensors and Matrix Market), element-wise math (abs,
sqrt, exp, log, …) and reductions (sum, mean, max,
min). These are documented in the API reference.