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
//! Conjugate Gradient optimization methods
//!
//! This module provides nonlinear conjugate gradient methods for unconstrained optimization:
//! - Fletcher-Reeves (FR)
//! - Polak-Ribiere (PR)
//! - Hestenes-Stiefel (HS)
//!
//! Conjugate gradient methods are efficient for large-scale optimization problems
//! where computing and storing the full Hessian matrix (as in Newton methods) is impractical.
//!
//! # Examples
//!
//! ```
//! use numrs2::optimize::*;
//!
//! // Minimize the Rosenbrock function using Fletcher-Reeves
//! let f = |x: &[f64]| {
//!     let (x0, x1) = (x[0], x[1]);
//!     (1.0 - x0).powi(2) + 100.0 * (x1 - x0 * x0).powi(2)
//! };
//!
//! let grad = |x: &[f64]| {
//!     let (x0, x1) = (x[0], x[1]);
//!     vec![
//!         -2.0 * (1.0 - x0) - 400.0 * x0 * (x1 - x0 * x0),
//!         200.0 * (x1 - x0 * x0),
//!     ]
//! };
//!
//! let result = conjugate_gradient_fr(f, grad, &[0.0, 0.0], None)
//!     .expect("CG-FR optimization should succeed");
//! assert!(result.success);
//! ```

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

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

/// Conjugate gradient method variant
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConjugateGradientMethod {
    /// Fletcher-Reeves method
    FletcherReeves,
    /// Polak-Ribiere method
    PolakRibiere,
    /// Hestenes-Stiefel method
    HesteneStiefel,
}

/// Trait for computing beta parameter in conjugate gradient methods
pub trait BetaComputation<T: Float> {
    /// Compute beta parameter for direction update
    ///
    /// # Arguments
    /// * `g_new` - Current gradient
    /// * `g_old` - Previous gradient
    /// * `d_old` - Previous search direction
    fn compute_beta(g_new: &[T], g_old: &[T], d_old: &[T]) -> T;
}

/// Fletcher-Reeves beta computation: β_k = ||g_k||² / ||g_{k-1}||²
pub struct FletcherReevesBeta;

impl<T: Float + std::iter::Sum> BetaComputation<T> for FletcherReevesBeta {
    fn compute_beta(g_new: &[T], g_old: &[T], _d_old: &[T]) -> T {
        let g_new_norm_sq: T = g_new.iter().map(|&x| x * x).sum();
        let g_old_norm_sq: T = g_old.iter().map(|&x| x * x).sum();

        // Avoid division by zero
        if g_old_norm_sq < T::epsilon() {
            T::zero()
        } else {
            g_new_norm_sq / g_old_norm_sq
        }
    }
}

/// Polak-Ribiere beta computation: β_k = g_k^T(g_k - g_{k-1}) / ||g_{k-1}||²
pub struct PolakRibiereBeta;

impl<T: Float + std::iter::Sum> BetaComputation<T> for PolakRibiereBeta {
    fn compute_beta(g_new: &[T], g_old: &[T], _d_old: &[T]) -> T {
        let numerator: T = g_new
            .iter()
            .zip(g_old.iter())
            .map(|(&gi_new, &gi_old)| gi_new * (gi_new - gi_old))
            .sum();
        let g_old_norm_sq: T = g_old.iter().map(|&x| x * x).sum();

        // Avoid division by zero
        if g_old_norm_sq < T::epsilon() {
            T::zero()
        } else {
            // Polak-Ribiere can be negative, so we take max with 0 for robustness
            T::max(numerator / g_old_norm_sq, T::zero())
        }
    }
}

/// Hestenes-Stiefel beta computation: β_k = g_k^T(g_k - g_{k-1}) / (g_k - g_{k-1})^T d_{k-1}
pub struct HesteneStiefelBeta;

impl<T: Float + std::iter::Sum> BetaComputation<T> for HesteneStiefelBeta {
    fn compute_beta(g_new: &[T], g_old: &[T], d_old: &[T]) -> T {
        // Compute g_k - g_{k-1}
        let g_diff: Vec<T> = g_new
            .iter()
            .zip(g_old.iter())
            .map(|(&gi_new, &gi_old)| gi_new - gi_old)
            .collect();

        let numerator: T = g_new
            .iter()
            .zip(g_diff.iter())
            .map(|(&gi_new, &gi_diff)| gi_new * gi_diff)
            .sum();

        let denominator: T = g_diff
            .iter()
            .zip(d_old.iter())
            .map(|(&gi_diff, &di_old)| gi_diff * di_old)
            .sum();

        // Avoid division by zero
        if denominator.abs() < T::epsilon() {
            T::zero()
        } else {
            numerator / denominator
        }
    }
}

/// Generic conjugate gradient optimization
///
/// Minimizes a scalar function using a conjugate gradient method with the specified
/// beta computation strategy.
///
/// # Arguments
///
/// * `f` - Objective function to minimize
/// * `grad` - Gradient function
/// * `x0` - Initial guess
/// * `config` - Optional configuration (uses defaults if None)
///
/// # Type Parameters
///
/// * `T` - Float type (f32 or f64)
/// * `F` - Objective function type
/// * `G` - Gradient function type
/// * `B` - Beta computation strategy
///
/// # Returns
///
/// An `OptimizeResult` containing the optimal point and convergence information
fn conjugate_gradient_generic<T, F, G, B>(
    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>,
    B: BetaComputation<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;

    // Initial search direction is negative gradient
    let mut d: Vec<T> = g.iter().map(|&gi| -gi).collect();

    // Compute initial gradient norm
    let mut 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(),
        });
    }

    // Store old gradient for beta computation
    let mut g_old = g.clone();

    // Conjugate gradient iteration
    for k in 0..cfg.max_iter {
        // Line search along direction d
        let (alpha, f_new, nfev_ls) = wolfe_line_search(&f, &grad, &x, &d, f_val, &g, &cfg)?;
        nfev += nfev_ls;
        njev += nfev_ls;

        // Update position
        for i in 0..n {
            x[i] = x[i] + alpha * d[i];
        }

        // Evaluate at new position
        f_val = f_new;
        g = grad(&x);
        njev += 1;

        g_norm = compute_norm(&g);

        // Check convergence
        if g_norm < cfg.gtol {
            return Ok(OptimizeResult {
                x,
                fun: f_val,
                grad: g,
                nit: k + 1,
                nfev,
                njev,
                success: true,
                message: "Optimization terminated successfully (gradient norm below tolerance)"
                    .to_string(),
            });
        }

        // Compute beta parameter using the specific method
        let beta = B::compute_beta(&g, &g_old, &d);

        // Update search direction: d = -g + beta * d_old
        let d_old = d.clone();
        for i in 0..n {
            d[i] = -g[i] + beta * d_old[i];
        }

        // Ensure descent direction (should satisfy d^T g < 0)
        let dg: T = dot_product(&d, &g);
        if dg >= T::zero() {
            // If not a descent direction, restart with steepest descent
            for i in 0..n {
                d[i] = -g[i];
            }
        }

        // Update old gradient
        g_old = g.clone();

        // Check function value change for convergence
        if k > 0 {
            // This is a safeguard - we check relative function change
            // (not always reliable for CG but prevents infinite loops)
        }
    }

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

/// Fletcher-Reeves conjugate gradient method
///
/// This method uses β_k = ||g_k||² / ||g_{k-1}||² for direction updates.
/// It is guaranteed to produce descent directions and has good global convergence properties.
///
/// # 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 = conjugate_gradient_fr(f, grad, &[3.0, 4.0], None)
///     .expect("CG-FR should succeed");
/// assert!(result.success);
/// ```
pub fn conjugate_gradient_fr<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>,
{
    conjugate_gradient_generic::<T, F, G, FletcherReevesBeta>(f, grad, x0, config)
}

/// Polak-Ribiere conjugate gradient method
///
/// This method uses β_k = g_k^T(g_k - g_{k-1}) / ||g_{k-1}||² for direction updates.
/// It often performs better than Fletcher-Reeves in practice and automatically restarts
/// when β becomes negative.
///
/// # Examples
///
/// ```
/// use numrs2::optimize::*;
///
/// let f = |x: &[f64]| (x[0] - 1.0).powi(2) + (x[1] - 2.0).powi(2);
/// let grad = |x: &[f64]| vec![2.0*(x[0] - 1.0), 2.0*(x[1] - 2.0)];
///
/// let result = conjugate_gradient_pr(f, grad, &[0.0, 0.0], None)
///     .expect("CG-PR should succeed");
/// assert!(result.success);
/// ```
pub fn conjugate_gradient_pr<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>,
{
    conjugate_gradient_generic::<T, F, G, PolakRibiereBeta>(f, grad, x0, config)
}

/// Hestenes-Stiefel conjugate gradient method
///
/// This method uses β_k = g_k^T(g_k - g_{k-1}) / (g_k - g_{k-1})^T d_{k-1} for direction updates.
/// It is more robust than Fletcher-Reeves and often competitive with Polak-Ribiere.
///
/// # Examples
///
/// ```
/// use numrs2::optimize::*;
///
/// let f = |x: &[f64]| x[0].powi(4) + x[1].powi(4);
/// let grad = |x: &[f64]| vec![4.0*x[0].powi(3), 4.0*x[1].powi(3)];
///
/// let result = conjugate_gradient_hs(f, grad, &[2.0, 2.0], None)
///     .expect("CG-HS should succeed");
/// assert!(result.success);
/// ```
pub fn conjugate_gradient_hs<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>,
{
    conjugate_gradient_generic::<T, F, G, HesteneStiefelBeta>(f, grad, x0, config)
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_cg_fr_quadratic() {
        // 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 = conjugate_gradient_fr(f, grad, &[3.0, 4.0], None)
            .expect("CG-FR should succeed for quadratic");
        assert!(result.success);
        assert!(result.fun < 1e-10);
        assert_relative_eq!(result.x[0], 0.0, epsilon = 1e-5);
        assert_relative_eq!(result.x[1], 0.0, epsilon = 1e-5);
    }

    #[test]
    fn test_cg_pr_quadratic() {
        // Minimize f(x,y) = (x-2)^2 + (y-3)^2
        let f = |x: &[f64]| (x[0] - 2.0).powi(2) + (x[1] - 3.0).powi(2);
        let grad = |x: &[f64]| vec![2.0 * (x[0] - 2.0), 2.0 * (x[1] - 3.0)];

        let result = conjugate_gradient_pr(f, grad, &[0.0, 0.0], None)
            .expect("CG-PR should succeed for quadratic");
        assert!(result.success);
        assert_relative_eq!(result.x[0], 2.0, epsilon = 1e-5);
        assert_relative_eq!(result.x[1], 3.0, epsilon = 1e-5);
    }

    #[test]
    fn test_cg_hs_quadratic() {
        // Minimize f(x,y) = x^2 + 4*y^2
        let f = |x: &[f64]| x[0] * x[0] + 4.0 * x[1] * x[1];
        let grad = |x: &[f64]| vec![2.0 * x[0], 8.0 * x[1]];

        let result = conjugate_gradient_hs(f, grad, &[5.0, 5.0], None)
            .expect("CG-HS should succeed for quadratic");
        assert!(result.success);
        assert_relative_eq!(result.x[0], 0.0, epsilon = 1e-5);
        assert_relative_eq!(result.x[1], 0.0, epsilon = 1e-5);
    }

    #[test]
    fn test_cg_fr_rosenbrock() {
        // Rosenbrock function
        let f = |x: &[f64]| {
            let (x0, x1) = (x[0], x[1]);
            (1.0 - x0).powi(2) + 100.0 * (x1 - x0 * x0).powi(2)
        };
        let grad = |x: &[f64]| {
            let (x0, x1) = (x[0], x[1]);
            vec![
                -2.0 * (1.0 - x0) - 400.0 * x0 * (x1 - x0 * x0),
                200.0 * (x1 - x0 * x0),
            ]
        };

        let result = conjugate_gradient_fr(f, grad, &[0.0, 0.0], None)
            .expect("CG-FR should succeed for Rosenbrock");
        assert!(result.success);
        assert_relative_eq!(result.x[0], 1.0, epsilon = 1e-2);
        assert_relative_eq!(result.x[1], 1.0, epsilon = 1e-2);
    }

    #[test]
    fn test_cg_pr_rosenbrock() {
        // Polak-Ribiere often performs better on Rosenbrock
        let f = |x: &[f64]| {
            let (x0, x1) = (x[0], x[1]);
            (1.0 - x0).powi(2) + 100.0 * (x1 - x0 * x0).powi(2)
        };
        let grad = |x: &[f64]| {
            let (x0, x1) = (x[0], x[1]);
            vec![
                -2.0 * (1.0 - x0) - 400.0 * x0 * (x1 - x0 * x0),
                200.0 * (x1 - x0 * x0),
            ]
        };

        let result = conjugate_gradient_pr(f, grad, &[0.0, 0.0], None)
            .expect("CG-PR should succeed for Rosenbrock");
        assert!(result.success);
        assert_relative_eq!(result.x[0], 1.0, epsilon = 1e-2);
        assert_relative_eq!(result.x[1], 1.0, epsilon = 1e-2);
    }

    #[test]
    fn test_cg_hs_beale() {
        // Beale's function
        let f = |x: &[f64]| {
            let (x0, x1) = (x[0], x[1]);
            (1.5 - x0 + x0 * x1).powi(2)
                + (2.25 - x0 + x0 * x1 * x1).powi(2)
                + (2.625 - x0 + x0 * x1.powi(3)).powi(2)
        };

        let grad = |x: &[f64]| {
            let (x0, x1) = (x[0], x[1]);
            let t1 = 1.5 - x0 + x0 * x1;
            let t2 = 2.25 - x0 + x0 * x1 * x1;
            let t3 = 2.625 - x0 + x0 * x1.powi(3);

            let df_dx0 = 2.0 * t1 * (-1.0 + x1)
                + 2.0 * t2 * (-1.0 + x1 * x1)
                + 2.0 * t3 * (-1.0 + x1.powi(3));
            let df_dx1 =
                2.0 * t1 * x0 + 2.0 * t2 * 2.0 * x0 * x1 + 2.0 * t3 * 3.0 * x0 * x1.powi(2);

            vec![df_dx0, df_dx1]
        };

        let result = conjugate_gradient_hs(f, grad, &[1.0, 1.0], None)
            .expect("CG-HS should succeed for Beale");
        assert!(result.success);
        assert_relative_eq!(result.x[0], 3.0, epsilon = 1e-2);
        assert_relative_eq!(result.x[1], 0.5, epsilon = 1e-2);
    }

    #[test]
    fn test_cg_higher_dimension() {
        // Test in higher dimensions
        let f = |x: &[f64]| x.iter().map(|&xi| xi * xi).sum();
        let grad = |x: &[f64]| x.iter().map(|&xi| 2.0 * xi).collect();

        let x0 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let result = conjugate_gradient_fr(f, grad, &x0, None)
            .expect("CG-FR should succeed in higher dimensions");

        assert!(result.success);
        for &xi in &result.x {
            assert_relative_eq!(xi, 0.0, epsilon = 1e-4);
        }
    }

    #[test]
    fn test_cg_initial_optimum() {
        // Test when starting at optimum
        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 = conjugate_gradient_fr(f, grad, &[0.0, 0.0], None)
            .expect("CG-FR should handle initial optimum");
        assert!(result.success);
        assert_eq!(result.nit, 0);
    }

    // Property-based tests
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn prop_cg_fr_quadratic_convergence(
            a in -10.0f64..10.0,
            b in -10.0f64..10.0,
            x0 in -5.0f64..5.0,
            y0 in -5.0f64..5.0
        ) {
            // Any quadratic should converge to minimum
            let f = |x: &[f64]| (x[0] - a).powi(2) + (x[1] - b).powi(2);
            let grad = |x: &[f64]| vec![2.0 * (x[0] - a), 2.0 * (x[1] - b)];

            let result = conjugate_gradient_fr(f, grad, &[x0, y0], None)
                .expect("CG-FR should succeed for any quadratic");
            prop_assert!(result.success);
            prop_assert!((result.x[0] - a).abs() < 1e-3);
            prop_assert!((result.x[1] - b).abs() < 1e-3);
        }

        #[test]
        fn prop_cg_pr_always_descends(
            x0 in -5.0f64..5.0,
            y0 in -5.0f64..5.0
        ) {
            // CG-PR should always find a local minimum or better
            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 initial_val = f(&[x0, y0]);
            let result = conjugate_gradient_pr(f, grad, &[x0, y0], None)
                .expect("CG-PR should succeed");

            prop_assert!(result.fun <= initial_val + 1e-6);
        }
    }
}