scipy.sparse — Sparse Matrices and Sparse Linear Algebra¶
Provides sparse matrix construction, conversion, inspection, and linear algebra
from scipy.sparse and scipy.sparse.linalg as importable clausal predicates.
Import¶
or via the canonical py.* path:
Tiers¶
| Tier | Predicates | Notes |
|---|---|---|
| 3 — handle | MakeCSR, MakeCSC, MakeCOO, MakeDiagonals, MakeEye |
Sparse matrix construction |
| 3 — handle | ToDense, FromDense |
Dense/sparse conversions |
| 3 — handle | Shape, NonzeroCount |
Inspection |
| linalg | Solve, EigenDecomposeHermitian, SingularValueDecompose |
Sparse linear algebra |
| lifecycle | Free |
Release any handle |
Tier 3 — Construction¶
All construction predicates allocate a sparse matrix and return an opaque integer HANDLE. Pass the HANDLE to conversion, inspection, or linalg predicates.
MakeCSR¶
# skip
MakeCSR(DATA, INDICES, INDPTR, RESULT)
MakeCSR(DATA, INDICES, INDPTR, SHAPE, RESULT)
MakeCSR(DATA, INDICES, INDPTR, SHAPE, DTYPE, RESULT)
Build a CSR (Compressed Sparse Row) matrix.
Wraps scipy.sparse.csr_matrix((data, indices, indptr), shape=SHAPE, dtype=DTYPE).
DATA: 1-D array of non-zero valuesINDICES: 1-D array of column indices for each stored valueINDPTR: 1-D array of row pointers (length = rows + 1)SHAPE:(rows, cols)tuple (optional; inferred when omitted)DTYPE: NumPy dtype string, e.g.'float64'(optional)RESULT: integer HANDLE
# skip
DATA is ++([1.0, 2.0, 3.0]),
IDX is ++([0, 1, 2]),
PTR is ++([0, 1, 2, 3]),
MakeCSR(DATA, IDX, PTR, H)
MakeCSC¶
# skip
MakeCSC(DATA, INDICES, INDPTR, RESULT)
MakeCSC(DATA, INDICES, INDPTR, SHAPE, RESULT)
MakeCSC(DATA, INDICES, INDPTR, SHAPE, DTYPE, RESULT)
Build a CSC (Compressed Sparse Column) matrix.
Wraps scipy.sparse.csc_matrix(...). Arguments have the same meaning as
MakeCSR but INDICES contains row indices and INDPTR contains column
pointers.
MakeCOO¶
Build a COO (Coordinate) sparse matrix.
Wraps scipy.sparse.coo_matrix((data, (row, col)), shape=SHAPE).
DATA: 1-D array of non-zero valuesROW,COL: 1-D arrays of row and column indices for each valueSHAPE:(rows, cols)tuple (optional)RESULT: integer HANDLE
# skip
DATA is ++([1.0, 2.0]),
ROW is ++([0, 1]),
COL is ++([1, 0]),
MakeCOO(DATA, ROW, COL, ++(tuple([2, 2])), H)
MakeDiagonals¶
# skip
MakeDiagonals(DIAGONALS, RESULT)
MakeDiagonals(DIAGONALS, OFFSETS, RESULT)
MakeDiagonals(DIAGONALS, OFFSETS, SHAPE, RESULT)
Build a sparse diagonal matrix.
Wraps scipy.sparse.diags(diagonals, offsets=OFFSETS, shape=SHAPE).
DIAGONALS: a single 1-D array (main diagonal) or a list of arraysOFFSETS: integer (0 = main diagonal, +k = k-th superdiagonal, -k = k-th subdiagonal) or a list matchingDIAGONALSSHAPE:(rows, cols)tuple (optional)RESULT: integer HANDLE
# skip
MakeDiagonals(++([1.0, 2.0, 3.0]), H) % 3x3 identity-like diagonal
MakeDiagonals(++([1.0, 2.0]), 1, ++(tuple([3, 3])), H) % superdiagonal
MakeEye¶
Build a sparse identity or shifted-diagonal matrix.
Wraps scipy.sparse.eye(N, M=M, k=K).
N: number of rowsM: number of columns (default = N)K: diagonal offset (0 = main diagonal, default = 0)RESULT: integer HANDLE
Tier 3 — Conversions¶
ToDense¶
Convert a sparse matrix to a dense NumPy array.
Wraps handle.toarray(order=ORDER).
HANDLE: integer handle to a sparse matrixORDER:'C'(row-major) or'F'(column-major); default → row-majorRESULT: 2-D NumPy array
FromDense¶
Convert a dense array to a sparse matrix handle.
DENSE: 2-D NumPy array (or nested lists)FORMAT: sparse format string:'csr','csc','coo', etc. Default (arity 2) is'csr'.RESULT: integer HANDLE
Tier 3 — Inspection¶
Shape¶
Return the shape of the sparse matrix as a (rows, cols) tuple.
NonzeroCount¶
Return the number of stored (non-zero) elements (handle.nnz).
scipy.sparse.linalg¶
Solve¶
# skip
Solve(A, B, RESULT)
Solve(A, B, PERMC_SPEC, RESULT)
Solve(A, B, PERMC_SPEC, USE_UMFPACK, RESULT)
Solve the sparse linear system A @ X = B.
Wraps scipy.sparse.linalg.spsolve(a, b, permc_spec=..., use_umfpack=...).
A: HANDLE to a square sparse matrixB: 1-D (or 2-D) right-hand-side arrayPERMC_SPEC: column permutation strategy:'NATURAL','MMD_ATA','MMD_AT_PLUS_A','COLAMD', orNone(default)USE_UMFPACK: boolean, use UMFPACK if available (defaultTrue)RESULT: dense solution array X
EigenDecomposeHermitian¶
Compute K eigenvalues and eigenvectors of a real-symmetric or complex-Hermitian
sparse matrix. Wraps scipy.sparse.linalg.eigsh(a, k=K).
A: HANDLE to a sparse symmetric/Hermitian matrixK: number of eigenvalues to compute (default =min(6, n-1))RESULT: dict with keys'eigenvalues'(1-D array) and'eigenvectors'(n × K array)
SingularValueDecompose¶
Compute K largest singular values and vectors via ARPACK.
Wraps scipy.sparse.linalg.svds(a, k=K).
A: HANDLE to a sparse matrixK: number of singular values to compute (default =min(6, min(shape)-1))RESULT: dict with keys'u'(left singular vectors, n × K),'s'(singular values, length K),'vt'(right singular vectors, K × m)
Lifecycle¶
Free¶
Release the sparse matrix registered under HANDLE. Always succeeds.
Call Free when the handle is no longer needed to avoid memory leaks.
Usage example¶
-import_from(scipy_sparse, [MakeCSR, Solve, NonzeroCount, Free])
solve_sparse(DATA, IDX, PTR, SHAPE, B, X) <- (
MakeCSR(DATA, IDX, PTR, SHAPE, A),
Solve(A, B, X),
Free(A)
)
sparsity(DATA, IDX, PTR, SPARSITY) <- (
MakeCSR(DATA, IDX, PTR, A),
Shape(A, S),
NonzeroCount(A, NNZ),
TOTAL is ++(S[0] * S[1]),
SPARSITY is ++(1.0 - NNZ / TOTAL),
Free(A)
)