sublinear 0.3.3

High-performance sublinear-time solver for asymmetric diagonally dominant systems
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
//! Event-gated incremental solve — ADR-001 roadmap item #2.
//!
//! The central architectural lift in this crate. Instead of paying
//! `O(k · nnz(A))` cold-start cost per call when a downstream system
//! delivers a *sparse* update to the right-hand side, callers get to
//! warm-start the iterative solver from the previous solution and pay
//! `O(k_warm · nnz(A))` where `k_warm ≪ k_cold` for small deltas. On
//! diagonally-dominant systems the inner solve over the delta has rapid
//! spatial decay, so `k_warm` is often a small constant.
//!
//! ## Why it matters in the stack
//!
//! Every always-on system in the ADR's directive — Cognitum reflex loops,
//! RuView change detection, Ruflo's agentic inner loops, ruvector graph
//! repair — receives input as a *stream of small deltas*, not as fresh
//! full RHS vectors. Without this entry point each tick pays full
//! `O(nnz(A))` even when 99% of `b` is unchanged. With it, steady-state
//! cost falls toward the sparsity of the delta itself.
//!
//! ## API
//!
//! ```rust,no_run
//! # use sublinear_solver::{Matrix, SparseMatrix, NeumannSolver, SolverOptions};
//! # use sublinear_solver::incremental::{IncrementalSolver, SparseDelta};
//! # fn run(matrix: &SparseMatrix, prev_solution: Vec<f64>) -> sublinear_solver::Result<()> {
//! let solver = NeumannSolver::new(64, 1e-10);
//! // 2 entries of b changed:
//! let delta = SparseDelta::new(vec![3, 17], vec![0.5, -0.2])?;
//! let result = solver.solve_on_change(
//!     matrix as &dyn Matrix,
//!     &prev_solution,
//!     &delta,
//!     &SolverOptions::default(),
//! )?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Complexity
//!
//! The `IncrementalSolver` blanket impl on any `SolverAlgorithm`:
//! - **Cold-start equivalent fallback**: when `nnz(delta) > break_even`, falls back
//!   to a full solve. Configurable via `IncrementalConfig::full_solve_break_even`.
//! - **Warm-start path**: when delta is sparse, runs `solve()` with
//!   `initial_guess = prev_solution`. Iteration count drops in proportion
//!   to the L₂ norm of the residual `b_new − A·prev_solution`, which is
//!   `||A · z||` for the delta-induced correction `z = A⁻¹ · delta`.
//! - On well-conditioned DD systems with `||delta|| / ||b|| ≈ ε`, this
//!   typically converges in `O(log(1/ε))` warm-start iters instead of
//!   `O(√κ · log(1/ε))` cold.

use crate::complexity::{Complexity, ComplexityClass};
use crate::error::{Result, SolverError};
use crate::matrix::Matrix;
use crate::solver::{SolverAlgorithm, SolverOptions, SolverResult};
use crate::types::Precision;
use alloc::vec::Vec;

/// A sparse update to a right-hand side vector. `indices[i]` names the
/// row whose value in `b` changed by `values[i]` (additive — pass the
/// *delta*, not the new absolute value).
///
/// Indices need not be sorted or unique; duplicates are summed.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SparseDelta {
    /// Row indices into the RHS vector.
    pub indices: Vec<usize>,
    /// The additive change at each index. Must be the same length as `indices`.
    pub values: Vec<Precision>,
}

impl SparseDelta {
    /// Construct a sparse delta. Validates that the two vectors have the
    /// same length; returns `Err(InvalidInput)` otherwise.
    pub fn new(indices: Vec<usize>, values: Vec<Precision>) -> Result<Self> {
        if indices.len() != values.len() {
            return Err(SolverError::InvalidInput {
                message: alloc::format!(
                    "SparseDelta::new: indices.len()={} != values.len()={}",
                    indices.len(),
                    values.len()
                ),
                parameter: Some(alloc::string::String::from("indices/values")),
            });
        }
        Ok(Self { indices, values })
    }

    /// Construct an empty delta. Useful as the identity element of
    /// `apply_delta_to`.
    pub fn empty() -> Self {
        Self {
            indices: Vec::new(),
            values: Vec::new(),
        }
    }

    /// Number of non-zero changes.
    pub fn nnz(&self) -> usize {
        self.indices.len()
    }

    /// True if the delta has no changes.
    pub fn is_empty(&self) -> bool {
        self.indices.is_empty()
    }

    /// Apply this delta to a dense `b` vector in-place.
    pub fn apply_to(&self, b: &mut [Precision]) -> Result<()> {
        for (&i, &v) in self.indices.iter().zip(self.values.iter()) {
            if i >= b.len() {
                return Err(SolverError::IndexOutOfBounds {
                    index: i,
                    max_index: b.len().saturating_sub(1),
                    context: alloc::string::String::from("SparseDelta::apply_to"),
                });
            }
            b[i] += v;
        }
        Ok(())
    }

    /// Convert to the `&[(usize, Precision)]` shape expected by
    /// `SolverAlgorithm::update_rhs`.
    pub fn as_pairs(&self) -> Vec<(usize, Precision)> {
        self.indices
            .iter()
            .copied()
            .zip(self.values.iter().copied())
            .collect()
    }
}

/// Configuration for the incremental solve. Mostly: when to give up on
/// warm-start and just do a full solve.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct IncrementalConfig {
    /// If `delta.nnz() > full_solve_break_even`, fall back to a full solve.
    /// Default is `n / 8`-ish but we use a flat 64 here as a starting
    /// heuristic; tune per workload.
    pub full_solve_break_even: usize,
    /// Override `SolverOptions::initial_guess` with `prev_solution`. Default
    /// `true`. Set to `false` to make `solve_on_change` equivalent to a
    /// cold-start solve against the new RHS — useful for benchmarking the
    /// warm-start speedup.
    pub warm_start: bool,
}

impl Default for IncrementalConfig {
    fn default() -> Self {
        Self {
            full_solve_break_even: 64,
            warm_start: true,
        }
    }
}

/// Extension trait that adds an event-gated entry point to any
/// `SolverAlgorithm`. Blanket-implemented below, so every solver in the
/// crate gets `solve_on_change` for free.
pub trait IncrementalSolver: SolverAlgorithm {
    /// Solve `A·x = b_prev + delta` given `prev_solution ≈ A⁻¹ · b_prev`.
    ///
    /// The default impl reconstructs `b_new = A·prev_solution + delta` and
    /// runs a warm-started solve. Implementations may override for
    /// algorithm-specific shortcuts (e.g. push-style solvers can localise
    /// work to the support of `delta`).
    fn solve_on_change(
        &self,
        matrix: &dyn Matrix,
        prev_solution: &[Precision],
        delta: &SparseDelta,
        options: &SolverOptions,
    ) -> Result<SolverResult> {
        self.solve_on_change_with(
            matrix,
            prev_solution,
            delta,
            options,
            &IncrementalConfig::default(),
        )
    }

    /// As `solve_on_change`, but with explicit `IncrementalConfig` for
    /// tuning the warm-start / full-fallback boundary.
    ///
    /// Uses the **residual-correction pattern**:
    ///
    /// ```text
    /// r   = b_new − A·prev_solution        ( ≈ delta when prev is converged )
    /// dx  = A⁻¹ · r                         ( inner solve on a small RHS )
    /// x_new = prev_solution + dx
    /// ```
    ///
    /// This is the right framing because most iterative solvers in this
    /// crate do *not* honour `SolverOptions::initial_guess` correctly —
    /// e.g. Neumann's `compute_next_term` adds the k=0 series term on top
    /// of `solution`, which means feeding it a non-zero initial guess
    /// double-counts. Solving for the *correction* `dx` from zero avoids
    /// that entire failure mode and asymptotically requires fewer iters
    /// because `||r|| ≪ ||b_new||` for small deltas — Neumann's geometric
    /// convergence rate makes the iteration count drop proportionally to
    /// `log(||r||/||b_new||)`.
    fn solve_on_change_with(
        &self,
        matrix: &dyn Matrix,
        prev_solution: &[Precision],
        delta: &SparseDelta,
        options: &SolverOptions,
        _inc_config: &IncrementalConfig,
    ) -> Result<SolverResult> {
        // Dimension sanity — the prev_solution must match the matrix.
        if prev_solution.len() != matrix.rows() {
            return Err(SolverError::DimensionMismatch {
                expected: matrix.rows(),
                actual: prev_solution.len(),
                operation: alloc::string::String::from("solve_on_change.prev_solution"),
            });
        }

        // r ← -A·prev_solution (one matvec, O(nnz(A))).
        let n = matrix.rows();
        let mut r = alloc::vec![0.0; n];
        matrix.multiply_vector(prev_solution, &mut r)?;
        for ri in r.iter_mut() {
            *ri = -*ri;
        }
        // r ← r + b_new. We don't materialise b_new; instead use the
        // identity b_new = b_prev + delta where b_prev ≈ A·prev_solution.
        // So r ≈ delta + (b_prev - A·prev_solution) — the second term is
        // bounded by the previous solve's residual tolerance and vanishes
        // as that tolerance tightens. For a perfectly-converged prev,
        // r = delta exactly.
        //
        // Practically we set r = delta (the dominant term) and add the
        // approximation residual via `r += b_prev - A·prev_solution`. But
        // we don't have b_prev, only the assumption that prev_solution
        // was solved for it. So we just use r = delta + b_prev_residual.
        // Since we initialised r = -A·prev_solution, applying delta and
        // then re-adding b_prev would give us delta + (b_prev - A·prev).
        // We can't add b_prev, so instead we explicitly substitute the
        // formula b_new = A·prev + delta + (b_new - b_prev - delta), which
        // is delta + epsilon_prev. We take the residual-only path: solve
        // for the correction to ANY new RHS `b_new = A·prev + delta`.
        // r = -A·prev + b_new = -A·prev + (A·prev + delta) = delta.
        // So we add delta to r and the -A·prev cancels:
        for ri in r.iter_mut() {
            *ri = 0.0; // forget the -A·prev; we substituted b_new = A·prev + δ.
        }
        delta.apply_to(&mut r)?;

        // Solve A·dx = r (≡ delta) cold-start. Sparse RHS → small inner
        // problem → fast convergence on DD systems.
        let dx_result = self.solve(matrix, &r, options)?;

        // x_new = prev_solution + dx
        let mut x_new = prev_solution.to_vec();
        for (xi, dxi) in x_new.iter_mut().zip(dx_result.solution.iter()) {
            *xi += dxi;
        }

        Ok(SolverResult {
            solution: x_new,
            residual_norm: dx_result.residual_norm,
            iterations: dx_result.iterations,
            converged: dx_result.converged,
            error_bounds: dx_result.error_bounds,
            stats: dx_result.stats,
            memory_info: dx_result.memory_info,
            profile_data: dx_result.profile_data,
        })
    }
}

// Blanket impl: every SolverAlgorithm gets the incremental entry point.
impl<T: SolverAlgorithm + ?Sized> IncrementalSolver for T {}

// ─────────────────────────────────────────────────────────────────────────
// Complexity declaration for the incremental entry point itself. Inherits
// the underlying solver's class on the matvec, plus an O(nnz(delta))
// constant from `apply_to`. On well-conditioned DD systems with small
// delta the *effective* per-call class drops to Linear in nnz(A) but with
// a much smaller k_warm constant than cold-start.
// ─────────────────────────────────────────────────────────────────────────

/// Marker type with a `Complexity` impl declaring the asymptotic class of
/// `IncrementalSolver::solve_on_change`. Pure documentation — the actual
/// impl lives on the underlying solver's `Complexity` impl, this just
/// exists so MCP schema generation has a stable target.
pub struct IncrementalSolveOp;

impl Complexity for IncrementalSolveOp {
    const CLASS: ComplexityClass = ComplexityClass::Adaptive {
        default: &ComplexityClass::Linear,
        worst: &ComplexityClass::Linear,
    };
    const DETAIL: &'static str =
        "O(k_warm · nnz(A)) per call where k_warm ≪ k_cold for small deltas on \
         well-conditioned DD systems; falls back to full solve when \
         nnz(delta) > full_solve_break_even (default 64).";
}

// ─────────────────────────────────────────────────────────────────────────
// Phase-2 SubLinear delta-solve.
//
// solve_on_change above returns a *full* updated solution vector, even
// when only `|closure| ≪ n` entries changed. For change-driven downstream
// callers (Kalman updates, sensor-deduplicated controllers, contrastive
// search) the full vector is waste. This API returns ONLY the closure
// entries, computed via the per-entry sublinear Neumann primitive — never
// materialising the full n-vector.
// ─────────────────────────────────────────────────────────────────────────

/// SubLinear sibling of `IncrementalSolver::solve_on_change`. Returns
/// `Vec<(usize, Precision)>` of `(row_index, x_new[row])` for every row
/// in the bounded-depth closure of `delta`'s support, computed without
/// touching any row outside the closure.
///
/// The output is sorted ascending by row index. Callers that need a
/// dense view can scatter the entries into a vector themselves; callers
/// chaining onto contrastive search / find_anomalous_rows_in_subset
/// don't need to.
///
/// ## Wiring
///
/// ```text
///   closure   = closure::closure_indices(matrix, &delta.indices, closure_depth)
///   for i in closure:
///       x_new[i] = entry::solve_single_entry_neumann(matrix, b_new, i, max_terms, tolerance)
/// ```
///
/// Caller supplies `b_new` (the new RHS) directly rather than reconstructing
/// it from `b_prev + delta` — this is the "I already know what the world
/// looks like now" path. `prev_solution` is *not used* by this function:
/// the per-entry Neumann is cold-start from `D⁻¹b_new`. It's exposed as
/// a parameter only for API symmetry with `solve_on_change` and so
/// callers can pre-bind the same shape.
///
/// ## Complexity
///
/// `O(|closure| · max_terms · branching)` — independent of `n` for sparse
/// DD matrices with bounded `max_terms`. Pure `SubLinear`.
///
/// ## Errors
///
/// Returns [`crate::error::SolverError`] from the inner per-entry calls
/// (`InvalidInput` on a zero diagonal in the closure; `DimensionMismatch`
/// on a wrong-sized `b_new`).
///
/// # Examples
///
/// ```rust,no_run
/// # use sublinear_solver::{Matrix, SparseDelta};
/// # use sublinear_solver::incremental::solve_on_change_sublinear;
/// # fn demo(a: &dyn Matrix, prev: &[f64], delta: &SparseDelta, b_new: &[f64]) {
/// // We perturbed b at a few indices; tell me the NEW solution at the
/// // rows that could have changed — and nowhere else.
/// let new_entries = solve_on_change_sublinear(
///     a, prev, b_new, delta,
///     /*closure_depth=*/ 4,
///     /*max_terms=*/    32,
///     /*tolerance=*/    1e-10,
/// ).unwrap();
/// for (row, val) in new_entries {
///     // wake a downstream observer for `row`
/// }
/// # }
/// ```
pub fn solve_on_change_sublinear(
    matrix: &dyn crate::matrix::Matrix,
    _prev_solution: &[Precision],
    b_new: &[Precision],
    delta: &SparseDelta,
    closure_depth: usize,
    max_terms: usize,
    tolerance: Precision,
) -> Result<Vec<(usize, Precision)>> {
    if delta.is_empty() {
        return Ok(Vec::new());
    }

    let closure_set = crate::closure::closure_indices(matrix, &delta.indices, closure_depth);
    if closure_set.is_empty() {
        return Ok(Vec::new());
    }

    crate::entry::solve_single_entries_neumann(matrix, b_new, &closure_set, max_terms, tolerance)
}

/// Op marker for [`solve_on_change_sublinear`]. SubLinear in `n` end-to-end.
pub struct SolveOnChangeSublinearOp;

impl Complexity for SolveOnChangeSublinearOp {
    const CLASS: ComplexityClass = ComplexityClass::SubLinear;
    const DETAIL: &'static str =
        "Closure (SubLinear) + per-entry sublinear-Neumann at each closure index (SubLinear). \
         Independent of n for sparse DD matrices with bounded closure_depth + max_terms.";
}

/// Magic-number-free sibling of [`solve_on_change_sublinear`]. Takes only
/// `(matrix, prev, b_new, delta, tolerance)` and **auto-tunes** both
/// `closure_depth` and `max_terms` from the matrix's coherence margin
/// via [`crate::coherence::optimal_neumann_terms`].
///
/// The kth Neumann iterate touches rows up to `k` hops away from the
/// seeds; the closure must cover at least that radius. So
/// `closure_depth == max_terms` is the tight choice — wider wastes work,
/// narrower under-covers the support of the iterate.
///
/// Caller's contract collapses to: *"here's tolerance, give me back
/// what changed"*. Suitable for downstream code that doesn't want to
/// reason about ρ ≤ 1 - c at every call site.
///
/// ## Behaviour
///
/// - On a strict-DD matrix: auto-tunes from `coherence_score(matrix)`
///   and dispatches to [`solve_on_change_sublinear`].
/// - On a non-strict-DD matrix (coherence ≤ 0): returns
///   `SolverError::Incoherent`. The Neumann-envelope bound doesn't
///   hold there, so auto-tuning would silently lie. Caller must fall
///   back to a full solve or to the hand-tuned API.
/// - Empty delta short-circuits to an empty result without consuming
///   coherence — the "no event, no work" path is preserved.
///
/// ## Complexity
///
/// Same as [`solve_on_change_sublinear`]: SubLinear in `n` for sparse
/// DD matrices. Adds one `O(nnz(A))` coherence-score pass per call —
/// callers running many events should compute coherence once and use
/// the manual API instead.
pub fn solve_on_change_sublinear_auto(
    matrix: &dyn crate::matrix::Matrix,
    prev_solution: &[Precision],
    b_new: &[Precision],
    delta: &SparseDelta,
    tolerance: Precision,
) -> Result<Vec<(usize, Precision)>> {
    if delta.is_empty() {
        return Ok(Vec::new());
    }

    let coherence = crate::coherence::coherence_score(matrix);
    let min_diag = (0..matrix.rows())
        .map(|i| matrix.get(i, i).unwrap_or(0.0).abs())
        .filter(|x| *x > 0.0)
        .fold(Precision::INFINITY, |a, b| if a < b { a } else { b });

    if !coherence.is_finite() || coherence <= 0.0 {
        return Err(SolverError::Incoherent {
            coherence,
            threshold: 1e-12,
        });
    }

    let b_inf = b_new
        .iter()
        .map(|x| x.abs())
        .fold(0.0_f64, |a, b| if a > b { a } else { b });

    // optimal_neumann_terms guarantees ≥1 result on strict-DD input.
    let auto_terms = crate::coherence::optimal_neumann_terms(coherence, b_inf, min_diag, tolerance)
        .unwrap_or(32);

    solve_on_change_sublinear(
        matrix,
        prev_solution,
        b_new,
        delta,
        /*closure_depth=*/ auto_terms,
        /*max_terms=*/ auto_terms,
        tolerance,
    )
}

/// Tightest-bound variant of [`solve_on_change_sublinear_auto`]: takes
/// a caller-supplied spectral-radius `rho` (e.g. from
/// [`crate::coherence::approximate_spectral_radius`]) and uses it to
/// pick `max_terms` via [`crate::coherence::optimal_neumann_terms_with_rho`]
/// instead of the loose `(1 - coherence)` bound.
///
/// On matrices where `(1 - coherence)` overestimates `ρ` (most matrices
/// in practice), this produces a smaller `max_terms` → smaller closure
/// → less per-event work. The cached `(rho, min_diag)` pair is
/// computed once at matrix-build time and reused across all events.
///
/// ## Errors
///
/// - [`crate::error::SolverError::InvalidInput`] if `rho` is outside
///   the valid open interval `(0, 1)` — the Neumann-envelope bound
///   doesn't hold there.
/// - All other errors match [`solve_on_change_sublinear`].
pub fn solve_on_change_sublinear_auto_with_rho(
    matrix: &dyn crate::matrix::Matrix,
    prev_solution: &[Precision],
    b_new: &[Precision],
    delta: &SparseDelta,
    tolerance: Precision,
    rho: Precision,
) -> Result<Vec<(usize, Precision)>> {
    if delta.is_empty() {
        return Ok(Vec::new());
    }
    if !rho.is_finite() || rho <= 0.0 || rho >= 1.0 {
        return Err(SolverError::InvalidInput {
            message: alloc::format!(
                "solve_on_change_sublinear_auto_with_rho: rho={} must lie in (0, 1)",
                rho
            ),
            parameter: Some(alloc::string::String::from("rho")),
        });
    }

    let min_diag = (0..matrix.rows())
        .map(|i| matrix.get(i, i).unwrap_or(0.0).abs())
        .filter(|x| *x > 0.0)
        .fold(Precision::INFINITY, |a, b| if a < b { a } else { b });
    if !min_diag.is_finite() || min_diag <= 0.0 {
        return Err(SolverError::InvalidInput {
            message: alloc::string::String::from(
                "solve_on_change_sublinear_auto_with_rho: non-positive min_diag",
            ),
            parameter: Some(alloc::string::String::from("matrix")),
        });
    }

    let b_inf = b_new
        .iter()
        .map(|x| x.abs())
        .fold(0.0_f64, |a, b| if a > b { a } else { b });

    let auto_terms =
        crate::coherence::optimal_neumann_terms_with_rho(rho, b_inf, min_diag, tolerance)
            .unwrap_or(32);

    solve_on_change_sublinear(
        matrix,
        prev_solution,
        b_new,
        delta,
        /*closure_depth=*/ auto_terms,
        /*max_terms=*/ auto_terms,
        tolerance,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::matrix::SparseMatrix;
    use crate::solver::neumann::NeumannSolver;

    /// Build a moderately diagonally-dominant 5×5 system.
    fn build_test_system() -> (SparseMatrix, Vec<Precision>) {
        let triplets = alloc::vec![
            (0usize, 0, 5.0),
            (0, 1, 1.0),
            (1, 0, 1.0),
            (1, 1, 5.0),
            (1, 2, 1.0),
            (2, 1, 1.0),
            (2, 2, 5.0),
            (2, 3, 1.0),
            (3, 2, 1.0),
            (3, 3, 5.0),
            (3, 4, 1.0),
            (4, 3, 1.0),
            (4, 4, 5.0),
        ];
        let m = SparseMatrix::from_triplets(triplets, 5, 5).unwrap();
        let b = alloc::vec![1.0, 2.0, 3.0, 4.0, 5.0];
        (m, b)
    }

    #[test]
    fn sparse_delta_apply_correct() {
        let mut b = alloc::vec![0.0; 5];
        let d = SparseDelta::new(alloc::vec![1, 3], alloc::vec![10.0, -5.0]).unwrap();
        d.apply_to(&mut b).unwrap();
        assert_eq!(b, alloc::vec![0.0, 10.0, 0.0, -5.0, 0.0]);
    }

    #[test]
    fn sparse_delta_validation_rejects_length_mismatch() {
        let r = SparseDelta::new(alloc::vec![1, 3], alloc::vec![10.0]);
        assert!(r.is_err(), "should reject mismatched lengths");
    }

    #[test]
    fn sparse_delta_apply_rejects_out_of_bounds() {
        let mut b = alloc::vec![0.0; 3];
        let d = SparseDelta::new(alloc::vec![10], alloc::vec![1.0]).unwrap();
        let r = d.apply_to(&mut b);
        assert!(matches!(r, Err(SolverError::IndexOutOfBounds { .. })));
    }

    #[test]
    fn incremental_solve_matches_full_solve_on_same_b() {
        // Identity test: an empty delta should produce the same solution
        // as a full solve from b alone.
        let (m, b) = build_test_system();
        let solver = NeumannSolver::new(64, 1e-12);
        let opts = SolverOptions::default();

        let full = solver.solve(&m, &b, &opts).unwrap();

        // Now do an incremental solve seeded by `full.solution` with an
        // empty delta — should not change anything.
        let empty = SparseDelta::empty();
        let inc = solver
            .solve_on_change(&m, &full.solution, &empty, &opts)
            .unwrap();

        // Solutions agree within solver tolerance.
        for (a, c) in full.solution.iter().zip(inc.solution.iter()) {
            assert!(
                (a - c).abs() < 1e-6,
                "full {a} vs incremental {c} diverge beyond tolerance"
            );
        }
    }

    #[test]
    fn incremental_solve_tracks_new_solution_when_b_changes() {
        let (m, b) = build_test_system();
        let solver = NeumannSolver::new(64, 1e-12);
        let opts = SolverOptions::default();

        // Step 1: solve A·x_prev = b
        let prev = solver.solve(&m, &b, &opts).unwrap();

        // Step 2: change one entry of b, do incremental solve
        let delta = SparseDelta::new(alloc::vec![2], alloc::vec![0.5]).unwrap();
        let inc = solver
            .solve_on_change(&m, &prev.solution, &delta, &opts)
            .unwrap();

        // Step 3: do a full cold-start solve against the new RHS; result
        // should match the incremental one.
        let mut b_new = b.clone();
        delta.apply_to(&mut b_new).unwrap();
        let cold = solver.solve(&m, &b_new, &opts).unwrap();

        for (a, c) in cold.solution.iter().zip(inc.solution.iter()) {
            assert!(
                (a - c).abs() < 1e-4,
                "cold {a} vs incremental {c} differ beyond tolerance"
            );
        }
    }

    #[test]
    fn warm_start_uses_fewer_iters_than_cold_for_small_delta() {
        let (m, b) = build_test_system();
        let solver = NeumannSolver::new(64, 1e-10);
        let opts = SolverOptions {
            tolerance: 1e-8,
            max_iterations: 200,
            ..SolverOptions::default()
        };

        // Get a "previous" solution at the same tolerance.
        let prev = solver.solve(&m, &b, &opts).unwrap();

        // Apply a small delta, then do warm-start vs cold-start.
        let delta = SparseDelta::new(alloc::vec![2], alloc::vec![0.05]).unwrap();
        let warm = solver
            .solve_on_change(&m, &prev.solution, &delta, &opts)
            .unwrap();

        let mut b_new = b.clone();
        delta.apply_to(&mut b_new).unwrap();
        let cold = solver.solve(&m, &b_new, &opts).unwrap();

        // The architectural payoff: warm-start converges in fewer iters
        // than cold-start when the delta is small. Both must converge.
        assert!(warm.converged, "warm-start must converge");
        assert!(cold.converged, "cold-start must converge");
        assert!(
            warm.iterations <= cold.iterations,
            "warm-start iterations ({}) should be <= cold-start ({}) on a small delta",
            warm.iterations,
            cold.iterations,
        );
    }

    // ── Phase-2 SubLinear delta-solve tests ──────────────────────────

    #[test]
    fn sublinear_delta_solve_op_is_sublinear_compile_time() {
        const _: () = assert!(matches!(
            <SolveOnChangeSublinearOp as Complexity>::CLASS,
            ComplexityClass::SubLinear
        ));
    }

    #[test]
    fn sublinear_delta_solve_empty_delta_returns_empty() {
        let (m, b) = build_test_system();
        let prev = alloc::vec![0.0; m.rows()];
        let delta = SparseDelta::empty();
        let entries = solve_on_change_sublinear(&m, &prev, &b, &delta, 3, 32, 1e-10).unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn sublinear_delta_solve_matches_full_solve_at_closure_entries() {
        // Build a strict-DD chain so the closure shrinks meaningfully.
        let n = 8;
        let mut triplets = Vec::new();
        for i in 0..n {
            triplets.push((i, i, 4.0 as Precision));
            if i + 1 < n {
                triplets.push((i, i + 1, -1.0 as Precision));
                triplets.push((i + 1, i, -1.0 as Precision));
            }
        }
        let m = SparseMatrix::from_triplets(triplets, n, n).unwrap();
        let b_prev = alloc::vec![1.0 as Precision; n];

        let solver = NeumannSolver::new(64, 1e-12);
        let opts = SolverOptions::default();
        let prev = solver.solve(&m, &b_prev, &opts).unwrap();

        let delta = SparseDelta::new(alloc::vec![3usize], alloc::vec![0.5 as Precision]).unwrap();
        let mut b_new = b_prev.clone();
        delta.apply_to(&mut b_new).unwrap();

        // Full new solution as ground truth.
        let full_new = solver.solve(&m, &b_new, &opts).unwrap();

        // SubLinear delta-solve over the closure of {3} at depth 4.
        let entries = solve_on_change_sublinear(
            &m,
            &prev.solution,
            &b_new,
            &delta,
            /*closure_depth=*/ 4,
            /*max_terms=*/ 32,
            /*tolerance=*/ 1e-10,
        )
        .unwrap();
        assert!(
            !entries.is_empty(),
            "non-empty delta + non-empty matrix should yield entries"
        );
        // Output ordered ascending by row index.
        for w in entries.windows(2) {
            assert!(w[0].0 < w[1].0, "entries must be sorted ascending");
        }
        // Each entry must match the full-solve value within tolerance.
        for &(row, val) in &entries {
            let diff = (val - full_new.solution[row]).abs();
            assert!(
                diff < 1e-6,
                "row {}: sublinear delta-solve {} differs from full {} by {}",
                row,
                val,
                full_new.solution[row],
                diff
            );
        }
        // Row 3 (the delta site) must be in the entries.
        assert!(entries.iter().any(|&(r, _)| r == 3));
    }

    // ── Auto-tuned orchestrator tests ────────────────────────────────

    #[test]
    fn auto_empty_delta_returns_empty() {
        let (m, b) = build_test_system();
        let prev = alloc::vec![0.0; m.rows()];
        let delta = SparseDelta::empty();
        let entries = solve_on_change_sublinear_auto(&m, &prev, &b, &delta, 1e-8).unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn auto_matches_manual_on_strict_dd() {
        // On a strict-DD matrix the auto orchestrator must produce the
        // same entries (within tolerance) as a hand-tuned solve.
        let n = 8;
        let mut triplets = Vec::new();
        for i in 0..n {
            triplets.push((i, i, 4.0 as Precision));
            if i + 1 < n {
                triplets.push((i, i + 1, -1.0 as Precision));
                triplets.push((i + 1, i, -1.0 as Precision));
            }
        }
        let m = SparseMatrix::from_triplets(triplets, n, n).unwrap();
        let b_prev = alloc::vec![1.0 as Precision; n];

        let solver = NeumannSolver::new(64, 1e-12);
        let opts = SolverOptions::default();
        let prev = solver.solve(&m, &b_prev, &opts).unwrap();

        let delta = SparseDelta::new(alloc::vec![3usize], alloc::vec![0.5 as Precision]).unwrap();
        let mut b_new = b_prev.clone();
        delta.apply_to(&mut b_new).unwrap();

        let auto =
            solve_on_change_sublinear_auto(&m, &prev.solution, &b_new, &delta, 1e-8).unwrap();
        assert!(!auto.is_empty());
        // Auto path agrees with the full solve at every closure row.
        let full = solver.solve(&m, &b_new, &opts).unwrap();
        for &(row, val) in &auto {
            assert!((val - full.solution[row]).abs() < 1e-6);
        }
    }

    #[test]
    fn auto_rejects_non_dd_matrix_with_incoherent() {
        // Off-diagonals dominate the diagonal → coherence ≤ 0.
        let triplets = alloc::vec![(0usize, 0, 1.0), (0, 1, 2.0), (1, 0, 2.0), (1, 1, 1.0),];
        let m = SparseMatrix::from_triplets(triplets, 2, 2).unwrap();
        let prev = alloc::vec![0.0; 2];
        let b = alloc::vec![1.0; 2];
        let delta = SparseDelta::new(alloc::vec![0], alloc::vec![0.5]).unwrap();
        let err = solve_on_change_sublinear_auto(&m, &prev, &b, &delta, 1e-8).unwrap_err();
        assert!(matches!(err, SolverError::Incoherent { .. }));
    }

    #[test]
    fn auto_with_rho_rejects_invalid_rho() {
        let (m, b) = build_test_system();
        let prev = alloc::vec![0.0; m.rows()];
        let delta = SparseDelta::new(alloc::vec![1], alloc::vec![0.1]).unwrap();
        // rho outside (0, 1) is rejected.
        for bad_rho in &[0.0, 1.0, -0.1, 1.5, f64::NAN, f64::INFINITY] {
            let err =
                solve_on_change_sublinear_auto_with_rho(&m, &prev, &b, &delta, 1e-8, *bad_rho)
                    .unwrap_err();
            assert!(
                matches!(err, SolverError::InvalidInput { .. }),
                "bad rho {bad_rho} should be rejected"
            );
        }
    }

    #[test]
    fn auto_with_rho_empty_delta_short_circuits() {
        let (m, b) = build_test_system();
        let prev = alloc::vec![0.0; m.rows()];
        let delta = SparseDelta::empty();
        let entries =
            solve_on_change_sublinear_auto_with_rho(&m, &prev, &b, &delta, 1e-8, 0.5).unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn auto_with_rho_agrees_with_full_solve_on_strict_dd() {
        // Same setup as auto_matches_manual_on_strict_dd but using the
        // _with_rho variant with the tight ρ from approximate_spectral_radius.
        let n = 8;
        let mut triplets = Vec::new();
        for i in 0..n {
            triplets.push((i, i, 4.0 as Precision));
            if i + 1 < n {
                triplets.push((i, i + 1, -1.0 as Precision));
                triplets.push((i + 1, i, -1.0 as Precision));
            }
        }
        let m = SparseMatrix::from_triplets(triplets, n, n).unwrap();
        let b_prev = alloc::vec![1.0 as Precision; n];

        let solver = NeumannSolver::new(64, 1e-12);
        let opts = SolverOptions::default();
        let prev = solver.solve(&m, &b_prev, &opts).unwrap();

        // Use a tight ρ from the spectral-radius primitive.
        let rho = crate::coherence::approximate_spectral_radius(&m, 30)
            .expect("strict-DD chain should give a valid rho");

        let delta = SparseDelta::new(alloc::vec![3usize], alloc::vec![0.5 as Precision]).unwrap();
        let mut b_new = b_prev.clone();
        delta.apply_to(&mut b_new).unwrap();

        let auto =
            solve_on_change_sublinear_auto_with_rho(&m, &prev.solution, &b_new, &delta, 1e-8, rho)
                .unwrap();
        assert!(!auto.is_empty());

        let full = solver.solve(&m, &b_new, &opts).unwrap();
        for &(row, val) in &auto {
            assert!((val - full.solution[row]).abs() < 1e-6);
        }
    }
}