sparse #
Sparse tensors
vtl.sparse is an incremental sparse-tensor implementation for #746. The first slice covers two-dimensional coordinate (COO) tensors on CPU.
Current API
sparse.from_coo[T](shape, rows, cols, values)validates a 2-D shape and every coordinate, then copies the arrays into aCooTensor[T].sparse.from_dense[T](dense)extracts non-zero coordinates from a 2-D dense tensor.sparse.csr_from_dense[T](dense)andsparse.from_coo_arrays[T](shape, rows, cols, values)build a validatedCsrTensor[T](row_ptr,col_idx,values).CooTensor.nnz()reports the stored coordinate count.CooTensor.to_dense()expands to a dense row-major tensor; values at the same coordinate are summed in insertion order.CooTensor.coalesce()sorts coordinates by row/column and sums duplicates.
Explicit zeros are allowed and are retained by coalesce, so nnz() counts stored entries rather than mathematical non-zeros. Empty and zero-sized dimensions are supported. Duplicate coordinates are legal and are defined to sum in insertion order.
Run the focused tests with:
v test tests/sparse/coo_test.v
Not yet supported
CSR/CSC formats, sparse-dense and sparse-sparse arithmetic, reductions, autograd participation, GPU kernels, and SciPy/PyTorch reference fixtures are tracked in #746. Unsupported operations must not be assumed to work through an implicit dense conversion.
fn csr_from_dense #
fn csr_from_dense[T](dense &vtl.Tensor[T]) !CsrTensor[T]
csr_from_dense builds a CSR tensor from a 2-D dense tensor, skipping zeros.
fn from_coo #
fn from_coo[T](shape []int, rows []int, cols []int, values []T) !CooTensor[T]
from_coo builds a COO tensor and validates the shape and every coordinate.
fn from_coo_arrays #
fn from_coo_arrays[T](shape []int, rows []int, cols []int, values []T) !CsrTensor[T]
from_coo_arrays builds a CSR tensor from parallel COO arrays. Duplicate coordinates are summed; explicit zeros are retained.
fn from_dense #
fn from_dense[T](dense &vtl.Tensor[T]) !CooTensor[T]
from_dense extracts the non-zero coordinates of a 2-D dense tensor.
fn validate #
fn validate[T](t &CsrTensor[T]) !
validate checks all CSR invariants: shape, pointer monotonicity, column bounds and ordering, and storage lengths.
fn (CooTensor[T]) nnz #
fn (t &CooTensor[T]) nnz() int
nnz reports the number of stored coordinates, including duplicates and explicit zeros.
fn (CooTensor[T]) to_dense #
fn (t &CooTensor[T]) to_dense() !&vtl.Tensor[T]
to_dense expands the COO tensor into a dense row-major tensor. Values at the same coordinate are summed in insertion order.
fn (CooTensor[T]) coalesce #
fn (t &CooTensor[T]) coalesce() !CooTensor[T]
coalesce returns a canonical COO tensor with coordinates sorted by row and column and duplicates summed. Explicit zeros are retained so that the stored count stays predictable.
fn (CsrTensor[T]) nnz #
fn (t &CsrTensor[T]) nnz() int
nnz reports the number of stored values.
fn (CsrTensor[T]) to_dense #
fn (t &CsrTensor[T]) to_dense() !&vtl.Tensor[T]
to_dense expands the CSR tensor into a dense row-major tensor.
struct CooTensor #
struct CooTensor[T] {
pub:
shape []int
rows []int
cols []int
values []T
}
CooTensor is a two-dimensional sparse tensor in coordinate (COO) format. Rows, cols and values are parallel arrays. Duplicate coordinates and explicit zeros are permitted; to_dense sums values in insertion order.
struct CsrTensor #
struct CsrTensor[T] {
pub:
shape []int
row_ptr []int
col_idx []int
values []T
}
CsrTensor is a two-dimensional sparse tensor in compressed sparse row format. row_ptr has rows + 1 entries; row i owns entries [row_ptr[i], row_ptr[i + 1]). Columns within a row are sorted and unique.