Documentation
¶
Overview ¶
Package sparse provides sparse matrices (COO, CSR, CSC, DOK), conversions, products, sums and scaling in pure Go, plus the LinearOperator abstraction used by the iterative solvers in package iter.
Element types are float64 and complex128 (the Scalar constraint). Indices are Go ints, so the package works unchanged on 32-bit targets.
Equivalents: scipy.sparse.{coo,csr,csc,dok}_array, MATLAB sparse, find, nnz, spdiags, speye.
Index ¶
- Constants
- Variables
- type COO
- type CSC
- func (a *CSC[T]) At(i, j int) T
- func (a *CSC[T]) Clone() *CSC[T]
- func (a *CSC[T]) Dense() []T
- func (a *CSC[T]) Diagonal(k int) []T
- func (a *CSC[T]) Dims() (int, int)
- func (a *CSC[T]) MulVec(dst, x []T)
- func (a *CSC[T]) MulVecT(dst, x []T)
- func (a *CSC[T]) NNZ() int
- func (a *CSC[T]) Scale(s T)
- func (a *CSC[T]) ToCOO() *COO[T]
- func (a *CSC[T]) ToCSR() *CSR[T]
- func (a *CSC[T]) Transpose() *CSR[T]
- type CSR
- func Add[T Scalar](a, b *CSR[T]) (*CSR[T], error)
- func Axpby[T Scalar](alpha T, a *CSR[T], beta T, b *CSR[T]) (*CSR[T], error)
- func Diags[T Scalar](diagonals [][]T, offsets []int, rows, cols int) (*CSR[T], error)
- func Eye[T Scalar](n int) *CSR[T]
- func FromDense[T Scalar](rows, cols int, d []T) (*CSR[T], error)
- func Mul[T Scalar](a, b *CSR[T]) (*CSR[T], error)
- func NewCSR[T Scalar](rows, cols int, indptr []int, indices []int32, data []T) (*CSR[T], error)
- func Sub[T Scalar](a, b *CSR[T]) (*CSR[T], error)
- func (a *CSR[T]) At(i, j int) T
- func (a *CSR[T]) Clone() *CSR[T]
- func (a *CSR[T]) Dense() []T
- func (a *CSR[T]) Diagonal(k int) []T
- func (a *CSR[T]) Dims() (int, int)
- func (a *CSR[T]) H() *CSR[T]
- func (a *CSR[T]) IsSymmetric(tol float64) bool
- func (a *CSR[T]) MulDense(dst, b []T, k int)
- func (a *CSR[T]) MulVec(dst, x []T)
- func (a *CSR[T]) MulVecT(dst, x []T)
- func (a *CSR[T]) NNZ() int
- func (a *CSR[T]) Norm1() float64
- func (a *CSR[T]) NormFro() float64
- func (a *CSR[T]) NormInf() float64
- func (a *CSR[T]) Permute(p, q []int) (*CSR[T], error)
- func (a *CSR[T]) Prune(tol float64)
- func (a *CSR[T]) Scale(s T)
- func (a *CSR[T]) ScaleCols(d []T) error
- func (a *CSR[T]) ScaleRows(d []T) error
- func (a *CSR[T]) T() *CSR[T]
- func (a *CSR[T]) ToCOO() *COO[T]
- func (a *CSR[T]) ToCSC() *CSC[T]
- func (a *CSR[T]) Transpose() *CSC[T]
- func (a *CSR[T]) Tril(k int) *CSR[T]
- func (a *CSR[T]) Triu(k int) *CSR[T]
- func (a *CSR[T]) Validate() error
- type DOK
- type FuncOperator
- type LinearOperator
- type Scalar
- type TransposeOperator
Constants ¶
const MaxDim = math.MaxInt32
MaxDim is the largest number of rows or columns of a CSR or CSC matrix: minor indices are stored as int32.
Variables ¶
var ( // ErrShape reports incompatible dimensions. ErrShape = errors.New("sparse: incompatible shapes") // ErrIndex reports an index outside the matrix. ErrIndex = errors.New("sparse: index out of range") // ErrFormat reports a malformed compressed structure. ErrFormat = errors.New("sparse: malformed structure") )
Errors returned (or wrapped) by this package.
Functions ¶
This section is empty.
Types ¶
type COO ¶
COO is a coordinate-format matrix: parallel slices of row index, column index and value. Duplicate (i, j) entries are allowed and are summed on conversion, as in scipy.sparse.coo_array.
type CSC ¶
CSC is a compressed-sparse-column matrix. Column j occupies Indices[Indptr[j]:Indptr[j+1]] (row indices, strictly increasing).
func (*CSC[T]) Dense ¶
func (a *CSC[T]) Dense() []T
Dense returns the matrix as a row-major dense slice.
type CSR ¶
CSR is a compressed-sparse-row matrix. Row i occupies Indices[Indptr[i]:Indptr[i+1]] / Data[...] with strictly increasing column indices (canonical form, as produced by every constructor here). Column indices are int32, as scipy.sparse stores them whenever they fit: the products stream one index per stored value, so their width is a third of the memory traffic (dimensions are limited to MaxDim).
func Diags ¶
Diags builds a rows x cols matrix from diagonals placed at the given offsets (scipy.sparse.diags / MATLAB spdiags). diagonals[t] has the length of diagonal offsets[t].
func NewCSR ¶
NewCSR wraps the given arrays (without copying) after validating that they form a canonical CSR matrix.
func (*CSR[T]) Dense ¶
func (a *CSR[T]) Dense() []T
Dense returns the matrix as a row-major dense slice of length Rows*Cols.
func (*CSR[T]) IsSymmetric ¶
IsSymmetric reports whether A equals Aᵀ within |a_ij - a_ji| <= tol.
func (*CSR[T]) MulDense ¶
MulDense computes dst = A B for row-major dense B (Cols x k) and dst (Rows x k): the SpMM kernel (scipy A @ B for a dense B).
func (*CSR[T]) MulVecT ¶
func (a *CSR[T]) MulVecT(dst, x []T)
MulVecT computes dst = Aᵀ x (plain transpose, no conjugation).
func (*CSR[T]) Permute ¶
Permute returns B with B[i, j] = A[p[i], q[j]]. A nil permutation is the identity.
func (*CSR[T]) Prune ¶
Prune removes stored entries with |value| <= tol (tol = 0 removes explicit zeros) in place.
type DOK ¶
DOK is a dictionary-of-keys matrix, convenient for incremental construction (scipy.sparse.dok_array).
type FuncOperator ¶
FuncOperator builds an operator from functions. MvT may be nil when no transpose product is available.
func (FuncOperator) MulVec ¶
func (f FuncOperator) MulVec(dst, x []float64)
MulVec implements LinearOperator.
func (FuncOperator) MulVecT ¶
func (f FuncOperator) MulVecT(dst, x []float64)
MulVecT implements TransposeOperator; it panics when MvT is nil.
type LinearOperator ¶
type LinearOperator interface {
// Dims returns the number of rows and columns of A.
Dims() (rows, cols int)
// MulVec stores A x in dst; len(x) == cols, len(dst) == rows.
MulVec(dst, x []float64)
}
LinearOperator is a real matrix-free linear map x -> A x, the abstraction the iterative solvers work on (scipy.sparse.linalg.LinearOperator). *CSR[float64] and *CSC[float64] implement it.
func Shifted ¶
func Shifted(a LinearOperator, shift float64) LinearOperator
Shifted returns the operator A + shift*I.
type Scalar ¶
type Scalar interface{ float64 | complex128 }
Scalar is the element type of a sparse matrix.
type TransposeOperator ¶
type TransposeOperator interface {
LinearOperator
// MulVecT stores Aᵀ x in dst; len(x) == rows, len(dst) == cols.
MulVecT(dst, x []float64)
}
TransposeOperator is a LinearOperator that can also apply Aᵀ, which LSQR requires. *CSR[float64] and *CSC[float64] implement it.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
conformance
command
Command conformance writes docs/verification.md: the committed fixture compared with the Go implementation, and with -corpus a live comparison against SciPy on the NIST Harwell-Boeing matrices, downloaded into a cache directory outside the repository.
|
Command conformance writes docs/verification.md: the committed fixture compared with the Go implementation, and with -corpus a live comparison against SciPy on the NIST Harwell-Boeing matrices, downloaded into a cache directory outside the repository. |
|
genfixtures
command
Command genfixtures runs the conformance suite against a live SciPy oracle and writes the fixture file the regression tests read.
|
Command genfixtures runs the conformance suite against a live SciPy oracle and writes the fixture file the regression tests read. |
|
perfbench
command
Command perfbench compares this module with SciPy on one core: SpMV, CG and GMRES on 2-D Laplacians of about 1e5 and 1e6 nonzeros.
|
Command perfbench compares this module with SciPy on one core: SpMV, CG and GMRES on 2-D Laplacians of about 1e5 and 1e6 nonzeros. |
|
Package gallery generates deterministic test matrices: finite-difference Laplacians, a convection-diffusion operator, and seeded random sparse matrices.
|
Package gallery generates deterministic test matrices: finite-difference Laplacians, a convection-diffusion operator, and seeded random sparse matrices. |
|
internal
|
|
|
conformance
Package conformance compares this module with SciPy.
|
Package conformance compares this module with SciPy. |
|
scipy
Package scipy drives SciPy as a verification oracle: one Python process per Session, spoken to in JSON lines over a pipe.
|
Package scipy drives SciPy as a verification oracle: one Python process per Session, spoken to in JSON lines over a pipe. |
|
Package iter provides Krylov-subspace solvers for sparse linear systems (CG, GMRES(m), BiCGSTAB, MINRES, LSQR) and preconditioners (Jacobi, SSOR, IC(0), ILU(0), ILUT).
|
Package iter provides Krylov-subspace solvers for sparse linear systems (CG, GMRES(m), BiCGSTAB, MINRES, LSQR) and preconditioners (Jacobi, SSOR, IC(0), ILU(0), ILUT). |
|
Package mtx reads and writes the Matrix Market exchange format (https://math.nist.gov/MatrixMarket/formats.html): coordinate and array layouts; real, integer, pattern and complex fields; general, symmetric, skew-symmetric and hermitian symmetry.
|
Package mtx reads and writes the Matrix Market exchange format (https://math.nist.gov/MatrixMarket/formats.html): coordinate and array layouts; real, integer, pattern and complex fields; general, symmetric, skew-symmetric and hermitian symmetry. |
|
Package nd connects sparse matrices to go-ndarray arrays: dense vectors and matrices go in and out of the solvers as *ndarray.Array.
|
Package nd connects sparse matrices to go-ndarray arrays: dense vectors and matrices go in and out of the solvers as *ndarray.Array. |
|
Package order computes sparse-matrix orderings and measures their quality.
|
Package order computes sparse-matrix orderings and measures their quality. |