scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
//! Krylov subspace methods for dense linear systems
//!
//! This module implements classical Krylov subspace iterative solvers for
//! dense linear systems Ax = b. These complement sparse-specific solvers by
//! providing preconditioned and restarted variants for dense problems.
//!
//! ## Algorithms implemented
//!
//! - **CG (Conjugate Gradient)**: For symmetric positive definite (SPD) systems,
//!   based on the Hestenes-Stiefel 1952 three-term recurrence. Supports
//!   optional preconditioning via an arbitrary operator.
//! - **GMRES (restarted)**: For general non-symmetric systems. Uses the Arnoldi
//!   process with modified Gram-Schmidt orthogonalisation and Givens rotations
//!   to solve the projected least-squares problem.
//! - **BiCGSTAB**: For non-symmetric systems. Uses Van der Vorst's stabilization
//!   to prevent the irregular convergence behaviour of BiCG.
//! - **MINRES**: For symmetric (possibly indefinite) systems. Based on the
//!   Paige-Saunders 1975 Lanczos-based algorithm.
//!
//! Convergence criterion for all solvers: `‖r‖ / ‖b‖ < tol`.

use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use std::iter::Sum;

use crate::error::{LinalgError, LinalgResult};
use crate::validation::{validate_linear_system, validate_iteration_parameters};

// ─────────────────────────────────────────────────────────────────────────────
// Result type
// ─────────────────────────────────────────────────────────────────────────────

/// Result returned by every Krylov solver.
#[derive(Debug, Clone)]
pub struct IterativeSolveResult<T> {
    /// Approximate solution vector.
    pub x: Array1<T>,
    /// Number of iterations performed.
    pub iterations: usize,
    /// Euclidean norm of the final residual.
    pub residual_norm: T,
    /// Whether the tolerance was reached before `max_iter`.
    pub converged: bool,
}

// ─────────────────────────────────────────────────────────────────────────────
// Internal helpers
// ─────────────────────────────────────────────────────────────────────────────

/// Matrix-vector product  y = A · x.
fn matvec<F>(a: &Array2<F>, x: &Array1<F>) -> Array1<F>
where
    F: Float + NumAssign + Sum + ScalarOperand,
{
    let n = a.nrows();
    let mut y = Array1::zeros(n);
    for i in 0..n {
        let mut acc = F::zero();
        for j in 0..a.ncols() {
            acc += a[[i, j]] * x[j];
        }
        y[i] = acc;
    }
    y
}

/// Euclidean norm of a vector.
#[inline]
fn norm2<F>(v: &Array1<F>) -> F
where
    F: Float + NumAssign + Sum + ScalarOperand,
{
    let sq: F = v.iter().map(|&x| x * x).fold(F::zero(), |a, b| a + b);
    sq.sqrt()
}

/// Dot product of two vectors.
#[inline]
fn dot<F>(a: &Array1<F>, b: &Array1<F>) -> F
where
    F: Float + NumAssign + Sum + ScalarOperand,
{
    a.iter().zip(b.iter()).map(|(&x, &y)| x * y).fold(F::zero(), |acc, v| acc + v)
}

// ─────────────────────────────────────────────────────────────────────────────
// Conjugate Gradient
// ─────────────────────────────────────────────────────────────────────────────

/// Conjugate Gradient solver for symmetric positive definite systems.
///
/// Implements the classic Hestenes-Stiefel (1952) three-term recurrence with
/// optional left preconditioning. For a preconditioned solve the system solved
/// is conceptually `M⁻¹ A x = M⁻¹ b` where `preconditioner(v)` approximates
/// `M⁻¹ v`.
///
/// # Arguments
///
/// * `a` – Square symmetric positive definite matrix.
/// * `b` – Right-hand side vector.
/// * `x0` – Optional initial guess. Defaults to zero.
/// * `tol` – Relative residual tolerance: `‖r‖/‖b‖ < tol`.
/// * `max_iter` – Maximum iteration count.
/// * `preconditioner` – Optional preconditioning operator `M⁻¹`.
///
/// # Returns
///
/// An [`IterativeSolveResult`] containing the solution and convergence info.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::iterative::conjugate_gradient;
///
/// let a = array![[4.0_f64, 1.0], [1.0, 3.0]];
/// let b = array![1.0_f64, 2.0];
/// let result = conjugate_gradient(&a, &b.view(), None, 1e-12, 100, None)
///     .expect("CG failed");
/// assert!(result.converged);
/// ```
pub fn conjugate_gradient<F>(
    a: &Array2<F>,
    b: &ArrayView1<F>,
    x0: Option<&Array1<F>>,
    tol: F,
    max_iter: usize,
    preconditioner: Option<&dyn Fn(&Array1<F>) -> Array1<F>>,
) -> LinalgResult<IterativeSolveResult<F>>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Debug,
{
    validate_linear_system(&a.view(), b, "Conjugate Gradient")?;
    validate_iteration_parameters(max_iter, tol, "Conjugate Gradient")?;

    let n = a.nrows();

    // Initial guess
    let mut x: Array1<F> = match x0 {
        Some(x_init) => {
            if x_init.len() != n {
                return Err(LinalgError::DimensionError(format!(
                    "Initial guess length {} does not match system dimension {}",
                    x_init.len(),
                    n
                )));
            }
            x_init.clone()
        }
        None => Array1::zeros(n),
    };

    // Compute ‖b‖ for relative tolerance
    let b_norm = norm2(&b.to_owned());
    if b_norm <= F::epsilon() {
        // b is zero → x = 0 is the solution
        return Ok(IterativeSolveResult {
            x: Array1::zeros(n),
            iterations: 0,
            residual_norm: F::zero(),
            converged: true,
        });
    }

    let abs_tol = tol * b_norm;

    // r = b - A·x
    let ax = matvec(a, &x);
    let mut r: Array1<F> = Array1::from_iter(b.iter().zip(ax.iter()).map(|(&bi, &axi)| bi - axi));

    // Preconditioned direction z = M⁻¹ r
    let mut z: Array1<F> = match &preconditioner {
        Some(m_inv) => m_inv(&r),
        None => r.clone(),
    };

    let mut p = z.clone();
    let mut rz_old = dot(&r, &z);

    let mut residual_norm = norm2(&r);

    if residual_norm <= abs_tol {
        return Ok(IterativeSolveResult {
            x,
            iterations: 0,
            residual_norm,
            converged: true,
        });
    }

    let mut iter_count = 0usize;

    for iter in 0..max_iter {
        iter_count = iter + 1;

        let ap = matvec(a, &p);
        let pap = dot(&p, &ap);

        if pap.abs() < F::epsilon() {
            // Breakdown: p is in the null space of A
            break;
        }

        let alpha = rz_old / pap;

        // x = x + α·p
        for i in 0..n {
            x[i] += alpha * p[i];
        }

        // r = r - α·A·p
        for i in 0..n {
            r[i] -= alpha * ap[i];
        }

        residual_norm = norm2(&r);

        if residual_norm <= abs_tol {
            return Ok(IterativeSolveResult {
                x,
                iterations: iter_count,
                residual_norm,
                converged: true,
            });
        }

        // z = M⁻¹ r
        z = match &preconditioner {
            Some(m_inv) => m_inv(&r),
            None => r.clone(),
        };

        let rz_new = dot(&r, &z);
        let beta = rz_new / rz_old;

        // p = z + β·p
        for i in 0..n {
            p[i] = z[i] + beta * p[i];
        }

        rz_old = rz_new;
    }

    Ok(IterativeSolveResult {
        x,
        iterations: iter_count,
        residual_norm,
        converged: false,
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// GMRES (restarted)
// ─────────────────────────────────────────────────────────────────────────────

/// Apply a single Givens rotation in-place: `[c, s; -s, c] · [a; b]`.
fn apply_givens_rotation<F>(h: &mut [F], cs: &[F], sn: &[F], k: usize)
where
    F: Float + NumAssign,
{
    for i in 0..k {
        let h0 = h[i];
        let h1 = h[i + 1];
        h[i]     =  cs[i] * h0 + sn[i] * h1;
        h[i + 1] = -sn[i] * h0 + cs[i] * h1;
    }
}

/// Compute Givens coefficients to zero out the sub-diagonal of h[k+1, k].
fn compute_givens<F>(h_k: F, h_k1: F) -> (F, F)
where
    F: Float,
{
    let t = (h_k * h_k + h_k1 * h_k1).sqrt();
    if t < F::epsilon() {
        (F::one(), F::zero())
    } else {
        (h_k / t, h_k1 / t)
    }
}

/// Restarted GMRES solver for general (non-symmetric) linear systems.
///
/// Uses the Arnoldi process with modified Gram-Schmidt orthogonalization and
/// Givens rotations to solve the projected least-squares problem. The outer
/// loop restarts the Krylov basis every `restart` steps.
///
/// # Arguments
///
/// * `a` – Square matrix (need not be symmetric).
/// * `b` – Right-hand side vector.
/// * `x0` – Optional initial guess. Defaults to zero.
/// * `tol` – Relative residual tolerance: `‖r‖/‖b‖ < tol`.
/// * `max_iter` – Maximum *outer* restart iterations.
/// * `restart` – Krylov subspace dimension before restart (Arnoldi restart).
///
/// # Returns
///
/// An [`IterativeSolveResult`] containing the solution and convergence info.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::iterative::gmres;
///
/// let a = array![[3.0_f64, 1.0], [1.0, 4.0]];
/// let b = array![5.0_f64, 6.0];
/// let result = gmres(&a, &b.view(), None, 1e-12, 50, 10)
///     .expect("GMRES failed");
/// assert!(result.converged);
/// ```
pub fn gmres<F>(
    a: &Array2<F>,
    b: &ArrayView1<F>,
    x0: Option<&Array1<F>>,
    tol: F,
    max_iter: usize,
    restart: usize,
) -> LinalgResult<IterativeSolveResult<F>>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Debug,
{
    validate_linear_system(&a.view(), b, "GMRES")?;
    validate_iteration_parameters(max_iter, tol, "GMRES")?;

    if restart == 0 {
        return Err(LinalgError::InvalidInputError(
            "GMRES restart parameter must be positive".to_string(),
        ));
    }

    let n = a.nrows();

    let mut x: Array1<F> = match x0 {
        Some(x_init) => {
            if x_init.len() != n {
                return Err(LinalgError::DimensionError(format!(
                    "Initial guess length {} does not match system dimension {}",
                    x_init.len(),
                    n
                )));
            }
            x_init.clone()
        }
        None => Array1::zeros(n),
    };

    let b_norm = norm2(&b.to_owned());
    if b_norm <= F::epsilon() {
        return Ok(IterativeSolveResult {
            x: Array1::zeros(n),
            iterations: 0,
            residual_norm: F::zero(),
            converged: true,
        });
    }

    let abs_tol = tol * b_norm;
    let mut total_iters = 0usize;
    let mut residual_norm = F::zero();

    'outer: for outer in 0..max_iter {
        let _ = outer; // suppress unused warning

        // r = b - A·x
        let ax = matvec(a, &x);
        let r: Array1<F> = Array1::from_iter(b.iter().zip(ax.iter()).map(|(&bi, &axi)| bi - axi));
        let beta = norm2(&r);

        residual_norm = beta;
        if beta <= abs_tol {
            return Ok(IterativeSolveResult {
                x,
                iterations: total_iters,
                residual_norm,
                converged: true,
            });
        }

        // Krylov basis Q (columns are basis vectors)
        let m = restart;
        let mut q: Vec<Array1<F>> = Vec::with_capacity(m + 1);

        // q₀ = r / ‖r‖
        let q0: Array1<F> = r.mapv(|v| v / beta);
        q.push(q0);

        // Upper Hessenberg matrix H (stored as column-major vectors of length m+1)
        let mut hess: Vec<Vec<F>> = Vec::with_capacity(m);

        // Givens rotation sines / cosines
        let mut cs: Vec<F> = Vec::with_capacity(m);
        let mut sn: Vec<F> = Vec::with_capacity(m);

        // Right-hand side of least-squares: e₁ · β
        let mut g: Vec<F> = Vec::with_capacity(m + 1);
        g.push(beta);

        let mut inner_iters = 0usize;

        for j in 0..m {
            total_iters += 1;

            // w = A · q[j]
            let mut w = matvec(a, &q[j]);

            // Modified Gram-Schmidt orthogonalisation
            let mut h_col: Vec<F> = vec![F::zero(); j + 2];
            for (i, qi) in q.iter().enumerate().take(j + 1) {
                let h_ij = dot(&w, qi);
                h_col[i] = h_ij;
                for k in 0..n {
                    w[k] -= h_ij * qi[k];
                }
            }
            let h_j1j = norm2(&w);
            h_col[j + 1] = h_j1j;

            // Apply previous Givens rotations to h_col[0..j+1]
            apply_givens_rotation(&mut h_col, &cs, &sn, j);

            // Compute new Givens rotation to zero h_col[j+1]
            let (c, s) = compute_givens(h_col[j], h_col[j + 1]);
            cs.push(c);
            sn.push(s);

            // Apply new rotation to h_col and g
            h_col[j] = c * h_col[j] + s * h_col[j + 1];
            h_col[j + 1] = F::zero();

            // Extend g
            let g_j = g[j];
            g.push(-sn[j] * g_j);
            g[j] = cs[j] * g_j;

            hess.push(h_col);

            residual_norm = g[j + 1].abs();
            inner_iters = j + 1;

            // Add next Krylov vector if not the last step and h_j1j > 0
            if h_j1j > F::epsilon() && j < m - 1 {
                let q_next: Array1<F> = w.mapv(|v| v / h_j1j);
                q.push(q_next);
            } else if h_j1j <= F::epsilon() {
                // Lucky breakdown – exact solution found in Krylov space
                break;
            } else {
                // Last iteration of this cycle
                let q_next: Array1<F> = w.mapv(|v| v / h_j1j);
                q.push(q_next);
            }

            if residual_norm <= abs_tol {
                break;
            }
        }

        // Back-substitution to compute y from upper triangular R (stored in hess)
        // R y = g[0..inner_iters]
        let mut y: Vec<F> = vec![F::zero(); inner_iters];
        for i in (0..inner_iters).rev() {
            let mut sum = g[i];
            for k in (i + 1)..inner_iters {
                sum -= hess[k][i] * y[k];
            }
            let diag = hess[i][i];
            if diag.abs() < F::epsilon() {
                y[i] = F::zero();
            } else {
                y[i] = sum / diag;
            }
        }

        // Update solution x += Q[:, 0..m] · y
        for (i, yi) in y.iter().enumerate() {
            for k in 0..n {
                x[k] += *yi * q[i][k];
            }
        }

        if residual_norm <= abs_tol {
            return Ok(IterativeSolveResult {
                x,
                iterations: total_iters,
                residual_norm,
                converged: true,
            });
        }

        // Check outer iteration limit
        if total_iters >= max_iter * restart {
            break 'outer;
        }
    }

    Ok(IterativeSolveResult {
        x,
        iterations: total_iters,
        residual_norm,
        converged: false,
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// BiCGSTAB
// ─────────────────────────────────────────────────────────────────────────────

/// BiCGSTAB solver for general non-symmetric linear systems.
///
/// Implements Van der Vorst's (1992) BiConjugate Gradient Stabilized algorithm.
/// Compared to BiCG it avoids the erratic convergence behaviour by applying a
/// second stabilization polynomial. Works well for mildly non-symmetric and
/// non-Hermitian systems.
///
/// # Arguments
///
/// * `a` – Square matrix.
/// * `b` – Right-hand side vector.
/// * `x0` – Optional initial guess. Defaults to zero.
/// * `tol` – Relative residual tolerance: `‖r‖/‖b‖ < tol`.
/// * `max_iter` – Maximum number of BiCGSTAB steps.
///
/// # Returns
///
/// An [`IterativeSolveResult`] containing the solution and convergence info.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::iterative::bicgstab;
///
/// let a = array![[5.0_f64, 1.0], [1.0, 4.0]];
/// let b = array![6.0_f64, 5.0];
/// let result = bicgstab(&a, &b.view(), None, 1e-12, 100)
///     .expect("BiCGSTAB failed");
/// assert!(result.converged);
/// ```
pub fn bicgstab<F>(
    a: &Array2<F>,
    b: &ArrayView1<F>,
    x0: Option<&Array1<F>>,
    tol: F,
    max_iter: usize,
) -> LinalgResult<IterativeSolveResult<F>>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Debug,
{
    validate_linear_system(&a.view(), b, "BiCGSTAB")?;
    validate_iteration_parameters(max_iter, tol, "BiCGSTAB")?;

    let n = a.nrows();

    let mut x: Array1<F> = match x0 {
        Some(x_init) => {
            if x_init.len() != n {
                return Err(LinalgError::DimensionError(format!(
                    "Initial guess length {} does not match system dimension {}",
                    x_init.len(),
                    n
                )));
            }
            x_init.clone()
        }
        None => Array1::zeros(n),
    };

    let b_norm = norm2(&b.to_owned());
    if b_norm <= F::epsilon() {
        return Ok(IterativeSolveResult {
            x: Array1::zeros(n),
            iterations: 0,
            residual_norm: F::zero(),
            converged: true,
        });
    }

    let abs_tol = tol * b_norm;

    // r = b - A·x
    let ax = matvec(a, &x);
    let r: Array1<F> = Array1::from_iter(b.iter().zip(ax.iter()).map(|(&bi, &axi)| bi - axi));

    let mut residual_norm = norm2(&r);
    if residual_norm <= abs_tol {
        return Ok(IterativeSolveResult {
            x,
            iterations: 0,
            residual_norm,
            converged: true,
        });
    }

    // r̂ = r (arbitrary, conventionally the initial residual)
    let r_hat = r.clone();
    let mut r = r;

    let mut p: Array1<F> = r.clone();
    let mut rho_old = F::one();
    let mut alpha = F::one();
    let mut omega = F::one();
    let mut v: Array1<F> = Array1::zeros(n);

    let mut iter_count = 0usize;

    for iter in 0..max_iter {
        iter_count = iter + 1;

        let rho_new = dot(&r_hat, &r);

        if rho_new.abs() < F::epsilon() {
            // Breakdown: r̂ and r are orthogonal, restart with new r̂
            // For simplicity, report non-convergence at this point
            break;
        }

        let beta = (rho_new / rho_old) * (alpha / omega);

        // p = r + β·(p - ω·v)
        for i in 0..n {
            p[i] = r[i] + beta * (p[i] - omega * v[i]);
        }

        v = matvec(a, &p);
        let r_hat_v = dot(&r_hat, &v);

        if r_hat_v.abs() < F::epsilon() {
            break;
        }

        alpha = rho_new / r_hat_v;

        // s = r - α·v
        let mut s: Array1<F> = Array1::zeros(n);
        for i in 0..n {
            s[i] = r[i] - alpha * v[i];
        }

        residual_norm = norm2(&s);
        if residual_norm <= abs_tol {
            // x = x + α·p
            for i in 0..n {
                x[i] += alpha * p[i];
            }
            return Ok(IterativeSolveResult {
                x,
                iterations: iter_count,
                residual_norm,
                converged: true,
            });
        }

        let t = matvec(a, &s);
        let t_norm_sq: F = t.iter().map(|&ti| ti * ti).fold(F::zero(), |acc, v| acc + v);

        omega = if t_norm_sq < F::epsilon() {
            F::zero()
        } else {
            dot(&t, &s) / t_norm_sq
        };

        // x = x + α·p + ω·s
        for i in 0..n {
            x[i] += alpha * p[i] + omega * s[i];
        }

        // r = s - ω·t
        for i in 0..n {
            r[i] = s[i] - omega * t[i];
        }

        residual_norm = norm2(&r);

        if residual_norm <= abs_tol {
            return Ok(IterativeSolveResult {
                x,
                iterations: iter_count,
                residual_norm,
                converged: true,
            });
        }

        if omega.abs() < F::epsilon() {
            break;
        }

        rho_old = rho_new;
    }

    Ok(IterativeSolveResult {
        x,
        iterations: iter_count,
        residual_norm,
        converged: false,
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// MINRES
// ─────────────────────────────────────────────────────────────────────────────

/// MINRES solver for symmetric (possibly indefinite) linear systems.
///
/// Based on the Paige-Saunders (1975) Lanczos-based MINRES algorithm.
/// Unlike CG, MINRES does not require positive definiteness and minimises
/// ‖r_k‖₂ over the Krylov subspace at each step.
///
/// # Arguments
///
/// * `a` – Square symmetric matrix (may be indefinite).
/// * `b` – Right-hand side vector.
/// * `x0` – Optional initial guess. Defaults to zero.
/// * `tol` – Relative residual tolerance: `‖r‖/‖b‖ < tol`.
/// * `max_iter` – Maximum number of MINRES steps.
///
/// # Returns
///
/// An [`IterativeSolveResult`] containing the solution and convergence info.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::iterative::minres;
///
/// // Symmetric indefinite system
/// let a = array![[2.0_f64, 1.0], [1.0, -1.0]];
/// let b = array![3.0_f64, 0.0];
/// let result = minres(&a, &b.view(), None, 1e-10, 100)
///     .expect("MINRES failed");
/// assert!(result.converged);
/// ```
pub fn minres<F>(
    a: &Array2<F>,
    b: &ArrayView1<F>,
    x0: Option<&Array1<F>>,
    tol: F,
    max_iter: usize,
) -> LinalgResult<IterativeSolveResult<F>>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Debug,
{
    validate_linear_system(&a.view(), b, "MINRES")?;
    validate_iteration_parameters(max_iter, tol, "MINRES")?;

    let n = a.nrows();

    let x_init: Array1<F> = match x0 {
        Some(xi) => {
            if xi.len() != n {
                return Err(LinalgError::DimensionError(format!(
                    "Initial guess length {} does not match system dimension {}",
                    xi.len(),
                    n
                )));
            }
            xi.clone()
        }
        None => Array1::zeros(n),
    };

    let b_norm = norm2(&b.to_owned());
    if b_norm <= F::epsilon() {
        return Ok(IterativeSolveResult {
            x: Array1::zeros(n),
            iterations: 0,
            residual_norm: F::zero(),
            converged: true,
        });
    }

    let abs_tol = tol * b_norm;

    // Initialise Lanczos recurrence
    // r = b - A·x0
    let ax0 = matvec(a, &x_init);
    let r1: Array1<F> = Array1::from_iter(b.iter().zip(ax0.iter()).map(|(&bi, &axi)| bi - axi));

    let mut beta1 = norm2(&r1);
    let mut residual_norm = beta1;

    if residual_norm <= abs_tol {
        return Ok(IterativeSolveResult {
            x: x_init,
            iterations: 0,
            residual_norm,
            converged: true,
        });
    }

    // Normalise first Lanczos vector
    let mut v_old: Array1<F> = Array1::zeros(n);
    let mut v: Array1<F> = r1.mapv(|vi| vi / beta1);

    // Solution update vectors: MINRES needs w_{k}, w_{k-1}, w_{k-2}
    // w_old = w_{k-2}, w_cur = w_{k-1}, w_new = w_k (computed each iteration)
    let mut w_old: Array1<F> = Array1::zeros(n);
    let mut w_cur: Array1<F> = Array1::zeros(n);

    let mut x = x_init;

    // Lanczos scalars
    let mut beta = beta1;
    // Givens rotation history: we maintain two previous rotation pairs
    let mut c_old = F::one();   // c_{k-2}
    let mut s_old = F::zero();  // s_{k-2}
    let mut c = F::one();       // c_{k-1}
    let mut s = F::zero();      // s_{k-1}

    // QR factorization tracking
    let mut rho_bar: F;
    let mut phi_bar = beta1;
    let mut delta_bar: F;

    let mut iter_count = 0usize;

    for iter in 0..max_iter {
        iter_count = iter + 1;

        // ── Lanczos step ────────────────────────────────────────────────────
        let av = matvec(a, &v);
        let alpha = dot(&v, &av);

        // v_new = A·v - alpha·v - beta·v_old
        let mut v_new: Array1<F> = Array1::zeros(n);
        for i in 0..n {
            v_new[i] = av[i] - alpha * v[i] - beta * v_old[i];
        }

        let beta_new = norm2(&v_new);

        // ── Apply previous two Givens rotations to new Hessenberg column ───
        // The 3-element column from the Lanczos recurrence for step k is:
        //   [beta_{k-1}, alpha_k, beta_k]  (tridiagonal)
        // We need to apply rotations G_{k-2} and G_{k-1} before computing G_k.

        // Apply G_{k-2}
        let eps1 = s_old * beta;
        let delta1 = c_old * beta;

        // Apply G_{k-1}
        let eps2 = s * delta1;
        delta_bar = -c * delta1;
        rho_bar = c * alpha - s * eps2;
        let _rho_bar_orig = rho_bar; // for debugging

        // Now compute G_k to zero out beta_new
        let gamma = (rho_bar * rho_bar + beta_new * beta_new).sqrt();
        let gamma_safe = if gamma < F::epsilon() { F::epsilon() } else { gamma };

        let c_new = rho_bar / gamma_safe;
        let s_new = beta_new / gamma_safe;

        // ── Solution update ─────────────────────────────────────────────────
        let phi = c_new * phi_bar;
        phi_bar = s_new * phi_bar;

        // w_new = (v - eps1 * w_old - delta_bar * w_cur) / gamma_safe
        // This is the proper three-term recurrence for the w vectors
        let mut w_new: Array1<F> = Array1::zeros(n);
        for i in 0..n {
            w_new[i] = (v[i] - eps1 * w_old[i] - delta_bar * w_cur[i]) / gamma_safe;
        }

        // x = x + phi * w_new
        for i in 0..n {
            x[i] = x[i] + phi * w_new[i];
        }

        residual_norm = phi_bar.abs();

        if residual_norm <= abs_tol {
            return Ok(IterativeSolveResult {
                x,
                iterations: iter_count,
                residual_norm,
                converged: true,
            });
        }

        // ── Breakdown check ─────────────────────────────────────────────────
        if beta_new < F::epsilon() {
            // Lanczos breakdown: A-invariant subspace found.
            // The current iterate is the best we can get.
            break;
        }

        // ── Advance recurrence ──────────────────────────────────────────────
        v_old = v;
        v = v_new.mapv(|vi| vi / beta_new);

        w_old = w_cur;
        w_cur = w_new;

        beta = beta_new;
        c_old = c;
        s_old = s;
        c = c_new;
        s = s_new;
    }

    Ok(IterativeSolveResult {
        x,
        iterations: iter_count,
        residual_norm,
        converged: false,
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::array;

    // ── helper: compute ‖A·x - b‖ ──
    fn residual_norm_check(a: &Array2<f64>, x: &Array1<f64>, b: &[f64]) -> f64 {
        let n = a.nrows();
        let mut norm = 0.0_f64;
        for i in 0..n {
            let mut ai_x = 0.0_f64;
            for j in 0..n {
                ai_x += a[[i, j]] * x[j];
            }
            let diff = ai_x - b[i];
            norm += diff * diff;
        }
        norm.sqrt()
    }

    // ════════════════════════════════════════════════════
    // CG tests
    // ════════════════════════════════════════════════════

    #[test]
    fn test_cg_simple_2x2() {
        // [[4,1],[1,3]] x = [1,2] → x = [1/11, 7/11]
        let a = array![[4.0_f64, 1.0], [1.0, 3.0]];
        let b = array![1.0_f64, 2.0];
        let result = conjugate_gradient(&a, &b.view(), None, 1e-12, 100, None)
            .expect("CG must succeed");
        assert!(result.converged, "CG should converge on SPD 2×2");
        assert!(
            residual_norm_check(&a, &result.x, &[1.0, 2.0]) < 1e-10,
            "CG residual too large"
        );
    }

    #[test]
    fn test_cg_identity() {
        // I·x = b → x = b
        let a = Array2::eye(4);
        let b = array![1.0_f64, 2.0, 3.0, 4.0];
        let result = conjugate_gradient(&a, &b.view(), None, 1e-12, 100, None)
            .expect("CG on identity must succeed");
        assert!(result.converged);
        for i in 0..4 {
            assert_relative_eq!(result.x[i], b[i], epsilon = 1e-10);
        }
    }

    #[test]
    fn test_cg_diagonal_spd() {
        // Diagonal SPD system
        let mut a = Array2::zeros((5, 5));
        for i in 0..5 {
            a[[i, i]] = (i as f64 + 1.0) * 2.0;
        }
        let b = array![2.0_f64, 4.0, 6.0, 8.0, 10.0];
        let result = conjugate_gradient(&a, &b.view(), None, 1e-12, 100, None)
            .expect("CG on diagonal SPD must succeed");
        assert!(result.converged);
        for i in 0..5 {
            assert_relative_eq!(result.x[i], 1.0, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_cg_with_initial_guess() {
        let a = array![[4.0_f64, 1.0], [1.0, 3.0]];
        let b = array![1.0_f64, 2.0];
        let x0 = array![0.5_f64, 0.5];
        let result = conjugate_gradient(&a, &b.view(), Some(&x0), 1e-12, 100, None)
            .expect("CG with initial guess must succeed");
        assert!(result.converged);
        assert!(residual_norm_check(&a, &result.x, &[1.0, 2.0]) < 1e-10);
    }

    #[test]
    fn test_cg_with_diagonal_preconditioner() {
        // Jacobi preconditioner: M⁻¹ v = v / diag(A)
        let a = array![[10.0_f64, 1.0], [1.0, 5.0]];
        let b = array![11.0_f64, 6.0];
        let diag = vec![10.0_f64, 5.0];
        let precond = move |v: &Array1<f64>| -> Array1<f64> {
            Array1::from_iter(v.iter().zip(diag.iter()).map(|(&vi, &di)| vi / di))
        };
        let result = conjugate_gradient(&a, &b.view(), None, 1e-12, 100, Some(&precond))
            .expect("Preconditioned CG must succeed");
        assert!(result.converged);
        for i in 0..2 {
            assert_relative_eq!(result.x[i], 1.0, epsilon = 1e-9);
        }
    }

    #[test]
    fn test_cg_zero_rhs() {
        let a = array![[4.0_f64, 1.0], [1.0, 3.0]];
        let b = array![0.0_f64, 0.0];
        let result = conjugate_gradient(&a, &b.view(), None, 1e-12, 100, None)
            .expect("CG zero rhs must succeed");
        assert!(result.converged);
        assert_relative_eq!(result.x[0], 0.0, epsilon = 1e-14);
        assert_relative_eq!(result.x[1], 0.0, epsilon = 1e-14);
    }

    #[test]
    fn test_cg_3x3_spd() {
        // Tridiagonal SPD: [[4,-1,0],[-1,4,-1],[0,-1,4]]
        let a = array![
            [4.0_f64, -1.0, 0.0],
            [-1.0, 4.0, -1.0],
            [0.0, -1.0, 4.0]
        ];
        let b = array![1.0_f64, 0.0, 1.0];
        let result = conjugate_gradient(&a, &b.view(), None, 1e-12, 100, None)
            .expect("CG 3×3 must succeed");
        assert!(result.converged);
        assert!(residual_norm_check(&a, &result.x, &[1.0, 0.0, 1.0]) < 1e-10);
    }

    #[test]
    fn test_cg_convergence_iterations() {
        // CG converges in at most n steps for n-dim SPD (exact arithmetic)
        let n = 5usize;
        let mut a = Array2::zeros((n, n));
        for i in 0..n {
            a[[i, i]] = 4.0;
            if i > 0 {
                a[[i, i - 1]] = -1.0;
                a[[i - 1, i]] = -1.0;
            }
        }
        let b = Array1::from_iter((0..n).map(|i| i as f64 + 1.0));
        let result = conjugate_gradient(&a, &b.view(), None, 1e-10, 50, None)
            .expect("CG must succeed");
        assert!(result.converged);
        assert!(result.iterations <= n + 1, "CG should converge in ≤ n steps");
    }

    // ════════════════════════════════════════════════════
    // GMRES tests
    // ════════════════════════════════════════════════════

    #[test]
    fn test_gmres_symmetric() {
        let a = array![[3.0_f64, 1.0], [1.0, 4.0]];
        let b = array![5.0_f64, 6.0];
        let result = gmres(&a, &b.view(), None, 1e-12, 50, 10)
            .expect("GMRES must succeed");
        assert!(result.converged);
        assert!(residual_norm_check(&a, &result.x, &[5.0, 6.0]) < 1e-9);
    }

    #[test]
    fn test_gmres_nonsymmetric() {
        // Non-symmetric matrix
        let a = array![[4.0_f64, 2.0], [1.0, 3.0]];
        let b = array![8.0_f64, 5.0];
        let result = gmres(&a, &b.view(), None, 1e-12, 50, 10)
            .expect("GMRES non-symmetric must succeed");
        assert!(result.converged);
        assert!(residual_norm_check(&a, &result.x, &[8.0, 5.0]) < 1e-9);
    }

    #[test]
    fn test_gmres_identity() {
        let a: Array2<f64> = Array2::eye(3);
        let b = array![1.0_f64, 2.0, 3.0];
        let result = gmres(&a, &b.view(), None, 1e-12, 50, 5)
            .expect("GMRES identity must succeed");
        assert!(result.converged);
        for i in 0..3 {
            assert_relative_eq!(result.x[i], b[i], epsilon = 1e-10);
        }
    }

    #[test]
    fn test_gmres_restart_parameter() {
        // Test with small restart = 2 (forces multiple outer iterations)
        let a = array![[4.0_f64, 1.0, 0.0], [1.0, 3.0, 1.0], [0.0, 1.0, 5.0]];
        let b = array![5.0_f64, 5.0, 6.0];
        let result = gmres(&a, &b.view(), None, 1e-10, 50, 2)
            .expect("GMRES with small restart must succeed");
        assert!(result.converged || result.residual_norm < 1e-8);
    }

    #[test]
    fn test_gmres_zero_rhs() {
        let a = array![[2.0_f64, 1.0], [1.0, 3.0]];
        let b = array![0.0_f64, 0.0];
        let result = gmres(&a, &b.view(), None, 1e-12, 50, 5)
            .expect("GMRES zero rhs must succeed");
        assert!(result.converged);
    }

    #[test]
    fn test_gmres_upper_triangular() {
        // Upper triangular non-symmetric
        let a = array![[2.0_f64, 3.0], [0.0, 4.0]];
        let b = array![11.0_f64, 8.0];
        let result = gmres(&a, &b.view(), None, 1e-12, 50, 5)
            .expect("GMRES upper-triangular must succeed");
        assert!(result.converged);
        assert!(residual_norm_check(&a, &result.x, &[11.0, 8.0]) < 1e-9);
    }

    // ════════════════════════════════════════════════════
    // BiCGSTAB tests
    // ════════════════════════════════════════════════════

    #[test]
    fn test_bicgstab_symmetric() {
        let a = array![[4.0_f64, 1.0], [1.0, 3.0]];
        let b = array![1.0_f64, 2.0];
        let result = bicgstab(&a, &b.view(), None, 1e-12, 100)
            .expect("BiCGSTAB must succeed");
        assert!(result.converged);
        assert!(residual_norm_check(&a, &result.x, &[1.0, 2.0]) < 1e-10);
    }

    #[test]
    fn test_bicgstab_nonsymmetric() {
        let a = array![[5.0_f64, 2.0], [1.0, 4.0]];
        let b = array![12.0_f64, 9.0];
        let result = bicgstab(&a, &b.view(), None, 1e-12, 100)
            .expect("BiCGSTAB non-symmetric must succeed");
        assert!(result.converged);
        assert!(residual_norm_check(&a, &result.x, &[12.0, 9.0]) < 1e-9);
    }

    #[test]
    fn test_bicgstab_3x3() {
        let a = array![
            [6.0_f64, 2.0, 1.0],
            [2.0, 5.0, 1.0],
            [1.0, 1.0, 4.0]
        ];
        let b = array![9.0_f64, 8.0, 6.0];
        let result = bicgstab(&a, &b.view(), None, 1e-12, 100)
            .expect("BiCGSTAB 3×3 must succeed");
        assert!(result.converged);
        assert!(residual_norm_check(&a, &result.x, &[9.0, 8.0, 6.0]) < 1e-9);
    }

    #[test]
    fn test_bicgstab_identity() {
        let a: Array2<f64> = Array2::eye(4);
        let b = array![1.0_f64, 2.0, 3.0, 4.0];
        let result = bicgstab(&a, &b.view(), None, 1e-12, 100)
            .expect("BiCGSTAB identity must succeed");
        assert!(result.converged);
        for i in 0..4 {
            assert_relative_eq!(result.x[i], b[i], epsilon = 1e-10);
        }
    }

    #[test]
    fn test_bicgstab_with_initial_guess() {
        let a = array![[4.0_f64, 1.0], [1.0, 3.0]];
        let b = array![1.0_f64, 2.0];
        let x0 = array![0.2_f64, 0.6];
        let result = bicgstab(&a, &b.view(), Some(&x0), 1e-12, 100)
            .expect("BiCGSTAB with initial guess must succeed");
        assert!(result.converged);
        assert!(residual_norm_check(&a, &result.x, &[1.0, 2.0]) < 1e-10);
    }

    #[test]
    fn test_bicgstab_zero_rhs() {
        let a = array![[3.0_f64, 1.0], [1.0, 2.0]];
        let b = array![0.0_f64, 0.0];
        let result = bicgstab(&a, &b.view(), None, 1e-12, 100)
            .expect("BiCGSTAB zero rhs must succeed");
        assert!(result.converged);
    }

    // ════════════════════════════════════════════════════
    // MINRES tests
    // ════════════════════════════════════════════════════

    #[test]
    fn test_minres_spd() {
        // MINRES should also work on SPD systems
        let a = array![[4.0_f64, 1.0], [1.0, 3.0]];
        let b = array![1.0_f64, 2.0];
        let result = minres(&a, &b.view(), None, 1e-10, 200)
            .expect("MINRES on SPD must succeed");
        assert!(result.converged || result.residual_norm < 1e-8,
            "MINRES residual {}", result.residual_norm);
    }

    #[test]
    fn test_minres_symmetric_indefinite() {
        // Symmetric indefinite 2×2
        let a = array![[2.0_f64, 1.0], [1.0, -1.0]];
        let b = array![3.0_f64, 0.0];
        let result = minres(&a, &b.view(), None, 1e-10, 200)
            .expect("MINRES symmetric indefinite must succeed");
        // Verify residual quality
        let res = residual_norm_check(&a, &result.x, &[3.0, 0.0]);
        assert!(res < 1e-8 || result.converged, "MINRES residual {res}");
    }

    #[test]
    fn test_minres_identity() {
        let a: Array2<f64> = Array2::eye(3);
        let b = array![2.0_f64, 4.0, 6.0];
        let result = minres(&a, &b.view(), None, 1e-12, 100)
            .expect("MINRES identity must succeed");
        assert!(result.converged);
        for i in 0..3 {
            assert_relative_eq!(result.x[i], b[i], epsilon = 1e-9);
        }
    }

    #[test]
    fn test_minres_zero_rhs() {
        let a = array![[2.0_f64, 1.0], [1.0, 3.0]];
        let b = array![0.0_f64, 0.0];
        let result = minres(&a, &b.view(), None, 1e-12, 100)
            .expect("MINRES zero rhs must succeed");
        assert!(result.converged);
    }

    #[test]
    fn test_minres_diagonal() {
        let mut a = Array2::<f64>::zeros((4, 4));
        a[[0, 0]] = 2.0;
        a[[1, 1]] = -3.0;
        a[[2, 2]] = 4.0;
        a[[3, 3]] = -1.0;
        let b = array![2.0_f64, -3.0, 4.0, -1.0];
        let result = minres(&a, &b.view(), None, 1e-10, 100)
            .expect("MINRES diagonal indefinite must succeed");
        let res = residual_norm_check(&a, &result.x, &[2.0, -3.0, 4.0, -1.0]);
        assert!(res < 1e-8 || result.converged, "MINRES residual {res}");
    }

    // ════════════════════════════════════════════════════
    // Cross-solver comparison tests
    // ════════════════════════════════════════════════════

    #[test]
    fn test_all_solvers_agree_on_spd() {
        // All solvers should agree on an SPD system
        let a = array![[5.0_f64, 2.0], [2.0, 4.0]];
        let b = array![9.0_f64, 8.0];

        let cg = conjugate_gradient(&a, &b.view(), None, 1e-12, 100, None)
            .expect("CG failed");
        let gm = gmres(&a, &b.view(), None, 1e-12, 50, 10)
            .expect("GMRES failed");
        let bi = bicgstab(&a, &b.view(), None, 1e-12, 100)
            .expect("BiCGSTAB failed");

        assert!(cg.converged && gm.converged && bi.converged,
            "All solvers should converge: CG={} GMRES={} BiCGSTAB={}",
            cg.converged, gm.converged, bi.converged);

        for i in 0..2 {
            assert_relative_eq!(cg.x[i], gm.x[i], epsilon = 1e-8);
            assert_relative_eq!(cg.x[i], bi.x[i], epsilon = 1e-8);
        }
    }

    #[test]
    fn test_invalid_initial_guess_dimension() {
        let a = array![[4.0_f64, 1.0], [1.0, 3.0]];
        let b = array![1.0_f64, 2.0];
        let x0 = array![0.5_f64, 0.5, 0.5]; // wrong dimension
        let result = conjugate_gradient(&a, &b.view(), Some(&x0), 1e-12, 100, None);
        assert!(result.is_err(), "Should error on wrong x0 dimension");
    }

    #[test]
    fn test_minres_lanczos_breakdown() {
        // A system where Lanczos might break down early
        // (identity matrix: Lanczos generates exact solution in 1 step)
        let a: Array2<f64> = Array2::eye(3);
        let b = array![1.0_f64, 2.0, 3.0];
        let result = minres(&a, &b.view(), None, 1e-12, 100)
            .expect("MINRES on identity must succeed");
        // Should converge very quickly
        assert!(result.converged || result.iterations <= 3,
            "MINRES on identity: iters={} converged={}", result.iterations, result.converged);
    }

    #[test]
    fn test_minres_3x3_symmetric_indefinite() {
        // Larger symmetric indefinite system
        let a = array![
            [3.0_f64, 1.0, 0.0],
            [1.0, -2.0, 1.0],
            [0.0, 1.0, 4.0]
        ];
        let b = array![4.0_f64, 0.0, 5.0];
        let result = minres(&a, &b.view(), None, 1e-10, 200)
            .expect("MINRES 3x3 indefinite must succeed");
        let res = residual_norm_check(&a, &result.x, &[4.0, 0.0, 5.0]);
        assert!(res < 1e-6 || result.converged,
            "MINRES 3x3 indefinite residual {res}");
    }
}