#[cfg(feature = "sparse")]
fn main() -> numr::error::Result<()> {
use numr::prelude::*;
use numr::sparse::SparseTensor;
let device = CpuDevice::new();
let _client = CpuRuntime::default_client(&device);
let rows = [0i64, 0, 1, 3, 3];
let cols = [0i64, 3, 1, 0, 2];
let vals = [2.0f32, 1.0, 3.0, 4.0, 5.0];
let sparse = SparseTensor::<CpuRuntime>::from_coo_slices(
&rows,
&cols,
&vals,
[4, 4], &device,
)?;
println!("Created COO sparse matrix (4×4, {} non-zeros)", vals.len());
let csr = sparse.to_csr()?;
println!("Converted to CSR format");
let x = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[4], &device);
let y = csr.spmv(&x)?;
let y_vec: Vec<f32> = y.to_vec();
println!("\nSpMV: A · [1, 2, 3, 4]");
println!("Result: {y_vec:?}");
println!("Expected: [6.0, 6.0, 0.0, 19.0]");
let dense = sparse.to_dense(&device)?;
let dense_data: Vec<f32> = dense.to_vec();
println!("\nDense representation:");
for row in 0..4 {
let start = row * 4;
println!(" {:?}", &dense_data[start..start + 4]);
}
let x2 = Tensor::<CpuRuntime>::from_slice(
&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0],
&[4, 2],
&device,
);
let y2 = csr.spmm(&x2)?;
println!("\nSpMM: A · B result (shape {:?}):", y2.shape());
println!("{:?}", y2.to_vec::<f32>());
println!("\nSparse workflow example completed successfully!");
Ok(())
}
#[cfg(not(feature = "sparse"))]
fn main() {
eprintln!("This example requires the `sparse` feature.");
eprintln!("Run with: cargo run --example sparse_coo_csr_workflow --features sparse");
std::process::exit(1);
}