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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! CSR matrix multiplication: spmv, spmm
use super::CsrData;
use crate::dtype::{DType, Element};
use crate::error::{Error, Result};
use crate::runtime::Runtime;
use crate::sparse::{CscData, SparseStorage};
use crate::tensor::Tensor;
impl<R: Runtime<DType = DType>> CsrData<R> {
/// Sparse matrix-vector multiplication: y = A * x
///
/// Computes the product of this sparse matrix with a dense vector.
///
/// # Arguments
///
/// * `x` - Dense vector of length `ncols` (or shape `` `[ncols]` `` or `` `[ncols, 1]` ``)
///
/// # Returns
///
/// Dense vector of length `` `nrows` ``
///
/// # Errors
///
/// Returns error if:
/// - `x` length doesn't match matrix ncols
/// - dtype mismatch between matrix and vector
///
/// # Algorithm
///
/// For each row i:
/// ```text
/// `` `y[i] = sum(values[j] * x[col_indices[j]]) for j in row_ptrs[i]..row_ptrs[i+1]` ``
/// ```
///
/// # Performance
///
/// - O(nnz) time complexity
/// - CSR format provides optimal memory access pattern for SpMV
/// - Each row's non-zeros are contiguous in memory
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # #[cfg(feature = "sparse")]
/// # {
/// # use numr::sparse::SparseTensor;
/// # let device = CpuDevice::new();
/// # let sp = SparseTensor::<CpuRuntime>::from_coo_slices(&[0, 0, 1], &[0, 1, 0], &[1.0f32, 2.0, 3.0], [2, 2], &device)?.to_csr()?;
/// # if let numr::sparse::SparseTensor::Csr(csr) = sp {
/// let x = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0], &[2], &device);
/// let y = csr.spmv(&x)?; // y = [1*1 + 2*2, 3*1] = [5, 3]
/// # }
/// # }
/// # Ok::<(), numr::error::Error>(())
/// ```
pub fn spmv(&self, x: &Tensor<R>) -> Result<Tensor<R>>
where
R::Client: crate::sparse::SparseOps<R>,
{
use crate::sparse::SparseOps;
let [nrows, ncols] = self.shape;
let dtype = self.dtype();
let device = self.values.device();
// Validate vector length
let x_len = x.numel();
if x_len != ncols {
return Err(Error::ShapeMismatch {
expected: vec![ncols],
got: vec![x_len],
});
}
// Validate dtype match
if x.dtype() != dtype {
return Err(Error::DTypeMismatch {
lhs: dtype,
rhs: x.dtype(),
});
}
// Handle empty matrix case
if self.is_empty() {
crate::dispatch_dtype!(dtype, T => {
let zeros: Vec<T> = vec![T::zero(); nrows];
return Ok(Tensor::from_slice(&zeros, &[nrows], device));
}, "spmv empty");
}
// Get runtime client to dispatch to backend-specific implementation
let client = R::default_client(device);
// Dispatch on dtype to call backend spmv_csr
crate::dispatch_dtype!(dtype, T => {
return client.spmv_csr::<T>(
&self.row_ptrs,
&self.col_indices,
&self.values,
x,
self.shape,
);
}, "spmv");
}
/// Sparse matrix-dense matrix multiplication: C = A * B
///
/// Computes the product of this sparse matrix with a dense matrix.
///
/// # Arguments
///
/// * `b` - Dense matrix of shape `` `[K, N]` `` where K == ncols of sparse matrix
///
/// # Returns
///
/// Dense matrix of shape `` `[M, N]` `` where M == nrows of sparse matrix
///
/// # Errors
///
/// Returns error if:
/// - `b` first dimension doesn't match matrix ncols
/// - `b` is not 2D
/// - dtype mismatch between matrix and input
///
/// # Algorithm
///
/// For each row i of A and each column n of B:
/// ```text
/// `` `C[i, n] = sum(A.values[j] * B[A.col_indices[j], n])` ``
/// for j in `` `row_ptrs[i]..row_ptrs[i+1]` ``
/// ```
///
/// # Performance
///
/// - `O(nnz * N)` time complexity
/// - CSR format provides good memory access for row-wise traversal
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # #[cfg(feature = "sparse")]
/// # {
/// # use numr::sparse::SparseTensor;
/// # let device = CpuDevice::new();
/// // A: `[2, 3]` sparse, B: `[3, 2]` dense -> C: `[2, 2]` dense
/// # let sp = SparseTensor::<CpuRuntime>::from_coo_slices(&[0, 0, 1], &[0, 1, 2], &[1.0f32, 2.0, 3.0], [2, 3], &device)?.to_csr()?;
/// # if let numr::sparse::SparseTensor::Csr(csr) = sp {
/// # let b = Tensor::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0f32], &[3, 2], &device);
/// let c = csr.spmm(&b)?;
/// # }
/// # }
/// # Ok::<(), numr::error::Error>(())
/// ```
pub fn spmm(&self, b: &Tensor<R>) -> Result<Tensor<R>>
where
R::Client: crate::sparse::SparseOps<R>,
{
use crate::sparse::SparseOps;
let [m, k] = self.shape;
let dtype = self.dtype();
let device = self.values.device();
// Validate B is 2D
if b.ndim() != 2 {
return Err(Error::Internal(format!(
"Expected 2D tensor for SpMM, got {}D",
b.ndim()
)));
}
let b_shape = b.shape();
let b_k = b_shape[0];
let n = b_shape[1];
// Validate dimensions match
if b_k != k {
return Err(Error::ShapeMismatch {
expected: vec![k],
got: vec![b_k],
});
}
// Validate dtype match
if b.dtype() != dtype {
return Err(Error::DTypeMismatch {
lhs: dtype,
rhs: b.dtype(),
});
}
// Handle empty matrix case
if self.is_empty() {
crate::dispatch_dtype!(dtype, T => {
let zeros: Vec<T> = vec![T::zero(); m * n];
return Ok(Tensor::from_slice(&zeros, &[m, n], device));
}, "spmm empty");
}
// Get runtime client to dispatch to backend-specific implementation
let client = R::default_client(device);
// Dispatch on dtype to call backend spmm_csr
crate::dispatch_dtype!(dtype, T => {
return client.spmm_csr::<T>(
&self.row_ptrs,
&self.col_indices,
&self.values,
b,
self.shape,
);
}, "spmm");
}
/// Transpose the sparse matrix: B = A^T
///
/// Returns the transpose as a CSC matrix. This is an `O(1)` operation
/// that reinterprets the CSR structure as CSC:
/// - `row_ptrs` become `col_ptrs`
/// - `col_indices` become `row_indices`
/// - `values` remain the same
/// - `shape` is swapped
///
/// # Returns
///
/// CSC matrix representing the transpose
///
/// # Performance
///
/// `O(1)` - structural reinterpretation, no data copying beyond cloning tensors.
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # #[cfg(feature = "sparse")]
/// # {
/// # use numr::sparse::SparseTensor;
/// # let device = CpuDevice::new();
/// // A `[2, 3]` in CSR:
/// // `[1, 0, 2]`
/// // `[0, 3, 0]`
/// # let sp = SparseTensor::<CpuRuntime>::from_coo_slices(&[0, 0, 1], &[0, 2, 1], &[1.0f32, 2.0, 3.0], [2, 3], &device)?.to_csr()?;
/// # if let numr::sparse::SparseTensor::Csr(a) = sp {
/// let a_t = a.transpose();
/// // A^T `[3, 2]` in CSC (same underlying data)
/// # }
/// # }
/// # Ok::<(), numr::error::Error>(())
/// ```
pub fn transpose(&self) -> CscData<R> {
let [nrows, ncols] = self.shape;
// CSR row_ptrs -> CSC col_ptrs
// CSR col_indices -> CSC row_indices
// Shape [nrows, ncols] -> [ncols, nrows]
CscData {
col_ptrs: self.row_ptrs.clone(),
row_indices: self.col_indices.clone(),
values: self.values.clone(),
shape: [ncols, nrows],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dtype::DType;
use crate::runtime::Runtime;
use crate::runtime::cpu::CpuRuntime;
use crate::sparse::{SparseFormat, SparseStorage};
use crate::tensor::Tensor;
// =========================================================================
// SpMV tests
// =========================================================================
#[test]
fn test_spmv_basic() {
let device = <CpuRuntime as Runtime>::Device::default();
// Matrix:
// [1, 0, 2]
// [0, 0, 3]
// [4, 5, 0]
let row_ptrs = vec![0i64, 2, 3, 5];
let col_indices = vec![0i64, 2, 2, 0, 1];
let values = vec![1.0f32, 2.0, 3.0, 4.0, 5.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [3, 3], &device)
.unwrap();
// x = [1, 2, 3]
let x = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0], &[3], &device);
// y = A * x
// y[0] = 1*1 + 2*3 = 7
// y[1] = 3*3 = 9
// y[2] = 4*1 + 5*2 = 14
let y = csr.spmv(&x).unwrap();
assert_eq!(y.shape(), &[3]);
let y_data: Vec<f32> = y.to_vec();
assert_eq!(y_data, vec![7.0, 9.0, 14.0]);
}
#[test]
fn test_spmv_empty_matrix() {
let device = <CpuRuntime as Runtime>::Device::default();
let csr = CsrData::<CpuRuntime>::empty([3, 3], DType::F32, &device);
let x = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0], &[3], &device);
let y = csr.spmv(&x).unwrap();
assert_eq!(y.shape(), &[3]);
let y_data: Vec<f32> = y.to_vec();
assert_eq!(y_data, vec![0.0, 0.0, 0.0]);
}
#[test]
fn test_spmv_identity() {
let device = <CpuRuntime as Runtime>::Device::default();
// Identity matrix:
// [1, 0, 0]
// [0, 1, 0]
// [0, 0, 1]
let row_ptrs = vec![0i64, 1, 2, 3];
let col_indices = vec![0i64, 1, 2];
let values = vec![1.0f32, 1.0, 1.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [3, 3], &device)
.unwrap();
let x = Tensor::<CpuRuntime>::from_slice(&[7.0f32, 8.0, 9.0], &[3], &device);
let y = csr.spmv(&x).unwrap();
let y_data: Vec<f32> = y.to_vec();
assert_eq!(y_data, vec![7.0, 8.0, 9.0]);
}
#[test]
fn test_spmv_non_square() {
let device = <CpuRuntime as Runtime>::Device::default();
// Matrix [2, 4]:
// [1, 2, 0, 3]
// [0, 4, 5, 0]
let row_ptrs = vec![0i64, 3, 5];
let col_indices = vec![0i64, 1, 3, 1, 2];
let values = vec![1.0f32, 2.0, 3.0, 4.0, 5.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [2, 4], &device)
.unwrap();
// x = [1, 2, 3, 4]
let x = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[4], &device);
// y = A * x
// y[0] = 1*1 + 2*2 + 3*4 = 17
// y[1] = 4*2 + 5*3 = 23
let y = csr.spmv(&x).unwrap();
assert_eq!(y.shape(), &[2]);
let y_data: Vec<f32> = y.to_vec();
assert_eq!(y_data, vec![17.0, 23.0]);
}
#[test]
fn test_spmv_shape_mismatch() {
let device = <CpuRuntime as Runtime>::Device::default();
let row_ptrs = vec![0i64, 2, 3, 5];
let col_indices = vec![0i64, 2, 2, 0, 1];
let values = vec![1.0f32, 2.0, 3.0, 4.0, 5.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [3, 3], &device)
.unwrap();
// Wrong vector length (2 instead of 3)
let x = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0], &[2], &device);
let result = csr.spmv(&x);
assert!(result.is_err());
}
#[test]
fn test_spmv_dtype_mismatch() {
let device = <CpuRuntime as Runtime>::Device::default();
let row_ptrs = vec![0i64, 2, 3, 5];
let col_indices = vec![0i64, 2, 2, 0, 1];
let values = vec![1.0f32, 2.0, 3.0, 4.0, 5.0]; // F32
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [3, 3], &device)
.unwrap();
// F64 vector
let x = Tensor::<CpuRuntime>::from_slice(&[1.0f64, 2.0, 3.0], &[3], &device);
let result = csr.spmv(&x);
assert!(result.is_err());
}
#[test]
fn test_spmv_f64() {
let device = <CpuRuntime as Runtime>::Device::default();
// Matrix:
// [1, 2]
// [3, 4]
let row_ptrs = vec![0i64, 2, 4];
let col_indices = vec![0i64, 1, 0, 1];
let values = vec![1.0f64, 2.0, 3.0, 4.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [2, 2], &device)
.unwrap();
let x = Tensor::<CpuRuntime>::from_slice(&[1.0f64, 1.0], &[2], &device);
// y = A * x
// y[0] = 1 + 2 = 3
// y[1] = 3 + 4 = 7
let y = csr.spmv(&x).unwrap();
assert_eq!(y.dtype(), DType::F64);
let y_data: Vec<f64> = y.to_vec();
assert_eq!(y_data, vec![3.0, 7.0]);
}
#[test]
fn test_spmv_single_element() {
let device = <CpuRuntime as Runtime>::Device::default();
// Single element at (1, 2) with value 5
let row_ptrs = vec![0i64, 0, 1, 1];
let col_indices = vec![2i64];
let values = vec![5.0f32];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [3, 3], &device)
.unwrap();
let x = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0], &[3], &device);
// y = A * x
// y[0] = 0
// y[1] = 5 * 3 = 15
// y[2] = 0
let y = csr.spmv(&x).unwrap();
let y_data: Vec<f32> = y.to_vec();
assert_eq!(y_data, vec![0.0, 15.0, 0.0]);
}
// =========================================================================
// SpMM tests
// =========================================================================
#[test]
fn test_spmm_basic() {
let device = <CpuRuntime as Runtime>::Device::default();
// Sparse A [2, 3]:
// [1, 0, 2]
// [0, 3, 0]
let row_ptrs = vec![0i64, 2, 3];
let col_indices = vec![0i64, 2, 1];
let values = vec![1.0f32, 2.0, 3.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [2, 3], &device)
.unwrap();
// Dense B [3, 2]:
// [1, 2]
// [3, 4]
// [5, 6]
let b =
Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2], &device);
// C = A * B [2, 2]:
// C[0,0] = 1*1 + 2*5 = 11
// C[0,1] = 1*2 + 2*6 = 14
// C[1,0] = 3*3 = 9
// C[1,1] = 3*4 = 12
let c = csr.spmm(&b).unwrap();
assert_eq!(c.shape(), &[2, 2]);
let c_data: Vec<f32> = c.to_vec();
assert_eq!(c_data, vec![11.0, 14.0, 9.0, 12.0]);
}
#[test]
fn test_spmm_empty_matrix() {
let device = <CpuRuntime as Runtime>::Device::default();
let csr = CsrData::<CpuRuntime>::empty([2, 3], DType::F32, &device);
let b =
Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2], &device);
let c = csr.spmm(&b).unwrap();
assert_eq!(c.shape(), &[2, 2]);
let c_data: Vec<f32> = c.to_vec();
assert_eq!(c_data, vec![0.0, 0.0, 0.0, 0.0]);
}
#[test]
fn test_spmm_identity() {
let device = <CpuRuntime as Runtime>::Device::default();
// Identity matrix [3, 3]
let row_ptrs = vec![0i64, 1, 2, 3];
let col_indices = vec![0i64, 1, 2];
let values = vec![1.0f32, 1.0, 1.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [3, 3], &device)
.unwrap();
// B [3, 2]
let b =
Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2], &device);
// I * B = B
let c = csr.spmm(&b).unwrap();
let c_data: Vec<f32> = c.to_vec();
assert_eq!(c_data, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
}
#[test]
fn test_spmm_shape_mismatch() {
let device = <CpuRuntime as Runtime>::Device::default();
// A [2, 3]
let row_ptrs = vec![0i64, 2, 3];
let col_indices = vec![0i64, 2, 1];
let values = vec![1.0f32, 2.0, 3.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [2, 3], &device)
.unwrap();
// B [2, 2] - wrong dimension (should be [3, ...])
let b = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[2, 2], &device);
let result = csr.spmm(&b);
assert!(result.is_err());
}
#[test]
fn test_spmm_not_2d() {
let device = <CpuRuntime as Runtime>::Device::default();
let row_ptrs = vec![0i64, 2, 3];
let col_indices = vec![0i64, 2, 1];
let values = vec![1.0f32, 2.0, 3.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [2, 3], &device)
.unwrap();
// 1D tensor instead of 2D
let b = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0], &[3], &device);
let result = csr.spmm(&b);
assert!(result.is_err());
}
#[test]
fn test_spmm_dtype_mismatch() {
let device = <CpuRuntime as Runtime>::Device::default();
let row_ptrs = vec![0i64, 2, 3];
let col_indices = vec![0i64, 2, 1];
let values = vec![1.0f32, 2.0, 3.0]; // F32
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [2, 3], &device)
.unwrap();
// F64 matrix
let b =
Tensor::<CpuRuntime>::from_slice(&[1.0f64, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2], &device);
let result = csr.spmm(&b);
assert!(result.is_err());
}
#[test]
fn test_spmm_f64() {
let device = <CpuRuntime as Runtime>::Device::default();
// A [2, 2]
let row_ptrs = vec![0i64, 2, 4];
let col_indices = vec![0i64, 1, 0, 1];
let values = vec![1.0f64, 2.0, 3.0, 4.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [2, 2], &device)
.unwrap();
// B [2, 2]
let b = Tensor::<CpuRuntime>::from_slice(&[1.0f64, 0.0, 0.0, 1.0], &[2, 2], &device);
// C = A * I = A
let c = csr.spmm(&b).unwrap();
assert_eq!(c.dtype(), DType::F64);
let c_data: Vec<f64> = c.to_vec();
assert_eq!(c_data, vec![1.0, 2.0, 3.0, 4.0]);
}
#[test]
fn test_spmm_single_column() {
let device = <CpuRuntime as Runtime>::Device::default();
// A [3, 3]
let row_ptrs = vec![0i64, 2, 3, 5];
let col_indices = vec![0i64, 2, 2, 0, 1];
let values = vec![1.0f32, 2.0, 3.0, 4.0, 5.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [3, 3], &device)
.unwrap();
// B [3, 1] - single column (like a vector reshaped)
let b = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0], &[3, 1], &device);
// Should match spmv result
let c = csr.spmm(&b).unwrap();
assert_eq!(c.shape(), &[3, 1]);
let c_data: Vec<f32> = c.to_vec();
// Same as spmv: [7, 9, 14]
assert_eq!(c_data, vec![7.0, 9.0, 14.0]);
}
// =========================================================================
// Transpose tests
// =========================================================================
#[test]
fn test_csr_transpose() {
let device = <CpuRuntime as Runtime>::Device::default();
// Matrix [2, 3]:
// [1, 0, 2]
// [0, 3, 0]
let row_ptrs = vec![0i64, 2, 3];
let col_indices = vec![0i64, 2, 1];
let values = vec![1.0f32, 2.0, 3.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [2, 3], &device)
.unwrap();
let csc = csr.transpose();
// Transposed [3, 2] as CSC
assert_eq!(csc.shape(), [3, 2]);
assert_eq!(csc.nnz(), 3);
assert_eq!(csc.format(), SparseFormat::Csc);
// CSR row_ptrs become CSC col_ptrs
let col_ptrs: Vec<i64> = csc.col_ptrs().to_vec();
let row_indices: Vec<i64> = csc.row_indices().to_vec();
let t_values: Vec<f32> = csc.values().to_vec();
assert_eq!(col_ptrs, vec![0, 2, 3]); // Same as original row_ptrs
assert_eq!(row_indices, vec![0, 2, 1]); // Same as original col_indices
assert_eq!(t_values, vec![1.0, 2.0, 3.0]); // Values unchanged
}
#[test]
fn test_csr_transpose_empty() {
let device = <CpuRuntime as Runtime>::Device::default();
let csr = CsrData::<CpuRuntime>::empty([3, 5], DType::F32, &device);
let csc = csr.transpose();
assert_eq!(csc.shape(), [5, 3]);
assert_eq!(csc.nnz(), 0);
assert_eq!(csc.format(), SparseFormat::Csc);
}
#[test]
fn test_csr_transpose_to_dense_matches() {
let device = <CpuRuntime as Runtime>::Device::default();
// Matrix [2, 3]:
// [1, 0, 2]
// [0, 3, 0]
let row_ptrs = vec![0i64, 2, 3];
let col_indices = vec![0i64, 2, 1];
let values = vec![1.0f32, 2.0, 3.0];
let csr =
CsrData::<CpuRuntime>::from_slices(&row_ptrs, &col_indices, &values, [2, 3], &device)
.unwrap();
// Convert to dense, then transpose CSC to dense
let csc = csr.transpose();
// Convert CSC transpose to CSR to use to_dense via COO
let csr_t = csc.to_csr().unwrap();
let coo_t = csr_t.to_coo().unwrap();
// Build dense from COO
let t_rows: Vec<i64> = coo_t.row_indices().to_vec();
let t_cols: Vec<i64> = coo_t.col_indices().to_vec();
let t_vals: Vec<f32> = coo_t.values().to_vec();
// Transposed [3, 2]:
// [1, 0]
// [0, 3]
// [2, 0]
// Check that values are in correct positions
let mut dense_t = vec![0.0f32; 6];
for i in 0..t_vals.len() {
let r = t_rows[i] as usize;
let c = t_cols[i] as usize;
dense_t[r * 2 + c] = t_vals[i];
}
assert_eq!(dense_t, vec![1.0, 0.0, 0.0, 3.0, 2.0, 0.0]);
}
}