numr 0.5.1

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Core SparseTensor implementation: enum, creation, format queries

use crate::dtype::DType;
use crate::error::Result;
use crate::runtime::Runtime;
use crate::tensor::Tensor;

use super::super::coo::CooData;
use super::super::csc::CscData;
use super::super::csr::CsrData;
use super::super::format::{SparseFormat, SparseStorage};

/// Sparse tensor with runtime-selected storage format
///
/// `SparseTensor` is the high-level sparse tensor type that wraps different
/// sparse formats (COO, CSR, CSC). It provides a unified interface for
/// sparse operations regardless of the underlying storage.
///
/// # Format Selection
///
/// - **COO**: Best for construction and random insertion
/// - **CSR**: Best for row operations and SpMV (most common)
/// - **CSC**: Best for column operations
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # #[cfg(feature = "sparse")]
/// # {
/// # use numr::sparse::SparseTensor;
/// # let device = CpuDevice::new();
/// // Create from COO triplets
/// let sparse = SparseTensor::<CpuRuntime>::from_coo_slices(
///     &[0, 1, 2],      // rows
///     &[1, 0, 2],      // cols
///     &[1.0f32, 2.0, 3.0],  // values
///     [3, 3],           // shape
///     &device,
/// )?;
///
/// // Convert to CSR for efficient SpMV
/// let csr = sparse.to_csr()?;
/// # }
/// # Ok::<(), numr::error::Error>(())
/// ```
#[derive(Debug, Clone)]
pub enum SparseTensor<R: Runtime> {
    /// COO (Coordinate) format - best for construction, incremental updates, and format conversion.
    ///
    /// Stores (row, col, value) triplets. Flexible for building sparse matrices but slower
    /// for arithmetic operations. Use `to_csr()` or `to_csc()` for efficient computation.
    Coo(CooData<R>),

    /// CSR (Compressed Sparse Row) format - best for row-wise operations and SpMV (y = Ax).
    ///
    /// Compresses row indices using pointer array. Efficient for row slicing, matrix-vector
    /// products, and operations that iterate over rows.
    Csr(CsrData<R>),

    /// CSC (Compressed Sparse Column) format - best for column-wise operations and SpMV (y = A^T x).
    ///
    /// Compresses column indices using pointer array. Efficient for column slicing and
    /// operations that iterate over columns. Transpose of CSC is CSR.
    Csc(CscData<R>),
}

impl<R: Runtime<DType = DType>> SparseTensor<R> {
    // =========================================================================
    // Constructors
    // =========================================================================

    /// Create a sparse tensor from COO data
    pub fn from_coo(data: CooData<R>) -> Self {
        SparseTensor::Coo(data)
    }

    /// Create a sparse tensor from CSR data
    pub fn from_csr(data: CsrData<R>) -> Self {
        SparseTensor::Csr(data)
    }

    /// Create a sparse tensor from CSC data
    pub fn from_csc(data: CscData<R>) -> Self {
        SparseTensor::Csc(data)
    }

    /// Create an empty sparse tensor
    pub fn empty(
        shape: [usize; 2],
        dtype: DType,
        format: SparseFormat,
        device: &R::Device,
    ) -> Self {
        match format {
            SparseFormat::Coo => SparseTensor::Coo(CooData::empty(shape, dtype, device)),
            SparseFormat::Csr => SparseTensor::Csr(CsrData::empty(shape, dtype, device)),
            SparseFormat::Csc => SparseTensor::Csc(CscData::empty(shape, dtype, device)),
        }
    }

    /// Create sparse tensor from COO triplet slices
    pub fn from_coo_slices<T: crate::dtype::Element>(
        rows: &[i64],
        cols: &[i64],
        values: &[T],
        shape: [usize; 2],
        device: &R::Device,
    ) -> Result<Self> {
        let coo = CooData::from_slices(rows, cols, values, shape, device)?;
        Ok(SparseTensor::Coo(coo))
    }

    /// Create sparse tensor from CSR component slices
    pub fn from_csr_slices<T: crate::dtype::Element>(
        row_ptrs: &[i64],
        col_indices: &[i64],
        values: &[T],
        shape: [usize; 2],
        device: &R::Device,
    ) -> Result<Self> {
        let csr = CsrData::from_slices(row_ptrs, col_indices, values, shape, device)?;
        Ok(SparseTensor::Csr(csr))
    }

    /// Create sparse tensor from CSC component slices
    pub fn from_csc_slices<T: crate::dtype::Element>(
        col_ptrs: &[i64],
        row_indices: &[i64],
        values: &[T],
        shape: [usize; 2],
        device: &R::Device,
    ) -> Result<Self> {
        let csc = CscData::from_slices(col_ptrs, row_indices, values, shape, device)?;
        Ok(SparseTensor::Csc(csc))
    }

    /// Create sparse tensor from dense tensor
    ///
    /// Elements with absolute value below `threshold` are treated as zero.
    /// The resulting sparse tensor is in COO format.
    ///
    /// This method delegates to the backend's `SparseOps::dense_to_sparse()`
    /// implementation, which uses GPU-native kernels on CUDA/WebGPU backends.
    ///
    /// # Arguments
    ///
    /// * `client` - Runtime client that implements `SparseOps`
    /// * `tensor` - Dense 2D tensor to convert
    /// * `threshold` - Values with |value| < threshold become zero
    ///
    /// # Errors
    ///
    /// Returns error if tensor is not 2D or if the backend doesn't support
    /// dense-to-sparse conversion.
    ///
    /// # Example
    ///
    /// ```
    /// # use numr::prelude::*;
    /// # #[cfg(feature = "sparse")]
    /// # {
    /// # use numr::sparse::SparseTensor;
    /// # let device = CpuDevice::new();
    /// # let client = CpuRuntime::default_client(&device);
    /// let dense = Tensor::<CpuRuntime>::from_slice(&[1.0, 0.0, 2.0, 0.0], &[2, 2], &device);
    /// let sparse = SparseTensor::from_dense(&client, &dense, 1e-10)?;
    /// assert_eq!(sparse.nnz(), 2);  // Only non-zero elements
    /// # }
    /// # Ok::<(), numr::error::Error>(())
    /// ```
    pub fn from_dense<C>(client: &C, tensor: &Tensor<R>, threshold: f64) -> Result<Self>
    where
        C: crate::sparse::SparseOps<R>,
    {
        client.dense_to_sparse(tensor, threshold)
    }

    // =========================================================================
    // Properties
    // =========================================================================

    /// Returns the sparse format
    pub fn format(&self) -> SparseFormat {
        match self {
            SparseTensor::Coo(d) => d.format(),
            SparseTensor::Csr(d) => d.format(),
            SparseTensor::Csc(d) => d.format(),
        }
    }

    /// Returns the matrix shape [nrows, ncols]
    pub fn shape(&self) -> [usize; 2] {
        match self {
            SparseTensor::Coo(d) => d.shape(),
            SparseTensor::Csr(d) => d.shape(),
            SparseTensor::Csc(d) => d.shape(),
        }
    }

    /// Returns the number of rows
    pub fn nrows(&self) -> usize {
        self.shape()[0]
    }

    /// Returns the number of columns
    pub fn ncols(&self) -> usize {
        self.shape()[1]
    }

    /// Returns the number of non-zero elements
    pub fn nnz(&self) -> usize {
        match self {
            SparseTensor::Coo(d) => d.nnz(),
            SparseTensor::Csr(d) => d.nnz(),
            SparseTensor::Csc(d) => d.nnz(),
        }
    }

    /// Returns the data type
    pub fn dtype(&self) -> DType {
        match self {
            SparseTensor::Coo(d) => d.dtype(),
            SparseTensor::Csr(d) => d.dtype(),
            SparseTensor::Csc(d) => d.dtype(),
        }
    }

    /// Returns the sparsity ratio (fraction of zeros)
    pub fn sparsity(&self) -> f64 {
        match self {
            SparseTensor::Coo(d) => d.sparsity(),
            SparseTensor::Csr(d) => d.sparsity(),
            SparseTensor::Csc(d) => d.sparsity(),
        }
    }

    /// Returns the density ratio (fraction of non-zeros)
    pub fn density(&self) -> f64 {
        match self {
            SparseTensor::Coo(d) => d.density(),
            SparseTensor::Csr(d) => d.density(),
            SparseTensor::Csc(d) => d.density(),
        }
    }

    /// Returns true if the tensor has no non-zero elements
    pub fn is_empty(&self) -> bool {
        self.nnz() == 0
    }

    /// Returns the memory usage in bytes
    pub fn memory_usage(&self) -> usize {
        match self {
            SparseTensor::Coo(d) => d.memory_usage(),
            SparseTensor::Csr(d) => d.memory_usage(),
            SparseTensor::Csc(d) => d.memory_usage(),
        }
    }

    /// Returns true if stored in COO format
    pub fn is_coo(&self) -> bool {
        matches!(self, SparseTensor::Coo(_))
    }

    /// Returns true if stored in CSR format
    pub fn is_csr(&self) -> bool {
        matches!(self, SparseTensor::Csr(_))
    }

    /// Returns true if stored in CSC format
    pub fn is_csc(&self) -> bool {
        matches!(self, SparseTensor::Csc(_))
    }

    // =========================================================================
    // Format Access
    // =========================================================================

    /// Returns reference to COO data if in COO format
    pub fn as_coo(&self) -> Option<&CooData<R>> {
        match self {
            SparseTensor::Coo(d) => Some(d),
            _ => None,
        }
    }

    /// Returns reference to CSR data if in CSR format
    pub fn as_csr(&self) -> Option<&CsrData<R>> {
        match self {
            SparseTensor::Csr(d) => Some(d),
            _ => None,
        }
    }

    /// Returns reference to CSC data if in CSC format
    pub fn as_csc(&self) -> Option<&CscData<R>> {
        match self {
            SparseTensor::Csc(d) => Some(d),
            _ => None,
        }
    }
}

impl<R: Runtime<DType = DType>> std::fmt::Display for SparseTensor<R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "SparseTensor({:?}, nnz={}, format={}, dtype={}, sparsity={:.1}%)",
            self.shape(),
            self.nnz(),
            self.format(),
            self.dtype(),
            self.sparsity() * 100.0
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dtype::DType;
    use crate::runtime::Runtime;
    use crate::runtime::cpu::{CpuClient, CpuRuntime};
    use crate::sparse::SparseFormat;
    use crate::tensor::Tensor;

    #[test]
    fn test_sparse_tensor_coo() {
        let device = <CpuRuntime as Runtime>::Device::default();
        let sparse = SparseTensor::<CpuRuntime>::from_coo_slices(
            &[0i64, 1, 2],
            &[1i64, 0, 2],
            &[1.0f32, 2.0, 3.0],
            [3, 3],
            &device,
        )
        .unwrap();

        assert!(sparse.is_coo());
        assert_eq!(sparse.format(), SparseFormat::Coo);
        assert_eq!(sparse.nnz(), 3);
        assert_eq!(sparse.shape(), [3, 3]);
    }

    #[test]
    fn test_sparse_tensor_csr() {
        let device = <CpuRuntime as Runtime>::Device::default();
        let sparse = SparseTensor::<CpuRuntime>::from_csr_slices(
            &[0i64, 2, 3, 5],
            &[0i64, 2, 2, 0, 1],
            &[1.0f32, 2.0, 3.0, 4.0, 5.0],
            [3, 3],
            &device,
        )
        .unwrap();

        assert!(sparse.is_csr());
        assert_eq!(sparse.format(), SparseFormat::Csr);
        assert_eq!(sparse.nnz(), 5);
    }

    #[test]
    fn test_sparse_tensor_coo_to_csr_conversion() {
        let device = <CpuRuntime as Runtime>::Device::default();

        // Create COO tensor
        let coo = SparseTensor::<CpuRuntime>::from_coo_slices(
            &[2i64, 0, 1, 0, 2], // unsorted rows
            &[1i64, 0, 2, 2, 0],
            &[5.0f32, 1.0, 3.0, 2.0, 4.0],
            [3, 3],
            &device,
        )
        .unwrap();

        assert!(coo.is_coo());
        assert_eq!(coo.nnz(), 5);

        // Convert to CSR
        let csr = coo.to_csr().unwrap();

        assert!(csr.is_csr());
        assert_eq!(csr.format(), SparseFormat::Csr);
        assert_eq!(csr.nnz(), 5);
        assert_eq!(csr.shape(), [3, 3]);

        // Verify CSR data
        let csr_data = csr.as_csr().unwrap();
        let row_ptrs: Vec<i64> = csr_data.row_ptrs().to_vec();
        let col_indices: Vec<i64> = csr_data.col_indices().to_vec();
        let values: Vec<f32> = csr_data.values().to_vec();

        assert_eq!(row_ptrs, vec![0, 2, 3, 5]);
        assert_eq!(col_indices, vec![0, 2, 2, 0, 1]);
        assert_eq!(values, vec![1.0, 2.0, 3.0, 4.0, 5.0]);
    }

    #[test]
    fn test_sparse_tensor_coo_to_csc_conversion() {
        let device = <CpuRuntime as Runtime>::Device::default();

        // Create COO tensor
        let coo = SparseTensor::<CpuRuntime>::from_coo_slices(
            &[2i64, 0, 1, 0, 2], // unsorted rows
            &[1i64, 0, 2, 2, 0],
            &[5.0f32, 1.0, 3.0, 2.0, 4.0],
            [3, 3],
            &device,
        )
        .unwrap();

        assert!(coo.is_coo());

        // Convert to CSC
        let csc = coo.to_csc().unwrap();

        assert!(csc.is_csc());
        assert_eq!(csc.format(), SparseFormat::Csc);
        assert_eq!(csc.nnz(), 5);
        assert_eq!(csc.shape(), [3, 3]);

        // Verify CSC data
        let csc_data = csc.as_csc().unwrap();
        let col_ptrs: Vec<i64> = csc_data.col_ptrs().to_vec();
        let row_indices: Vec<i64> = csc_data.row_indices().to_vec();
        let values: Vec<f32> = csc_data.values().to_vec();

        assert_eq!(col_ptrs, vec![0, 2, 3, 5]);
        assert_eq!(row_indices, vec![0, 2, 2, 0, 1]);
        assert_eq!(values, vec![1.0, 4.0, 5.0, 2.0, 3.0]);
    }

    #[test]
    fn test_sparse_tensor_display() {
        let device = <CpuRuntime as Runtime>::Device::default();
        let sparse = SparseTensor::<CpuRuntime>::from_coo_slices(
            &[0i64, 1],
            &[0i64, 1],
            &[1.0f32, 2.0],
            [10, 10],
            &device,
        )
        .unwrap();

        let display = format!("{}", sparse);
        assert!(display.contains("SparseTensor"));
        assert!(display.contains("nnz=2"));
        assert!(display.contains("COO"));
    }

    #[test]
    fn test_from_dense() {
        let device = <CpuRuntime as Runtime>::Device::default();
        let client = CpuClient::new(device.clone());

        // Dense matrix:
        // [1, 0, 2]
        // [0, 0, 3]
        // [4, 5, 0]
        let data = vec![1.0f32, 0.0, 2.0, 0.0, 0.0, 3.0, 4.0, 5.0, 0.0];
        let dense = Tensor::<CpuRuntime>::from_slice(&data, &[3, 3], &device);

        let sparse = SparseTensor::from_dense(&client, &dense, 1e-10).unwrap();

        assert!(sparse.is_coo());
        assert_eq!(sparse.nnz(), 5);
        assert_eq!(sparse.shape(), [3, 3]);

        // Verify COO data (row-major order)
        let coo = sparse.as_coo().unwrap();
        let rows: Vec<i64> = coo.row_indices().to_vec();
        let cols: Vec<i64> = coo.col_indices().to_vec();
        let values: Vec<f32> = coo.values().to_vec();

        assert_eq!(rows, vec![0, 0, 1, 2, 2]);
        assert_eq!(cols, vec![0, 2, 2, 0, 1]);
        assert_eq!(values, vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        assert!(coo.is_sorted());
    }

    #[test]
    fn test_from_dense_empty() {
        let device = <CpuRuntime as Runtime>::Device::default();
        let client = CpuClient::new(device.clone());

        // All zeros
        let data = vec![0.0f32; 9];
        let dense = Tensor::<CpuRuntime>::from_slice(&data, &[3, 3], &device);

        let sparse = SparseTensor::from_dense(&client, &dense, 1e-10).unwrap();

        assert_eq!(sparse.nnz(), 0);
        assert_eq!(sparse.shape(), [3, 3]);
    }

    #[test]
    fn test_from_dense_with_threshold() {
        let device = <CpuRuntime as Runtime>::Device::default();
        let client = CpuClient::new(device.clone());

        // Dense matrix with small values:
        // [1.0, 0.001, 2.0]
        // [0.0, 0.0,   0.002]
        let data = vec![1.0f32, 0.001, 2.0, 0.0, 0.0, 0.002];
        let dense = Tensor::<CpuRuntime>::from_slice(&data, &[2, 3], &device);

        // With threshold 0.01, values below should be treated as zero
        let sparse = SparseTensor::from_dense(&client, &dense, 0.01).unwrap();

        assert_eq!(sparse.nnz(), 2); // Only 1.0 and 2.0

        let coo = sparse.as_coo().unwrap();
        let values: Vec<f32> = coo.values().to_vec();
        assert_eq!(values, vec![1.0, 2.0]);
    }

    #[test]
    fn test_from_dense_single_element() {
        let device = <CpuRuntime as Runtime>::Device::default();
        let client = CpuClient::new(device.clone());

        let data = vec![0.0f32, 0.0, 0.0, 42.0];
        let dense = Tensor::<CpuRuntime>::from_slice(&data, &[2, 2], &device);

        let sparse = SparseTensor::from_dense(&client, &dense, 1e-10).unwrap();

        assert_eq!(sparse.nnz(), 1);

        let coo = sparse.as_coo().unwrap();
        let rows: Vec<i64> = coo.row_indices().to_vec();
        let cols: Vec<i64> = coo.col_indices().to_vec();
        let values: Vec<f32> = coo.values().to_vec();

        assert_eq!(rows, vec![1]);
        assert_eq!(cols, vec![1]);
        assert_eq!(values, vec![42.0]);
    }

    #[test]
    fn test_from_dense_f64() {
        let device = <CpuRuntime as Runtime>::Device::default();
        let client = CpuClient::new(device.clone());

        let data = vec![1.0f64, 0.0, 2.0, 3.0];
        let dense = Tensor::<CpuRuntime>::from_slice(&data, &[2, 2], &device);

        let sparse = SparseTensor::from_dense(&client, &dense, 1e-10).unwrap();

        assert_eq!(sparse.nnz(), 3);
        assert_eq!(sparse.dtype(), DType::F64);
    }
}