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()

Solve \(Ax = b\); auto-selects a direct or iterative backend.

solve_batch

solve_batch()

Many right-hand sides / value sets sharing one sparsity pattern.

lu

lu()

Cache an LU factorization for repeated solves.

Nonlinear

Operation

API

Description

nonlinear_solve (grad)

nonlinear_solve()

Newton / Picard / Anderson solve of \(F(u, \theta) = 0\).

Eigen / spectral

Operation

API

Description

eigsh (grad)

eigsh()

Top-k eigenpairs of a symmetric/Hermitian matrix.

eigs

eigs()

Top-k eigenpairs of a general matrix.

svd (grad)

svd()

Truncated rank-k singular value decomposition.

Matrix–vector

Operation

API

Description

matvec / @

__matmul__()

Sparse matrix–vector / matrix–matrix product (SpMV).

Scalar / structural

Operation

API

Description

det (grad)

det()

Determinant via sparse LU.

logdet (grad)

logdet()

Log-determinant (numerically stable for large matrices).

norm (grad)

norm()

Frobenius / 1- / 2-norm.

condition_number

condition_number()

Ratio \(\sigma_{\max}/\sigma_{\min}\).

is_symmetric / is_positive_definite

is_symmetric()

Structural predicates for solver selection.

Graph

Operation

API

Description

connected_components

connected_components()

Label connected components of the adjacency pattern.

Visualization

Operation

API

Description

spy

spy()

Plot the sparsity pattern as a matplotlib figure.

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.