oxiblas-sparse 0.2.2

Sparse matrix support for OxiBLAS
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
//! Standard test matrix generators for sparse operations.
//!
//! Provides well-known test matrices commonly used in numerical linear algebra
//! benchmarks and validation. All generators return `CsrMatrix<f64>`.
//!
//! # Available Generators
//!
//! - [`laplacian_2d`] - 2D Laplacian with 5-point stencil
//! - [`laplacian_3d`] - 3D Laplacian with 7-point stencil
//! - [`tridiagonal`] - General tridiagonal matrix
//! - [`diagonal`] - Diagonal matrix
//! - [`arrow_matrix`] - Arrow/arrowhead matrix
//! - [`random_spd`] - Random symmetric positive definite matrix
//! - [`poisson_1d`] - 1D Poisson matrix
//!
//! # Properties
//!
//! Most generators produce matrices with known mathematical properties
//! (symmetry, positive definiteness, specific eigenvalue distributions)
//! making them suitable for validating solvers and preconditioners.

use crate::CooMatrixBuilder;
use crate::csr::CsrMatrix;

/// Error type for test matrix generation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TestMatrixError {
    /// Invalid dimensions (zero or too large).
    InvalidDimension {
        /// Description of the invalid parameter.
        param: String,
        /// The invalid value.
        value: usize,
    },
    /// Density out of valid range [0, 1].
    InvalidDensity {
        /// The invalid density value.
        density: String,
    },
    /// Matrix construction failed internally.
    ConstructionError {
        /// Description of the failure.
        description: String,
    },
}

impl core::fmt::Display for TestMatrixError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidDimension { param, value } => {
                write!(f, "Invalid dimension for {param}: {value}")
            }
            Self::InvalidDensity { density } => {
                write!(f, "Invalid density: {density} (must be in [0, 1])")
            }
            Self::ConstructionError { description } => {
                write!(f, "Matrix construction error: {description}")
            }
        }
    }
}

impl std::error::Error for TestMatrixError {}

/// Creates a 2D Laplacian matrix using the 5-point stencil.
///
/// For an `nx` x `ny` grid, produces an `(nx*ny)` x `(nx*ny)` sparse matrix
/// representing the discrete Laplacian operator with Dirichlet boundary conditions.
///
/// The stencil at interior point (i,j) is:
/// ```text
///       -1
///   -1   4  -1
///       -1
/// ```
///
/// # Properties
/// - Symmetric positive definite (SPD)
/// - Eigenvalues: 4 - 2*cos(pi*k/(nx+1)) - 2*cos(pi*l/(ny+1)) for k=1..nx, l=1..ny
/// - Condition number grows as O(max(nx,ny)^2)
/// - nnz = 5*nx*ny - 2*(nx+ny)
///
/// # Errors
/// Returns error if `nx` or `ny` is zero.
pub fn laplacian_2d(nx: usize, ny: usize) -> Result<CsrMatrix<f64>, TestMatrixError> {
    if nx == 0 {
        return Err(TestMatrixError::InvalidDimension {
            param: "nx".to_string(),
            value: 0,
        });
    }
    if ny == 0 {
        return Err(TestMatrixError::InvalidDimension {
            param: "ny".to_string(),
            value: 0,
        });
    }

    let n = nx * ny;
    let mut builder = CooMatrixBuilder::new(n, n);

    for j in 0..ny {
        for i in 0..nx {
            let idx = j * nx + i;

            // Diagonal: 4
            builder.add(idx, idx, 4.0);

            // Left neighbor: -1
            if i > 0 {
                builder.add(idx, idx - 1, -1.0);
            }

            // Right neighbor: -1
            if i < nx - 1 {
                builder.add(idx, idx + 1, -1.0);
            }

            // Bottom neighbor: -1
            if j > 0 {
                builder.add(idx, idx - nx, -1.0);
            }

            // Top neighbor: -1
            if j < ny - 1 {
                builder.add(idx, idx + nx, -1.0);
            }
        }
    }

    Ok(builder.build().to_csr())
}

/// Creates a 3D Laplacian matrix using the 7-point stencil.
///
/// For an `nx` x `ny` x `nz` grid, produces an `(nx*ny*nz)` x `(nx*ny*nz)` sparse matrix
/// representing the discrete 3D Laplacian operator with Dirichlet boundary conditions.
///
/// The stencil at interior point (i,j,k) is:
/// ```text
/// z-1: -1    y-1: -1    center: -1  6  -1    y+1: -1    z+1: -1
///                                x-1     x+1
/// ```
///
/// # Properties
/// - Symmetric positive definite (SPD)
/// - Diagonal value: 6, off-diagonal values: -1
/// - nnz = 7*nx*ny*nz - 2*(nx*ny + ny*nz + nx*nz)
///
/// # Errors
/// Returns error if `nx`, `ny`, or `nz` is zero.
pub fn laplacian_3d(nx: usize, ny: usize, nz: usize) -> Result<CsrMatrix<f64>, TestMatrixError> {
    if nx == 0 {
        return Err(TestMatrixError::InvalidDimension {
            param: "nx".to_string(),
            value: 0,
        });
    }
    if ny == 0 {
        return Err(TestMatrixError::InvalidDimension {
            param: "ny".to_string(),
            value: 0,
        });
    }
    if nz == 0 {
        return Err(TestMatrixError::InvalidDimension {
            param: "nz".to_string(),
            value: 0,
        });
    }

    let n = nx * ny * nz;
    let nxy = nx * ny;
    let mut builder = CooMatrixBuilder::new(n, n);

    for k in 0..nz {
        for j in 0..ny {
            for i in 0..nx {
                let idx = k * nxy + j * nx + i;

                // Diagonal: 6
                builder.add(idx, idx, 6.0);

                // x-direction neighbors
                if i > 0 {
                    builder.add(idx, idx - 1, -1.0);
                }
                if i < nx - 1 {
                    builder.add(idx, idx + 1, -1.0);
                }

                // y-direction neighbors
                if j > 0 {
                    builder.add(idx, idx - nx, -1.0);
                }
                if j < ny - 1 {
                    builder.add(idx, idx + nx, -1.0);
                }

                // z-direction neighbors
                if k > 0 {
                    builder.add(idx, idx - nxy, -1.0);
                }
                if k < nz - 1 {
                    builder.add(idx, idx + nxy, -1.0);
                }
            }
        }
    }

    Ok(builder.build().to_csr())
}

/// Creates a tridiagonal matrix with specified sub-diagonal, diagonal, and super-diagonal values.
///
/// Produces an `n` x `n` matrix:
/// ```text
/// [ diag  sup   0    0  ... ]
/// [ sub   diag  sup  0  ... ]
/// [ 0     sub   diag sup ... ]
/// [ ...                      ]
/// ```
///
/// # Properties
/// - Bandwidth = 1
/// - SPD if `sub == sup` and `diag > 2*|sub|` (diagonal dominance)
/// - nnz = 3n - 2 for n >= 2
///
/// # Errors
/// Returns error if `n` is zero.
pub fn tridiagonal(
    n: usize,
    sub: f64,
    diag: f64,
    sup: f64,
) -> Result<CsrMatrix<f64>, TestMatrixError> {
    if n == 0 {
        return Err(TestMatrixError::InvalidDimension {
            param: "n".to_string(),
            value: 0,
        });
    }

    let mut builder = CooMatrixBuilder::new(n, n);

    for i in 0..n {
        // Main diagonal
        builder.add(i, i, diag);

        // Sub-diagonal
        if i > 0 {
            builder.add(i, i - 1, sub);
        }

        // Super-diagonal
        if i < n - 1 {
            builder.add(i, i + 1, sup);
        }
    }

    Ok(builder.build().to_csr())
}

/// Creates a diagonal matrix with a constant value on the main diagonal.
///
/// Produces an `n` x `n` matrix with `value` on all diagonal entries and zeros elsewhere.
///
/// # Properties
/// - Symmetric
/// - Positive definite if `value > 0`
/// - All eigenvalues equal `value`
/// - nnz = n
/// - Condition number = 1
///
/// # Errors
/// Returns error if `n` is zero.
pub fn diagonal(n: usize, value: f64) -> Result<CsrMatrix<f64>, TestMatrixError> {
    if n == 0 {
        return Err(TestMatrixError::InvalidDimension {
            param: "n".to_string(),
            value: 0,
        });
    }

    let mut builder = CooMatrixBuilder::new(n, n);

    for i in 0..n {
        builder.add(i, i, value);
    }

    Ok(builder.build().to_csr())
}

/// Creates an arrow (arrowhead) matrix.
///
/// The arrow matrix has a dense first row, dense first column, and a diagonal:
/// ```text
/// [ d0  a1  a2  a3 ... ]
/// [ a1  d1  0   0  ... ]
/// [ a2  0   d2  0  ... ]
/// [ a3  0   0   d3 ... ]
/// [ ...                 ]
/// ```
///
/// where `d_i = n + 1 - i` (diagonal values) and `a_i = 1` (arrow entries).
///
/// # Properties
/// - Symmetric
/// - Positive definite (with chosen diagonal values)
/// - Tests sparse solvers with dense row/column structure
/// - nnz = 3n - 2 for n >= 2
///
/// # Errors
/// Returns error if `n` is zero.
pub fn arrow_matrix(n: usize) -> Result<CsrMatrix<f64>, TestMatrixError> {
    if n == 0 {
        return Err(TestMatrixError::InvalidDimension {
            param: "n".to_string(),
            value: 0,
        });
    }

    if n == 1 {
        let mut builder = CooMatrixBuilder::new(1, 1);
        builder.add(0, 0, 1.0);
        return Ok(builder.build().to_csr());
    }

    let mut builder = CooMatrixBuilder::new(n, n);

    // First row and column (arrow entries)
    // Diagonal d0 = n + 1 to ensure diagonal dominance
    builder.add(0, 0, (n + 1) as f64);

    for i in 1..n {
        // First row: a_i = 1
        builder.add(0, i, 1.0);
        // First column: a_i = 1 (symmetric)
        builder.add(i, 0, 1.0);
        // Diagonal: d_i = n + 1 - i to ensure positive definiteness
        // We need d_i > 1/d_0 * sum(...) for SPD, so use generous values
        builder.add(i, i, (n + 1 - i) as f64);
    }

    Ok(builder.build().to_csr())
}

/// Creates a random symmetric positive definite (SPD) matrix with a given density.
///
/// Starts from the diagonal matrix `n*I` and, for each generated
/// off-diagonal pair `(row, col)` with `row > col`, adds a symmetric
/// pseudo-random entry at `(row, col)` and `(col, row)` while adding the
/// *same* value to both `(row, row)` and `(col, col)`. Because every
/// off-diagonal contribution to a row is matched by an equal contribution
/// to that row's diagonal, each row satisfies
/// `diagonal_i - sum_{j != i} |a_ij| = n > 0`, i.e. the matrix is strictly
/// diagonally dominant with a positive diagonal. By the Gershgorin circle
/// theorem a symmetric, strictly diagonally dominant matrix with positive
/// diagonal is positive definite, so this construction guarantees an SPD
/// result without ever forming an explicit Cholesky-like factor `L`.
///
/// Entry values and positions are generated by a simple deterministic
/// xorshift-style hash (not the `rand` crate), so results are reproducible
/// across calls with the same arguments.
///
/// # Arguments
/// - `n` - Matrix dimension
/// - `density` - Target density of non-zeros in [0.0, 1.0]
///
/// # Properties
/// - Symmetric positive definite (guaranteed by strict diagonal dominance)
/// - Approximately `density * n * n` non-zeros
///
/// # Errors
/// Returns error if `n` is zero or `density` is not in [0, 1].
pub fn random_spd(n: usize, density: f64) -> Result<CsrMatrix<f64>, TestMatrixError> {
    if n == 0 {
        return Err(TestMatrixError::InvalidDimension {
            param: "n".to_string(),
            value: 0,
        });
    }
    if !(0.0..=1.0).contains(&density) {
        return Err(TestMatrixError::InvalidDensity {
            density: format!("{density}"),
        });
    }

    // Use a simple deterministic hash-based pseudo-random generator
    // to avoid needing the rand crate in production code.
    // We build a lower triangular L, then compute A = L*L^T + n*I.
    let mut builder = CooMatrixBuilder::new(n, n);

    // Add n*I for guaranteed positive definiteness
    for i in 0..n {
        builder.add(i, i, n as f64);
    }

    if density <= 0.0 || n == 1 {
        return Ok(builder.build().to_csr());
    }

    // Target number of lower-triangular non-zeros (excluding diagonal)
    let max_lt_entries = n * (n - 1) / 2;
    let target_lt_nnz = ((max_lt_entries as f64) * density).ceil() as usize;

    // Deterministic pseudo-random entry generation using a simple hash
    let mut seed: u64 = 0x517cc1b727220a95;
    let mut generated = 0usize;
    let max_attempts = max_lt_entries * 3; // prevent infinite loop on high density

    for attempt in 0..max_attempts {
        if generated >= target_lt_nnz {
            break;
        }

        // Simple xorshift-style hash
        seed ^= seed.wrapping_shl(13);
        seed ^= seed.wrapping_shr(7);
        seed ^= seed.wrapping_shl(17);
        seed = seed.wrapping_add(attempt as u64);

        let row = ((seed >> 16) as usize) % n;
        let col = ((seed >> 32) as usize) % n;

        if row > col {
            // Deterministic value in range [0.1, 1.0]
            let val = 0.1 + 0.9 * ((seed & 0xFF) as f64) / 255.0;
            // Add both (row, col) and (col, row) for symmetry
            builder.add(row, col, val);
            builder.add(col, row, val);
            // Also strengthen the diagonal to maintain SPD
            builder.add(row, row, val);
            builder.add(col, col, val);
            generated += 1;
        }
    }

    let mut coo = builder.build();
    coo.sum_duplicates();
    Ok(coo.to_csr())
}

/// Creates the 1D Poisson matrix (second-difference operator).
///
/// Produces an `n` x `n` tridiagonal matrix:
/// ```text
/// [ 2  -1   0   0  ... ]
/// [-1   2  -1   0  ... ]
/// [ 0  -1   2  -1  ... ]
/// [ ...                 ]
/// [ 0   0  ... -1   2  ]
/// ```
///
/// This is the standard discretization of -u''(x) = f(x) on \[0,1\]
/// with uniform mesh spacing h = 1/(n+1), scaled by h^2.
///
/// # Properties
/// - Symmetric positive definite (SPD)
/// - Eigenvalues: 2 - 2*cos(k*pi/(n+1)) for k=1..n
/// - Condition number: O(n^2)
/// - nnz = 3n - 2 for n >= 2
///
/// # Errors
/// Returns error if `n` is zero.
pub fn poisson_1d(n: usize) -> Result<CsrMatrix<f64>, TestMatrixError> {
    tridiagonal(n, -1.0, 2.0, -1.0)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ========================================================================
    // laplacian_2d tests
    // ========================================================================

    #[test]
    fn test_laplacian_2d_basic() {
        let mat = laplacian_2d(3, 3).expect("Failed to create 2D Laplacian");
        assert_eq!(mat.nrows(), 9);
        assert_eq!(mat.ncols(), 9);
        // Interior points have 4 neighbors, boundary points have 2-3
        // 5-point stencil on 3x3 grid:
        // corners: 2 off-diag + 1 diag = 3 entries each (4 corners)
        // edges: 3 off-diag + 1 diag = 4 entries each (4 edges for 3x3 minus corners = 4)
        // center: 4 off-diag + 1 diag = 5 entries (1 center)
        // Total: 4*3 + 4*4 + 1*5 = 12 + 16 + 5 = 33? No...
        // Actually: nnz = 5*nx*ny - 2*(nx+ny) = 5*9 - 2*6 = 45 - 12 = 33
        // But the formula includes the diagonal, so nnz = 5*3*3 - 2*(3+3) = 33
        assert_eq!(mat.nnz(), 33);
    }

    #[test]
    fn test_laplacian_2d_symmetry() {
        let mat = laplacian_2d(4, 3).expect("Failed to create 2D Laplacian");
        let n = mat.nrows();

        // Check A[i,j] == A[j,i] for all entries
        for (row, col, &val) in mat.iter() {
            let transpose_val = mat.get_or_zero(col, row);
            assert!(
                (val - transpose_val).abs() < 1e-14,
                "Laplacian 2D not symmetric at ({row}, {col}): {val} vs {transpose_val}"
            );
        }
        // Also check dimensions
        assert_eq!(n, 12);
    }

    #[test]
    fn test_laplacian_2d_positive_definiteness() {
        // Verify diagonal dominance (sufficient for SPD)
        let mat = laplacian_2d(5, 5).expect("Failed to create 2D Laplacian");
        let n = mat.nrows();

        for i in 0..n {
            let diag = mat.get_or_zero(i, i);
            let mut off_diag_sum = 0.0;
            for (col, val) in mat.row_iter(i) {
                if col != i {
                    off_diag_sum += val.abs();
                }
            }
            assert!(
                diag >= off_diag_sum,
                "Row {i}: diagonal {diag} < off-diagonal sum {off_diag_sum}"
            );
        }
    }

    #[test]
    fn test_laplacian_2d_1x1() {
        let mat = laplacian_2d(1, 1).expect("Failed to create 1x1 Laplacian");
        assert_eq!(mat.nrows(), 1);
        assert_eq!(mat.ncols(), 1);
        assert_eq!(mat.nnz(), 1);
        assert!((mat.get_or_zero(0, 0) - 4.0).abs() < 1e-14);
    }

    #[test]
    fn test_laplacian_2d_zero_dimension() {
        assert!(laplacian_2d(0, 5).is_err());
        assert!(laplacian_2d(5, 0).is_err());
    }

    // ========================================================================
    // laplacian_3d tests
    // ========================================================================

    #[test]
    fn test_laplacian_3d_basic() {
        let mat = laplacian_3d(3, 3, 3).expect("Failed to create 3D Laplacian");
        assert_eq!(mat.nrows(), 27);
        assert_eq!(mat.ncols(), 27);
        // nnz = 7*nx*ny*nz - 2*(nx*ny + ny*nz + nx*nz)
        // = 7*27 - 2*(9 + 9 + 9) = 189 - 54 = 135
        assert_eq!(mat.nnz(), 135);
    }

    #[test]
    fn test_laplacian_3d_symmetry() {
        let mat = laplacian_3d(3, 2, 2).expect("Failed to create 3D Laplacian");

        for (row, col, &val) in mat.iter() {
            let transpose_val = mat.get_or_zero(col, row);
            assert!(
                (val - transpose_val).abs() < 1e-14,
                "Laplacian 3D not symmetric at ({row}, {col}): {val} vs {transpose_val}"
            );
        }
    }

    #[test]
    fn test_laplacian_3d_diagonal_dominance() {
        let mat = laplacian_3d(3, 3, 3).expect("Failed to create 3D Laplacian");
        let n = mat.nrows();

        for i in 0..n {
            let diag = mat.get_or_zero(i, i);
            let mut off_diag_sum = 0.0;
            for (col, val) in mat.row_iter(i) {
                if col != i {
                    off_diag_sum += val.abs();
                }
            }
            assert!(
                diag >= off_diag_sum,
                "Row {i}: diagonal {diag} < off-diagonal sum {off_diag_sum}"
            );
        }
    }

    #[test]
    fn test_laplacian_3d_zero_dimension() {
        assert!(laplacian_3d(0, 3, 3).is_err());
        assert!(laplacian_3d(3, 0, 3).is_err());
        assert!(laplacian_3d(3, 3, 0).is_err());
    }

    // ========================================================================
    // tridiagonal tests
    // ========================================================================

    #[test]
    fn test_tridiagonal_basic() {
        let mat = tridiagonal(5, -1.0, 2.0, -1.0).expect("Failed to create tridiagonal");
        assert_eq!(mat.nrows(), 5);
        assert_eq!(mat.ncols(), 5);
        // 3*5 - 2 = 13
        assert_eq!(mat.nnz(), 13);
    }

    #[test]
    fn test_tridiagonal_values() {
        let mat = tridiagonal(4, -1.0, 3.0, -2.0).expect("Failed to create tridiagonal");

        // Check diagonal
        for i in 0..4 {
            assert!((mat.get_or_zero(i, i) - 3.0).abs() < 1e-14);
        }

        // Check sub-diagonal
        for i in 1..4 {
            assert!((mat.get_or_zero(i, i - 1) - (-1.0)).abs() < 1e-14);
        }

        // Check super-diagonal
        for i in 0..3 {
            assert!((mat.get_or_zero(i, i + 1) - (-2.0)).abs() < 1e-14);
        }

        // Check zeros
        assert!((mat.get_or_zero(0, 2)).abs() < 1e-14);
        assert!((mat.get_or_zero(0, 3)).abs() < 1e-14);
        assert!((mat.get_or_zero(3, 0)).abs() < 1e-14);
    }

    #[test]
    fn test_tridiagonal_symmetric() {
        let mat = tridiagonal(5, -1.0, 4.0, -1.0).expect("Failed to create symmetric tridiagonal");

        for (row, col, &val) in mat.iter() {
            let transpose_val = mat.get_or_zero(col, row);
            assert!(
                (val - transpose_val).abs() < 1e-14,
                "Symmetric tridiagonal not symmetric at ({row}, {col})"
            );
        }
    }

    #[test]
    fn test_tridiagonal_size_1() {
        let mat = tridiagonal(1, -1.0, 5.0, -1.0).expect("Failed to create 1x1 tridiagonal");
        assert_eq!(mat.nrows(), 1);
        assert_eq!(mat.nnz(), 1);
        assert!((mat.get_or_zero(0, 0) - 5.0).abs() < 1e-14);
    }

    #[test]
    fn test_tridiagonal_zero() {
        assert!(tridiagonal(0, -1.0, 2.0, -1.0).is_err());
    }

    // ========================================================================
    // diagonal tests
    // ========================================================================

    #[test]
    fn test_diagonal_basic() {
        let mat = diagonal(5, 3.0).expect("Failed to create diagonal");
        assert_eq!(mat.nrows(), 5);
        assert_eq!(mat.ncols(), 5);
        assert_eq!(mat.nnz(), 5);

        for i in 0..5 {
            assert!((mat.get_or_zero(i, i) - 3.0).abs() < 1e-14);
        }
    }

    #[test]
    fn test_diagonal_off_diagonal_zeros() {
        let mat = diagonal(4, 2.0).expect("Failed to create diagonal");

        for i in 0..4 {
            for j in 0..4 {
                if i != j {
                    assert!(
                        mat.get_or_zero(i, j).abs() < 1e-14,
                        "Off-diagonal entry ({i}, {j}) is not zero"
                    );
                }
            }
        }
    }

    #[test]
    fn test_diagonal_symmetry() {
        let mat = diagonal(10, 7.5).expect("Failed to create diagonal");

        for (row, col, &val) in mat.iter() {
            let transpose_val = mat.get_or_zero(col, row);
            assert!(
                (val - transpose_val).abs() < 1e-14,
                "Diagonal matrix not symmetric at ({row}, {col})"
            );
        }
    }

    #[test]
    fn test_diagonal_zero() {
        assert!(diagonal(0, 1.0).is_err());
    }

    // ========================================================================
    // arrow_matrix tests
    // ========================================================================

    #[test]
    fn test_arrow_basic() {
        let mat = arrow_matrix(5).expect("Failed to create arrow matrix");
        assert_eq!(mat.nrows(), 5);
        assert_eq!(mat.ncols(), 5);
        // nnz = n (diagonal) + 2*(n-1) (first row/col excluding diagonal) = 3n - 2
        assert_eq!(mat.nnz(), 13);
    }

    #[test]
    fn test_arrow_symmetry() {
        let mat = arrow_matrix(8).expect("Failed to create arrow matrix");

        for (row, col, &val) in mat.iter() {
            let transpose_val = mat.get_or_zero(col, row);
            assert!(
                (val - transpose_val).abs() < 1e-14,
                "Arrow matrix not symmetric at ({row}, {col}): {val} vs {transpose_val}"
            );
        }
    }

    #[test]
    fn test_arrow_structure() {
        let n = 5;
        let mat = arrow_matrix(n).expect("Failed to create arrow matrix");

        // First row should have entries in all columns
        let mut first_row_cols = Vec::new();
        for (col, _) in mat.row_iter(0) {
            first_row_cols.push(col);
        }
        assert_eq!(first_row_cols.len(), n, "First row should have {n} entries");

        // Other rows should have exactly 2 entries (diagonal + first column)
        for i in 1..n {
            let mut row_nnz = 0;
            for _ in mat.row_iter(i) {
                row_nnz += 1;
            }
            assert_eq!(
                row_nnz, 2,
                "Row {i} should have exactly 2 entries, got {row_nnz}"
            );
        }
    }

    #[test]
    fn test_arrow_diagonal_dominance() {
        let mat = arrow_matrix(10).expect("Failed to create arrow matrix");
        let n = mat.nrows();

        for i in 0..n {
            let diag = mat.get_or_zero(i, i);
            let mut off_diag_sum = 0.0;
            for (col, val) in mat.row_iter(i) {
                if col != i {
                    off_diag_sum += val.abs();
                }
            }
            assert!(
                diag >= off_diag_sum,
                "Arrow row {i}: diagonal {diag} < off-diagonal sum {off_diag_sum}"
            );
        }
    }

    #[test]
    fn test_arrow_size_1() {
        let mat = arrow_matrix(1).expect("Failed to create 1x1 arrow");
        assert_eq!(mat.nrows(), 1);
        assert_eq!(mat.nnz(), 1);
    }

    #[test]
    fn test_arrow_zero() {
        assert!(arrow_matrix(0).is_err());
    }

    // ========================================================================
    // random_spd tests
    // ========================================================================

    #[test]
    fn test_random_spd_basic() {
        let mat = random_spd(10, 0.3).expect("Failed to create random SPD");
        assert_eq!(mat.nrows(), 10);
        assert_eq!(mat.ncols(), 10);
        assert!(mat.nnz() >= 10, "Should have at least n entries (diagonal)");
    }

    #[test]
    fn test_random_spd_symmetry() {
        let mat = random_spd(20, 0.2).expect("Failed to create random SPD");

        for (row, col, &val) in mat.iter() {
            let transpose_val = mat.get_or_zero(col, row);
            assert!(
                (val - transpose_val).abs() < 1e-10,
                "Random SPD not symmetric at ({row}, {col}): {val} vs {transpose_val}"
            );
        }
    }

    #[test]
    fn test_random_spd_positive_diagonal() {
        let mat = random_spd(15, 0.4).expect("Failed to create random SPD");

        for i in 0..mat.nrows() {
            let diag = mat.get_or_zero(i, i);
            assert!(
                diag > 0.0,
                "Random SPD diagonal at {i} should be positive, got {diag}"
            );
        }
    }

    #[test]
    fn test_random_spd_zero_density() {
        let mat = random_spd(5, 0.0).expect("Failed to create diagonal SPD");
        assert_eq!(mat.nnz(), 5, "Zero density should yield diagonal matrix");
    }

    #[test]
    fn test_random_spd_diagonally_dominant() {
        // Verify the strict diagonal dominance argument used to guarantee
        // positive definiteness: diagonal_i - sum_{j != i} |a_ij| == n for
        // every row, for several sizes and densities.
        for &(n, density) in &[(5usize, 0.1f64), (10, 0.3), (25, 0.5), (30, 0.9)] {
            let mat = random_spd(n, density).expect("Failed to create random SPD");
            for i in 0..mat.nrows() {
                let diag = mat.get_or_zero(i, i);
                let mut off_diag_sum = 0.0;
                for (col, val) in mat.row_iter(i) {
                    if col != i {
                        off_diag_sum += val.abs();
                    }
                }
                let margin = diag - off_diag_sum;
                assert!(
                    (margin - n as f64).abs() < 1e-9,
                    "random_spd({n}, {density}) row {i}: expected dominance margin {n}, got {margin}"
                );
            }
        }
    }

    #[test]
    fn test_random_spd_is_genuinely_positive_definite() {
        // The doc-comment argument (strict diagonal dominance + symmetry)
        // implies positive definiteness, but the definitive test is that a
        // Cholesky factorization actually succeeds: A = L * L^T only exists
        // (with real, positive diagonal L) when A is SPD.
        use crate::linalg::cholesky::SparseCholesky;

        for &(n, density) in &[
            (1usize, 0.0f64),
            (5, 0.0),
            (10, 0.3),
            (20, 0.2),
            (15, 0.4),
            (40, 0.6),
        ] {
            let mat = random_spd(n, density).expect("Failed to create random SPD");
            let csc = mat.to_csc();
            let chol = SparseCholesky::new(&csc);
            assert!(
                chol.is_ok(),
                "random_spd({n}, {density}) is not positive definite: Cholesky failed with {:?}",
                chol.err()
            );
        }
    }

    #[test]
    fn test_random_spd_invalid() {
        assert!(random_spd(0, 0.5).is_err());
        assert!(random_spd(5, -0.1).is_err());
        assert!(random_spd(5, 1.1).is_err());
    }

    // ========================================================================
    // poisson_1d tests
    // ========================================================================

    #[test]
    fn test_poisson_1d_basic() {
        let mat = poisson_1d(5).expect("Failed to create 1D Poisson");
        assert_eq!(mat.nrows(), 5);
        assert_eq!(mat.ncols(), 5);
        assert_eq!(mat.nnz(), 13); // 3*5 - 2
    }

    #[test]
    fn test_poisson_1d_values() {
        let mat = poisson_1d(4).expect("Failed to create 1D Poisson");

        // Check diagonal = 2
        for i in 0..4 {
            assert!((mat.get_or_zero(i, i) - 2.0).abs() < 1e-14);
        }

        // Check off-diagonal = -1
        for i in 0..3 {
            assert!((mat.get_or_zero(i, i + 1) - (-1.0)).abs() < 1e-14);
            assert!((mat.get_or_zero(i + 1, i) - (-1.0)).abs() < 1e-14);
        }
    }

    #[test]
    fn test_poisson_1d_symmetry() {
        let mat = poisson_1d(10).expect("Failed to create 1D Poisson");

        for (row, col, &val) in mat.iter() {
            let transpose_val = mat.get_or_zero(col, row);
            assert!(
                (val - transpose_val).abs() < 1e-14,
                "Poisson 1D not symmetric at ({row}, {col})"
            );
        }
    }

    #[test]
    fn test_poisson_1d_spd() {
        // Check diagonal dominance (sufficient for SPD)
        let mat = poisson_1d(10).expect("Failed to create 1D Poisson");

        for i in 0..mat.nrows() {
            let diag = mat.get_or_zero(i, i);
            let mut off_diag_sum = 0.0;
            for (col, val) in mat.row_iter(i) {
                if col != i {
                    off_diag_sum += val.abs();
                }
            }
            assert!(
                diag >= off_diag_sum,
                "Poisson 1D row {i}: diagonal {diag} < off-diagonal sum {off_diag_sum}"
            );
        }
    }

    #[test]
    fn test_poisson_1d_zero() {
        assert!(poisson_1d(0).is_err());
    }

    // ========================================================================
    // Cross-generator property tests
    // ========================================================================

    #[test]
    fn test_poisson_1d_equals_tridiagonal() {
        let poisson = poisson_1d(10).expect("poisson_1d");
        let tri = tridiagonal(10, -1.0, 2.0, -1.0).expect("tridiagonal");

        assert_eq!(poisson.nnz(), tri.nnz());
        assert_eq!(poisson.nrows(), tri.nrows());

        for i in 0..10 {
            for j in 0..10 {
                let pval = poisson.get_or_zero(i, j);
                let tval = tri.get_or_zero(i, j);
                assert!(
                    (pval - tval).abs() < 1e-14,
                    "Poisson != tridiag at ({i}, {j}): {pval} vs {tval}"
                );
            }
        }
    }

    #[test]
    fn test_laplacian_2d_1d_consistency() {
        // laplacian_2d(n, 1) should produce a similar structure to a 1D Laplacian
        // (tridiagonal with diagonal=4, off-diagonal=-1) since there's only 1 row in y
        let mat = laplacian_2d(5, 1).expect("laplacian_2d(5,1)");
        assert_eq!(mat.nrows(), 5);
        assert_eq!(mat.ncols(), 5);
        // Should be tridiagonal-like with diagonal=4
        for i in 0..5 {
            assert!((mat.get_or_zero(i, i) - 4.0).abs() < 1e-14);
        }
    }

    #[test]
    fn test_generators_produce_valid_csr() {
        // All generators should produce valid CSR matrices
        let matrices: Vec<CsrMatrix<f64>> = vec![
            laplacian_2d(4, 4).expect("lap2d"),
            laplacian_3d(3, 3, 3).expect("lap3d"),
            tridiagonal(10, -1.0, 2.0, -1.0).expect("tridiag"),
            diagonal(10, 5.0).expect("diag"),
            arrow_matrix(10).expect("arrow"),
            random_spd(10, 0.3).expect("rspd"),
            poisson_1d(10).expect("poisson"),
        ];

        for mat in &matrices {
            // Row pointers should be monotonically increasing
            let row_ptrs = mat.row_ptrs();
            for i in 1..row_ptrs.len() {
                assert!(row_ptrs[i] >= row_ptrs[i - 1], "Row pointers not monotonic");
            }

            // Column indices should be in bounds
            for &col in mat.col_indices() {
                assert!(col < mat.ncols(), "Column index out of bounds");
            }

            // nnz should match row_ptrs
            assert_eq!(
                mat.nnz(),
                row_ptrs[mat.nrows()],
                "nnz mismatch with row_ptrs"
            );
        }
    }
}