basin 1.5.0

Numerical optimization in pure Rust, with pluggable linear-algebra backends and WASM support.
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
//! Trust-region (Newton) minimization.
//!
//! [`TrustRegion`] is a general unconstrained minimizer over the
//! second-order model `m(p) = f + gᵀp + ½ pᵀ B p`, where `g = ∇f(x)` and
//! `B = ∇²f(x)` are supplied by the problem's
//! [`Gradient`](crate::core::problem::Gradient) and [`Hessian`](crate::core::problem::Hessian) impls. Each iteration approximately
//! minimizes `m` over a ball `‖p‖ ≤ Δ`, compares the achieved reduction to
//! the model's prediction, and grows or shrinks the radius `Δ` accordingly
//! (Nocedal & Wright, *Numerical Optimization*, 2e, Algorithm 4.1).
//!
//! The trust-region *subproblem*, the constrained quadratic minimization,
//! is solved by a pluggable strategy implementing the crate-internal
//! `Subproblem` seam. Three ship today, mirroring the standard textbook
//! set:
//!
//! - [`Steihaug`]: truncated conjugate gradient (the default). Matrix-free
//!   (needs only Hessian-vector products), handles indefinite `B` by
//!   following a negative-curvature direction to the boundary. The
//!   large-scale-capable workhorse; runs on every backend.
//! - [`Dogleg`]: the Cauchy and Newton dogleg path (N&W eq. 4.16). Needs a
//!   Cholesky solve for the Newton step, so it requires a backend with
//!   [`LinearSolveSpd`](crate::core::math::LinearSolveSpd); it falls back to
//!   the Cauchy point when `B` is not positive definite.
//! - [`CauchyPoint`]: the steepest-descent-to-boundary closed form (N&W
//!   eq. 4.11–4.12). A bulletproof baseline with only linear convergence;
//!   mostly a reference strategy.
//!
//! The solver runs in one of two modes, selected by the `Mode` type
//! parameter: [`ExactHessian`] (the default) forms the full Hessian via the
//! problem's [`Hessian`](crate::core::problem::Hessian) impl, while
//! [`MatrixFree`] (via [`TrustRegion::matrix_free`]) drives the subproblem
//! purely through the problem's
//! [`HessianProduct`](crate::core::problem::HessianProduct) impl and never
//! forms a matrix.

pub mod dogleg;
pub mod steihaug;

pub use dogleg::Dogleg;
pub use steihaug::Steihaug;

use std::marker::PhantomData;

use crate::core::inner::InitialState;
use crate::core::math::{Dot, MatVec, NegInPlace, NormSquared, Scalar, ScaleInPlace, ScaledAdd};
use crate::core::problem::{CostFunction, Gradient, Hessian, HessianProduct, Problem};
use crate::core::solver::Solver;
use crate::core::state::BasicState;
use crate::core::termination::TerminationReason;

/// The outcome of an (approximate) trust-region subproblem solve: the step
/// `d`, the predicted model decrease `m(0) − m(d) ≥ 0`, and whether the
/// step landed on the trust-region boundary (which gates radius growth).
pub(crate) struct Step<V, F> {
    /// The step `d` (relative to the current iterate).
    pub(crate) d: V,
    /// Predicted reduction `m(0) − m(d) = −(gᵀd) − ½ dᵀBd`, always `≥ 0`
    /// for the shipped strategies. Zero only when `d = 0` (gradient already
    /// negligible), which the driver reads as convergence.
    pub(crate) predicted_reduction: F,
    /// `true` when `‖d‖ ≈ Δ`: the constraint is active. Only then may the
    /// driver grow the radius on a very good step (N&W Algorithm 4.1).
    pub(crate) hit_boundary: bool,
}

/// A strategy that approximately minimizes the quadratic model
/// `m(p) = gᵀp + ½ pᵀ B p` over the trust region `‖p‖ ≤ radius`.
///
/// Crate-internal: the shipped strategies ([`Steihaug`], [`Dogleg`],
/// [`CauchyPoint`]) are the closed set for now. Each binds `M` (the Hessian
/// matrix type) on only the ops it needs: [`MatVec`] for the matrix-free
/// strategies, plus [`LinearSolveSpd`](crate::core::math::LinearSolveSpd)
/// for [`Dogleg`], so a backend missing an op is a compile error for that
/// strategy alone (tenet 5). Promoting this trait to public is an additive,
/// non-breaking change if user-defined subproblem solvers are ever wanted.
pub(crate) trait Subproblem<V, M, F> {
    /// Approximately minimize `m(p) = gᵀp + ½ pᵀ B p` over `‖p‖ ≤ radius`.
    fn solve(&self, gradient: &V, hessian: &M, radius: F) -> Step<V, F>;
}

/// The matrix-free sibling of [`Subproblem`]: the Hessian is reachable only
/// through the fallible product closure `bv: v ↦ B·v` (in practice the
/// counted [`HessianProduct`] call, so
/// errors are the problem's own and must propagate). Crate-internal, like
/// [`Subproblem`]. [`Steihaug`] and [`CauchyPoint`] implement it; [`Dogleg`]
/// does not (its Newton step needs the actual matrix for the Cholesky
/// solve), so pairing `Dogleg` with [`MatrixFree`] mode is a compile error
/// (tenet 5).
pub(crate) trait SubproblemHvp<V, F> {
    /// Approximately minimize `m(p) = gᵀp + ½ pᵀ B p` over `‖p‖ ≤ radius`,
    /// touching `B` only through `bv`.
    fn solve_hvp<E>(
        &self,
        gradient: &V,
        radius: F,
        bv: impl FnMut(&V) -> Result<V, E>,
    ) -> Result<Step<V, F>, E>;
}

/// Model decrease `m(0) − m(d) = −(gᵀd) − ½ dᵀBd` from a precomputed
/// product `bd = B·d`. Shared by every subproblem strategy so the
/// predicted-reduction convention lives in one place.
pub(crate) fn model_decrease_from_bd<V, F>(g: &V, d: &V, bd: &V) -> F
where
    F: Scalar,
    V: Dot<F>,
{
    let half = F::from_f64(0.5).unwrap();
    -g.dot(d) - half * d.dot(bd)
}

/// [`model_decrease_from_bd`] with the product computed via [`MatVec`], for
/// the matrix-based strategies.
pub(crate) fn model_decrease<V, M, F>(g: &V, b: &M, d: &V) -> F
where
    F: Scalar,
    V: Dot<F>,
    M: MatVec<V>,
{
    let bd = b.matvec(d);
    model_decrease_from_bd(g, d, &bd)
}

/// The largest `τ ≥ 0` with `‖z + τ d‖ = radius`: the positive root of the
/// quadratic `‖z + τ d‖² = radius²`. Used to walk a CG iterate (Steihaug)
/// or a negative-curvature direction out to the trust-region boundary.
/// Assumes `z` lies inside the ball (`‖z‖ ≤ radius`), so the discriminant
/// is non-negative; it is clamped to zero defensively against roundoff.
pub(crate) fn tau_to_boundary<V, F>(z: &V, d: &V, radius: F) -> F
where
    F: Scalar,
    V: Dot<F>,
{
    let dd = d.dot(d);
    let zd = z.dot(d);
    let zz = z.dot(z);
    let rr = radius * radius;
    let disc = zd * zd - dd * (zz - rr);
    let disc = if disc < F::zero() { F::zero() } else { disc };
    (-zd + disc.sqrt()) / dd
}

/// Cauchy-point subproblem strategy: minimize the model along the
/// steepest-descent direction `−g`, capped at the trust-region boundary
/// (Nocedal & Wright eq. 4.11–4.12).
///
/// The step is `p = −τ (Δ / ‖g‖) g`, with the scalar `τ` chosen to minimize
/// the model along `−g` within the region: `τ = 1` when the curvature
/// `gᵀBg ≤ 0` (the model decreases without bound, so go to the boundary),
/// otherwise `τ = min(‖g‖³ / (Δ gᵀBg), 1)`. Robust but only linearly
/// convergent; it ignores all curvature off the gradient direction. Useful
/// as a baseline, and as [`Dogleg`]'s fallback when the Hessian is not
/// positive definite.
#[derive(Debug, Clone, Copy, Default)]
pub struct CauchyPoint;

impl<V, M, F> Subproblem<V, M, F> for CauchyPoint
where
    F: Scalar,
    V: Clone + Dot<F> + NormSquared<F> + ScaleInPlace<F> + NegInPlace,
    M: MatVec<V>,
{
    fn solve(&self, g: &V, b: &M, radius: F) -> Step<V, F> {
        let g_norm = g.norm_squared().sqrt();
        if g_norm <= F::zero() {
            // Gradient already negligible: the zero step is optimal.
            let mut d = g.clone();
            d.scale_in_place(F::zero());
            return Step {
                d,
                predicted_reduction: F::zero(),
                hit_boundary: false,
            };
        }
        let bg = b.matvec(g);
        let gbg = g.dot(&bg);
        let tau = if gbg <= F::zero() {
            F::one()
        } else {
            let t = g_norm * g_norm * g_norm / (radius * gbg);
            if t < F::one() { t } else { F::one() }
        };
        // p = −τ (Δ / ‖g‖) g.
        let mut d = g.clone();
        d.scale_in_place(-(tau * radius / g_norm));
        let predicted_reduction = model_decrease(g, b, &d);
        Step {
            d,
            predicted_reduction,
            // τ = 1 ⟺ the step is the full Δ-length steepest-descent step,
            // i.e. it sits on the boundary.
            hit_boundary: tau >= F::one(),
        }
    }
}

impl<V, F> SubproblemHvp<V, F> for CauchyPoint
where
    F: Scalar,
    V: Clone + Dot<F> + NormSquared<F> + ScaleInPlace<F> + NegInPlace,
{
    fn solve_hvp<E>(
        &self,
        g: &V,
        radius: F,
        mut bv: impl FnMut(&V) -> Result<V, E>,
    ) -> Result<Step<V, F>, E> {
        let g_norm = g.norm_squared().sqrt();
        if g_norm <= F::zero() {
            // Gradient already negligible: the zero step is optimal.
            let mut d = g.clone();
            d.scale_in_place(F::zero());
            return Ok(Step {
                d,
                predicted_reduction: F::zero(),
                hit_boundary: false,
            });
        }
        let bg = bv(g)?;
        let gbg = g.dot(&bg);
        let tau = if gbg <= F::zero() {
            F::one()
        } else {
            let t = g_norm * g_norm * g_norm / (radius * gbg);
            if t < F::one() { t } else { F::one() }
        };
        // p = −τ (Δ / ‖g‖) g, so B·p is the already-computed B·g rescaled:
        // one product per solve instead of two.
        let c = -(tau * radius / g_norm);
        let mut d = g.clone();
        d.scale_in_place(c);
        let mut bd = bg;
        bd.scale_in_place(c);
        let predicted_reduction = model_decrease_from_bd(g, &d, &bd);
        Ok(Step {
            d,
            predicted_reduction,
            // τ = 1 ⟺ the step is the full Δ-length steepest-descent step,
            // i.e. it sits on the boundary.
            hit_boundary: tau >= F::one(),
        })
    }
}

/// Trust-region Newton minimizer (Nocedal & Wright, *Numerical
/// Optimization*, 2e, §4 / Algorithm 4.1).
///
/// At each iterate `x` the local quadratic model
/// `m(p) = f(x) + ∇f(x)ᵀp + ½ pᵀ ∇²f(x) p` is approximately minimized over a
/// ball `‖p‖ ≤ Δ` by the configured `Subproblem` strategy. The ratio of
/// achieved to predicted reduction,
/// `ρ = (f(x) − f(x + p)) / (m(0) − m(p))`, drives the radius: `Δ` shrinks
/// when `ρ` is poor (`< ¼`), grows when `ρ` is excellent (`> ¾`) and the
/// step is constrained by the boundary, and the step is accepted when
/// `ρ > η`. A rejected step shrinks `Δ` and re-solves the subproblem with
/// the *same* gradient and Hessian (no extra derivative evaluations) up to
/// [`with_max_inner_attempts`](Self::with_max_inner_attempts) times per
/// outer iteration (the same reuse pattern as
/// [`LevenbergMarquardt`](crate::solver::LevenbergMarquardt)).
///
/// The subproblem strategy defaults to [`Steihaug`] (truncated CG); choose
/// another with [`with_subproblem`](Self::with_subproblem). The radius `Δ`
/// is solver-internal working state; there are no framework termination
/// knobs for it; pair the solver with
/// [`GradientTolerance`](crate::core::termination::GradientTolerance) /
/// [`MaxIter`](crate::core::termination::MaxIter) like any first-order
/// solver.
///
/// # Matrix-free mode
///
/// [`matrix_free`](Self::matrix_free) /
/// [`matrix_free_with`](Self::matrix_free_with) switch the `Mode` parameter
/// to [`MatrixFree`]: the problem then implements
/// [`HessianProduct`] instead of
/// [`Hessian`], and the subproblem reaches `B` only through counted
/// `v ↦ ∇²f(x)·v` calls. No matrix is ever formed, so the mode scales to
/// large `n` and runs on any vector backend, including plain `Vec<F>`
/// (which has no Hessian matrix type at all). [`Steihaug`] and
/// [`CauchyPoint`] support it; [`Dogleg`] needs the actual matrix for its
/// Cholesky solve, so pairing it with matrix-free mode is a compile error.
///
/// One cost asymmetry to know about: exact mode evaluates one Hessian per
/// outer iteration and reuses it across inner radius reductions for free,
/// while matrix-free mode re-pays the CG products when a rejected step
/// re-solves at the same iterate (the shrunken radius truncates CG earlier,
/// so re-attempts get cheaper). This is inherent to truncated-CG trust
/// regions; [`EvalCounts::hessian_product_evals`](crate::EvalCounts) makes
/// the cost visible.
///
/// # Backends
///
/// The solver itself needs only `Clone`, [`ScaledAdd`], and
/// [`NormSquared`] on the parameter vector, plus a [`Hessian`] impl in the
/// default [`ExactHessian`] mode. There, the effective backend coverage is
/// set by the chosen subproblem:
/// [`Steihaug`] and [`CauchyPoint`] need only [`MatVec`] on the Hessian, so
/// they run on every backend that has a dense matrix type (`Vec<f64>` via
/// [`DenseMatrix`](crate::core::math::DenseMatrix), nalgebra, faer, and
/// `ndarray`); [`Dogleg`] additionally needs
/// [`LinearSolveSpd`](crate::core::math::LinearSolveSpd), which `ndarray`
/// does not provide (a compile error there, per tenet 5). In
/// [`MatrixFree`] mode no matrix type is bound at all, so every vector
/// backend works. All shipped strategies are wasm-clean (pure-Rust, no
/// BLAS/LAPACK).
///
/// Second-order information can reach the solver by three routes: an
/// analytic [`Hessian`] (exact mode), an analytic
/// [`HessianProduct`] (matrix-free
/// mode; see also the gradient-difference helpers
/// [`forward_difference_hessian_product`](crate::core::numdiff::forward_difference_hessian_product)
/// /
/// [`central_difference_hessian_product`](crate::core::numdiff::central_difference_hessian_product)),
/// or [`FiniteDiff`](crate::core::numdiff::FiniteDiff), which synthesizes
/// both: a finite-difference [`Hessian`] over a backend with a dense matrix
/// type (nalgebra / faer), or a finite-difference Hessian product on any
/// backend.
///
/// # References
///
/// Nocedal, J., & Wright, S. J. (2006). *Numerical Optimization* (2nd ed.),
/// Chapter 4 (trust-region methods). Springer.
/// [doi:10.1007/978-0-387-40065-5](https://doi.org/10.1007/978-0-387-40065-5).
///
/// # Examples
///
/// Minimize the 2-D Rosenbrock function with an analytic Hessian over the
/// nalgebra backend (the dense-matrix backend the example's `DMatrix`
/// Hessian uses):
///
/// ```
/// # #[cfg(feature = "nalgebra")] {
/// use basin::{CostFunction, Executor, Gradient, GradientTolerance, Hessian, TrustRegion};
/// use nalgebra::{DMatrix, DVector};
///
/// struct Rosenbrock;
/// impl CostFunction for Rosenbrock {
///     type Param = DVector<f64>;
///     type Output = f64;
///     type Error = std::convert::Infallible;
///     fn cost(&self, x: &DVector<f64>) -> Result<f64, Self::Error> {
///         Ok((1.0 - x[0]).powi(2) + 100.0 * (x[1] - x[0].powi(2)).powi(2))
///     }
/// }
/// impl Gradient for Rosenbrock {
///     type Gradient = DVector<f64>;
///     fn gradient(&self, x: &DVector<f64>) -> Result<DVector<f64>, Self::Error> {
///         Ok(DVector::from_vec(vec![
///             -2.0 * (1.0 - x[0]) - 400.0 * x[0] * (x[1] - x[0].powi(2)),
///             200.0 * (x[1] - x[0].powi(2)),
///         ]))
///     }
/// }
/// impl Hessian for Rosenbrock {
///     type Hessian = DMatrix<f64>;
///     fn hessian(&self, x: &DVector<f64>) -> Result<DMatrix<f64>, Self::Error> {
///         let h11 = 2.0 - 400.0 * (x[1] - 3.0 * x[0].powi(2));
///         Ok(DMatrix::from_row_slice(2, 2, &[
///             h11, -400.0 * x[0],
///             -400.0 * x[0], 200.0,
///         ]))
///     }
/// }
///
/// let result = Executor::new(Rosenbrock, TrustRegion::new(), basin::BasicState::new(DVector::from_vec(vec![-1.2, 1.0])))
///     .max_iter(100)
///     .terminate_on(GradientTolerance(1e-8))
///     .run()
///     .unwrap();
/// assert!(result.cost() < 1e-10);
/// # }
/// ```
///
/// Matrix-free on the dependency-free `Vec<f64>` backend: the problem
/// implements [`HessianProduct`]
/// (here `∇²f = diag(1, 100)`, so `∇²f·v` is a componentwise scale) and no
/// Hessian matrix ever exists:
///
/// ```
/// use basin::{
///     BasicState, CostFunction, Executor, Gradient, GradientTolerance, HessianProduct,
///     TrustRegion,
/// };
///
/// struct IllQuadratic;
/// impl CostFunction for IllQuadratic {
///     type Param = Vec<f64>;
///     type Output = f64;
///     type Error = std::convert::Infallible;
///     fn cost(&self, x: &Vec<f64>) -> Result<f64, Self::Error> {
///         Ok(0.5 * (x[0] * x[0] + 100.0 * x[1] * x[1]))
///     }
/// }
/// impl Gradient for IllQuadratic {
///     type Gradient = Vec<f64>;
///     fn gradient(&self, x: &Vec<f64>) -> Result<Vec<f64>, Self::Error> {
///         Ok(vec![x[0], 100.0 * x[1]])
///     }
/// }
/// impl HessianProduct for IllQuadratic {
///     fn hessian_product(&self, _x: &Vec<f64>, v: &Vec<f64>) -> Result<Vec<f64>, Self::Error> {
///         Ok(vec![v[0], 100.0 * v[1]])
///     }
/// }
///
/// let result = Executor::new(
///     IllQuadratic,
///     TrustRegion::matrix_free(),
///     BasicState::new(vec![5.0, 1.0]),
/// )
/// .max_iter(100)
/// .terminate_on(GradientTolerance(1e-10))
/// .run()
/// .unwrap();
/// assert!(result.cost() < 1e-16);
/// ```
pub struct TrustRegion<Sub = Steihaug, F = f64, Mode = ExactHessian> {
    subproblem: Sub,
    /// Current trust radius `Δ`, mutated across iterations. Reset to
    /// `initial_radius` by [`Solver::init`].
    radius: F,
    initial_radius: F,
    max_radius: F,
    eta: F,
    max_inner: u32,
    mode: PhantomData<Mode>,
}

/// Marker for [`TrustRegion`]'s default mode: the full Hessian matrix `B`
/// is formed once per outer iteration via the problem's
/// [`Hessian`] impl and reused across inner
/// radius reductions.
#[derive(Debug, Clone, Copy, Default)]
pub struct ExactHessian;

/// Marker for [`TrustRegion`]'s matrix-free mode: `B` is never formed; the
/// subproblem touches it only through the problem's
/// [`HessianProduct`] impl. See
/// [`TrustRegion::matrix_free`].
#[derive(Debug, Clone, Copy, Default)]
pub struct MatrixFree;

impl Default for TrustRegion<Steihaug> {
    fn default() -> Self {
        Self::new()
    }
}

impl TrustRegion<Steihaug> {
    /// Trust-region solver with the default [`Steihaug`] (truncated CG)
    /// subproblem: initial radius `1.0`, maximum radius `100.0`, acceptance
    /// threshold `η = 0.125`, and up to `10` radius reductions per outer
    /// iteration.
    pub fn new() -> Self {
        Self::with_subproblem(Steihaug::new())
    }
}

impl TrustRegion<Steihaug, f64, MatrixFree> {
    /// Matrix-free trust-region solver with the default [`Steihaug`]
    /// subproblem and the same defaults as [`new`](TrustRegion::new). The
    /// problem must implement
    /// [`HessianProduct`] instead of
    /// [`Hessian`]; no Hessian matrix is ever
    /// formed. See the type-level "Matrix-free mode" section.
    pub fn matrix_free() -> Self {
        Self::matrix_free_with(Steihaug::new())
    }
}

impl<Sub, F: Scalar> TrustRegion<Sub, F, MatrixFree> {
    /// Matrix-free trust-region solver with an explicit subproblem strategy
    /// ([`Steihaug`] or [`CauchyPoint`]; [`Dogleg`] needs the actual matrix
    /// and is a compile error here).
    pub fn matrix_free_with(subproblem: Sub) -> Self {
        Self {
            subproblem,
            radius: F::one(),
            initial_radius: F::one(),
            max_radius: F::from_f64(100.0).unwrap(),
            eta: F::from_f64(0.125).unwrap(),
            max_inner: 10,
            mode: PhantomData,
        }
    }
}

impl<Sub, F: Scalar> TrustRegion<Sub, F> {
    /// Trust-region solver with an explicit subproblem strategy
    /// ([`Steihaug`], [`Dogleg`], or [`CauchyPoint`]).
    pub fn with_subproblem(subproblem: Sub) -> Self {
        Self {
            subproblem,
            radius: F::one(),
            initial_radius: F::one(),
            max_radius: F::from_f64(100.0).unwrap(),
            eta: F::from_f64(0.125).unwrap(),
            max_inner: 10,
            mode: PhantomData,
        }
    }
}

impl<Sub, F: Scalar, Mode> TrustRegion<Sub, F, Mode> {
    /// Initial trust radius `Δ₀` (default `1.0`). Must be positive. A good
    /// `Δ₀` is the order of magnitude of the expected step to the minimum.
    pub fn with_radius(mut self, radius: F) -> Self {
        assert!(radius > F::zero(), "initial radius must be > 0");
        self.initial_radius = radius;
        self.radius = radius;
        self
    }

    /// Upper bound on the trust radius `Δ_max` (default `100.0`). Must be
    /// positive. Caps radius growth on a run of excellent steps.
    pub fn with_max_radius(mut self, max_radius: F) -> Self {
        assert!(max_radius > F::zero(), "max radius must be > 0");
        self.max_radius = max_radius;
        self
    }

    /// Step-acceptance threshold `η` (default `0.125`): a step is accepted
    /// when the reduction ratio `ρ > η`. Nocedal & Wright require
    /// `η ∈ [0, ¼)`; this is asserted.
    pub fn with_eta(mut self, eta: F) -> Self {
        assert!(
            eta >= F::zero() && eta < F::from_f64(0.25).unwrap(),
            "eta must be in [0, 1/4)"
        );
        self.eta = eta;
        self
    }

    /// Maximum radius reductions per outer iteration (default `10`). Each
    /// rejected step shrinks `Δ` and re-solves the subproblem with the same
    /// gradient and Hessian; after this many rejections the iteration
    /// returns the iterate unmoved with the shrunken radius, and the next
    /// outer iteration retries. Must be `≥ 1`.
    pub fn with_max_inner_attempts(mut self, n: u32) -> Self {
        assert!(n >= 1, "max inner attempts must be ≥ 1");
        self.max_inner = n;
        self
    }
}

impl<Sub, V, F, Mode> InitialState<V> for TrustRegion<Sub, F, Mode>
where
    F: Scalar,
    V: Clone,
{
    type State = BasicState<V, F>;
    fn seed(&self, x: &V) -> Self::State {
        BasicState::new(x.clone())
    }
}

/// Shared `Solver::init` body for both modes: reset the radius and seed
/// cost + gradient so iter-0 termination checks (e.g. `GradientTolerance`
/// on a near-optimal start) see a complete state. Second-order information
/// is (re)computed per iteration in `next_iter`, so none is seeded here.
fn tr_init<P, V, F>(
    radius: &mut F,
    initial_radius: F,
    problem: &mut Problem<P>,
    mut state: BasicState<V, F>,
) -> Result<BasicState<V, F>, P::Error>
where
    F: Scalar,
    P: CostFunction<Param = V, Output = F> + Gradient<Gradient = V>,
{
    // A reused solver instance must restart from the configured radius.
    *radius = initial_radius;
    let (cost, grad) = problem.cost_and_gradient(&state.param)?;
    state.cost = Some(cost);
    state.gradient = Some(grad);
    Ok(state)
}

/// Shared `Solver::next_iter` body for both modes: the accept/shrink loop
/// of N&W Algorithm 4.1. The mode-specific part — how a subproblem attempt
/// obtains `B·v` — is injected as `attempt(problem, x, g, radius)`, called
/// once per inner radius reduction at the *same* iterate `x`.
#[allow(clippy::type_complexity)]
fn tr_next_iter<P, V, F>(
    radius: &mut F,
    max_radius: F,
    eta: F,
    max_inner: u32,
    problem: &mut Problem<P>,
    mut state: BasicState<V, F>,
    mut attempt: impl FnMut(&mut Problem<P>, &V, &V, F) -> Result<Step<V, F>, P::Error>,
) -> Result<(BasicState<V, F>, Option<TerminationReason>), P::Error>
where
    F: Scalar,
    P: CostFunction<Param = V, Output = F> + Gradient<Gradient = V>,
    V: Clone + ScaledAdd<F> + NormSquared<F>,
{
    let g = state
        .gradient
        .take()
        .expect("gradient not set: Solver::init must run before next_iter");
    let cost_old = state
        .cost
        .expect("cost not set: Solver::init must run before next_iter");

    let quarter = F::from_f64(0.25).unwrap();
    let three_quarters = F::from_f64(0.75).unwrap();
    let two = F::from_f64(2.0).unwrap();

    for _ in 0..max_inner {
        let step = attempt(problem, &state.param, &g, *radius)?;

        // Predicted reduction ≤ 0 means the model cannot decrease; for
        // the shipped strategies this only happens at a stationary point
        // (g ≈ 0). Report a clean convergence stop.
        if step.predicted_reduction <= F::zero() {
            state.gradient = Some(g);
            return Ok((state, Some(TerminationReason::SolverConverged)));
        }

        let mut trial = state.param.clone();
        trial.scaled_add(F::one(), &step.d);
        let cost_trial = problem.cost(&trial)?;

        let rho = (cost_old - cost_trial) / step.predicted_reduction;
        let step_norm = step.d.norm_squared().sqrt();

        // Radius update (N&W Algorithm 4.1). A non-finite ρ (trial cost
        // Inf/NaN from a soft rejection) routes to the shrink branch, so
        // the radius always decreases on a bad step and the inner loop
        // can't stall.
        //
        // The shrink uses ¼‖p‖ rather than the literal ¼Δ of Algorithm
        // 4.1: the two agree for a boundary step (‖p‖ ≈ Δ) but ¼‖p‖
        // shrinks harder on a rejected *interior* step, anchoring the new
        // radius to the step the model actually mispredicted. This
        // follows argmin's `TrustRegion` (0.25 * pk_norm); deliberate, not
        // the textbook ¼Δ.
        if rho < quarter || !rho.is_finite() {
            *radius = quarter * step_norm;
        } else if rho > three_quarters && step.hit_boundary {
            let grown = two * *radius;
            *radius = if grown < max_radius {
                grown
            } else {
                max_radius
            };
        }

        if rho > eta {
            // Accept: move to the trial point and refresh the gradient
            // there (second-order information is recomputed by the next
            // iteration).
            state.param = trial;
            state.cost = Some(cost_trial);
            let g_new = problem.gradient(&state.param)?;
            state.gradient = Some(g_new);
            return Ok((state, None));
        }
        // Reject: radius has shrunk; retry at the same iterate.
    }

    // Inner attempts exhausted without an acceptable step. Keep the
    // shrunken radius and the current iterate; restore the gradient so
    // the state stays consistent and let the next outer iteration retry
    // (or a termination criterion fire).
    state.gradient = Some(g);
    Ok((state, None))
}

impl<P, Sub, V, M, F> Solver<P, BasicState<V, F>> for TrustRegion<Sub, F, ExactHessian>
where
    F: Scalar,
    P: CostFunction<Param = V, Output = F> + Gradient<Gradient = V> + Hessian<Hessian = M>,
    V: Clone + ScaledAdd<F> + NormSquared<F>,
    Sub: Subproblem<V, M, F>,
{
    type Error = P::Error;

    fn init(
        &mut self,
        problem: &mut Problem<P>,
        state: BasicState<V, F>,
    ) -> Result<BasicState<V, F>, Self::Error> {
        tr_init(&mut self.radius, self.initial_radius, problem, state)
    }

    fn next_iter(
        &mut self,
        problem: &mut Problem<P>,
        state: BasicState<V, F>,
    ) -> Result<(BasicState<V, F>, Option<TerminationReason>), Self::Error> {
        // One Hessian per outer iteration, reused across all inner radius
        // reductions at zero extra derivative evaluations (the gradient is
        // likewise fixed while x is).
        let b = problem.hessian(&state.param)?;
        let subproblem = &self.subproblem;
        tr_next_iter(
            &mut self.radius,
            self.max_radius,
            self.eta,
            self.max_inner,
            problem,
            state,
            |_, _, g, radius| Ok(subproblem.solve(g, &b, radius)),
        )
    }
}

impl<P, Sub, V, F> Solver<P, BasicState<V, F>> for TrustRegion<Sub, F, MatrixFree>
where
    F: Scalar,
    P: CostFunction<Param = V, Output = F> + Gradient<Gradient = V> + HessianProduct,
    V: Clone + ScaledAdd<F> + NormSquared<F>,
    Sub: SubproblemHvp<V, F>,
{
    type Error = P::Error;

    fn init(
        &mut self,
        problem: &mut Problem<P>,
        state: BasicState<V, F>,
    ) -> Result<BasicState<V, F>, Self::Error> {
        tr_init(&mut self.radius, self.initial_radius, problem, state)
    }

    fn next_iter(
        &mut self,
        problem: &mut Problem<P>,
        state: BasicState<V, F>,
    ) -> Result<(BasicState<V, F>, Option<TerminationReason>), Self::Error> {
        // No Hessian is formed: every product goes through the problem's
        // counted `hessian_product`. Unlike exact mode, an inner radius
        // reduction re-pays its CG products at the same iterate (the
        // shrunken radius truncates CG earlier, so re-attempts get
        // cheaper); `hessian_product_evals` makes that cost visible.
        let subproblem = &self.subproblem;
        tr_next_iter(
            &mut self.radius,
            self.max_radius,
            self.eta,
            self.max_inner,
            problem,
            state,
            |problem, x, g, radius| {
                subproblem.solve_hvp(g, radius, |v| problem.hessian_product(x, v))
            },
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{BasicState, Executor, GradientTolerance};

    /// Ill-conditioned quadratic `f(x) = ½ xᵀ A x` with `A = diag(1, 100)`,
    /// gradient `A x`, constant Hessian `A`. A single Newton step solves it
    /// exactly, so any curvature-aware trust-region run reaches the origin
    /// fast.
    struct Quadratic;

    impl CostFunction for Quadratic {
        type Param = Vec<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &Vec<f64>) -> Result<f64, Self::Error> {
            Ok(0.5 * (x[0] * x[0] + 100.0 * x[1] * x[1]))
        }
    }
    impl Gradient for Quadratic {
        type Gradient = Vec<f64>;
        fn gradient(&self, x: &Vec<f64>) -> Result<Vec<f64>, Self::Error> {
            Ok(vec![x[0], 100.0 * x[1]])
        }
    }
    impl Hessian for Quadratic {
        type Hessian = crate::core::math::DenseMatrix<f64>;
        fn hessian(&self, _x: &Vec<f64>) -> Result<Self::Hessian, Self::Error> {
            Ok(crate::core::math::DenseMatrix::from_row_slice(
                2,
                2,
                &[1.0, 0.0, 0.0, 100.0],
            ))
        }
    }

    /// 2-D Rosenbrock with an analytic Hessian over the dependency-free
    /// `Vec<f64>` backend (`DenseMatrix`). The classic nonconvex test: the
    /// Hessian is indefinite far from the valley floor, so it exercises the
    /// curvature handling of every subproblem.
    struct Rosenbrock;

    impl CostFunction for Rosenbrock {
        type Param = Vec<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &Vec<f64>) -> Result<f64, Self::Error> {
            Ok((1.0 - x[0]).powi(2) + 100.0 * (x[1] - x[0].powi(2)).powi(2))
        }
    }
    impl Gradient for Rosenbrock {
        type Gradient = Vec<f64>;
        fn gradient(&self, x: &Vec<f64>) -> Result<Vec<f64>, Self::Error> {
            Ok(vec![
                -2.0 * (1.0 - x[0]) - 400.0 * x[0] * (x[1] - x[0].powi(2)),
                200.0 * (x[1] - x[0].powi(2)),
            ])
        }
    }
    impl Hessian for Rosenbrock {
        type Hessian = crate::core::math::DenseMatrix<f64>;
        fn hessian(&self, x: &Vec<f64>) -> Result<Self::Hessian, Self::Error> {
            let h11 = 2.0 + 1200.0 * x[0] * x[0] - 400.0 * x[1];
            let h12 = -400.0 * x[0];
            Ok(crate::core::math::DenseMatrix::from_row_slice(
                2,
                2,
                &[h11, h12, h12, 200.0],
            ))
        }
    }

    #[test]
    fn cauchy_point_minimizes_quadratic() {
        let result = Executor::new(
            Quadratic,
            TrustRegion::with_subproblem(CauchyPoint),
            BasicState::new(vec![5.0, 1.0]),
        )
        .max_iter(500)
        .terminate_on(GradientTolerance(1e-8))
        .run()
        .unwrap();
        // Cauchy point is only linearly convergent on this conditioning, so
        // a strong-but-not-machine-precision bound is the honest check.
        assert!(result.cost() < 1e-8, "cost = {}", result.cost());
    }

    #[test]
    fn steihaug_minimizes_quadratic() {
        // Truncated CG is curvature-aware: it reaches the minimizer of a
        // quadratic to machine precision in a handful of iterations.
        let result = Executor::new(
            Quadratic,
            TrustRegion::with_subproblem(Steihaug::new()),
            BasicState::new(vec![5.0, 1.0]),
        )
        .max_iter(100)
        .terminate_on(GradientTolerance(1e-10))
        .run()
        .unwrap();
        assert!(result.cost() < 1e-16, "cost = {}", result.cost());
    }

    #[test]
    fn dogleg_minimizes_quadratic() {
        let result = Executor::new(
            Quadratic,
            TrustRegion::with_subproblem(Dogleg),
            BasicState::new(vec![5.0, 1.0]),
        )
        .max_iter(100)
        .terminate_on(GradientTolerance(1e-10))
        .run()
        .unwrap();
        assert!(result.cost() < 1e-16, "cost = {}", result.cost());
    }

    #[test]
    fn steihaug_minimizes_rosenbrock() {
        // The default subproblem on the canonical nonconvex problem, from
        // the standard hard start. Indefinite Hessians along the way are
        // handled by the negative-curvature-to-boundary rule.
        let result = Executor::new(
            Rosenbrock,
            TrustRegion::new(),
            BasicState::new(vec![-1.2, 1.0]),
        )
        .max_iter(200)
        .terminate_on(GradientTolerance(1e-8))
        .run()
        .unwrap();
        assert!(result.cost() < 1e-10, "cost = {}", result.cost());
    }

    #[test]
    fn dogleg_minimizes_rosenbrock() {
        // Dogleg falls back to the Cauchy point wherever the Hessian is
        // indefinite, so it still drives Rosenbrock to the minimum.
        let result = Executor::new(
            Rosenbrock,
            TrustRegion::with_subproblem(Dogleg),
            BasicState::new(vec![-1.2, 1.0]),
        )
        .max_iter(500)
        .terminate_on(GradientTolerance(1e-8))
        .run()
        .unwrap();
        assert!(result.cost() < 1e-10, "cost = {}", result.cost());
    }

    /// The quadratic again, but exposing only a Hessian-vector product and
    /// deliberately *not* implementing `Hessian`: that these tests compile
    /// is the proof that matrix-free mode never forms (or names) a matrix.
    struct QuadraticHvOnly;

    impl CostFunction for QuadraticHvOnly {
        type Param = Vec<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &Vec<f64>) -> Result<f64, Self::Error> {
            Ok(0.5 * (x[0] * x[0] + 100.0 * x[1] * x[1]))
        }
    }
    impl Gradient for QuadraticHvOnly {
        type Gradient = Vec<f64>;
        fn gradient(&self, x: &Vec<f64>) -> Result<Vec<f64>, Self::Error> {
            Ok(vec![x[0], 100.0 * x[1]])
        }
    }
    impl HessianProduct for QuadraticHvOnly {
        fn hessian_product(&self, _x: &Vec<f64>, v: &Vec<f64>) -> Result<Vec<f64>, Self::Error> {
            Ok(vec![v[0], 100.0 * v[1]])
        }
    }

    /// Rosenbrock with only an analytic Hessian-vector product (no
    /// `Hessian` impl).
    struct RosenbrockHvOnly;

    impl CostFunction for RosenbrockHvOnly {
        type Param = Vec<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &Vec<f64>) -> Result<f64, Self::Error> {
            Rosenbrock.cost(x)
        }
    }
    impl Gradient for RosenbrockHvOnly {
        type Gradient = Vec<f64>;
        fn gradient(&self, x: &Vec<f64>) -> Result<Vec<f64>, Self::Error> {
            Rosenbrock.gradient(x)
        }
    }
    impl HessianProduct for RosenbrockHvOnly {
        fn hessian_product(&self, x: &Vec<f64>, v: &Vec<f64>) -> Result<Vec<f64>, Self::Error> {
            let h11 = 2.0 + 1200.0 * x[0] * x[0] - 400.0 * x[1];
            let h12 = -400.0 * x[0];
            Ok(vec![h11 * v[0] + h12 * v[1], h12 * v[0] + 200.0 * v[1]])
        }
    }

    #[test]
    fn matrix_free_steihaug_minimizes_quadratic() {
        let result = Executor::new(
            QuadraticHvOnly,
            TrustRegion::matrix_free(),
            BasicState::new(vec![5.0, 1.0]),
        )
        .max_iter(100)
        .terminate_on(GradientTolerance(1e-10))
        .run()
        .unwrap();
        assert!(result.cost() < 1e-16, "cost = {}", result.cost());
    }

    #[test]
    fn matrix_free_steihaug_minimizes_rosenbrock() {
        let result = Executor::new(
            RosenbrockHvOnly,
            TrustRegion::matrix_free(),
            BasicState::new(vec![-1.2, 1.0]),
        )
        .max_iter(200)
        .terminate_on(GradientTolerance(1e-8))
        .run()
        .unwrap();
        assert!(result.cost() < 1e-10, "cost = {}", result.cost());
    }

    #[test]
    fn matrix_free_cauchy_point_minimizes_quadratic() {
        let result = Executor::new(
            QuadraticHvOnly,
            TrustRegion::matrix_free_with(CauchyPoint),
            BasicState::new(vec![5.0, 1.0]),
        )
        .max_iter(500)
        .terminate_on(GradientTolerance(1e-8))
        .run()
        .unwrap();
        assert!(result.cost() < 1e-8, "cost = {}", result.cost());
    }

    #[test]
    fn matrix_free_matches_exact_steihaug() {
        // The two modes run the identical CG arithmetic (one via `MatVec` on
        // the formed matrix, one via the analytic product), so on the same
        // problem they must land on the same iterate in the same number of
        // iterations, up to product-vs-matvec rounding.
        let exact = Executor::new(
            Quadratic,
            TrustRegion::new(),
            BasicState::new(vec![5.0, 1.0]),
        )
        .max_iter(100)
        .terminate_on(GradientTolerance(1e-10))
        .run()
        .unwrap();
        let free = Executor::new(
            QuadraticHvOnly,
            TrustRegion::matrix_free(),
            BasicState::new(vec![5.0, 1.0]),
        )
        .max_iter(100)
        .terminate_on(GradientTolerance(1e-10))
        .run()
        .unwrap();
        assert_eq!(exact.state.iter, free.state.iter);
        assert!((exact.cost() - free.cost()).abs() < 1e-15);
    }

    #[test]
    fn matrix_free_counts_products_not_hessians() {
        use crate::core::solver::Solver as _;

        let mut problem = Problem::new(RosenbrockHvOnly);
        let mut solver = TrustRegion::matrix_free();
        let mut state = solver
            .init(&mut problem, BasicState::new(vec![-1.2, 1.0]))
            .unwrap();
        for _ in 0..3 {
            let (next, _) = solver.next_iter(&mut problem, state).unwrap();
            state = next;
        }
        let counts = problem.counts();
        assert!(counts.hessian_product_evals > 0);
        assert_eq!(counts.hessian_evals, 0);

        // The state mirror folds products into the gradient slot.
        use crate::core::state::CountsMirror as _;
        state.mirror(counts);
        assert!(state.gradient_evals >= counts.hessian_product_evals);
    }
}