scirs2-autograd 0.3.2

Automatic differentiation module for SciRS2 (scirs2-autograd)
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
//! Numerical differentiation utilities
//!
//! This module provides finite-difference, Richardson extrapolation, and
//! complex-step methods for computing derivatives numerically.  All methods
//! operate on plain Rust slices and closures, making them useful for:
//!
//! - Gradient-checking analytic autodiff results against numerical estimates
//! - Differentiating black-box functions that have no analytic gradient
//! - Building higher-order derivative approximations via Richardson extrapolation
//!
//! # Methods overview
//!
//! | Method | Order | Notes |
//! |--------|-------|-------|
//! | Forward difference | O(h) | Cheapest; 1 extra eval per variable |
//! | Backward difference | O(h) | Identical cost; useful for boundary checks |
//! | Central difference | O(h²) | 2 extra evals; recommended default |
//! | Five-point stencil | O(h⁴) | 4 extra evals; high accuracy |
//! | Richardson extrapolation | O(h^(2n)) | n steps; near-machine-precision |
//! | Complex-step | ~ε_mach | Requires analytic extension to ℂ |
//!
//! # Examples
//!
//! ```rust
//! use scirs2_autograd::numerical_diff::{
//!     numerical_gradient, FiniteDiffMethod, check_gradient,
//! };
//!
//! let f = |xs: &[f64]| xs[0].powi(3) + xs[1].powi(2);
//! let grad_f = |xs: &[f64]| vec![3.0 * xs[0].powi(2), 2.0 * xs[1]];
//!
//! let x = vec![2.0_f64, 3.0];
//! let g = numerical_gradient(&f, &x, FiniteDiffMethod::Central, 1e-6);
//! assert!((g[0] - 12.0).abs() < 1e-5);
//! assert!((g[1] -  6.0).abs() < 1e-5);
//!
//! assert!(check_gradient(&f, &grad_f, &x, 1e-6, 1e-4, 1e-4));
//! ```

/// Finite-difference method selector.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FiniteDiffMethod {
    /// `(f(x+h) - f(x)) / h`  — first-order accuracy O(h)
    Forward,
    /// `(f(x) - f(x-h)) / h`  — first-order accuracy O(h)
    Backward,
    /// `(f(x+h) - f(x-h)) / (2h)` — second-order accuracy O(h²)
    Central,
    /// `(-f(x+2h) + 8f(x+h) - 8f(x-h) + f(x-2h)) / (12h)` — fourth-order O(h⁴)
    FivePoint,
}

// ─────────────────────────────────────────────────────────────────────────────
// Scalar-valued gradient
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the gradient of a scalar-valued function `f : ℝⁿ → ℝ` at `x`.
///
/// # Arguments
/// * `f` - scalar-valued function
/// * `x` - point at which to evaluate the gradient
/// * `method` - finite-difference stencil to use
/// * `eps` - step size (h)
///
/// # Returns
/// Gradient vector `∂f/∂xᵢ` for each `i ∈ 0..n`.
///
/// # Example
/// ```rust
/// use scirs2_autograd::numerical_diff::{numerical_gradient, FiniteDiffMethod};
///
/// let f = |xs: &[f64]| xs[0] * xs[0] + xs[1] * xs[1];
/// let g = numerical_gradient(&f, &[3.0, 4.0], FiniteDiffMethod::Central, 1e-6);
/// assert!((g[0] - 6.0).abs() < 1e-4);
/// assert!((g[1] - 8.0).abs() < 1e-4);
/// ```
pub fn numerical_gradient(
    f: &impl Fn(&[f64]) -> f64,
    x: &[f64],
    method: FiniteDiffMethod,
    eps: f64,
) -> Vec<f64> {
    let n = x.len();
    let mut grad = vec![0.0_f64; n];
    let mut xp = x.to_vec();

    for i in 0..n {
        let orig = xp[i];
        grad[i] = match method {
            FiniteDiffMethod::Forward => {
                xp[i] = orig + eps;
                let fp = f(&xp);
                xp[i] = orig;
                let f0 = f(&xp);
                (fp - f0) / eps
            }
            FiniteDiffMethod::Backward => {
                xp[i] = orig;
                let f0 = f(&xp);
                xp[i] = orig - eps;
                let fm = f(&xp);
                xp[i] = orig;
                (f0 - fm) / eps
            }
            FiniteDiffMethod::Central => {
                xp[i] = orig + eps;
                let fp = f(&xp);
                xp[i] = orig - eps;
                let fm = f(&xp);
                xp[i] = orig;
                (fp - fm) / (2.0 * eps)
            }
            FiniteDiffMethod::FivePoint => {
                xp[i] = orig + 2.0 * eps;
                let fp2 = f(&xp);
                xp[i] = orig + eps;
                let fp1 = f(&xp);
                xp[i] = orig - eps;
                let fm1 = f(&xp);
                xp[i] = orig - 2.0 * eps;
                let fm2 = f(&xp);
                xp[i] = orig;
                (-fp2 + 8.0 * fp1 - 8.0 * fm1 + fm2) / (12.0 * eps)
            }
        };
        xp[i] = orig; // restore (belt-and-suspenders)
    }
    grad
}

// ─────────────────────────────────────────────────────────────────────────────
// Jacobian of vector-valued function
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the Jacobian matrix of `f : ℝⁿ → ℝᵐ` at `x`.
///
/// The Jacobian `J ∈ ℝ^{m×n}` is laid out row-major: `J[i][j] = ∂fᵢ/∂xⱼ`.
///
/// # Arguments
/// * `f` - vector-valued function returning a `Vec<f64>` of length `m`
/// * `x` - point at which to evaluate the Jacobian (length `n`)
/// * `method` - finite-difference stencil
/// * `eps` - step size
///
/// # Returns
/// `Vec<Vec<f64>>` of shape `(m, n)`.
///
/// # Example
/// ```rust
/// use scirs2_autograd::numerical_diff::{numerical_jacobian, FiniteDiffMethod};
///
/// // f(x) = [x0 + x1, x0 * x1]  =>  J = [[1, 1], [x1, x0]]
/// let f = |xs: &[f64]| vec![xs[0] + xs[1], xs[0] * xs[1]];
/// let j = numerical_jacobian(&f, &[2.0, 3.0], FiniteDiffMethod::Central, 1e-6);
/// assert!((j[0][0] - 1.0).abs() < 1e-4); // ∂f0/∂x0
/// assert!((j[0][1] - 1.0).abs() < 1e-4); // ∂f0/∂x1
/// assert!((j[1][0] - 3.0).abs() < 1e-4); // ∂f1/∂x0 = x1
/// assert!((j[1][1] - 2.0).abs() < 1e-4); // ∂f1/∂x1 = x0
/// ```
pub fn numerical_jacobian(
    f: &impl Fn(&[f64]) -> Vec<f64>,
    x: &[f64],
    method: FiniteDiffMethod,
    eps: f64,
) -> Vec<Vec<f64>> {
    let n = x.len();
    // Determine output dimension by one evaluation at the current point.
    let f0 = f(x);
    let m = f0.len();

    // J[i][j] = ∂fᵢ/∂xⱼ — initialise from f0 columns to reuse f0 below.
    let mut jac = vec![vec![0.0_f64; n]; m];
    let mut xp = x.to_vec();

    for j in 0..n {
        let orig = xp[j];

        match method {
            FiniteDiffMethod::Forward => {
                xp[j] = orig + eps;
                let fp = f(&xp);
                xp[j] = orig;
                for i in 0..m {
                    jac[i][j] = (fp[i] - f0[i]) / eps;
                }
            }
            FiniteDiffMethod::Backward => {
                xp[j] = orig - eps;
                let fm = f(&xp);
                xp[j] = orig;
                for i in 0..m {
                    jac[i][j] = (f0[i] - fm[i]) / eps;
                }
            }
            FiniteDiffMethod::Central => {
                xp[j] = orig + eps;
                let fp = f(&xp);
                xp[j] = orig - eps;
                let fm = f(&xp);
                xp[j] = orig;
                for i in 0..m {
                    jac[i][j] = (fp[i] - fm[i]) / (2.0 * eps);
                }
            }
            FiniteDiffMethod::FivePoint => {
                xp[j] = orig + 2.0 * eps;
                let fp2 = f(&xp);
                xp[j] = orig + eps;
                let fp1 = f(&xp);
                xp[j] = orig - eps;
                let fm1 = f(&xp);
                xp[j] = orig - 2.0 * eps;
                let fm2 = f(&xp);
                xp[j] = orig;
                for i in 0..m {
                    jac[i][j] =
                        (-fp2[i] + 8.0 * fp1[i] - 8.0 * fm1[i] + fm2[i]) / (12.0 * eps);
                }
            }
        }
        xp[j] = orig; // restore
    }
    jac
}

// ─────────────────────────────────────────────────────────────────────────────
// Hessian
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the Hessian matrix of a scalar-valued function `f : ℝⁿ → ℝ` at `x`.
///
/// Uses the standard second-order central-difference formula:
///
/// ```text
/// H[i,j] = (f(x + hᵢ + hⱼ) - f(x + hᵢ - hⱼ) - f(x - hᵢ + hⱼ) + f(x - hᵢ - hⱼ)) / (4h²)
/// ```
///
/// For diagonal entries a simpler formula is used:
/// ```text
/// H[i,i] = (f(x + hᵢ) - 2f(x) + f(x - hᵢ)) / h²
/// ```
///
/// # Example
/// ```rust
/// use scirs2_autograd::numerical_diff::numerical_hessian;
///
/// // f(x) = x0² + x0*x1 + 2*x1²
/// // H = [[2, 1], [1, 4]]  (constant Hessian)
/// let f = |xs: &[f64]| xs[0].powi(2) + xs[0]*xs[1] + 2.0*xs[1].powi(2);
/// let h = numerical_hessian(&f, &[1.0, 1.0], 1e-4);
/// assert!((h[0][0] - 2.0).abs() < 1e-3);
/// assert!((h[0][1] - 1.0).abs() < 1e-3);
/// assert!((h[1][0] - 1.0).abs() < 1e-3);
/// assert!((h[1][1] - 4.0).abs() < 1e-3);
/// ```
pub fn numerical_hessian(f: &impl Fn(&[f64]) -> f64, x: &[f64], eps: f64) -> Vec<Vec<f64>> {
    let n = x.len();
    let f0 = f(x);
    let mut hess = vec![vec![0.0_f64; n]; n];
    let mut xp = x.to_vec();

    for i in 0..n {
        // Diagonal: (f(x+eᵢ) - 2f(x) + f(x-eᵢ)) / h²
        let orig_i = xp[i];
        xp[i] = orig_i + eps;
        let fp = f(&xp);
        xp[i] = orig_i - eps;
        let fm = f(&xp);
        xp[i] = orig_i;
        hess[i][i] = (fp - 2.0 * f0 + fm) / (eps * eps);

        // Off-diagonal (upper triangle, symmetrize)
        for j in (i + 1)..n {
            let orig_j = xp[j];

            xp[i] = orig_i + eps;
            xp[j] = orig_j + eps;
            let fpp = f(&xp);

            xp[i] = orig_i + eps;
            xp[j] = orig_j - eps;
            let fpm = f(&xp);

            xp[i] = orig_i - eps;
            xp[j] = orig_j + eps;
            let fmp = f(&xp);

            xp[i] = orig_i - eps;
            xp[j] = orig_j - eps;
            let fmm = f(&xp);

            xp[i] = orig_i;
            xp[j] = orig_j;

            let hij = (fpp - fpm - fmp + fmm) / (4.0 * eps * eps);
            hess[i][j] = hij;
            hess[j][i] = hij; // symmetry
        }
    }
    hess
}

// ─────────────────────────────────────────────────────────────────────────────
// Gradient checker
// ─────────────────────────────────────────────────────────────────────────────

/// Check an analytic gradient against a numerical central-difference estimate.
///
/// Returns `true` if every component satisfies the relative+absolute tolerance:
/// `|analytic - numeric| <= atol + rtol * max(|analytic|, |numeric|)`.
///
/// # Arguments
/// * `f` - scalar-valued function
/// * `grad` - analytic gradient function
/// * `x` - evaluation point
/// * `eps` - step size for numerical estimate
/// * `rtol` - relative tolerance
/// * `atol` - absolute tolerance
///
/// # Example
/// ```rust
/// use scirs2_autograd::numerical_diff::{check_gradient, FiniteDiffMethod};
///
/// let f = |xs: &[f64]| xs[0].sin() + xs[1].cos();
/// let grad_f = |xs: &[f64]| vec![xs[0].cos(), -xs[1].sin()];
///
/// assert!(check_gradient(&f, &grad_f, &[0.5, 1.2], 1e-6, 1e-4, 1e-6));
/// ```
pub fn check_gradient(
    f: &impl Fn(&[f64]) -> f64,
    grad: &impl Fn(&[f64]) -> Vec<f64>,
    x: &[f64],
    eps: f64,
    rtol: f64,
    atol: f64,
) -> bool {
    let analytic = grad(x);
    let numeric = numerical_gradient(f, x, FiniteDiffMethod::Central, eps);

    if analytic.len() != numeric.len() {
        return false;
    }

    analytic.iter().zip(numeric.iter()).all(|(&a, &n)| {
        let diff = (a - n).abs();
        let scale = a.abs().max(n.abs());
        diff <= atol + rtol * scale
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// Richardson extrapolation
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the gradient of `f` using Richardson extrapolation for improved accuracy.
///
/// Starting from step size `eps0`, each successive column of the Romberg table
/// halves the step and the extrapolated value achieves order O(h^(2n)).
///
/// # Arguments
/// * `f` - scalar-valued function
/// * `x` - evaluation point
/// * `n_steps` - number of Richardson levels (typically 4–6)
/// * `eps0` - initial step size (will be successively halved)
///
/// # Example
/// ```rust
/// use scirs2_autograd::numerical_diff::richardson_gradient;
///
/// // sin(x): analytic grad = cos(x)
/// let f = |xs: &[f64]| xs[0].sin();
/// let g = richardson_gradient(&f, &[1.0], 5, 0.1);
/// assert!((g[0] - 1.0_f64.cos()).abs() < 1e-12, "g[0]={}", g[0]);
/// ```
pub fn richardson_gradient(
    f: &impl Fn(&[f64]) -> f64,
    x: &[f64],
    n_steps: usize,
    eps0: f64,
) -> Vec<f64> {
    let n_steps = n_steps.max(1);
    let n = x.len();
    let mut grad = vec![0.0_f64; n];
    let mut xp = x.to_vec();

    for k in 0..n {
        let orig = xp[k];
        // Build Romberg table column by column.
        // D[i] holds the current-level estimate with step eps0 / 2^i.
        let mut d = Vec::with_capacity(n_steps);

        let mut h = eps0;
        // First column: central differences with decreasing h
        for _ in 0..n_steps {
            xp[k] = orig + h;
            let fp = f(&xp);
            xp[k] = orig - h;
            let fm = f(&xp);
            xp[k] = orig;
            d.push((fp - fm) / (2.0 * h));
            h *= 0.5;
        }

        // Richardson extrapolation: combine adjacent estimates.
        // For central difference (order 2): factor = 4^p / (4^p - 1)
        let mut power = 4.0_f64; // 4 for central difference (p=2)
        for _level in 1..n_steps {
            let len = d.len() - 1;
            for i in 0..len {
                d[i] = (power * d[i + 1] - d[i]) / (power - 1.0);
            }
            d.pop();
            power *= 4.0; // next level: 4^(level+1)
        }

        grad[k] = d.first().copied().unwrap_or(0.0);
        xp[k] = orig; // restore
    }
    grad
}

// ─────────────────────────────────────────────────────────────────────────────
// Complex-step differentiation
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the gradient of `f` using the complex-step method.
///
/// For an analytic function extended to the complex plane the identity
/// `Im(f(x + ih)) / h ≈ f'(x)` holds to machine precision because there is
/// no cancellation error (unlike finite differences).
///
/// The closure receives `(re_inputs, im_inputs)` — real and imaginary parts of
/// each input — and must return `(re_out, im_out)`.  For a real-valued function
/// the imaginary part propagates linearly through every elementary operation.
///
/// # Arguments
/// * `f_complex` - complex extension of `f`; signature `(re: &[f64], im: &[f64]) -> (f64, f64)`
/// * `x_real` - real evaluation point
/// * `eps` - imaginary perturbation (typically 1e-20 to 1e-100)
///
/// # Example
/// ```rust
/// use scirs2_autograd::numerical_diff::complex_step_gradient;
///
/// // f(x) = x[0]^3 + x[1]^2  =>  df/dx0 = 3*x0^2, df/dx1 = 2*x1
/// // Complex extension: (re + i*im)^n  ≈ re^n + i*n*re^(n-1)*im  (first order)
/// let f_cx = |re: &[f64], im: &[f64]| {
///     // re part: re0^3 + re1^2
///     let re_out = re[0].powi(3) + re[1].powi(2);
///     // im part: 3*re0^2*im0 + 2*re1*im1
///     let im_out = 3.0 * re[0].powi(2) * im[0] + 2.0 * re[1] * im[1];
///     (re_out, im_out)
/// };
///
/// let x = vec![2.0_f64, 3.0];
/// let g = complex_step_gradient(&f_cx, &x, 1e-20);
/// assert!((g[0] - 12.0).abs() < 1e-10, "df/dx0={}", g[0]);
/// assert!((g[1] -  6.0).abs() < 1e-10, "df/dx1={}", g[1]);
/// ```
pub fn complex_step_gradient(
    f_complex: &impl Fn(&[f64], &[f64]) -> (f64, f64),
    x_real: &[f64],
    eps: f64,
) -> Vec<f64> {
    let n = x_real.len();
    let mut grad = vec![0.0_f64; n];
    // im_inputs: all-zero except the perturbed component
    let mut im_inputs = vec![0.0_f64; n];

    for k in 0..n {
        im_inputs[k] = eps;
        let (_, im_out) = f_complex(x_real, &im_inputs);
        grad[k] = im_out / eps;
        im_inputs[k] = 0.0; // restore
    }
    grad
}

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

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

    // ── numerical_gradient ────────────────────────────────────────────────

    #[test]
    fn test_numerical_gradient_forward_quadratic() {
        let f = |xs: &[f64]| xs[0] * xs[0] + xs[1] * xs[1];
        let g = numerical_gradient(&f, &[3.0, 4.0], FiniteDiffMethod::Forward, 1e-6);
        assert!((g[0] - 6.0).abs() < 1e-3, "g[0]={}", g[0]);
        assert!((g[1] - 8.0).abs() < 1e-3, "g[1]={}", g[1]);
    }

    #[test]
    fn test_numerical_gradient_backward_quadratic() {
        let f = |xs: &[f64]| xs[0] * xs[0] + xs[1] * xs[1];
        let g = numerical_gradient(&f, &[3.0, 4.0], FiniteDiffMethod::Backward, 1e-6);
        assert!((g[0] - 6.0).abs() < 1e-3, "g[0]={}", g[0]);
        assert!((g[1] - 8.0).abs() < 1e-3, "g[1]={}", g[1]);
    }

    #[test]
    fn test_numerical_gradient_central_quadratic() {
        let f = |xs: &[f64]| xs[0] * xs[0] + xs[1] * xs[1];
        let g = numerical_gradient(&f, &[3.0, 4.0], FiniteDiffMethod::Central, 1e-6);
        assert!((g[0] - 6.0).abs() < 1e-5, "g[0]={}", g[0]);
        assert!((g[1] - 8.0).abs() < 1e-5, "g[1]={}", g[1]);
    }

    #[test]
    fn test_numerical_gradient_five_point_trig() {
        let f = |xs: &[f64]| xs[0].sin();
        // df/dx0 = cos(x0) at x0 = 1.0
        let g = numerical_gradient(&f, &[1.0], FiniteDiffMethod::FivePoint, 1e-5);
        assert!((g[0] - 1.0_f64.cos()).abs() < 1e-9, "g[0]={}", g[0]);
    }

    // ── numerical_jacobian ────────────────────────────────────────────────

    #[test]
    fn test_numerical_jacobian_linear() {
        // f(x) = [x0 + x1, 2*x0 - x1]
        // J = [[1, 1], [2, -1]]
        let f = |xs: &[f64]| vec![xs[0] + xs[1], 2.0 * xs[0] - xs[1]];
        let j = numerical_jacobian(&f, &[1.0, 2.0], FiniteDiffMethod::Central, 1e-6);
        assert!((j[0][0] - 1.0).abs() < 1e-5);
        assert!((j[0][1] - 1.0).abs() < 1e-5);
        assert!((j[1][0] - 2.0).abs() < 1e-5);
        assert!((j[1][1] + 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_numerical_jacobian_nonlinear() {
        // f(x) = [x0 + x1, x0 * x1]  =>  J at (2,3) = [[1,1],[3,2]]
        let f = |xs: &[f64]| vec![xs[0] + xs[1], xs[0] * xs[1]];
        let j = numerical_jacobian(&f, &[2.0, 3.0], FiniteDiffMethod::Central, 1e-6);
        assert!((j[1][0] - 3.0).abs() < 1e-4, "j[1][0]={}", j[1][0]);
        assert!((j[1][1] - 2.0).abs() < 1e-4, "j[1][1]={}", j[1][1]);
    }

    // ── numerical_hessian ─────────────────────────────────────────────────

    #[test]
    fn test_numerical_hessian_quadratic() {
        // f(x) = x0^2 + x0*x1 + 2*x1^2  =>  H = [[2,1],[1,4]]
        let f = |xs: &[f64]| xs[0].powi(2) + xs[0] * xs[1] + 2.0 * xs[1].powi(2);
        let h = numerical_hessian(&f, &[1.0, 1.0], 1e-4);
        assert!((h[0][0] - 2.0).abs() < 1e-3, "H[0,0]={}", h[0][0]);
        assert!((h[0][1] - 1.0).abs() < 1e-3, "H[0,1]={}", h[0][1]);
        assert!((h[1][0] - 1.0).abs() < 1e-3, "H[1,0]={}", h[1][0]);
        assert!((h[1][1] - 4.0).abs() < 1e-3, "H[1,1]={}", h[1][1]);
    }

    #[test]
    fn test_numerical_hessian_symmetric() {
        let f = |xs: &[f64]| xs[0].powi(3) * xs[1] + xs[1].powi(3);
        let h = numerical_hessian(&f, &[1.0, 2.0], 1e-4);
        assert!(
            (h[0][1] - h[1][0]).abs() < 1e-3,
            "H not symmetric: {} vs {}",
            h[0][1],
            h[1][0]
        );
    }

    // ── check_gradient ────────────────────────────────────────────────────

    #[test]
    fn test_check_gradient_correct() {
        let f = |xs: &[f64]| xs[0].sin() + xs[1].cos();
        let grad_f = |xs: &[f64]| vec![xs[0].cos(), -xs[1].sin()];
        assert!(check_gradient(&f, &grad_f, &[0.5, 1.2], 1e-6, 1e-4, 1e-6));
    }

    #[test]
    fn test_check_gradient_wrong_fails() {
        let f = |xs: &[f64]| xs[0].sin();
        let correct_grad = |xs: &[f64]| vec![xs[0].cos()];
        let wrong_grad = |xs: &[f64]| vec![xs[0].sin()]; // deliberately wrong
        // Correct gradient should pass
        let correct = check_gradient(&f, &correct_grad, &[0.5], 1e-6, 1e-3, 1e-6);
        assert!(correct, "correct gradient should pass check");
        // Wrong gradient should fail
        let wrong = check_gradient(&f, &wrong_grad, &[0.5], 1e-6, 1e-4, 1e-6);
        assert!(!wrong, "wrong gradient should fail check");
    }

    // ── richardson_gradient ───────────────────────────────────────────────

    #[test]
    fn test_richardson_gradient_sin() {
        let f = |xs: &[f64]| xs[0].sin();
        let g = richardson_gradient(&f, &[1.0], 5, 0.1);
        let expected = 1.0_f64.cos();
        assert!((g[0] - expected).abs() < 1e-11, "g[0]={}", g[0]);
    }

    #[test]
    fn test_richardson_gradient_poly() {
        // f(x) = x^5, f'(x) = 5x^4 at x=2 => 80
        let f = |xs: &[f64]| xs[0].powi(5);
        let g = richardson_gradient(&f, &[2.0], 6, 0.5);
        assert!((g[0] - 80.0).abs() < 1e-8, "g[0]={}", g[0]);
    }

    // ── complex_step_gradient ─────────────────────────────────────────────

    #[test]
    fn test_complex_step_gradient_poly() {
        // f(x) = x0^3 + x1^2, cx extension first-order in im
        let f_cx = |re: &[f64], im: &[f64]| {
            let re_out = re[0].powi(3) + re[1].powi(2);
            let im_out = 3.0 * re[0].powi(2) * im[0] + 2.0 * re[1] * im[1];
            (re_out, im_out)
        };
        let x = vec![2.0_f64, 3.0];
        let g = complex_step_gradient(&f_cx, &x, 1e-20);
        assert!((g[0] - 12.0).abs() < 1e-10, "df/dx0={}", g[0]);
        assert!((g[1] - 6.0).abs() < 1e-10, "df/dx1={}", g[1]);
    }

    #[test]
    fn test_complex_step_gradient_exp() {
        // f(x) = exp(x), cx: re_out = exp(re), im_out = exp(re)*im
        let f_cx = |re: &[f64], im: &[f64]| {
            let e = re[0].exp();
            (e, e * im[0])
        };
        let g = complex_step_gradient(&f_cx, &[1.0], 1e-20);
        assert!((g[0] - 1.0_f64.exp()).abs() < 1e-12, "g[0]={}", g[0]);
    }
}