numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Gradient-based optimization algorithms
//!
//! This module provides gradient-based optimization methods including:
//! - BFGS quasi-Newton method
//! - L-BFGS limited-memory variant
//! - Wolfe line search
//! - Gradient verification utilities

use crate::error::{NumRs2Error, Result};
use num_traits::Float;

use super::{compute_norm, dot_product, OptimizeConfig, OptimizeResult};

/// BFGS (Broyden-Fletcher-Goldfarb-Shanno) quasi-Newton method
///
/// Minimizes a scalar function using gradient information and a quasi-Newton
/// approximation of the Hessian matrix.
///
/// # Arguments
///
/// * `f` - Objective function to minimize
/// * `grad` - Gradient function
/// * `x0` - Initial guess
/// * `config` - Optional configuration (uses defaults if None)
///
/// # Returns
///
/// An `OptimizeResult` containing the optimal point and convergence information
///
/// # Examples
///
/// ```
/// use numrs2::optimize::*;
///
/// // Minimize f(x,y) = x^2 + y^2
/// let f = |x: &[f64]| x[0]*x[0] + x[1]*x[1];
/// let grad = |x: &[f64]| vec![2.0*x[0], 2.0*x[1]];
///
/// let result = bfgs(f, grad, &[3.0, 4.0], None).expect("BFGS optimization should succeed");
/// assert!(result.success);
/// assert!(result.fun < 1e-10); // Should find minimum at (0,0)
/// ```
pub fn bfgs<T, F, G>(
    f: F,
    grad: G,
    x0: &[T],
    config: Option<OptimizeConfig<T>>,
) -> Result<OptimizeResult<T>>
where
    T: Float + std::fmt::Debug + std::iter::Sum,
    F: Fn(&[T]) -> T,
    G: Fn(&[T]) -> Vec<T>,
{
    let cfg = config.unwrap_or_default();
    let n = x0.len();

    // Initialize
    let mut x = x0.to_vec();
    let mut f_val = f(&x);
    let mut g = grad(&x);
    let mut nfev = 1;
    let mut njev = 1;

    // Initialize inverse Hessian approximation to identity
    let mut h_inv = vec![vec![T::zero(); n]; n];
    for i in 0..n {
        h_inv[i][i] = T::one();
    }

    // Compute initial gradient norm
    let g_norm = compute_norm(&g);

    // Check if already at minimum
    if g_norm < cfg.gtol {
        return Ok(OptimizeResult {
            x,
            fun: f_val,
            grad: g,
            nit: 0,
            nfev,
            njev,
            success: true,
            message: "Optimization terminated successfully (initial point is optimal)".to_string(),
        });
    }

    // BFGS iteration
    for k in 0..cfg.max_iter {
        // Compute search direction: p = -H_inv * g
        let mut p = vec![T::zero(); n];
        for i in 0..n {
            for j in 0..n {
                p[i] = p[i] - h_inv[i][j] * g[j];
            }
        }

        // Line search along direction p
        let (alpha, f_new, nfev_ls) = wolfe_line_search(&f, &grad, &x, &p, f_val, &g, &cfg)?;
        nfev += nfev_ls;
        njev += nfev_ls; // Gradient evaluated in each line search step

        // Update x
        let x_new: Vec<T> = x
            .iter()
            .zip(p.iter())
            .map(|(&xi, &pi)| xi + alpha * pi)
            .collect();

        // Compute new gradient
        let g_new = grad(&x_new);
        njev += 1;

        // Check convergence criteria
        let dx_norm = compute_norm(
            &x_new
                .iter()
                .zip(x.iter())
                .map(|(&xi_new, &xi)| xi_new - xi)
                .collect::<Vec<_>>(),
        );
        let df = (f_new - f_val).abs();
        let g_new_norm = compute_norm(&g_new);

        if g_new_norm < cfg.gtol {
            return Ok(OptimizeResult {
                x: x_new,
                fun: f_new,
                grad: g_new,
                nit: k + 1,
                nfev,
                njev,
                success: true,
                message: "Optimization terminated successfully (gradient norm converged)"
                    .to_string(),
            });
        }

        if dx_norm < cfg.xtol {
            return Ok(OptimizeResult {
                x: x_new,
                fun: f_new,
                grad: g_new,
                nit: k + 1,
                nfev,
                njev,
                success: true,
                message: "Optimization terminated successfully (parameter change converged)"
                    .to_string(),
            });
        }

        if df < cfg.ftol {
            return Ok(OptimizeResult {
                x: x_new,
                fun: f_new,
                grad: g_new,
                nit: k + 1,
                nfev,
                njev,
                success: true,
                message: "Optimization terminated successfully (function value converged)"
                    .to_string(),
            });
        }

        // Compute s_k = x_{k+1} - x_k and y_k = g_{k+1} - g_k
        let s: Vec<T> = x_new
            .iter()
            .zip(x.iter())
            .map(|(&xi_new, &xi)| xi_new - xi)
            .collect();
        let y: Vec<T> = g_new
            .iter()
            .zip(g.iter())
            .map(|(&gi_new, &gi)| gi_new - gi)
            .collect();

        // Compute y^T * s (curvature condition)
        let ys: T = y.iter().zip(s.iter()).map(|(&yi, &si)| yi * si).sum();

        // Update inverse Hessian approximation using BFGS formula
        if ys > T::from(1e-14).expect("1e-14 should be representable in Float") {
            // Compute H * y
            let mut hy = vec![T::zero(); n];
            for i in 0..n {
                for j in 0..n {
                    hy[i] = hy[i] + h_inv[i][j] * y[j];
                }
            }

            // Compute y^T * H * y
            let yhy: T = y.iter().zip(hy.iter()).map(|(&yi, &hyi)| yi * hyi).sum();

            // BFGS update: H_new = H + (1 + yHy/ys) * (s*s^T)/ys - (s*Hy^T + Hy*s^T)/ys
            for i in 0..n {
                for j in 0..n {
                    let term1 = (T::one() + yhy / ys) * s[i] * s[j] / ys;
                    let term2 = (s[i] * hy[j] + hy[i] * s[j]) / ys;
                    h_inv[i][j] = h_inv[i][j] + term1 - term2;
                }
            }
        }

        // Update for next iteration
        x = x_new;
        f_val = f_new;
        g = g_new;
    }

    // Max iterations reached
    Ok(OptimizeResult {
        x,
        fun: f_val,
        grad: g,
        nit: cfg.max_iter,
        nfev,
        njev,
        success: false,
        message: "Maximum iterations reached".to_string(),
    })
}

/// Wolfe line search for step size selection
///
/// Finds a step size alpha that satisfies both the Armijo (sufficient decrease)
/// and curvature (Wolfe) conditions.
pub fn wolfe_line_search<T, F, G>(
    f: &F,
    grad: &G,
    x: &[T],
    p: &[T],
    f0: T,
    g0: &[T],
    config: &OptimizeConfig<T>,
) -> Result<(T, T, usize)>
where
    T: Float + std::iter::Sum,
    F: Fn(&[T]) -> T,
    G: Fn(&[T]) -> Vec<T>,
{
    let mut alpha = T::one();
    let mut nfev = 0;

    // Compute directional derivative: g0^T * p
    let dg: T = g0.iter().zip(p.iter()).map(|(&gi, &pi)| gi * pi).sum();

    // Armijo condition check
    for _ in 0..config.ls_max_iter {
        // Compute x_new = x + alpha * p
        let x_new: Vec<T> = x
            .iter()
            .zip(p.iter())
            .map(|(&xi, &pi)| xi + alpha * pi)
            .collect();

        let f_new = f(&x_new);
        nfev += 1;

        // Check Armijo (sufficient decrease) condition
        if f_new <= f0 + config.c1 * alpha * dg {
            // Check strong Wolfe curvature condition
            let g_new = grad(&x_new);
            let dg_new: T = g_new.iter().zip(p.iter()).map(|(&gi, &pi)| gi * pi).sum();

            if dg_new.abs() <= config.c2 * dg.abs() {
                return Ok((alpha, f_new, nfev + 1)); // +1 for gradient eval
            }
        }

        // Reduce step size
        alpha = alpha * T::from(0.5).expect("0.5 should be representable in Float");

        if alpha < T::from(1e-10).expect("1e-10 should be representable in Float") {
            break;
        }
    }

    // If line search fails, return small step
    let alpha_min = T::from(1e-8).expect("1e-8 should be representable in Float");
    let x_new: Vec<T> = x
        .iter()
        .zip(p.iter())
        .map(|(&xi, &pi)| xi + alpha_min * pi)
        .collect();
    let f_new = f(&x_new);

    Ok((alpha_min, f_new, nfev + 1))
}

/// L-BFGS (Limited-memory BFGS) optimization
///
/// Memory-efficient variant of BFGS that stores only a few recent update vectors
/// instead of the full inverse Hessian approximation.
///
/// # Arguments
///
/// * `f` - Objective function to minimize
/// * `grad` - Gradient function
/// * `x0` - Initial guess
/// * `m` - Number of correction pairs to store (typically 5-20)
/// * `config` - Optional configuration
///
/// # Examples
///
/// ```
/// use numrs2::optimize::*;
///
/// let f = |x: &[f64]| x[0]*x[0] + x[1]*x[1];
/// let grad = |x: &[f64]| vec![2.0*x[0], 2.0*x[1]];
///
/// let result = lbfgs(f, grad, &[3.0, 4.0], 10, None).expect("L-BFGS optimization should succeed");
/// assert!(result.success);
/// ```
pub fn lbfgs<T, F, G>(
    f: F,
    grad: G,
    x0: &[T],
    m: usize, // Number of correction pairs
    config: Option<OptimizeConfig<T>>,
) -> Result<OptimizeResult<T>>
where
    T: Float + std::fmt::Debug + std::iter::Sum,
    F: Fn(&[T]) -> T,
    G: Fn(&[T]) -> Vec<T>,
{
    let cfg = config.unwrap_or_default();
    let n = x0.len();

    if m == 0 {
        return Err(NumRs2Error::ValueError(
            "L-BFGS memory parameter m must be > 0".to_string(),
        ));
    }

    // Initialize
    let mut x = x0.to_vec();
    let mut f_val = f(&x);
    let mut g = grad(&x);
    let mut nfev = 1;
    let mut njev = 1;

    // Storage for L-BFGS: s and y vectors
    let mut s_history: Vec<Vec<T>> = Vec::with_capacity(m);
    let mut y_history: Vec<Vec<T>> = Vec::with_capacity(m);
    let mut rho_history: Vec<T> = Vec::with_capacity(m);

    // Check initial gradient
    let g_norm = compute_norm(&g);
    if g_norm < cfg.gtol {
        return Ok(OptimizeResult {
            x,
            fun: f_val,
            grad: g,
            nit: 0,
            nfev,
            njev,
            success: true,
            message: "Optimization terminated successfully (initial point is optimal)".to_string(),
        });
    }

    // L-BFGS iteration
    for k in 0..cfg.max_iter {
        // Compute search direction using L-BFGS two-loop recursion
        let p = lbfgs_two_loop_recursion(&g, &s_history, &y_history, &rho_history);

        // Line search
        let (alpha, f_new, nfev_ls) = wolfe_line_search(&f, &grad, &x, &p, f_val, &g, &cfg)?;
        nfev += nfev_ls;
        njev += nfev_ls;

        // Update parameters
        let x_new: Vec<T> = x
            .iter()
            .zip(p.iter())
            .map(|(&xi, &pi)| xi + alpha * pi)
            .collect();

        // Compute new gradient
        let g_new = grad(&x_new);
        njev += 1;

        // Compute s and y
        let s: Vec<T> = x_new
            .iter()
            .zip(x.iter())
            .map(|(&xi_new, &xi)| xi_new - xi)
            .collect();
        let y: Vec<T> = g_new
            .iter()
            .zip(g.iter())
            .map(|(&gi_new, &gi)| gi_new - gi)
            .collect();

        // Compute rho = 1 / (y^T * s)
        let ys: T = y.iter().zip(s.iter()).map(|(&yi, &si)| yi * si).sum();

        if ys > T::from(1e-14).expect("1e-14 should be representable in Float") {
            let rho = T::one() / ys;

            // Store in history (maintain max size m)
            if s_history.len() >= m {
                s_history.remove(0);
                y_history.remove(0);
                rho_history.remove(0);
            }
            s_history.push(s);
            y_history.push(y);
            rho_history.push(rho);
        }

        // Check convergence
        let g_new_norm = compute_norm(&g_new);
        let dx_norm = compute_norm(
            &x_new
                .iter()
                .zip(x.iter())
                .map(|(&xi_new, &xi)| xi_new - xi)
                .collect::<Vec<_>>(),
        );
        let df = (f_new - f_val).abs();

        if g_new_norm < cfg.gtol {
            return Ok(OptimizeResult {
                x: x_new,
                fun: f_new,
                grad: g_new,
                nit: k + 1,
                nfev,
                njev,
                success: true,
                message: "Optimization terminated successfully (gradient converged)".to_string(),
            });
        }

        if dx_norm < cfg.xtol {
            return Ok(OptimizeResult {
                x: x_new,
                fun: f_new,
                grad: g_new,
                nit: k + 1,
                nfev,
                njev,
                success: true,
                message: "Optimization terminated successfully (parameter converged)".to_string(),
            });
        }

        if df < cfg.ftol {
            return Ok(OptimizeResult {
                x: x_new,
                fun: f_new,
                grad: g_new,
                nit: k + 1,
                nfev,
                njev,
                success: true,
                message: "Optimization terminated successfully (function value converged)"
                    .to_string(),
            });
        }

        // Update for next iteration
        x = x_new;
        f_val = f_new;
        g = g_new;
    }

    // Maximum iterations reached
    Ok(OptimizeResult {
        x,
        fun: f_val,
        grad: g,
        nit: cfg.max_iter,
        nfev,
        njev,
        success: false,
        message: "Maximum iterations reached".to_string(),
    })
}

/// L-BFGS two-loop recursion for computing search direction
///
/// Computes H_k * g using the stored correction pairs without forming H_k explicitly
fn lbfgs_two_loop_recursion<T: Float + std::iter::Sum>(
    g: &[T],
    s_history: &[Vec<T>],
    y_history: &[Vec<T>],
    rho_history: &[T],
) -> Vec<T> {
    let n = g.len();
    let m = s_history.len();

    if m == 0 {
        // No history: use steepest descent
        return g.iter().map(|&gi| -gi).collect();
    }

    let mut q = g.to_vec();
    let mut alpha = vec![T::zero(); m];

    // First loop (backward)
    for i in (0..m).rev() {
        alpha[i] = rho_history[i] * dot_product(&s_history[i], &q);
        for j in 0..n {
            q[j] = q[j] - alpha[i] * y_history[i][j];
        }
    }

    // Initialize H_0 = gamma * I where gamma = s^T*y / y^T*y
    let last_idx = m - 1;
    let sy: T = dot_product(&s_history[last_idx], &y_history[last_idx]);
    let yy: T = dot_product(&y_history[last_idx], &y_history[last_idx]);
    let gamma = if yy > T::from(1e-14).expect("1e-14 should be representable in Float") {
        sy / yy
    } else {
        T::one()
    };

    // r = gamma * q
    let mut r: Vec<T> = q.iter().map(|&qi| gamma * qi).collect();

    // Second loop (forward)
    for i in 0..m {
        let beta = rho_history[i] * dot_product(&y_history[i], &r);
        for j in 0..n {
            r[j] = r[j] + (alpha[i] - beta) * s_history[i][j];
        }
    }

    // Return -r (search direction)
    r.iter().map(|&ri| -ri).collect()
}

/// Check gradient accuracy using finite differences
///
/// Verifies that the analytical gradient matches numerical approximation.
///
/// # Arguments
///
/// * `f` - Objective function
/// * `grad` - Gradient function to verify
/// * `x` - Point at which to check gradient
/// * `tol` - Tolerance for relative error
///
/// # Examples
///
/// ```
/// use numrs2::optimize::*;
///
/// let f = |x: &[f64]| x[0]*x[0] + x[1]*x[1];
/// let grad = |x: &[f64]| vec![2.0*x[0], 2.0*x[1]];
///
/// assert!(check_gradient(&f, &grad, &[1.0, 2.0], 1e-6));
/// ```
pub fn check_gradient<T, F, G>(f: &F, grad: &G, x: &[T], tol: T) -> bool
where
    T: Float + std::iter::Sum,
    F: Fn(&[T]) -> T,
    G: Fn(&[T]) -> Vec<T>,
{
    let n = x.len();
    let eps = T::from(1e-8).expect("1e-8 should be representable in Float");
    let g_analytical = grad(x);

    for i in 0..n {
        let mut x_plus = x.to_vec();
        let mut x_minus = x.to_vec();
        x_plus[i] = x_plus[i] + eps;
        x_minus[i] = x_minus[i] - eps;

        let f_plus = f(&x_plus);
        let f_minus = f(&x_minus);
        let g_numerical = (f_plus - f_minus)
            / (T::from(2.0).expect("2.0 should be representable in Float") * eps);

        let relative_error = if g_analytical[i].abs()
            > T::from(1e-10).expect("1e-10 should be representable in Float")
        {
            ((g_analytical[i] - g_numerical) / g_analytical[i]).abs()
        } else {
            (g_analytical[i] - g_numerical).abs()
        };

        if relative_error > tol {
            return false;
        }
    }

    true
}