scirs2-optimize 0.4.4

Optimization module for SciRS2 (scirs2-optimize)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
//! Symbolic-gradient-based optimization.
//!
//! Use `scirs2-symbolic`'s symbolic gradient + Hessian as the input to
//! Newton's method, L-BFGS, and trust-region solvers — avoiding finite-difference
//! approximations and their associated step-size sensitivity.
//!
//! # Example
//! ```no_run
//! use scirs2_optimize::symbolic::newton;
//! use scirs2_symbolic::eml::LoweredOp;
//!
//! // Cost function: f(x) = x² (minimum at x=0)
//! let cost = LoweredOp::Mul(
//!     Box::new(LoweredOp::Var(0)),
//!     Box::new(LoweredOp::Var(0)),
//! );
//! let result = newton(&cost, &[5.0], 50, 1e-8).expect("converge");
//! assert!(result.x[0].abs() < 1e-6);
//! ```

use std::sync::Arc;

use scirs2_symbolic::eml::eval::{eval_real, EvalCtx};
use scirs2_symbolic::eml::{grad, hessian, LoweredOp};

/// Result of a symbolic optimization run.
#[derive(Debug, Clone)]
pub struct SymbolicNewtonResult {
    /// Solution vector.
    pub x: Vec<f64>,
    /// Final cost function value.
    pub f_final: f64,
    /// Number of iterations performed.
    pub iters: usize,
    /// True if convergence tolerance was reached.
    pub converged: bool,
}

/// Errors from symbolic Newton.
#[derive(Debug, thiserror::Error)]
pub enum SymbolicNewtonError {
    /// Underlying `LoweredOp` evaluation failure (domain, division by zero,
    /// unbound variable, etc.).
    #[error("evaluation error: {0}")]
    EvalError(String),
    /// Hessian matrix is singular (or numerically too ill-conditioned) so the
    /// Newton system H·dx = -grad cannot be solved.
    #[error("singular Hessian — cannot invert")]
    SingularHessian,
    /// Initial point dimension does not match the cost function's variable count.
    #[error("dimension mismatch: x0 has {x0_len}, cost expects {n_vars}")]
    DimMismatch {
        /// Length of the supplied `x0` vector.
        x0_len: usize,
        /// Number of variables `cost.count_vars()` reported.
        n_vars: usize,
    },
}

/// Newton's method using scirs2-symbolic gradient and Hessian.
///
/// # Arguments
/// - `cost`: scalar cost function as a `LoweredOp` (must be twice-differentiable)
/// - `x0`: initial point, length matches `cost.count_vars()`
/// - `max_iter`: max iterations
/// - `tol`: convergence tolerance on `||grad||₂`
///
/// # Errors
/// - [`SymbolicNewtonError::DimMismatch`] when `x0.len() < cost.count_vars()`
/// - [`SymbolicNewtonError::EvalError`] when symbolic gradient/Hessian
///   evaluation fails (domain violation, unbound variable, etc.)
/// - [`SymbolicNewtonError::SingularHessian`] when the Hessian is singular at
///   the current iterate
pub fn newton(
    cost: &LoweredOp,
    x0: &[f64],
    max_iter: usize,
    tol: f64,
) -> Result<SymbolicNewtonResult, SymbolicNewtonError> {
    let n_vars = cost.count_vars();
    if x0.len() < n_vars {
        return Err(SymbolicNewtonError::DimMismatch {
            x0_len: x0.len(),
            n_vars,
        });
    }

    // Precompute symbolic grad and Hessian — once
    let grad_ops: Vec<LoweredOp> = (0..n_vars).map(|i| grad(cost, i)).collect();
    let hess_ops: Vec<Vec<LoweredOp>> = hessian(cost, n_vars);

    let mut x = x0.to_vec();
    let mut converged = false;
    let mut iters = 0;

    for k in 0..max_iter {
        iters = k + 1;
        let ctx = EvalCtx::new(&x);

        // Evaluate gradient
        let mut grad_vec: Vec<f64> = Vec::with_capacity(n_vars);
        for g_op in &grad_ops {
            let v =
                eval_real(g_op, &ctx).map_err(|e| SymbolicNewtonError::EvalError(e.to_string()))?;
            grad_vec.push(v);
        }

        // Check convergence: ||grad||₂ < tol
        let grad_norm: f64 = grad_vec.iter().map(|g| g * g).sum::<f64>().sqrt();
        if grad_norm < tol {
            converged = true;
            break;
        }

        // Evaluate Hessian
        let mut hess_mat: Vec<Vec<f64>> = Vec::with_capacity(n_vars);
        for row in &hess_ops {
            let mut hess_row: Vec<f64> = Vec::with_capacity(n_vars);
            for h_op in row {
                let v = eval_real(h_op, &ctx)
                    .map_err(|e| SymbolicNewtonError::EvalError(e.to_string()))?;
                hess_row.push(v);
            }
            hess_mat.push(hess_row);
        }

        // Solve H·dx = -grad → solve_linear(H, grad) returns H⁻¹·grad
        let dx = solve_linear(&hess_mat, &grad_vec)?;

        // Update: x ← x - dx (Newton step is dx = H⁻¹·grad, so x ← x - H⁻¹·grad)
        for (xi, dxi) in x.iter_mut().zip(dx.iter()) {
            *xi -= *dxi;
        }
    }

    let final_ctx = EvalCtx::new(&x);
    let f_final =
        eval_real(cost, &final_ctx).map_err(|e| SymbolicNewtonError::EvalError(e.to_string()))?;

    Ok(SymbolicNewtonResult {
        x,
        f_final,
        iters,
        converged,
    })
}

// ────────────────────────────────────────────────────────────────────────────
// Shared result / error types for L-BFGS and trust-region symbolic solvers
// ────────────────────────────────────────────────────────────────────────────

/// Result returned by [`lbfgs_symbolic`] and [`trust_region_symbolic`].
#[derive(Debug, Clone)]
pub struct SymbolicOptResult {
    /// Solution vector.
    pub x: scirs2_core::ndarray::Array1<f64>,
    /// Objective value at `x`.
    pub f_val: f64,
    /// L₂ norm of the gradient at `x`.
    pub grad_norm: f64,
    /// Number of iterations performed.
    pub iters: usize,
    /// True when `grad_norm < tol`.
    pub converged: bool,
}

/// Errors from [`lbfgs_symbolic`] and [`trust_region_symbolic`].
#[derive(Debug, thiserror::Error)]
pub enum SymbolicOptError {
    /// Objective evaluation failed.
    #[error("eval error: {0}")]
    EvalError(String),
    /// Symbolic gradient evaluation failed.
    #[error("gradient eval error: {0}")]
    GradEvalError(String),
    /// Symbolic Hessian evaluation failed.
    #[error("Hessian eval error: {0}")]
    HessEvalError(String),
    /// `x0.len()` does not match the number of variables in the objective.
    #[error("dimension mismatch: expected {expected} variables, got {got}")]
    DimMismatch {
        /// Variables inferred from the objective.
        expected: usize,
        /// Length of the supplied `x0`.
        got: usize,
    },
    /// Optimizer exhausted its iteration budget without reaching `tol`.
    #[error("not converged after {iters} iters (grad_norm = {grad_norm:.6e})")]
    NotConverged {
        /// Iteration count at termination.
        iters: usize,
        /// Gradient norm at termination.
        grad_norm: f64,
    },
    /// Wolfe-condition line search failed to find a suitable step.
    #[error("line search failed")]
    LineSearchFailed,
}

// ────────────────────────────────────────────────────────────────────────────
// Shared helpers
// ────────────────────────────────────────────────────────────────────────────

/// Evaluate `grad_ops` at `x`, returning an error variant on failure.
fn eval_gradient(grad_ops: &[LoweredOp], x: &[f64]) -> Result<Vec<f64>, SymbolicOptError> {
    let ctx = EvalCtx::new(x);
    let mut g = Vec::with_capacity(grad_ops.len());
    for op in grad_ops {
        let v = eval_real(op, &ctx).map_err(|e| SymbolicOptError::GradEvalError(e.to_string()))?;
        g.push(v);
    }
    Ok(g)
}

/// Evaluate objective at `x`.
fn eval_objective(obj: &LoweredOp, x: &[f64]) -> Result<f64, SymbolicOptError> {
    let ctx = EvalCtx::new(x);
    eval_real(obj, &ctx).map_err(|e| SymbolicOptError::EvalError(e.to_string()))
}

/// L₂ norm of a slice.
fn l2_norm(v: &[f64]) -> f64 {
    v.iter().map(|x| x * x).sum::<f64>().sqrt()
}

/// Dot product.
fn dot(a: &[f64], b: &[f64]) -> f64 {
    a.iter().zip(b.iter()).map(|(ai, bi)| ai * bi).sum()
}

// ────────────────────────────────────────────────────────────────────────────
// L-BFGS with exact symbolic gradient
// ────────────────────────────────────────────────────────────────────────────

/// L-BFGS optimizer using exact symbolic gradient.
///
/// Converges faster than L-BFGS with finite-difference gradients because the
/// gradient is exact (no step-size tuning needed). Uses a two-loop L-BFGS
/// recursion with Wolfe-condition line search.
///
/// # Arguments
/// - `objective`: scalar objective as an `Arc<LoweredOp>`
/// - `x0`: initial point; length must equal `objective.count_vars()`
/// - `max_iter`: maximum number of iterations
/// - `tol`: convergence tolerance on `||∇f||₂`
/// - `memory`: L-BFGS history length (5–20 is typical)
///
/// # Errors
/// - [`SymbolicOptError::DimMismatch`] when `x0.len() != objective.count_vars()`
/// - [`SymbolicOptError::EvalError`] / [`SymbolicOptError::GradEvalError`] on
///   symbolic evaluation failures
/// - [`SymbolicOptError::NotConverged`] when `max_iter` is exhausted
///
/// # Example
/// ```no_run
/// # #[cfg(feature = "symbolic")]
/// # {
/// use std::sync::Arc;
/// use scirs2_optimize::symbolic::lbfgs_symbolic;
/// use scirs2_symbolic::eml::LoweredOp;
/// use scirs2_core::ndarray::array;
///
/// let obj = Arc::new(LoweredOp::Mul(
///     Box::new(LoweredOp::Var(0)),
///     Box::new(LoweredOp::Var(0)),
/// ));
/// let result = lbfgs_symbolic(&obj, array![3.0].view(), 100, 1e-8, 10).expect("converge");
/// assert!(result.x[0].abs() < 1e-6);
/// # }
/// ```
pub fn lbfgs_symbolic(
    objective: &Arc<LoweredOp>,
    x0: scirs2_core::ndarray::ArrayView1<f64>,
    max_iter: usize,
    tol: f64,
    memory: usize,
) -> Result<SymbolicOptResult, SymbolicOptError> {
    use scirs2_core::ndarray::Array1;

    let n_vars = objective.count_vars();
    if x0.len() != n_vars {
        return Err(SymbolicOptError::DimMismatch {
            expected: n_vars,
            got: x0.len(),
        });
    }

    // Precompute symbolic gradient ops once.
    let grad_ops: Vec<LoweredOp> = (0..n_vars).map(|i| grad(objective.as_ref(), i)).collect();

    let mut x: Vec<f64> = x0.iter().copied().collect();

    // Evaluate initial gradient before the loop.
    let mut g = eval_gradient(&grad_ops, &x)?;
    let mut grad_norm = l2_norm(&g);

    // Handle max_iter == 0 immediately.
    if max_iter == 0 {
        return Err(SymbolicOptError::NotConverged {
            iters: 0,
            grad_norm,
        });
    }

    if grad_norm < tol {
        let f_val = eval_objective(objective.as_ref(), &x)?;
        return Ok(SymbolicOptResult {
            x: Array1::from_vec(x),
            f_val,
            grad_norm,
            iters: 0,
            converged: true,
        });
    }

    // History: ring buffer of (s_k, y_k, rho_k) triples.
    // s_k = x_{k+1} - x_k, y_k = g_{k+1} - g_k, rho_k = 1 / (y_k^T s_k)
    let mem = memory.max(1);
    let mut history: std::collections::VecDeque<(Vec<f64>, Vec<f64>, f64)> =
        std::collections::VecDeque::with_capacity(mem);

    let mut converged = false;
    let mut iters = 0;

    for k in 0..max_iter {
        iters = k + 1;

        // ── Two-loop L-BFGS recursion ────────────────────────────────────
        let direction = lbfgs_direction(&g, &history);

        // ── Wolfe-condition line search ───────────────────────────────────
        let f_k = eval_objective(objective.as_ref(), &x)?;
        let dg = dot(&g, &direction); // ∇f^T d (should be < 0)

        const C1: f64 = 1e-4; // sufficient decrease
        const C2: f64 = 0.9; // curvature
        const MAX_LS: usize = 20;

        let mut alpha = 1.0_f64;
        let mut x_new: Vec<f64> = Vec::with_capacity(n_vars);
        let mut g_new: Vec<f64>;
        let mut armijo_ok = false;

        // Try to satisfy both Wolfe conditions with backtracking.
        let mut ls_iter = 0;
        loop {
            x_new = x
                .iter()
                .zip(&direction)
                .map(|(xi, di)| xi + alpha * di)
                .collect();
            let f_new = eval_objective(objective.as_ref(), &x_new)?;
            let suff_dec = f_new <= f_k + C1 * alpha * dg;

            if suff_dec {
                g_new = eval_gradient(&grad_ops, &x_new)?;
                let curvature = dot(&g_new, &direction).abs() <= C2 * dg.abs();
                if curvature {
                    armijo_ok = true;
                    break;
                }
                // Armijo satisfied but curvature not — still usable.
                armijo_ok = true;
                // Continue backtracking hoping to also satisfy curvature.
            } else {
                g_new = Vec::new(); // placeholder — will recompute on acceptance
            }

            ls_iter += 1;
            if ls_iter >= MAX_LS {
                break;
            }
            alpha *= 0.5;
        }

        // If we left the loop without a good step, accept Armijo-only or fail.
        if !armijo_ok {
            // Accept the last alpha that passed Armijo (may be very small).
            x_new = x
                .iter()
                .zip(&direction)
                .map(|(xi, di)| xi + alpha * di)
                .collect();
            let f_new = eval_objective(objective.as_ref(), &x_new)?;
            if f_new > f_k + C1 * alpha * dg {
                return Err(SymbolicOptError::LineSearchFailed);
            }
            g_new = eval_gradient(&grad_ops, &x_new)?;
        } else if g_new.is_empty() {
            // Filled placeholder path — curvature branch that broke early
            g_new = eval_gradient(&grad_ops, &x_new)?;
        }

        // ── Update history ────────────────────────────────────────────────
        let s_k: Vec<f64> = x_new.iter().zip(&x).map(|(xn, xo)| xn - xo).collect();
        let y_k: Vec<f64> = g_new.iter().zip(&g).map(|(gn, go)| gn - go).collect();
        let sy = dot(&s_k, &y_k);
        if sy.abs() > 1e-20 {
            let rho = 1.0 / sy;
            if history.len() == mem {
                history.pop_front();
            }
            history.push_back((s_k, y_k, rho));
        }

        // ── Advance ───────────────────────────────────────────────────────
        x = x_new;
        g = g_new;
        grad_norm = l2_norm(&g);

        if grad_norm < tol {
            converged = true;
            break;
        }
    }

    let f_val = eval_objective(objective.as_ref(), &x)?;

    if converged {
        Ok(SymbolicOptResult {
            x: Array1::from_vec(x),
            f_val,
            grad_norm,
            iters,
            converged: true,
        })
    } else {
        Err(SymbolicOptError::NotConverged { iters, grad_norm })
    }
}

/// Compute the L-BFGS search direction given the current gradient and history.
///
/// Standard two-loop recursion; returns `-H_k · g`.
fn lbfgs_direction(
    g: &[f64],
    history: &std::collections::VecDeque<(Vec<f64>, Vec<f64>, f64)>,
) -> Vec<f64> {
    let n = g.len();
    let m = history.len();
    let mut q: Vec<f64> = g.to_vec();
    let mut alphas: Vec<f64> = vec![0.0; m];

    // First loop (newest to oldest)
    for (idx, (s, y, rho)) in history.iter().rev().enumerate() {
        let a = rho * dot(s, &q);
        alphas[m - 1 - idx] = a;
        for j in 0..n {
            q[j] -= a * y[j];
        }
    }

    // Initial Hessian scaling γ_k = s_{k-1}^T y_{k-1} / y_{k-1}^T y_{k-1}
    let mut r: Vec<f64> = if let Some((s, y, _)) = history.back() {
        let sy = dot(s, y);
        let yy = dot(y, y);
        let gamma = if yy > 1e-20 { sy / yy } else { 1.0 };
        q.iter().map(|qi| gamma * qi).collect()
    } else {
        q.clone() // H_0 = I
    };

    // Second loop (oldest to newest)
    for (idx, (s, y, rho)) in history.iter().enumerate() {
        let beta = rho * dot(y, &r);
        let a = alphas[idx];
        for j in 0..n {
            r[j] += s[j] * (a - beta);
        }
    }

    // Search direction = -r
    r.iter().map(|ri| -ri).collect()
}

// ────────────────────────────────────────────────────────────────────────────
// Trust-region dogleg with exact symbolic gradient + Hessian
// ────────────────────────────────────────────────────────────────────────────

/// Trust-region optimizer using exact symbolic gradient + Hessian.
///
/// Uses the dogleg step: if the Newton step is within the trust radius, take it;
/// otherwise interpolate between the steepest-descent step and the Newton step
/// on the trust-region boundary.
///
/// # Arguments
/// - `objective`: scalar objective as an `Arc<LoweredOp>`
/// - `x0`: initial point; length must equal `objective.count_vars()`
/// - `max_iter`: maximum number of iterations
/// - `tol`: convergence tolerance on `||∇f||₂`
/// - `initial_radius`: starting trust-region radius (typically 1.0)
///
/// # Errors
/// - [`SymbolicOptError::DimMismatch`] when `x0.len() != objective.count_vars()`
/// - [`SymbolicOptError::EvalError`] / [`SymbolicOptError::GradEvalError`] /
///   [`SymbolicOptError::HessEvalError`] on symbolic evaluation failures
/// - [`SymbolicOptError::NotConverged`] when `max_iter` is exhausted
///
/// # Example
/// ```no_run
/// # #[cfg(feature = "symbolic")]
/// # {
/// use std::sync::Arc;
/// use scirs2_optimize::symbolic::trust_region_symbolic;
/// use scirs2_symbolic::eml::LoweredOp;
/// use scirs2_core::ndarray::array;
///
/// let obj = Arc::new(LoweredOp::Mul(
///     Box::new(LoweredOp::Var(0)),
///     Box::new(LoweredOp::Var(0)),
/// ));
/// let result = trust_region_symbolic(&obj, array![3.0].view(), 100, 1e-8, 1.0)
///     .expect("converge");
/// assert!(result.x[0].abs() < 1e-6);
/// # }
/// ```
pub fn trust_region_symbolic(
    objective: &Arc<LoweredOp>,
    x0: scirs2_core::ndarray::ArrayView1<f64>,
    max_iter: usize,
    tol: f64,
    initial_radius: f64,
) -> Result<SymbolicOptResult, SymbolicOptError> {
    use scirs2_core::ndarray::Array1;

    let n_vars = objective.count_vars();
    if x0.len() != n_vars {
        return Err(SymbolicOptError::DimMismatch {
            expected: n_vars,
            got: x0.len(),
        });
    }

    // Precompute symbolic gradient + Hessian ops once.
    let grad_ops: Vec<LoweredOp> = (0..n_vars).map(|i| grad(objective.as_ref(), i)).collect();
    let hess_ops: Vec<Vec<LoweredOp>> = hessian(objective.as_ref(), n_vars);

    let mut x: Vec<f64> = x0.iter().copied().collect();

    // Evaluate initial gradient.
    let mut g = eval_gradient(&grad_ops, &x)?;
    let mut grad_norm = l2_norm(&g);

    if max_iter == 0 {
        return Err(SymbolicOptError::NotConverged {
            iters: 0,
            grad_norm,
        });
    }

    if grad_norm < tol {
        let f_val = eval_objective(objective.as_ref(), &x)?;
        return Ok(SymbolicOptResult {
            x: Array1::from_vec(x),
            f_val,
            grad_norm,
            iters: 0,
            converged: true,
        });
    }

    let mut delta = initial_radius.max(1e-8);
    let mut converged = false;
    let mut iters = 0;

    for k in 0..max_iter {
        iters = k + 1;

        let f_k = eval_objective(objective.as_ref(), &x)?;

        // Evaluate Hessian at current x.
        let ctx = EvalCtx::new(&x);
        let mut h_mat: Vec<Vec<f64>> = Vec::with_capacity(n_vars);
        for row in &hess_ops {
            let mut h_row: Vec<f64> = Vec::with_capacity(n_vars);
            for h_op in row {
                let v = eval_real(h_op, &ctx)
                    .map_err(|e| SymbolicOptError::HessEvalError(e.to_string()))?;
                h_row.push(v);
            }
            h_mat.push(h_row);
        }

        // Compute dogleg step.
        let p = dogleg_step_sym(&g, &h_mat, delta);

        // Actual vs predicted reduction.
        let x_new: Vec<f64> = x.iter().zip(&p).map(|(xi, pi)| xi + pi).collect();
        let f_new = eval_objective(objective.as_ref(), &x_new)?;
        let actual_red = f_k - f_new;

        // Predicted reduction: m(0) - m(p) = -g^T p - 0.5 p^T H p
        let gtp = dot(&g, &p);
        let htp: Vec<f64> = matvec(&h_mat, &p);
        let phtp = dot(&p, &htp);
        let pred_red = -(gtp + 0.5 * phtp);

        // Update trust radius.
        let rho = if pred_red.abs() < 1e-20 {
            1.0
        } else {
            actual_red / pred_red
        };

        if rho >= 0.1 {
            // Accept step.
            x = x_new;
            g = eval_gradient(&grad_ops, &x)?;
            grad_norm = l2_norm(&g);

            if grad_norm < tol {
                converged = true;
                // Expand radius and break.
                if rho >= 0.75 {
                    let p_norm = l2_norm(&p);
                    if p_norm >= 0.8 * delta {
                        delta = (2.0 * delta).min(100.0);
                    }
                }
                break;
            }
        }
        // Else: reject step, x unchanged, g unchanged.

        // Adjust radius.
        if rho >= 0.75 {
            let p_norm = l2_norm(&p);
            if p_norm >= 0.8 * delta {
                delta = (2.0 * delta).min(100.0);
            }
        } else if rho < 0.25 {
            delta *= 0.25;
        }

        if delta < 1e-14 {
            // Trust region collapsed — treat as not converged.
            break;
        }
    }

    let f_val = eval_objective(objective.as_ref(), &x)?;

    if converged {
        Ok(SymbolicOptResult {
            x: Array1::from_vec(x),
            f_val,
            grad_norm,
            iters,
            converged: true,
        })
    } else {
        Err(SymbolicOptError::NotConverged { iters, grad_norm })
    }
}

/// Compute the dogleg step p for the trust-region subproblem:
///   min_p  g^T p + 0.5 p^T H p   s.t.  ||p|| ≤ Δ
///
/// Falls back to the steepest-descent Cauchy step when the Hessian is
/// indefinite or the Newton system is singular.
fn dogleg_step_sym(g: &[f64], h: &[Vec<f64>], delta: f64) -> Vec<f64> {
    let n = g.len();

    // Newton step: p_N = -H⁻¹ g  (solve H p_N = -g via Gaussian elimination)
    let neg_g: Vec<f64> = g.iter().map(|gi| -gi).collect();
    let p_n_opt = solve_linear_opt(h, &neg_g);

    // Steepest-descent step: p_SD = -(g^T g / g^T H g) * g
    let gtg = dot(g, g);
    let hg = matvec(h, g);
    let gthg = dot(g, &hg); // g^T H g

    let p_sd: Vec<f64> = if gthg > 1e-20 {
        let scale = gtg / gthg;
        g.iter().map(|gi| -scale * gi).collect()
    } else {
        // H is indefinite or zero — use steepest-descent direction with unit step.
        let gn = gtg.sqrt().max(1e-20);
        g.iter().map(|gi| -gi / gn).collect()
    };

    let p_sd_norm = l2_norm(&p_sd);

    // If Newton system succeeded, attempt full dogleg.
    if let Some(p_n) = p_n_opt {
        let p_n_norm = l2_norm(&p_n);

        if p_n_norm <= delta {
            // Newton step fits — take it.
            return p_n;
        }

        if p_sd_norm >= delta {
            // Cauchy point already outside trust region — scale SD.
            return p_sd.iter().map(|pi| delta / p_sd_norm * pi).collect();
        }

        // Interpolate: p = p_SD + τ * (p_N - p_SD), ||p|| = Δ
        // ||p_SD + τ * d||² = Δ²  where d = p_N - p_SD
        // ||p_SD||² + 2τ p_SD^T d + τ² ||d||² = Δ²
        let d: Vec<f64> = p_n.iter().zip(&p_sd).map(|(pni, psi)| pni - psi).collect();
        let dd = dot(&d, &d);
        let psd_d = dot(&p_sd, &d);
        let psd2 = p_sd_norm * p_sd_norm;
        let discriminant = psd_d * psd_d - dd * (psd2 - delta * delta);
        let tau = if dd < 1e-20 || discriminant < 0.0 {
            1.0
        } else {
            (-psd_d + discriminant.sqrt()) / dd
        };
        let tau = tau.clamp(0.0, 1.0);
        p_sd.iter()
            .zip(&d)
            .map(|(psi, di)| psi + tau * di)
            .collect()
    } else {
        // Singular Hessian — Cauchy step.
        if p_sd_norm <= delta {
            p_sd
        } else {
            p_sd.iter().map(|pi| delta / p_sd_norm * pi).collect()
        }
    }
}

/// Matrix-vector product H · v.
fn matvec(h: &[Vec<f64>], v: &[f64]) -> Vec<f64> {
    h.iter().map(|row| dot(row, v)).collect()
}

/// Partial-pivoting Gaussian elimination. Returns `None` when singular.
fn solve_linear_opt(a: &[Vec<f64>], b: &[f64]) -> Option<Vec<f64>> {
    let n = b.len();
    if n == 0 {
        return Some(Vec::new());
    }

    let mut mat: Vec<Vec<f64>> = a
        .iter()
        .zip(b.iter())
        .map(|(row, &bi)| {
            let mut r = row.clone();
            r.push(bi);
            r
        })
        .collect();

    for k in 0..n {
        // Find pivot
        let mut max_idx = k;
        let mut max_val = mat[k][k].abs();
        for i in (k + 1)..n {
            let v = mat[i][k].abs();
            if v > max_val {
                max_val = v;
                max_idx = i;
            }
        }
        if max_val < 1e-12 {
            return None; // singular
        }
        mat.swap(k, max_idx);
        for i in (k + 1)..n {
            let factor = mat[i][k] / mat[k][k];
            for j in k..(n + 1) {
                mat[i][j] -= factor * mat[k][j];
            }
        }
    }

    let mut x = vec![0.0; n];
    for i in (0..n).rev() {
        let mut sum = mat[i][n];
        for j in (i + 1)..n {
            sum -= mat[i][j] * x[j];
        }
        x[i] = sum / mat[i][i];
    }
    Some(x)
}

/// Solve linear system A·x = b via partial-pivoting Gaussian elimination.
fn solve_linear(a: &[Vec<f64>], b: &[f64]) -> Result<Vec<f64>, SymbolicNewtonError> {
    let n = b.len();
    if n == 0 {
        return Ok(Vec::new());
    }

    // Augmented matrix
    let mut mat: Vec<Vec<f64>> = a
        .iter()
        .zip(b.iter())
        .map(|(row, &bi)| {
            let mut r = row.clone();
            r.push(bi);
            r
        })
        .collect();

    // Forward elimination with partial pivoting
    for k in 0..n {
        // Find pivot
        let mut max_idx = k;
        let mut max_val = mat[k][k].abs();
        for i in (k + 1)..n {
            let v = mat[i][k].abs();
            if v > max_val {
                max_val = v;
                max_idx = i;
            }
        }
        if max_val < 1e-12 {
            return Err(SymbolicNewtonError::SingularHessian);
        }
        // Swap
        mat.swap(k, max_idx);
        // Eliminate
        for i in (k + 1)..n {
            let factor = mat[i][k] / mat[k][k];
            for j in k..(n + 1) {
                mat[i][j] -= factor * mat[k][j];
            }
        }
    }

    // Back-substitution
    let mut x = vec![0.0; n];
    for i in (0..n).rev() {
        let mut sum = mat[i][n];
        for j in (i + 1)..n {
            sum -= mat[i][j] * x[j];
        }
        x[i] = sum / mat[i][i];
    }

    Ok(x)
}

// ────────────────────────────────────────────────────────────────────────────
// Symbolic Lagrangian and KKT conditions
// ────────────────────────────────────────────────────────────────────────────

/// KKT system for equality-constrained optimization: min f(x) s.t. gᵢ(x) = 0.
///
/// Variables Var(0..n_vars) are the primal variables x.
/// Variables Var(n_vars..n_vars+n_constraints) are the Lagrange multipliers λᵢ.
pub struct KktSystem {
    /// Lagrangian L = f + Σ λᵢ·gᵢ.
    pub lagrangian: LoweredOp,
    /// Stationarity conditions: ∂L/∂xⱼ = 0 for j in 0..n_vars.
    pub stationarity: Vec<LoweredOp>,
    /// Constraint residuals: gᵢ(x) for i in 0..n_constraints.
    pub constraint_residuals: Vec<LoweredOp>,
    /// Number of primal variables.
    pub n_vars: usize,
    /// Number of equality constraints.
    pub n_constraints: usize,
}

/// Errors from [`build_kkt`].
#[derive(Debug)]
pub enum LagrangianError {
    /// Variable or constraint dimension was unexpected.
    DimMismatch {
        /// Expected number of variables.
        expected: usize,
        /// Actual number encountered.
        got: usize,
    },
    /// Error while computing a symbolic gradient.
    GradError(String),
}

/// Form the KKT system for equality-constrained optimization symbolically.
///
/// Builds the Lagrangian L = f + Σ λᵢ·gᵢ and derives stationarity conditions
/// ∂L/∂xⱼ = 0 for each primal variable.
///
/// # Arguments
/// - `objective`: scalar objective f(x)
/// - `constraints`: slice of equality constraints gᵢ(x) (each = 0 at a solution)
/// - `n_vars`: number of primal decision variables
///
/// # Errors
/// Returns [`LagrangianError::DimMismatch`] when the objective or a constraint
/// mentions more variables than `n_vars`.
pub fn build_kkt(
    objective: &Arc<LoweredOp>,
    constraints: &[Arc<LoweredOp>],
    n_vars: usize,
) -> Result<KktSystem, LagrangianError> {
    let m = constraints.len();

    // Validate: no operand should reference a variable index >= n_vars
    // (multiplier slots n_vars..n_vars+m are reserved by the KKT construction).
    let obj_vars = objective.count_vars();
    if obj_vars > n_vars {
        return Err(LagrangianError::DimMismatch {
            expected: n_vars,
            got: obj_vars,
        });
    }

    // Build lagrangian = f + Σ λᵢ·gᵢ
    // λᵢ occupies Var(n_vars + i).
    let mut lagrangian: LoweredOp = objective.as_ref().clone();
    for (i, g) in constraints.iter().enumerate() {
        let lam_i = LoweredOp::Var(n_vars + i);
        let term = LoweredOp::Mul(Box::new(lam_i), Box::new(g.as_ref().clone()));
        lagrangian = LoweredOp::Add(Box::new(lagrangian), Box::new(term));
    }

    // Stationarity: ∂L/∂xⱼ for j in 0..n_vars
    let stationarity: Vec<LoweredOp> = (0..n_vars).map(|j| grad(&lagrangian, j)).collect();

    // Constraint residuals: just the gᵢ themselves
    let constraint_residuals: Vec<LoweredOp> =
        constraints.iter().map(|g| g.as_ref().clone()).collect();

    Ok(KktSystem {
        lagrangian,
        stationarity,
        constraint_residuals,
        n_vars,
        n_constraints: m,
    })
}

/// Solve an equality-constrained optimization via Newton on the KKT system.
///
/// Forms the Lagrangian symbolically, derives KKT stationarity conditions, then
/// applies Newton's method on the full nonlinear KKT system
/// `[∂L/∂x; g(x)] = 0`.
///
/// # Arguments
/// - `objective`: scalar objective f(x)
/// - `constraints`: equality constraints gᵢ(x) = 0
/// - `x0`: initial primal point; length must equal `count_vars(objective)`
/// - `lambda0`: initial multiplier guess; length must equal `constraints.len()`
/// - `max_iter`: maximum Newton iterations
/// - `tol`: convergence tolerance on the L₂ norm of the KKT residual
///
/// # Errors
/// - [`SymbolicOptError::DimMismatch`] when `x0.len()` or `lambda0.len()` is wrong
/// - [`SymbolicOptError::EvalError`] on symbolic evaluation failures
/// - [`SymbolicOptError::NotConverged`] when `max_iter` is exhausted
pub fn solve_lagrangian_symbolic(
    objective: &Arc<LoweredOp>,
    constraints: &[Arc<LoweredOp>],
    x0: scirs2_core::ndarray::ArrayView1<f64>,
    lambda0: scirs2_core::ndarray::ArrayView1<f64>,
    max_iter: usize,
    tol: f64,
) -> Result<SymbolicOptResult, SymbolicOptError> {
    use scirs2_core::ndarray::Array1;

    let m = constraints.len();

    // Infer n_vars as the maximum variable index referenced across objective
    // and all constraints.
    let n_vars = std::iter::once(objective.count_vars())
        .chain(constraints.iter().map(|c| c.count_vars()))
        .max()
        .unwrap_or(0);

    if x0.len() != n_vars {
        return Err(SymbolicOptError::DimMismatch {
            expected: n_vars,
            got: x0.len(),
        });
    }
    if lambda0.len() != m {
        return Err(SymbolicOptError::DimMismatch {
            expected: m,
            got: lambda0.len(),
        });
    }

    // Build KKT system — map error variants
    let kkt = build_kkt(objective, constraints, n_vars).map_err(|e| match e {
        LagrangianError::DimMismatch { expected, got } => {
            SymbolicOptError::DimMismatch { expected, got }
        }
        LagrangianError::GradError(s) => SymbolicOptError::GradEvalError(s),
    })?;

    // Total unknowns: N = n_vars + m
    let big_n = n_vars + m;

    // Combined variable vector z = [x0..., lambda0...]
    let mut z: Vec<f64> = x0.iter().copied().chain(lambda0.iter().copied()).collect();

    // All KKT equations: stationarity (n_vars eqs) ++ constraint residuals (m eqs)
    let eqs: Vec<&LoweredOp> = kkt
        .stationarity
        .iter()
        .chain(kkt.constraint_residuals.iter())
        .collect();

    // Precompute symbolic Jacobian J[i][j] = ∂eqs[i]/∂z[j] — once before the loop.
    let jac_ops: Vec<Vec<LoweredOp>> = eqs
        .iter()
        .map(|eq| (0..big_n).map(|j| grad(eq, j)).collect())
        .collect();

    // Evaluate initial residual for max_iter==0 early exit.
    let initial_residual = {
        let ctx = EvalCtx::new(&z);
        let mut f_vec = Vec::with_capacity(big_n);
        for eq in &eqs {
            let v = eval_real(eq, &ctx).map_err(|e| SymbolicOptError::EvalError(e.to_string()))?;
            f_vec.push(v);
        }
        f_vec
    };
    let mut residual_norm = l2_norm(&initial_residual);

    if max_iter == 0 {
        return Err(SymbolicOptError::NotConverged {
            iters: 0,
            grad_norm: residual_norm,
        });
    }

    // Check if already converged at the initial point.
    if residual_norm < tol {
        let f_val = eval_objective(objective.as_ref(), &z[..n_vars])?;
        return Ok(SymbolicOptResult {
            x: Array1::from_vec(z[..n_vars].to_vec()),
            f_val,
            grad_norm: residual_norm,
            iters: 0,
            converged: true,
        });
    }

    let mut converged = false;
    let mut iters = 0;

    for k in 0..max_iter {
        iters = k + 1;

        // Evaluate KKT residual vector F
        let ctx = EvalCtx::new(&z);
        let mut f_vec: Vec<f64> = Vec::with_capacity(big_n);
        for eq in &eqs {
            let v = eval_real(eq, &ctx).map_err(|e| SymbolicOptError::EvalError(e.to_string()))?;
            f_vec.push(v);
        }

        residual_norm = l2_norm(&f_vec);
        if residual_norm < tol {
            converged = true;
            break;
        }

        // Evaluate Jacobian matrix J
        let mut jac_mat: Vec<Vec<f64>> = Vec::with_capacity(big_n);
        for row_ops in &jac_ops {
            let mut row: Vec<f64> = Vec::with_capacity(big_n);
            for op in row_ops {
                let v =
                    eval_real(op, &ctx).map_err(|e| SymbolicOptError::EvalError(e.to_string()))?;
                row.push(v);
            }
            jac_mat.push(row);
        }

        // Solve J · dz = -F
        let neg_f: Vec<f64> = f_vec.iter().map(|v| -v).collect();
        let dz = match solve_linear_opt(&jac_mat, &neg_f) {
            Some(d) => d,
            None => {
                // Singular Jacobian — fall back to negative residual step (gradient descent).
                neg_f
            }
        };

        // Update z ← z + dz
        for (zi, dzi) in z.iter_mut().zip(dz.iter()) {
            *zi += dzi;
        }
    }

    let f_val = eval_objective(objective.as_ref(), &z[..n_vars])?;

    if converged {
        Ok(SymbolicOptResult {
            x: Array1::from_vec(z[..n_vars].to_vec()),
            f_val,
            grad_norm: residual_norm,
            iters,
            converged: true,
        })
    } else {
        Err(SymbolicOptError::NotConverged {
            iters,
            grad_norm: residual_norm,
        })
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Closed-form quadratic line-search wrapper
// ─────────────────────────────────────────────────────────────────────────────

pub mod line_search;
pub use line_search::{OptLineSearchError, SymbolicLineSearch};

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

    fn var(i: usize) -> LoweredOp {
        LoweredOp::Var(i)
    }
    fn c(v: f64) -> LoweredOp {
        LoweredOp::Const(v)
    }

    #[test]
    fn newton_x_squared_one_step() {
        // f(x) = x² → grad = 2x, hess = 2; Newton step: dx = 2x / 2 = x; x_new = x - x = 0
        let cost = LoweredOp::Mul(Box::new(var(0)), Box::new(var(0)));
        let result = newton(&cost, &[5.0], 10, 1e-8).expect("converge");
        assert!(result.converged);
        assert!(result.x[0].abs() < 1e-6, "x = {}", result.x[0]);
        assert!(result.iters <= 2);
    }

    #[test]
    fn newton_quadratic_2d() {
        // f(x, y) = x² + y² → minimum at (0, 0)
        let cost = LoweredOp::Add(
            Box::new(LoweredOp::Mul(Box::new(var(0)), Box::new(var(0)))),
            Box::new(LoweredOp::Mul(Box::new(var(1)), Box::new(var(1)))),
        );
        let result = newton(&cost, &[3.0, -4.0], 10, 1e-8).expect("converge");
        assert!(result.converged);
        assert!(result.x[0].abs() < 1e-6);
        assert!(result.x[1].abs() < 1e-6);
    }

    #[test]
    fn newton_shifted_quadratic() {
        // f(x) = (x - 3)² → minimum at x = 3
        let inner = LoweredOp::Sub(Box::new(var(0)), Box::new(c(3.0)));
        let cost = LoweredOp::Mul(Box::new(inner.clone()), Box::new(inner));
        let result = newton(&cost, &[0.0], 10, 1e-8).expect("converge");
        assert!(result.converged);
        assert!((result.x[0] - 3.0).abs() < 1e-6, "x = {}", result.x[0]);
    }

    #[test]
    fn newton_dim_mismatch_returns_err() {
        let cost = LoweredOp::Mul(Box::new(var(0)), Box::new(var(0)));
        let result = newton(&cost, &[], 10, 1e-8);
        assert!(matches!(
            result,
            Err(SymbolicNewtonError::DimMismatch { .. })
        ));
    }

    #[test]
    fn solve_linear_2x2() {
        // [2 1; 1 3] · x = [5; 10]  → x = [1; 3]
        let a = vec![vec![2.0, 1.0], vec![1.0, 3.0]];
        let b = vec![5.0, 10.0];
        let x = solve_linear(&a, &b).expect("solve");
        assert!((x[0] - 1.0).abs() < 1e-10);
        assert!((x[1] - 3.0).abs() < 1e-10);
    }

    #[test]
    fn solve_linear_singular_returns_err() {
        let a = vec![vec![1.0, 2.0], vec![2.0, 4.0]]; // rank 1
        let b = vec![3.0, 6.0];
        assert!(matches!(
            solve_linear(&a, &b),
            Err(SymbolicNewtonError::SingularHessian)
        ));
    }
}