Numeric sparse matrix storage (COO/CSR/CSC), sparse kernels, and iterative/direct solvers for MoonBit.
| Package | Contents |
|---|---|
| types | SparseError, Shape, index validation |
| coo | Triplet storage and assembly, summing duplicate coordinates |
| csr | Compressed sparse row: y = A x, y = alpha A x + beta y, diagonal, symmetry check |
| csc | Compressed sparse column: y = A^T x, O(nnz) conversions |
| ops | Addition, subtraction, transposition, sparse-dense and sparse-sparse products |
| solve | Sparse Cholesky, conjugate gradient, BiCGSTAB, GMRES, Jacobi and ILU(0) preconditioners |
| reorder | Reverse Cuthill-McKee, bandwidth and profile measurement |
| io | Matrix Market (.mtx) reading and writing |
| graph | Degree, adjacency construction, Laplacians, connected components, PageRank |
| semiring | BoolSemiring and MinPlus, which turn the same product into reachability or shortest paths |
import {
"lhw153766-ctrl/sparse-linalg@0.1.2",
}import {
"lhw153766-ctrl/sparse-linalg/coo" @coo,
"lhw153766-ctrl/sparse-linalg/solve" @solve,
}let a : @coo.Coo[Double] = @coo.Coo::empty(3, 3)
a.push(0, 0, 2.0)
a.push(1, 1, 3.0)
a.push(2, 2, 4.0)
a.push(0, 1, -1.0)
a.push(1, 0, -1.0) // the mirror of the previous entry
let csr = a.to_csr()let y = csr.spmv([1.0, 1.0, 1.0])let opts = @solve.SolverOptions::new(1.0e-10, 5000)
let result = @solve.cg(a, b, options=opts, precond=@solve.Precond::Ilu0(@solve.Ilu0Precond::new(a)))
println(result.converged) // true
println(result.iterations) // 80
println(result.residual) // 7.5e-11let factor = @solve.CholeskyFactor::new_checked(a)
let x = factor.solve(b)
println(factor.log_determinant())let (ordered, perm) = @reorder.reorder_rcm(a)
let factor = @solve.CholeskyFactor::new(ordered)let file = @io.parse_matrix_market_full(text)
println(file.header.symmetry) // symmetric
println(file.matrix.nnz()) // stored triples, after mirroring
let a = file.matrix.to_csr()let adjacency = @graph.adjacency_from_edges(n, [(0, 1), (1, 2)])
let laplacian = @graph.laplacian(adjacency)
let ranks = @graph.page_rank(link_matrix)moon run examples/poisson
moon run examples/reordering
moon run examples/reachability
moon run examples/pagerank
moon run examples/matrix_market| numbering | bandwidth | factor entries |
|---|---|---|
| natural (row-major) | 60 | 216,059 |
| scrambled | 3,558 | 1,072,317 |
| scrambled, then RCM | 60 | 149,330 |
| kernel | time |
|---|---|
| spmv CSR, 1000x1000, 2,998 stored entries | 12.95 µs |
| spmv CSC, 1000x1000 | 14.53 µs |
| spmv_transpose CSC, 1000x1000 | 12.91 µs |
| spmv_add, 1000x1000 | 12.46 µs |
| spmv CSR, 10000x10000, 29,998 stored entries | 129.32 µs |
let result = @solve.cg(a, b, initial=Some(previous_solution))moon update
moon check --target all
moon build --target all
moon test --target all
moon fmt --check// Reachability: over the Boolean semiring, add is OR and multiply is AND.
let closure = @ops.spgemm(adjacency, adjacency)
// Shortest paths: over min-plus, add is min and multiply is plus.
let distances = @ops.spgemm(weights, weights)Install
Download zipNumeric sparse matrix storage (COO/CSR/CSC), sparse kernels, and iterative/direct solvers for MoonBit.