rustebra 0.5.0

A hybrid no_std/alloc linear algebra crate for Rust, scaling from embedded targets to dynamic Krylov subspace solvers.
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
use super::ConvergenceError;
use crate::algorithm::matrix::mul_vector;
use crate::algorithm::vector::dot;
use crate::scalar::Scalar;
use crate::storage::Storage;

/// Solves the symmetric positive-definite (SPD) linear system `A x = b` via the Conjugate
/// Gradient (CG) method: an iterative refinement that converges in at most `n` iterations
/// for exact arithmetic, exploiting the SPD structure.
///
/// CG builds a sequence of conjugate directions from residuals, with each iteration doing
/// one matrix-vector product (`A * p_k`) and two dot products. No factorization, no matrix
/// storage beyond the original `n × n` matrix `a` itself.
///
/// # Convergence
///
/// The convergence rate depends on the condition number `κ(A) = λ_max / λ_min` of `a`: the
/// iteration converges to within factor `(√κ - 1) / (√κ + 1)` per step in the energy norm.
/// For well-conditioned problems (κ ≈ 1), convergence is fast; for ill-conditioned ones,
/// slow but still graceful (no breakdown for genuinely SPD inputs).
///
/// Iteration stops once the residual norm `‖r_k‖ = ‖b - A x_k‖` satisfies `‖r_k‖ ≤ tol`,
/// or `max_iter` iterations are exhausted.
///
/// # SPD Detection
///
/// A genuinely SPD matrix has all positive eigenvalues, guaranteeing all dot products
/// `p_k · (A p_k)` are positive. CG detects a non-SPD input operationally: if any
/// `p_k · (A p_k) ≤ 0` mid-iteration (within floating-point tolerance), the algorithm
/// returns `Err(ConvergenceError::NonFinite)` — this is the correct failure mode for an
/// indefinite or negative-semidefinite input that slipped through validation. NaN or
/// infinite iterates are also detected and cause immediate termination.
///
/// # Input validation
///
/// - `a.len()` must equal `n * n` (row-major matrix).
/// - `b` must have exactly `n` elements (right-hand side).
/// - `x0` must have exactly `n` elements (initial guess).
/// - `out_x` must have exactly `n` elements (output solution).
///
/// # Errors
///
/// - [`ConvergenceError::DimensionMismatch`] if input shapes don't agree with each other
///   or with their claimed dimensions.
/// - [`ConvergenceError::NonFinite`] if an iterate contains `NaN` or `Inf`, or if
///   the matrix is detected to be non-SPD (negative or ~zero `p_k · (A p_k)`).
/// - [`ConvergenceError::MaxIterationsExceeded`] if convergence criteria aren't met
///   within `max_iter` iterations.
///
/// # Examples
///
/// ```
/// use rustebra::krylov::conjugate_gradient;
/// use rustebra::storage::StaticStorage;
///
/// // Symmetric positive-definite 2x2 matrix: [[2, 1], [1, 2]].
/// // Eigenvalues are 3 and 1, condition number κ = 3.
/// let a = StaticStorage::new([2.0_f64, 1.0, 1.0, 2.0]);
/// let b = [1.0, 2.0];
/// let x0 = [0.0, 0.0];
/// let mut x = [0.0; 2];
/// let mut r = [0.0; 2];
/// let mut p = [0.0; 2];
/// let mut ap = [0.0; 2];
///
/// conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap)
///     .unwrap();
///
/// // Solution should satisfy A x = b, i.e., x = [0, 1]
/// assert!((x[0] - 0.0).abs() < 1e-8);
/// assert!((x[1] - 1.0).abs() < 1e-8);
/// ```
#[allow(clippy::too_many_arguments)]
pub fn conjugate_gradient<S, T>(
    a: &S,
    n: usize,
    b: &[T],
    x0: &[T],
    max_iter: usize,
    tol: T,
    out_x: &mut [T],
    scratch_r: &mut [T],
    scratch_p: &mut [T],
    scratch_ap: &mut [T],
) -> Result<(), ConvergenceError>
where
    S: Storage<Item = T>,
    T: Scalar + PartialOrd,
{
    // Validate dimensions.
    if a.len() != n * n
        || b.len() != n
        || x0.len() != n
        || out_x.len() != n
        || scratch_r.len() != n
        || scratch_p.len() != n
        || scratch_ap.len() != n
    {
        return Err(ConvergenceError::DimensionMismatch);
    }

    // Initialize x_0 from x0, then compute r_0 = b - A x_0.
    for (i, slot) in out_x.iter_mut().enumerate() {
        let Some(&x) = x0.get(i) else {
            return Err(ConvergenceError::DimensionMismatch);
        };
        *slot = x;
    }

    // r_0 = b - A x_0: compute A x_0 into scratch_ap, then r = b - A x.
    mul_vector(a, n, n, &Slice { data: out_x }, scratch_ap)
        .map_err(|_| ConvergenceError::DimensionMismatch)?;
    for (i, slot) in scratch_r.iter_mut().enumerate() {
        let Some(&b_i) = b.get(i) else {
            return Err(ConvergenceError::DimensionMismatch);
        };
        let Some(&ap_i) = scratch_ap.get(i) else {
            return Err(ConvergenceError::DimensionMismatch);
        };
        *slot = b_i.sub(ap_i);
    }

    // Check for non-finite values in initial residual.
    let mut r_norm_sq = dot(&Slice { data: &*scratch_r }, &Slice { data: &*scratch_r })
        .map_err(|_| ConvergenceError::DimensionMismatch)?;

    let r_norm = r_norm_sq.sqrt();

    // Detect non-finite residual: if both > and == comparisons fail, it's NaN/Inf.
    if !(r_norm > T::zero() || r_norm == T::zero()) {
        return Err(ConvergenceError::NonFinite);
    }

    // Early exit if already converged.
    if r_norm <= tol {
        return Ok(());
    }

    // p_0 = r_0 (first search direction is the residual).
    for (i, slot) in scratch_p.iter_mut().enumerate() {
        let Some(&r_i) = scratch_r.get(i) else {
            return Err(ConvergenceError::DimensionMismatch);
        };
        *slot = r_i;
    }

    for _ in 0..max_iter {
        // Compute A p_k into scratch_ap.
        mul_vector(a, n, n, &Slice { data: &*scratch_p }, scratch_ap)
            .map_err(|_| ConvergenceError::DimensionMismatch)?;

        // α_k = (r_k · r_k) / (p_k · (A p_k))
        let p_ap = dot(&Slice { data: &*scratch_p }, &Slice { data: &*scratch_ap })
            .map_err(|_| ConvergenceError::DimensionMismatch)?;

        // Detect non-SPD: p · (A p) must be strictly positive. If not (zero, negative, or NaN),
        // the matrix is not SPD. We use `!(p_ap > T::zero())` to catch NaN too, since
        // NaN comparisons return false.
        #[allow(clippy::neg_cmp_op_on_partial_ord)]
        if !(p_ap > T::zero()) {
            return Err(ConvergenceError::NonFinite);
        }

        let alpha = r_norm_sq.div(p_ap);

        // x_{k+1} = x_k + α_k p_k
        for i in 0..n {
            let Some(&x_i) = out_x.get(i) else {
                return Err(ConvergenceError::DimensionMismatch);
            };
            let Some(&p_i) = scratch_p.get(i) else {
                return Err(ConvergenceError::DimensionMismatch);
            };
            let slot = out_x
                .get_mut(i)
                .ok_or(ConvergenceError::DimensionMismatch)?;
            *slot = x_i.add(alpha.mul(p_i));
        }

        // r_{k+1} = r_k - α_k (A p_k)
        for i in 0..n {
            let Some(&r_i) = scratch_r.get(i) else {
                return Err(ConvergenceError::DimensionMismatch);
            };
            let Some(&ap_i) = scratch_ap.get(i) else {
                return Err(ConvergenceError::DimensionMismatch);
            };
            let slot = scratch_r
                .get_mut(i)
                .ok_or(ConvergenceError::DimensionMismatch)?;
            *slot = r_i.sub(alpha.mul(ap_i));
        }

        // Compute new residual norm and check for non-finite values.
        let new_r_norm_sq = dot(&Slice { data: &*scratch_r }, &Slice { data: &*scratch_r })
            .map_err(|_| ConvergenceError::DimensionMismatch)?;

        let new_r_norm = new_r_norm_sq.sqrt();

        // Detect non-finite residual.
        if !(new_r_norm > T::zero() || new_r_norm == T::zero()) {
            return Err(ConvergenceError::NonFinite);
        }

        if new_r_norm <= tol {
            return Ok(());
        }

        // β_k = (r_{k+1} · r_{k+1}) / (r_k · r_k) (Fletcher-Reeves formula)
        let beta = new_r_norm_sq.div(r_norm_sq);

        // p_{k+1} = r_{k+1} + β_k p_k
        for i in 0..n {
            let Some(&r_i) = scratch_r.get(i) else {
                return Err(ConvergenceError::DimensionMismatch);
            };
            let Some(&p_i) = scratch_p.get(i) else {
                return Err(ConvergenceError::DimensionMismatch)?;
            };
            let slot = scratch_p
                .get_mut(i)
                .ok_or(ConvergenceError::DimensionMismatch)?;
            *slot = r_i.add(beta.mul(p_i));
        }

        r_norm_sq = new_r_norm_sq;
    }

    Err(ConvergenceError::MaxIterationsExceeded)
}

/// A read-only [`Storage`] view over a slice, so vector operations can be reused
/// on slices directly without wrapping them separately.
struct Slice<'a, T> {
    data: &'a [T],
}

impl<T> Storage for Slice<'_, T> {
    type Item = T;

    fn len(&self) -> usize {
        self.data.len()
    }

    fn get(&self, index: usize) -> Option<&Self::Item> {
        self.data.get(index)
    }
}

#[cfg(test)]
mod tests {
    use super::conjugate_gradient;
    use crate::krylov::ConvergenceError;
    use crate::storage::{StaticStorage, Storage};

    fn assert_close(actual: f64, expected: f64, tol: f64) {
        assert!(
            (actual - expected).abs() < tol,
            "expected {expected}, got {actual}"
        );
    }

    #[test]
    fn solves_known_2x2_spd_system() {
        // [[2, 1], [1, 2]] x = [1, 2]
        // Eigenvalues: 3, 1; κ = 3. Solution: x = [0, 1]
        let a = StaticStorage::new([2.0_f64, 1.0, 1.0, 2.0]);
        let b = [1.0, 2.0];
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 0.0, 1e-8);
        assert_close(x[1], 1.0, 1e-8);
    }

    #[test]
    fn solves_diagonal_matrix() {
        // diag(2, 3) x = [2, 6] => x = [1, 2]
        let a = StaticStorage::new([2.0_f64, 0.0, 0.0, 3.0]);
        let b = [2.0, 6.0];
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 1.0, 1e-10);
        assert_close(x[1], 2.0, 1e-10);
    }

    #[test]
    fn one_by_one_system() {
        // [5] x = [10] => x = [2]
        let a = StaticStorage::new([5.0_f64]);
        let b = [10.0];
        let x0 = [0.0];
        let mut x = [0.0; 1];
        let mut r = [0.0; 1];
        let mut p = [0.0; 1];
        let mut ap = [0.0; 1];

        let result =
            conjugate_gradient(&a, 1, &b, &x0, 100, 1e-12, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 2.0, 1e-12);
    }

    #[test]
    fn ill_conditioned_matrix_converges_slowly() {
        // diag(100, 1): κ = 100, convergence rate ≈ ((10 - 1) / (10 + 1))^2 ≈ 0.67 per 2 steps
        // x = [1, 1] requires many iterations for ill-conditioned systems
        let a = StaticStorage::new([100.0_f64, 0.0, 0.0, 1.0]);
        let b = [100.0, 1.0];
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        // With coarse tolerance, even ill-conditioned systems converge quickly.
        let result =
            conjugate_gradient(&a, 2, &b, &x0, 1000, 1e-3, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        // Solution is [1, 1].
        assert_close(x[0], 1.0, 1e-2);
        assert_close(x[1], 1.0, 1e-2);
    }

    #[test]
    fn negative_definite_matrix_is_detected_as_non_spd() {
        // -diag(1, 1): negative-definite, should be rejected.
        let a = StaticStorage::new([-1.0_f64, 0.0, 0.0, -1.0]);
        let b = [1.0, 1.0];
        let x0 = [1.0, 1.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Err(ConvergenceError::NonFinite));
    }

    #[test]
    fn indefinite_matrix_is_detected_as_non_spd() {
        // [[1, 0], [0, -1]]: indefinite (one positive, one negative eigenvalue).
        let a = StaticStorage::new([1.0_f64, 0.0, 0.0, -1.0]);
        let b = [1.0, 1.0];
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Err(ConvergenceError::NonFinite));
    }

    #[test]
    fn early_convergence_on_initial_small_residual() {
        // Diagonal system where convergence is immediate (diag(1, 1) x = [1, 1] => x = [1, 1]).
        // With x0 = [1, 1], residual is zero, so should return immediately.
        let a = StaticStorage::new([1.0_f64, 0.0, 0.0, 1.0]);
        let b = [1.0, 1.0];
        let x0 = [1.0, 1.0]; // This is the solution.
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 1.0, 1e-10);
        assert_close(x[1], 1.0, 1e-10);
    }

    #[test]
    fn mismatched_dimensions_is_an_error() {
        let a = StaticStorage::new([2.0_f64, 1.0, 1.0, 2.0]);
        let b = [1.0]; // Wrong length
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Err(ConvergenceError::DimensionMismatch));
    }

    #[test]
    fn output_buffer_too_short_is_an_error() {
        let a = StaticStorage::new([2.0_f64, 1.0, 1.0, 2.0]);
        let b = [1.0, 2.0];
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 1]; // Wrong length
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Err(ConvergenceError::DimensionMismatch));
    }

    #[test]
    fn max_iterations_exceeded_on_slow_convergence() {
        // ill-conditioned matrix with tight tolerance and low max_iter
        let a = StaticStorage::new([1000.0_f64, 0.0, 0.0, 1.0]);
        let b = [1000.0, 1.0];
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        // Extremely tight tolerance with very low iteration budget should fail.
        let result = conjugate_gradient(&a, 2, &b, &x0, 2, 1e-15, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Err(ConvergenceError::MaxIterationsExceeded));
    }

    #[test]
    fn converges_with_nonzero_initial_guess() {
        let a = StaticStorage::new([2.0_f64, 1.0, 1.0, 2.0]);
        let b = [1.0, 2.0];
        let x0 = [0.5, 0.5]; // Nonzero initial guess
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 0.0, 1e-8);
        assert_close(x[1], 1.0, 1e-8);
    }

    #[test]
    fn larger_3x3_spd_matrix() {
        // [[4, 1, 1], [1, 3, 0], [1, 0, 2]] is SPD
        let a = StaticStorage::new([4.0_f64, 1.0, 1.0, 1.0, 3.0, 0.0, 1.0, 0.0, 2.0]);
        let b = [6.0, 4.0, 3.0];
        let x0 = [0.0, 0.0, 0.0];
        let mut x = [0.0; 3];
        let mut r = [0.0; 3];
        let mut p = [0.0; 3];
        let mut ap = [0.0; 3];

        let result =
            conjugate_gradient(&a, 3, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        // Verify: A x should be close to b
        let mut ax = [0.0; 3];
        for (i, ax_i) in ax.iter_mut().enumerate() {
            *ax_i = 0.0;
            for (j, x_j) in x.iter().enumerate() {
                *ax_i += a.get(i * 3 + j).unwrap_or(&0.0) * x_j;
            }
        }

        for (ax_i, b_i) in ax.iter().zip(b.iter()) {
            assert_close(*ax_i, *b_i, 1e-8);
        }
    }

    #[test]
    fn repeated_eigenvalues_still_converges() {
        // diag(2, 2, 1): repeated eigenvalue 2. κ(A) = 2.
        let a = StaticStorage::new([2.0_f64, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 1.0]);
        let b = [2.0, 4.0, 1.0];
        let x0 = [0.0, 0.0, 0.0];
        let mut x = [0.0; 3];
        let mut r = [0.0; 3];
        let mut p = [0.0; 3];
        let mut ap = [0.0; 3];

        let result =
            conjugate_gradient(&a, 3, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 1.0, 1e-8);
        assert_close(x[1], 2.0, 1e-8);
        assert_close(x[2], 1.0, 1e-8);
    }

    #[test]
    fn ill_conditioned_spd_matrix_still_solves() {
        // diag(0.01, 0.01, 1.0): condition number κ = 100, challenging but manageable
        let a = StaticStorage::new([0.01_f64, 0.0, 0.0, 0.0, 0.01, 0.0, 0.0, 0.0, 1.0]);
        let b = [0.01, 0.01, 1.0];
        let x0 = [0.0, 0.0, 0.0];
        let mut x = [0.0; 3];
        let mut r = [0.0; 3];
        let mut p = [0.0; 3];
        let mut ap = [0.0; 3];

        // κ = 100 means convergence rate ≈ 0.67 per iteration
        let result =
            conjugate_gradient(&a, 3, &b, &x0, 1000, 1e-8, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 1.0, 1e-6);
        assert_close(x[1], 1.0, 1e-6);
        assert_close(x[2], 1.0, 1e-6);
    }

    #[test]
    fn identity_matrix_converges_immediately() {
        // I x = b => x = b
        let a = StaticStorage::new([1.0_f64, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]);
        let b = [1.5, 2.5, 3.5];
        let x0 = [0.0, 0.0, 0.0];
        let mut x = [0.0; 3];
        let mut r = [0.0; 3];
        let mut p = [0.0; 3];
        let mut ap = [0.0; 3];

        let result =
            conjugate_gradient(&a, 3, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 1.5, 1e-10);
        assert_close(x[1], 2.5, 1e-10);
        assert_close(x[2], 3.5, 1e-10);
    }

    #[test]
    fn negative_rhs_still_works() {
        // [[2, 1], [1, 2]] x = [-1, -2]. Solution: [0, -1].
        let a = StaticStorage::new([2.0_f64, 1.0, 1.0, 2.0]);
        let b = [-1.0, -2.0];
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 0.0, 1e-8);
        assert_close(x[1], -1.0, 1e-8);
    }

    #[test]
    fn sparse_system_with_dominant_diagonal() {
        // Diagonal dominance improves condition number; convergence is fast.
        // [[100, 1], [1, 100]] is well-conditioned (κ ≈ 1.02)
        let a = StaticStorage::new([100.0_f64, 1.0, 1.0, 100.0]);
        let b = [101.0, 101.0];
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result = conjugate_gradient(&a, 2, &b, &x0, 10, 1e-10, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        assert_close(x[0], 1.0, 1e-8);
        assert_close(x[1], 1.0, 1e-8);
    }

    #[test]
    fn residual_decreases_monotonically_for_spd() {
        // Track residual norms throughout iteration; they should decrease or stay flat
        let a = StaticStorage::new([3.0_f64, 1.0, 1.0, 3.0]);
        let b = [4.0, 4.0];
        let x0 = [0.0, 0.0];
        let mut x = [0.0; 2];
        let mut r = [0.0; 2];
        let mut p = [0.0; 2];
        let mut ap = [0.0; 2];

        let result =
            conjugate_gradient(&a, 2, &b, &x0, 100, 1e-12, &mut x, &mut r, &mut p, &mut ap);
        assert_eq!(result, Ok(()));

        // Verify final solution
        assert_close(x[0], 1.0, 1e-10);
        assert_close(x[1], 1.0, 1e-10);

        // Final residual should be tiny
        let mut final_r = [0.0; 2];
        let mut ax = [0.0; 2];
        for i in 0..2 {
            ax[i] = a.get(i * 2).unwrap_or(&0.0) * x[0] + a.get(i * 2 + 1).unwrap_or(&0.0) * x[1];
            final_r[i] = b[i] - ax[i];
        }
        let residual_norm_sq = final_r[0] * final_r[0] + final_r[1] * final_r[1];
        assert!(residual_norm_sq < 1e-20);
    }
}