trueno 0.17.1

High-performance SIMD compute library with GPU support for matrix operations
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
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! Contract tests for linear algebra: sparse matrices, dense solvers, BLAS Level 3.
//!
//! These tests exercise real mathematical properties against the implementations
//! in trueno-sparse and trueno-solve crates.

#[cfg(test)]
mod tests {
    // =========================================================================
    // Helper functions
    // =========================================================================

    /// Infinity norm of a vector.
    fn vec_inf_norm(v: &[f32]) -> f32 {
        v.iter().map(|x| x.abs()).fold(0.0_f32, f32::max)
    }

    /// Dense matrix-vector multiply: y = A * x (row-major A of shape rows x cols).
    fn dense_matvec(a: &[f32], x: &[f32], rows: usize, cols: usize) -> Vec<f32> {
        let mut y = vec![0.0_f32; rows];
        for i in 0..rows {
            for j in 0..cols {
                y[i] += a[i * cols + j] * x[j];
            }
        }
        y
    }

    /// Dense matrix-matrix multiply: C = A * B (row-major).
    fn dense_matmul(a: &[f32], b: &[f32], m: usize, k: usize, n: usize) -> Vec<f32> {
        let mut c = vec![0.0_f32; m * n];
        for i in 0..m {
            for j in 0..n {
                for p in 0..k {
                    c[i * n + j] += a[i * k + p] * b[p * n + j];
                }
            }
        }
        c
    }

    /// Check if a matrix is approximately symmetric (row-major, n x n).
    fn is_symmetric(m: &[f32], n: usize, tol: f32) -> bool {
        for i in 0..n {
            for j in (i + 1)..n {
                if (m[i * n + j] - m[j * n + i]).abs() > tol {
                    return false;
                }
            }
        }
        true
    }

    /// Infinity norm of a row-major matrix.
    fn mat_inf_norm(a: &[f32], rows: usize, cols: usize) -> f32 {
        let mut max = 0.0_f32;
        for i in 0..rows {
            let row_sum: f32 = (0..cols).map(|j| a[i * cols + j].abs()).sum();
            if row_sum > max {
                max = row_sum;
            }
        }
        max
    }

    // =========================================================================
    // SPARSE MATRIX TESTS
    // =========================================================================

    use trueno_sparse::{BsrMatrix, CooMatrix, CsrMatrix, SellMatrix, SparseOps};

    // ---- BSR round-trip ----

    #[test]
    fn test_bsr_to_csr_roundtrip() {
        // Create a known 4x4 dense matrix, convert to BSR with block_size=2,
        // then BSR -> CSR -> dense, and verify it matches the original.
        #[rustfmt::skip]
        let dense = vec![
            1.0, 2.0, 0.0, 0.0,
            3.0, 4.0, 0.0, 0.0,
            0.0, 0.0, 5.0, 6.0,
            0.0, 0.0, 7.0, 8.0,
        ];

        let bsr = BsrMatrix::from_dense(&dense, 4, 4, 2);
        assert_eq!(bsr.rows(), 4);
        assert_eq!(bsr.cols(), 4);

        let csr = bsr.to_csr().expect("BSR->CSR should succeed");
        assert_eq!(csr.rows(), 4);
        assert_eq!(csr.cols(), 4);

        // Convert CSR back to dense and compare
        let recovered = csr.to_dense();
        for i in 0..16 {
            assert!(
                (dense[i] - recovered[i]).abs() < 1e-6,
                "Mismatch at index {}: expected {}, got {}",
                i,
                dense[i],
                recovered[i]
            );
        }

        // Now go back to BSR from the recovered dense
        let bsr2 = BsrMatrix::from_dense(&recovered, 4, 4, 2);
        let csr2 = bsr2.to_csr().expect("second BSR->CSR");
        let recovered2 = csr2.to_dense();
        for i in 0..16 {
            assert!(
                (recovered[i] - recovered2[i]).abs() < 1e-6,
                "Round-trip mismatch at index {}",
                i
            );
        }
    }

    // ---- BSR alpha/beta SpMV ----

    #[test]
    fn test_bsr_alpha_beta() {
        // y = alpha * A * x + beta * y
        #[rustfmt::skip]
        let dense = vec![
            2.0, 1.0,
            1.0, 3.0,
        ];
        let bsr = BsrMatrix::from_dense(&dense, 2, 2, 2);

        let x = vec![1.0, 2.0];
        let mut y = vec![10.0, 20.0];
        let alpha = 2.0;
        let beta = 0.5;

        // Expected: y = 2.0 * A * [1,2] + 0.5 * [10,20]
        // A*x = [2*1+1*2, 1*1+3*2] = [4, 7]
        // y = 2*[4,7] + 0.5*[10,20] = [8,14] + [5,10] = [13,24]
        bsr.spmv(alpha, &x, beta, &mut y).unwrap();

        assert!((y[0] - 13.0).abs() < 1e-5, "y[0] = {}, expected 13.0", y[0]);
        assert!((y[1] - 24.0).abs() < 1e-5, "y[1] = {}, expected 24.0", y[1]);
    }

    // ---- SELL matches CSR ----

    #[test]
    fn test_sell_matches_csr() {
        // Build a 4x4 sparse matrix via COO, convert to CSR, then SELL.
        // Verify SpMV results match.
        let coo = CooMatrix::new(
            4,
            4,
            vec![0, 0, 1, 2, 2, 3],
            vec![0, 2, 1, 0, 2, 3],
            vec![1.0_f32, 3.0, 5.0, 2.0, 4.0, 6.0],
        )
        .unwrap();
        let csr = CsrMatrix::from_coo(&coo);
        let sell = SellMatrix::from_csr(&csr, 2);

        let x = vec![1.0_f32, 2.0, 3.0, 4.0];

        // CSR SpMV
        let mut y_csr = vec![0.0_f32; 4];
        csr.spmv(1.0, &x, 0.0, &mut y_csr).unwrap();

        // SELL SpMV
        let mut y_sell = vec![0.0_f32; 4];
        sell.spmv(1.0, &x, 0.0, &mut y_sell).unwrap();

        for i in 0..4 {
            assert!(
                (y_csr[i] - y_sell[i]).abs() < 1e-5,
                "Mismatch at {}: CSR={}, SELL={}",
                i,
                y_csr[i],
                y_sell[i]
            );
        }
    }

    // ---- SELL dimension mismatch ----

    #[test]
    fn test_sell_spmv_dimension_mismatch() {
        let csr = CsrMatrix::identity(3);
        let sell = SellMatrix::from_csr(&csr, 2);

        let x = vec![1.0_f32; 5]; // Wrong: should be 3
        let mut y = vec![0.0_f32; 3];
        let result = sell.spmv(1.0, &x, 0.0, &mut y);
        assert!(result.is_err(), "Should fail on dimension mismatch");
    }

    // ---- SELL alpha/beta ----

    #[test]
    fn test_sell_spmv_alpha_beta() {
        let csr = CsrMatrix::identity(3);
        let sell = SellMatrix::from_csr(&csr, 2);

        let x = vec![1.0_f32, 2.0, 3.0];
        let mut y = vec![10.0_f32, 20.0, 30.0];

        // y = 2.0 * I * x + 0.5 * y = [2,4,6] + [5,10,15] = [7,14,21]
        sell.spmv(2.0, &x, 0.5, &mut y).unwrap();

        assert!((y[0] - 7.0).abs() < 1e-5);
        assert!((y[1] - 14.0).abs() < 1e-5);
        assert!((y[2] - 21.0).abs() < 1e-5);
    }

    // ---- SpGEMM tests ----

    #[test]
    fn test_spgemm_identity() {
        // A * I = A
        let coo =
            CooMatrix::new(3, 3, vec![0, 1, 2, 0], vec![0, 1, 2, 2], vec![2.0_f32, 3.0, 4.0, 5.0])
                .unwrap();
        let a = CsrMatrix::from_coo(&coo);
        let identity = CsrMatrix::<f32>::identity(3);

        let c = trueno_sparse::spgemm(&a, &identity).unwrap();
        let a_dense = a.to_dense();
        let c_dense = c.to_dense();

        for i in 0..9 {
            assert!(
                (a_dense[i] - c_dense[i]).abs() < 1e-5,
                "A*I mismatch at {}: A={}, C={}",
                i,
                a_dense[i],
                c_dense[i]
            );
        }
    }

    #[test]
    fn test_spgemm_identity_left() {
        // I * A = A
        let coo =
            CooMatrix::new(3, 3, vec![0, 1, 2, 0], vec![0, 1, 2, 2], vec![2.0_f32, 3.0, 4.0, 5.0])
                .unwrap();
        let a = CsrMatrix::from_coo(&coo);
        let identity = CsrMatrix::<f32>::identity(3);

        let c = trueno_sparse::spgemm(&identity, &a).unwrap();
        let a_dense = a.to_dense();
        let c_dense = c.to_dense();

        for i in 0..9 {
            assert!(
                (a_dense[i] - c_dense[i]).abs() < 1e-5,
                "I*A mismatch at {}: A={}, C={}",
                i,
                a_dense[i],
                c_dense[i]
            );
        }
    }

    #[test]
    fn test_spgemm_known_product() {
        // A = [[1, 2], [0, 3]]
        // B = [[4, 0], [1, 2]]
        // A*B = [[6, 4], [3, 6]]
        let coo_a =
            CooMatrix::new(2, 2, vec![0, 0, 1], vec![0, 1, 1], vec![1.0_f32, 2.0, 3.0]).unwrap();
        let coo_b =
            CooMatrix::new(2, 2, vec![0, 1, 1], vec![0, 0, 1], vec![4.0_f32, 1.0, 2.0]).unwrap();
        let a = CsrMatrix::from_coo(&coo_a);
        let b = CsrMatrix::from_coo(&coo_b);

        let c = trueno_sparse::spgemm(&a, &b).unwrap();
        let c_dense = c.to_dense();

        let expected = vec![6.0, 4.0, 3.0, 6.0];
        for i in 0..4 {
            assert!(
                (c_dense[i] - expected[i]).abs() < 1e-5,
                "Product mismatch at {}: got {}, expected {}",
                i,
                c_dense[i],
                expected[i]
            );
        }
    }

    #[test]
    fn test_spgemm_dimension_mismatch() {
        // A is 2x3, B is 2x2 => dimension mismatch (A.cols=3 != B.rows=2)
        let a = CsrMatrix::new(2, 3, vec![0, 1, 2], vec![0, 1], vec![1.0_f32, 2.0]).unwrap();
        let b = CsrMatrix::<f32>::identity(2);
        let result = trueno_sparse::spgemm(&a, &b);
        assert!(result.is_err(), "Should fail: A.cols != B.rows");
    }

    #[test]
    fn test_spgemm_sparse_result() {
        // Multiply two sparse matrices and verify the result is also sparse.
        // A = diag(1, 2, 3), B = diag(4, 5, 6) => C = diag(4, 10, 18)
        let a =
            CsrMatrix::new(3, 3, vec![0, 1, 2, 3], vec![0, 1, 2], vec![1.0_f32, 2.0, 3.0]).unwrap();
        let b =
            CsrMatrix::new(3, 3, vec![0, 1, 2, 3], vec![0, 1, 2], vec![4.0_f32, 5.0, 6.0]).unwrap();

        let c = trueno_sparse::spgemm(&a, &b).unwrap();
        // Result should have exactly 3 nonzeros (diagonal)
        assert_eq!(c.nnz(), 3, "Product of diagonal matrices should have 3 nnz");

        let c_dense = c.to_dense();
        assert!((c_dense[0] - 4.0).abs() < 1e-5);
        assert!((c_dense[4] - 10.0).abs() < 1e-5);
        assert!((c_dense[8] - 18.0).abs() < 1e-5);
        // Off-diagonals should be zero
        assert!(c_dense[1].abs() < 1e-5);
        assert!(c_dense[3].abs() < 1e-5);
    }

    // ---- SpMM (sparse * dense matrix) ----

    #[test]
    fn test_spmm_matches_dense() {
        // Sparse A (3x3) times dense B (3x2), compare with dense A * B.
        let coo = CooMatrix::new(
            3,
            3,
            vec![0, 0, 1, 2, 2],
            vec![0, 1, 1, 0, 2],
            vec![1.0_f32, 2.0, 3.0, 4.0, 5.0],
        )
        .unwrap();
        let csr = CsrMatrix::from_coo(&coo);
        let a_dense = csr.to_dense();

        // B is 3x2, row-major
        let b = vec![1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0];
        let b_cols = 2;

        // Sparse SpMM
        let mut c_sparse = vec![0.0_f32; 3 * b_cols];
        csr.spmm(1.0, &b, b_cols, 0.0, &mut c_sparse).unwrap();

        // Dense matmul
        let c_dense = dense_matmul(&a_dense, &b, 3, 3, b_cols);

        for i in 0..6 {
            assert!(
                (c_sparse[i] - c_dense[i]).abs() < 1e-4,
                "SpMM mismatch at {}: sparse={}, dense={}",
                i,
                c_sparse[i],
                c_dense[i]
            );
        }
    }

    #[test]
    fn test_spmm_dimension_mismatch() {
        let csr = CsrMatrix::<f32>::identity(3);
        // B has wrong row count (4 instead of 3)
        let b = vec![1.0_f32; 4 * 2];
        let mut c = vec![0.0_f32; 3 * 2];
        let result = csr.spmm(1.0, &b, 2, 0.0, &mut c);
        assert!(result.is_err(), "Should fail on dimension mismatch");
    }

    // ---- SpMV tests ----

    #[test]
    fn test_spmv_known_result() {
        // A = [[1, 0, 2],
        //      [0, 3, 0],
        //      [4, 0, 5]]
        // x = [1, 2, 3]
        // y = A*x = [7, 6, 19]
        let coo = CooMatrix::new(
            3,
            3,
            vec![0, 0, 1, 2, 2],
            vec![0, 2, 1, 0, 2],
            vec![1.0_f32, 2.0, 3.0, 4.0, 5.0],
        )
        .unwrap();
        let csr = CsrMatrix::from_coo(&coo);

        let x = vec![1.0_f32, 2.0, 3.0];
        let mut y = vec![0.0_f32; 3];
        csr.spmv(1.0, &x, 0.0, &mut y).unwrap();

        assert!((y[0] - 7.0).abs() < 1e-5, "y[0]={}, expected 7", y[0]);
        assert!((y[1] - 6.0).abs() < 1e-5, "y[1]={}, expected 6", y[1]);
        assert!((y[2] - 19.0).abs() < 1e-5, "y[2]={}, expected 19", y[2]);
    }

    #[test]
    fn test_spmv_dimension_mismatch() {
        let csr = CsrMatrix::<f32>::identity(3);
        let x = vec![1.0_f32; 5]; // Wrong size
        let mut y = vec![0.0_f32; 3];
        let result = csr.spmv(1.0, &x, 0.0, &mut y);
        assert!(result.is_err());
    }

    #[test]
    fn test_spmv_alpha_beta() {
        // y = alpha * A * x + beta * y
        // A = I(3), x = [1,2,3], y_init = [10,20,30]
        // y = 2 * [1,2,3] + 0.5 * [10,20,30] = [2,4,6] + [5,10,15] = [7,14,21]
        let csr = CsrMatrix::<f32>::identity(3);
        let x = vec![1.0_f32, 2.0, 3.0];
        let mut y = vec![10.0_f32, 20.0, 30.0];

        csr.spmv(2.0, &x, 0.5, &mut y).unwrap();

        assert!((y[0] - 7.0).abs() < 1e-5);
        assert!((y[1] - 14.0).abs() < 1e-5);
        assert!((y[2] - 21.0).abs() < 1e-5);
    }

    #[test]
    fn prop_spmv_backward_error() {
        // For a known sparse matrix, verify ||Ax - y|| / (||A|| * ||x||) < eps
        // where eps is proportional to machine epsilon * nnz_per_row.
        let coo = CooMatrix::new(
            3,
            3,
            vec![0, 0, 0, 1, 1, 1, 2, 2, 2],
            vec![0, 1, 2, 0, 1, 2, 0, 1, 2],
            vec![1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
        )
        .unwrap();
        let csr = CsrMatrix::from_coo(&coo);
        let a_dense = csr.to_dense();

        let x = vec![0.1_f32, 0.2, 0.3];
        let mut y = vec![0.0_f32; 3];
        csr.spmv(1.0, &x, 0.0, &mut y).unwrap();

        // Compute reference dense result
        let y_ref = dense_matvec(&a_dense, &x, 3, 3);

        // Compute residual
        let residual: f32 =
            y.iter().zip(y_ref.iter()).map(|(a, b)| (a - b).abs()).fold(0.0_f32, f32::max);

        let a_norm = mat_inf_norm(&a_dense, 3, 3);
        let x_norm = vec_inf_norm(&x);

        // Backward error bound: proportional to nnz_per_row * machine epsilon
        let eps = 3.0 * f32::EPSILON * a_norm * x_norm;
        assert!(residual < eps, "Backward error too large: residual={}, bound={}", residual, eps);
    }

    // =========================================================================
    // SOLVER TESTS (dense factorizations)
    // =========================================================================

    use trueno_solve::{
        cholesky, lu_factorize, qr_factorize, svd, symm, syrk, trmm, trsm, DiagonalType,
        TriangularSide,
    };

    // ---- Cholesky tests ----

    #[test]
    fn test_cholesky_spd_solve() {
        // A = [[4, 2], [2, 3]] (symmetric positive definite)
        // b = [1, 2]
        // Solve Ax = b
        let a = [4.0_f32, 2.0, 2.0, 3.0];
        let b = [1.0_f32, 2.0];

        let chol = cholesky(&a, 2).unwrap();
        let x = chol.solve(&b).unwrap();

        // Verify: Ax should equal b
        let ax = dense_matvec(&a, &x, 2, 2);
        for i in 0..2 {
            assert!(
                (ax[i] - b[i]).abs() < 1e-4,
                "Cholesky solve: Ax[{}]={}, b[{}]={}",
                i,
                ax[i],
                i,
                b[i]
            );
        }
    }

    #[test]
    fn test_cholesky_non_spd() {
        // A = [[1, 0], [0, -1]] - not positive definite
        let a = [1.0_f32, 0.0, 0.0, -1.0];
        let result = cholesky(&a, 2);
        assert!(result.is_err(), "Should fail on non-SPD matrix");
    }

    #[test]
    fn test_cholesky_residual() {
        // A = [[9, 3, 3], [3, 5, 2], [3, 2, 4]] (SPD)
        // b = [1, 2, 3]
        // Verify ||Ax - b|| / ||b|| < eps
        let a = [9.0_f32, 3.0, 3.0, 3.0, 5.0, 2.0, 3.0, 2.0, 4.0];
        let b = [1.0_f32, 2.0, 3.0];

        let chol = cholesky(&a, 3).unwrap();
        let x = chol.solve(&b).unwrap();

        let ax = dense_matvec(&a, &x, 3, 3);
        let residual: Vec<f32> = ax.iter().zip(b.iter()).map(|(a, b)| a - b).collect();
        let res_norm = vec_inf_norm(&residual);
        let b_norm = vec_inf_norm(&b);

        let relative_residual = res_norm / b_norm;
        assert!(
            relative_residual < 1e-4,
            "Cholesky residual too large: ||Ax-b||/||b|| = {}",
            relative_residual
        );
    }

    // ---- LU tests ----

    #[test]
    fn test_lu_backward_error() {
        // A = [[2, 1, 1], [4, 3, 3], [8, 7, 9]]
        // b = [4, 10, 24]
        // Expected solution: x = [1, 1, 1]
        let a = [2.0_f32, 1.0, 1.0, 4.0, 3.0, 3.0, 8.0, 7.0, 9.0];
        let b = [4.0_f32, 10.0, 24.0];

        let lu = lu_factorize(&a, 3).unwrap();
        let x = lu.solve(&b).unwrap();

        // Verify backward error: ||Ax - b|| / (||A|| * ||x||)
        let ax = dense_matvec(&a, &x, 3, 3);
        let residual: Vec<f32> = ax.iter().zip(b.iter()).map(|(a, b)| a - b).collect();
        let res_norm = vec_inf_norm(&residual);
        let a_norm = mat_inf_norm(&a, 3, 3);
        let x_norm = vec_inf_norm(&x);

        let backward_error = res_norm / (a_norm * x_norm);
        assert!(backward_error < 1e-5, "LU backward error too large: {}", backward_error);
    }

    #[test]
    fn test_lu_singular_detected() {
        // Singular matrix: [[1, 2], [2, 4]]
        let a = [1.0_f32, 2.0, 2.0, 4.0];
        let result = lu_factorize(&a, 2);
        assert!(result.is_err(), "Should detect singular matrix");
    }

    #[test]
    fn test_lu_solution_residual() {
        // A = [[3, 2], [1, 4]], b = [5, 5]
        let a = [3.0_f32, 2.0, 1.0, 4.0];
        let b = [5.0_f32, 5.0];

        let lu = lu_factorize(&a, 2).unwrap();
        let x = lu.solve(&b).unwrap();

        // Check Ax = b
        let ax = dense_matvec(&a, &x, 2, 2);
        for i in 0..2 {
            assert!(
                (ax[i] - b[i]).abs() < 1e-4,
                "LU residual: Ax[{}]={}, b[{}]={}",
                i,
                ax[i],
                i,
                b[i]
            );
        }
    }

    // ---- QR tests ----

    #[test]
    fn test_qr_orthogonality() {
        // A = [[1, 2], [3, 4], [5, 6]] (3x2, tall-skinny)
        let a = [1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0];
        let qr = qr_factorize(&a, 3, 2).unwrap();
        let q = qr.extract_q(); // 3x3

        // Verify Q^T * Q = I (within tolerance)
        // Compute Q^T * Q properly:
        let mut qt = vec![0.0_f32; 9];
        for i in 0..3 {
            for j in 0..3 {
                qt[i * 3 + j] = q[j * 3 + i]; // transpose
            }
        }
        let qtq = dense_matmul(&qt, &q, 3, 3, 3);

        for i in 0..3 {
            for j in 0..3 {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert!(
                    (qtq[i * 3 + j] - expected).abs() < 1e-4,
                    "Q^T*Q[{},{}] = {}, expected {}",
                    i,
                    j,
                    qtq[i * 3 + j],
                    expected
                );
            }
        }
    }

    #[test]
    fn test_qr_reconstruction() {
        // Verify Q * R = A (within the first m rows, n cols)
        let a = [1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0]; // 3x2
        let qr = qr_factorize(&a, 3, 2).unwrap();
        let q = qr.extract_q(); // 3x3
        let r = qr.extract_r(); // 2x2

        // Q (3x3) * [R (2x2); 0] => we need Q * R_full where R_full is 3x2
        let mut r_full = vec![0.0_f32; 3 * 2];
        for i in 0..2 {
            for j in 0..2 {
                r_full[i * 2 + j] = r[i * 2 + j];
            }
        }
        // r_full row 2 is zeros

        let qr_product = dense_matmul(&q, &r_full, 3, 3, 2);

        for i in 0..6 {
            assert!(
                (qr_product[i] - a[i]).abs() < 1e-4,
                "QR reconstruction: (QR)[{}] = {}, A[{}] = {}",
                i,
                qr_product[i],
                i,
                a[i]
            );
        }
    }

    // ---- SVD tests ----

    #[test]
    fn test_svd_singular_values_nonneg_decreasing() {
        let a = [3.0_f32, 2.0, 2.0, 2.0, 3.0, -2.0]; // 2x3
        let result = svd(&a, 2, 3).unwrap();

        for s in &result.sigma {
            assert!(*s >= 0.0, "Singular value should be non-negative: {}", s);
        }

        for i in 1..result.sigma.len() {
            assert!(
                result.sigma[i - 1] >= result.sigma[i] - 1e-6,
                "Singular values not decreasing: s[{}]={} < s[{}]={}",
                i - 1,
                result.sigma[i - 1],
                i,
                result.sigma[i]
            );
        }
    }

    #[test]
    fn test_svd_reconstruction() {
        // A = U * diag(sigma) * V^T
        let a = [1.0_f32, 2.0, 3.0, 4.0]; // 2x2
        let result = svd(&a, 2, 2).unwrap();

        let m = result.m;
        let n = result.n;
        let min_mn = m.min(n);

        // Reconstruct: A_approx = U * diag(sigma) * V^T
        // U is m x m, sigma is min_mn, V^T is n x n
        // First compute U * diag(sigma): m x min_mn
        let mut u_sigma = vec![0.0_f32; m * n];
        for i in 0..m {
            for j in 0..min_mn {
                u_sigma[i * n + j] = result.u[i * m + j] * result.sigma[j];
            }
        }

        // Then u_sigma * V^T: m x n
        let reconstructed = dense_matmul(&u_sigma, &result.vt, m, n, n);

        for i in 0..4 {
            assert!(
                (reconstructed[i] - a[i]).abs() < 1e-3,
                "SVD reconstruction: A_approx[{}]={}, A[{}]={}",
                i,
                reconstructed[i],
                i,
                a[i]
            );
        }
    }

    #[test]
    fn test_svd_orthogonality_u() {
        // For a square matrix, U should be fully orthogonal.
        let a = [1.0_f32, 2.0, 3.0, 4.0]; // 2x2
        let result = svd(&a, 2, 2).unwrap();
        let m = result.m;

        // Compute U^T * U
        let mut ut = vec![0.0_f32; m * m];
        for i in 0..m {
            for j in 0..m {
                ut[i * m + j] = result.u[j * m + i];
            }
        }
        let utu = dense_matmul(&ut, &result.u, m, m, m);

        for i in 0..m {
            for j in 0..m {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert!(
                    (utu[i * m + j] - expected).abs() < 1e-3,
                    "U^T*U[{},{}] = {}, expected {}",
                    i,
                    j,
                    utu[i * m + j],
                    expected
                );
            }
        }

        // For a tall matrix, verify orthogonality of the first min(m,n) columns.
        let a_tall = [1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0]; // 3x2
        let result_tall = svd(&a_tall, 3, 2).unwrap();
        let m2 = result_tall.m;
        let min_mn = m2.min(result_tall.n);

        // Check that the first min_mn columns of U are orthonormal
        for i in 0..min_mn {
            for j in 0..min_mn {
                let mut dot = 0.0_f32;
                for k in 0..m2 {
                    dot += result_tall.u[k * m2 + i] * result_tall.u[k * m2 + j];
                }
                let expected = if i == j { 1.0 } else { 0.0 };
                assert!(
                    (dot - expected).abs() < 1e-3,
                    "U columns ({},{}) dot = {}, expected {}",
                    i,
                    j,
                    dot,
                    expected
                );
            }
        }
    }

    #[test]
    fn test_svd_singular_values_nonneg() {
        let a = [1.0_f32, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0]; // diagonal 3x3
        let result = svd(&a, 3, 3).unwrap();

        for (i, s) in result.sigma.iter().enumerate() {
            assert!(*s >= -1e-7, "Singular value {} should be non-negative: {}", i, s);
        }

        // For a diagonal matrix, singular values should be |diag| sorted descending
        assert!((result.sigma[0] - 3.0).abs() < 1e-4, "s[0]={}", result.sigma[0]);
        assert!((result.sigma[1] - 2.0).abs() < 1e-4, "s[1]={}", result.sigma[1]);
        assert!((result.sigma[2] - 1.0).abs() < 1e-4, "s[2]={}", result.sigma[2]);
    }

    // =========================================================================
    // BLAS LEVEL 3 TESTS
    // =========================================================================

    // ---- SYRK tests ----

    #[test]
    fn test_syrk_identity() {
        // C = alpha * A * A^T + beta * C with A = I(2), alpha=1, beta=0
        // Result should be I(2) since I * I^T = I
        let a = [1.0_f32, 0.0, 0.0, 1.0]; // 2x2 identity
        let mut c = vec![0.0_f32; 4];
        syrk(&a, &mut c, 2, 2, 1.0, 0.0).unwrap();

        // C should be I
        assert!((c[0] - 1.0).abs() < 1e-5);
        assert!((c[1] - 0.0).abs() < 1e-5);
        assert!((c[2] - 0.0).abs() < 1e-5);
        assert!((c[3] - 1.0).abs() < 1e-5);

        // Must be symmetric
        assert!(is_symmetric(&c, 2, 1e-6));
    }

    #[test]
    fn test_syrk_known_value() {
        // A = [[1, 2], [3, 4]] (2x2, n=2, k=2)
        // C = A * A^T = [[1*1+2*2, 1*3+2*4], [3*1+4*2, 3*3+4*4]]
        //             = [[5, 11], [11, 25]]
        let a = [1.0_f32, 2.0, 3.0, 4.0];
        let mut c = vec![0.0_f32; 4];
        syrk(&a, &mut c, 2, 2, 1.0, 0.0).unwrap();

        assert!((c[0] - 5.0).abs() < 1e-5, "C[0,0]={}, expected 5", c[0]);
        assert!((c[1] - 11.0).abs() < 1e-5, "C[0,1]={}, expected 11", c[1]);
        assert!((c[2] - 11.0).abs() < 1e-5, "C[1,0]={}, expected 11", c[2]);
        assert!((c[3] - 25.0).abs() < 1e-5, "C[1,1]={}, expected 25", c[3]);
    }

    #[test]
    fn test_syrk_symmetry() {
        // A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] (3x3, n=3, k=3)
        // C = A * A^T must be symmetric
        let a = [1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
        let mut c = vec![0.0_f32; 9];
        syrk(&a, &mut c, 3, 3, 1.0, 0.0).unwrap();

        assert!(is_symmetric(&c, 3, 1e-5), "SYRK result must be symmetric: {:?}", c);
    }

    // ---- TRMM tests ----

    #[test]
    fn test_trmm_identity() {
        // B = alpha * I * B = alpha * B
        // With alpha=1.0: result should be B unchanged
        let a = [1.0_f32, 0.0, 0.0, 1.0]; // 2x2 identity (lower triangular)
        let mut b = vec![5.0_f32, 6.0, 7.0, 8.0]; // 2x2

        trmm(&a, &mut b, 2, 2, 1.0).unwrap();

        assert!((b[0] - 5.0).abs() < 1e-5);
        assert!((b[1] - 6.0).abs() < 1e-5);
        assert!((b[2] - 7.0).abs() < 1e-5);
        assert!((b[3] - 8.0).abs() < 1e-5);
    }

    #[test]
    fn test_trmm_lower_triangular() {
        // A = [[2, 0], [3, 4]] (lower triangular)
        // B = [[1, 0], [0, 1]] (identity)
        // Result = alpha * A * B = A (when alpha=1)
        let a = [2.0_f32, 0.0, 3.0, 4.0];
        let mut b = vec![1.0_f32, 0.0, 0.0, 1.0];

        trmm(&a, &mut b, 2, 2, 1.0).unwrap();

        // Row 0: only A[0,0]*B[0,:] contributes (lower triangular)
        // B[0,:] = [2*1 + 0*0, 2*0 + 0*1] = [2, 0]
        // Row 1: A[1,0]*B[0,:] + A[1,1]*B[1,:]
        // B[1,:] = [3*1 + 4*0, 3*0 + 4*1] = [3, 4]
        assert!((b[0] - 2.0).abs() < 1e-5, "b[0,0]={}", b[0]);
        assert!((b[1] - 0.0).abs() < 1e-5, "b[0,1]={}", b[1]);
        assert!((b[2] - 3.0).abs() < 1e-5, "b[1,0]={}", b[2]);
        assert!((b[3] - 4.0).abs() < 1e-5, "b[1,1]={}", b[3]);
    }

    // ---- SYMM tests ----

    #[test]
    fn test_symm_known_product() {
        // A = [[2, 1], [1, 3]] (symmetric)
        // B = [[1, 0], [0, 2]]
        // C = alpha * A * B + beta * C
        // With alpha=1, beta=0:
        // C = A * B = [[2*1+1*0, 2*0+1*2], [1*1+3*0, 1*0+3*2]] = [[2, 2], [1, 6]]
        let a = [2.0_f32, 1.0, 1.0, 3.0];
        let b = [1.0_f32, 0.0, 0.0, 2.0];
        let mut c = vec![0.0_f32; 4];

        symm(&a, &b, &mut c, 2, 2, 1.0, 0.0).unwrap();

        assert!((c[0] - 2.0).abs() < 1e-5, "C[0,0]={}, expected 2", c[0]);
        assert!((c[1] - 2.0).abs() < 1e-5, "C[0,1]={}, expected 2", c[1]);
        assert!((c[2] - 1.0).abs() < 1e-5, "C[1,0]={}, expected 1", c[2]);
        assert!((c[3] - 6.0).abs() < 1e-5, "C[1,1]={}, expected 6", c[3]);
    }

    // ---- TRSM tests ----

    #[test]
    fn test_trsm_backward_error() {
        // Solve A * X = B where A is lower triangular
        // A = [[2, 0], [3, 4]]
        // B = [[4, 6], [11, 16]]
        // Expected: X = [[2, 3], [1.25, 1.75]]
        let a = [2.0_f32, 0.0, 3.0, 4.0];
        let b = [4.0_f32, 6.0, 11.0, 16.0];
        let n = 2;
        let nrhs = 2;

        let result = trsm(&a, &b, n, nrhs, TriangularSide::Lower, DiagonalType::NonUnit).unwrap();

        // Verify A * X = B (backward error check)
        // Compute A * X
        let x = &result.x;
        for col in 0..nrhs {
            for i in 0..n {
                let mut ax_ij = 0.0_f32;
                for j in 0..n {
                    ax_ij += a[i * n + j] * x[j * nrhs + col];
                }
                assert!(
                    (ax_ij - b[i * nrhs + col]).abs() < 1e-4,
                    "TRSM backward error: AX[{},{}]={}, B[{},{}]={}",
                    i,
                    col,
                    ax_ij,
                    i,
                    col,
                    b[i * nrhs + col]
                );
            }
        }
    }

    #[test]
    fn test_trsm_singular_detected() {
        // A = [[1, 0], [2, 0]] - singular (zero on diagonal)
        let a = [1.0_f32, 0.0, 2.0, 0.0];
        let b = [1.0_f32, 2.0];
        let result = trsm(&a, &b, 2, 1, TriangularSide::Lower, DiagonalType::NonUnit);
        assert!(result.is_err(), "Should detect singular triangular matrix");
    }
}