Skip to main content

Module sparse

Module sparse 

Source
Expand description

Sparse tensor support for TensorLogic.

This module provides comprehensive sparse tensor representations and operations:

  • CSR (Compressed Sparse Row) format for efficient row operations
  • CSC (Compressed Sparse Column) format for efficient column operations
  • COO (Coordinate) format for flexible construction
  • Sparse-dense hybrid operations
  • Automatic sparsity detection and conversion
  • Sparse matrix multiplication and linear algebra

§Example

use tensorlogic_infer::{SparseFormat, SparseTensor, SparseMatrix};

// Create a sparse matrix in COO format
let mut builder = SparseTensor::builder(vec![100, 100], SparseFormat::COO);
builder.add_entry(vec![5, 10], 3.14);
builder.add_entry(vec![20, 30], 2.71);
let sparse = builder.build()?;

// Convert to CSR for efficient operations
let csr = sparse.to_csr()?;

// Sparse-dense multiplication
let dense = vec![1.0; 100];
let result = csr.multiply_dense(&dense)?;

// Detect sparsity
let sparsity = sparse.sparsity_ratio();
println!("Sparsity: {:.2}%", sparsity * 100.0);

Structs§

SparseCOO
Sparse matrix in COO (Coordinate) format.
SparseCSC
Sparse matrix in CSC (Compressed Sparse Column) format.
SparseCSR
Sparse matrix in CSR (Compressed Sparse Row) format.
SparseTensorBuilder
Builder for sparse tensors.

Enums§

SparseError
Sparse tensor errors.
SparseFormat
Sparse tensor storage format.
SparseTensor
Sparse tensor representation.

Functions§

detect_sparsity
Detect sparsity in a dense tensor.
to_sparse_if_beneficial
Convert dense tensor to sparse if beneficial.