numra-optim 0.1.4

Optimization for Numra: BFGS, L-BFGS, L-BFGS-B, Levenberg-Marquardt, Nelder-Mead, CMA-ES, SQP, LP/MILP, augmented Lagrangian, NSGA-II.
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
//! Derivative-free optimization methods: Nelder-Mead and Powell.
//!
//! Author: Moussa Leblouba
//! Date: 9 February 2026
//! Modified: 2 May 2026

use numra_core::Scalar;

use crate::error::OptimError;
use crate::types::{IterationRecord, OptimResult, OptimStatus};

// ─── Nelder-Mead Simplex ─────────────────────────────────────────────────

/// Options for the Nelder-Mead simplex method.
#[derive(Clone, Debug)]
pub struct NelderMeadOptions<S: Scalar> {
    pub max_iter: usize,
    pub fatol: S,
    pub xatol: S,
    pub initial_simplex_scale: S,
    pub adaptive: bool,
    pub verbose: bool,
}

impl<S: Scalar> Default for NelderMeadOptions<S> {
    fn default() -> Self {
        Self {
            max_iter: 10_000,
            fatol: S::from_f64(1e-8),
            xatol: S::from_f64(1e-8),
            initial_simplex_scale: S::from_f64(0.05),
            adaptive: true,
            verbose: false,
        }
    }
}

#[allow(clippy::needless_range_loop)]
/// Nelder-Mead simplex method for derivative-free optimization.
///
/// Minimizes `f(x)` starting from `x0`. Uses the standard simplex operations:
/// reflection, expansion, contraction, and shrink.
///
/// # Arguments
///
/// * `f` - Objective function.
/// * `x0` - Initial guess.
/// * `opts` - Algorithm options.
pub fn nelder_mead<S, F>(
    f: F,
    x0: &[S],
    opts: &NelderMeadOptions<S>,
) -> Result<OptimResult<S>, OptimError>
where
    S: Scalar,
    F: Fn(&[S]) -> S,
{
    let start = std::time::Instant::now();
    let n = x0.len();
    if n == 0 {
        return Err(OptimError::DimensionMismatch {
            expected: 1,
            actual: 0,
        });
    }

    // Adaptive parameters (Gao & Han, 2012) for high-dimensional problems
    let (alpha, gamma, rho, sigma) = if opts.adaptive {
        let nf = n as f64;
        (
            S::ONE,
            S::ONE + S::TWO / S::from_f64(nf),
            S::from_f64(0.75 - 0.5 / nf),
            S::ONE - S::ONE / S::from_f64(nf),
        )
    } else {
        (S::ONE, S::TWO, S::HALF, S::HALF)
    };

    // Build initial simplex: x0 and n perturbations
    let mut simplex: Vec<Vec<S>> = Vec::with_capacity(n + 1);
    simplex.push(x0.to_vec());
    for i in 0..n {
        let mut v = x0.to_vec();
        let scale = if v[i].abs() > S::from_f64(1e-10) {
            v[i] * opts.initial_simplex_scale
        } else {
            opts.initial_simplex_scale
        };
        v[i] += scale;
        simplex.push(v);
    }

    let mut f_vals: Vec<S> = simplex.iter().map(|v| f(v)).collect();
    let mut n_feval = n + 1;

    let mut history: Vec<IterationRecord<S>> = Vec::new();
    let mut iterations = 0;
    let mut converged = false;

    for iter in 0..opts.max_iter {
        iterations = iter + 1;

        // Sort simplex by function value
        let mut indices: Vec<usize> = (0..=n).collect();
        indices.sort_by(|&a, &b| f_vals[a].to_f64().partial_cmp(&f_vals[b].to_f64()).unwrap());
        simplex = indices.iter().map(|&i| simplex[i].clone()).collect();
        f_vals = indices.iter().map(|&i| f_vals[i]).collect();

        let f_best = f_vals[0];
        let f_worst = f_vals[n];
        let f_second_worst = f_vals[n - 1];

        if opts.verbose && iter % 100 == 0 {
            eprintln!(
                "NM iter {}: f_best = {:.6e}, f_worst = {:.6e}",
                iter,
                f_best.to_f64(),
                f_worst.to_f64()
            );
        }

        history.push(IterationRecord {
            iteration: iter,
            objective: f_best,
            gradient_norm: S::ZERO,
            step_size: (f_worst - f_best).abs(),
            constraint_violation: S::ZERO,
        });

        // Check convergence: function values spread and simplex size
        let f_range = (f_worst - f_best).abs();
        let mut max_dx = S::ZERO;
        for i in 1..=n {
            for j in 0..n {
                let d = (simplex[i][j] - simplex[0][j]).abs();
                if d > max_dx {
                    max_dx = d;
                }
            }
        }

        if f_range < opts.fatol && max_dx < opts.xatol {
            converged = true;
            break;
        }

        // Centroid of the best n vertices (excluding worst)
        let mut centroid = vec![S::ZERO; n];
        for i in 0..n {
            for j in 0..n {
                centroid[j] += simplex[i][j];
            }
        }
        let n_s = S::from_usize(n);
        for c in centroid.iter_mut() {
            *c /= n_s;
        }

        // Reflection: x_r = centroid + alpha * (centroid - worst)
        let x_r: Vec<S> = (0..n)
            .map(|j| centroid[j] + alpha * (centroid[j] - simplex[n][j]))
            .collect();
        let f_r = f(&x_r);
        n_feval += 1;

        if f_r >= f_best && f_r < f_second_worst {
            // Accept reflection
            simplex[n] = x_r;
            f_vals[n] = f_r;
            continue;
        }

        if f_r < f_best {
            // Try expansion: x_e = centroid + gamma * (x_r - centroid)
            let x_e: Vec<S> = (0..n)
                .map(|j| centroid[j] + gamma * (x_r[j] - centroid[j]))
                .collect();
            let f_e = f(&x_e);
            n_feval += 1;

            if f_e < f_r {
                simplex[n] = x_e;
                f_vals[n] = f_e;
            } else {
                simplex[n] = x_r;
                f_vals[n] = f_r;
            }
            continue;
        }

        // f_r >= f_second_worst: contraction
        if f_r < f_worst {
            // Outside contraction: x_c = centroid + rho * (x_r - centroid)
            let x_c: Vec<S> = (0..n)
                .map(|j| centroid[j] + rho * (x_r[j] - centroid[j]))
                .collect();
            let f_c = f(&x_c);
            n_feval += 1;

            if f_c <= f_r {
                simplex[n] = x_c;
                f_vals[n] = f_c;
                continue;
            }
        } else {
            // Inside contraction: x_cc = centroid - rho * (centroid - worst)
            let x_cc: Vec<S> = (0..n)
                .map(|j| centroid[j] - rho * (centroid[j] - simplex[n][j]))
                .collect();
            let f_cc = f(&x_cc);
            n_feval += 1;

            if f_cc < f_worst {
                simplex[n] = x_cc;
                f_vals[n] = f_cc;
                continue;
            }
        }

        // Shrink: move all vertices toward the best
        for i in 1..=n {
            for j in 0..n {
                simplex[i][j] = simplex[0][j] + sigma * (simplex[i][j] - simplex[0][j]);
            }
            f_vals[i] = f(&simplex[i]);
            n_feval += 1;
        }
    }

    let (status, message) = if converged {
        (
            OptimStatus::FunctionConverged,
            format!("Nelder-Mead converged after {} iterations", iterations),
        )
    } else {
        (
            OptimStatus::MaxIterations,
            format!("Nelder-Mead: max iterations ({}) reached", opts.max_iter),
        )
    };

    Ok(OptimResult {
        x: simplex[0].clone(),
        f: f_vals[0],
        grad: Vec::new(),
        iterations,
        n_feval,
        n_geval: 0,
        converged,
        message,
        status,
        history,
        lambda_eq: Vec::new(),
        lambda_ineq: Vec::new(),
        active_bounds: Vec::new(),
        constraint_violation: S::ZERO,
        wall_time_secs: 0.0,
        pareto: None,
        sensitivity: None,
    }
    .with_wall_time(start))
}

// ─── Powell's Conjugate Direction Method ─────────────────────────────────

/// Options for Powell's conjugate direction method.
#[derive(Clone, Debug)]
pub struct PowellOptions<S: Scalar> {
    pub max_iter: usize,
    pub ftol: S,
    pub xtol: S,
    pub max_line_search_iter: usize,
    pub verbose: bool,
}

impl<S: Scalar> Default for PowellOptions<S> {
    fn default() -> Self {
        Self {
            max_iter: 10_000,
            ftol: S::from_f64(1e-8),
            xtol: S::from_f64(1e-8),
            max_line_search_iter: 100,
            verbose: false,
        }
    }
}

#[allow(clippy::needless_range_loop)]
/// Powell's conjugate direction method for derivative-free optimization.
///
/// Builds a set of conjugate directions by performing sequential line minimizations.
/// The direction set is updated each iteration to maintain conjugacy.
///
/// # Arguments
///
/// * `f` - Objective function.
/// * `x0` - Initial guess.
/// * `opts` - Algorithm options.
pub fn powell<S, F>(f: F, x0: &[S], opts: &PowellOptions<S>) -> Result<OptimResult<S>, OptimError>
where
    S: Scalar,
    F: Fn(&[S]) -> S,
{
    let start = std::time::Instant::now();
    let n = x0.len();
    if n == 0 {
        return Err(OptimError::DimensionMismatch {
            expected: 1,
            actual: 0,
        });
    }

    // Initialize direction set to identity (coordinate directions)
    let mut directions: Vec<Vec<S>> = (0..n)
        .map(|i| {
            let mut d = vec![S::ZERO; n];
            d[i] = S::ONE;
            d
        })
        .collect();

    let mut x = x0.to_vec();
    let mut f_x = f(&x);
    let mut n_feval = 1_usize;
    let mut history: Vec<IterationRecord<S>> = Vec::new();
    let mut converged = false;
    let mut iterations = 0;

    for iter in 0..opts.max_iter {
        iterations = iter + 1;
        let f_start = f_x;
        let x_start = x.clone();

        // Track which direction gave the biggest decrease
        let mut max_decrease = S::ZERO;
        let mut max_decrease_idx = 0;

        // Line search along each direction
        for i in 0..n {
            let f_before = f_x;
            let (x_new, f_new, evals) =
                line_minimize(&f, &x, &directions[i], f_x, opts.max_line_search_iter);
            n_feval += evals;
            let decrease = f_before - f_new;
            if decrease > max_decrease {
                max_decrease = decrease;
                max_decrease_idx = i;
            }
            x = x_new;
            f_x = f_new;
        }

        if opts.verbose && iter % 50 == 0 {
            eprintln!("Powell iter {}: f = {:.6e}", iter, f_x.to_f64());
        }

        history.push(IterationRecord {
            iteration: iter,
            objective: f_x,
            gradient_norm: S::ZERO,
            step_size: max_decrease,
            constraint_violation: S::ZERO,
        });

        // Check convergence
        let f_decrease = (f_start - f_x).abs();
        let mut x_change = S::ZERO;
        for j in 0..n {
            let d = (x[j] - x_start[j]).abs();
            if d > x_change {
                x_change = d;
            }
        }

        if f_decrease < opts.ftol && x_change < opts.xtol {
            converged = true;
            break;
        }

        // Update direction set: replace the direction with max decrease
        // with the overall displacement direction
        let new_dir: Vec<S> = (0..n).map(|j| x[j] - x_start[j]).collect();
        let dir_norm = new_dir.iter().map(|&d| d * d).sum::<S>().sqrt();
        if dir_norm > S::from_f64(1e-20) {
            let normalized: Vec<S> = new_dir.iter().map(|&d| d / dir_norm).collect();
            directions[max_decrease_idx] = normalized;
        }
    }

    let (status, message) = if converged {
        (
            OptimStatus::FunctionConverged,
            format!("Powell converged after {} iterations", iterations),
        )
    } else {
        (
            OptimStatus::MaxIterations,
            format!("Powell: max iterations ({}) reached", opts.max_iter),
        )
    };

    Ok(OptimResult {
        x,
        f: f_x,
        grad: Vec::new(),
        iterations,
        n_feval,
        n_geval: 0,
        converged,
        message,
        status,
        history,
        lambda_eq: Vec::new(),
        lambda_ineq: Vec::new(),
        active_bounds: Vec::new(),
        constraint_violation: S::ZERO,
        wall_time_secs: 0.0,
        pareto: None,
        sensitivity: None,
    }
    .with_wall_time(start))
}

/// Brent's method for 1-D line minimization along direction `d` from `x`.
///
/// Uses bracket-then-Brent approach for robust convergence.
/// Returns `(x_new, f_new, n_evaluations)`.
fn line_minimize<S, F>(f: &F, x: &[S], d: &[S], f_x: S, max_iter: usize) -> (Vec<S>, S, usize)
where
    S: Scalar,
    F: Fn(&[S]) -> S,
{
    let n = x.len();
    let mut evals = 0;

    // Helper: compute f at x + alpha * d
    let f_at_alpha = |alpha: S, evals: &mut usize| -> S {
        let xp: Vec<S> = (0..n).map(|j| x[j] + alpha * d[j]).collect();
        *evals += 1;
        f(&xp)
    };

    // Bracket the minimum using expansion (mnbrak-style)
    let mut ax = S::ZERO;
    let mut bx = S::ONE;
    let mut fa = f_x;
    let mut fb = f_at_alpha(bx, &mut evals);

    // If f(b) > f(a), swap to ensure we go downhill
    if fb > fa {
        core::mem::swap(&mut ax, &mut bx);
        core::mem::swap(&mut fa, &mut fb);
    }

    let gold = S::from_f64(1.618034); // golden ratio
    let mut cx = bx + gold * (bx - ax);
    let mut fc = f_at_alpha(cx, &mut evals);

    // Expand bracket until we have fa >= fb <= fc (i.e., fb is in the valley)
    let mut bracket_iters = 0;
    while fb > fc && bracket_iters < 50 {
        ax = bx;
        fa = fb;
        bx = cx;
        fb = fc;
        cx = bx + gold * (bx - ax);
        fc = f_at_alpha(cx, &mut evals);
        bracket_iters += 1;
    }

    // Ensure a < c
    if ax > cx {
        core::mem::swap(&mut ax, &mut cx);
        core::mem::swap(&mut fa, &mut fc);
    }

    // Brent's method on [ax, cx] with bx as initial best
    let tol = S::from_f64(1e-10);
    let cgold = S::from_f64(0.381966011250105);
    let mut a_br = ax;
    let mut b_br = cx;
    let mut w = bx; // second best
    let mut v = bx; // previous w
    let mut xmin = bx;
    let mut fw = fb;
    let mut fv = fb;
    let mut fx_br = fb;
    let mut e_br = S::ZERO; // distance moved two steps ago
    let mut delta = S::ZERO;

    for _ in 0..max_iter {
        let midpoint = S::HALF * (a_br + b_br);
        let tol1 = tol * xmin.abs() + S::from_f64(1e-20);
        let tol2 = S::TWO * tol1;

        if (xmin - midpoint).abs() <= tol2 - S::HALF * (b_br - a_br) {
            break;
        }

        // Try parabolic interpolation
        let mut use_golden = true;
        if e_br.abs() > tol1 {
            // Parabolic step
            let r = (xmin - w) * (fx_br - fv);
            let q = (xmin - v) * (fx_br - fw);
            let p = (xmin - v) * q - (xmin - w) * r;
            let q = S::TWO * (q - r);
            let (p, q) = if q > S::ZERO { (-p, q) } else { (p, -q) };
            let e_old = e_br;

            if p.abs() < (S::HALF * q * e_old).abs()
                && p > q * (a_br - xmin)
                && p < q * (b_br - xmin)
            {
                // Parabolic step
                delta = p / q;
                let u = xmin + delta;
                if (u - a_br) < tol2 || (b_br - u) < tol2 {
                    delta = if xmin < midpoint { tol1 } else { -tol1 };
                }
                use_golden = false;
                e_br = delta;
            }
        }

        if use_golden {
            // Golden section step
            e_br = if xmin >= midpoint {
                a_br - xmin
            } else {
                b_br - xmin
            };
            delta = cgold * e_br;
        }

        // Evaluate at new point
        let u = if delta.abs() >= tol1 {
            xmin + delta
        } else {
            xmin + if delta > S::ZERO { tol1 } else { -tol1 }
        };
        let fu = f_at_alpha(u, &mut evals);

        if fu <= fx_br {
            if u >= xmin {
                a_br = xmin;
            } else {
                b_br = xmin;
            }
            v = w;
            fv = fw;
            w = xmin;
            fw = fx_br;
            xmin = u;
            fx_br = fu;
        } else {
            if u < xmin {
                a_br = u;
            } else {
                b_br = u;
            }
            if fu <= fw || (w - xmin).abs() < S::from_f64(1e-20) {
                v = w;
                fv = fw;
                w = u;
                fw = fu;
            } else if fu <= fv
                || (v - xmin).abs() < S::from_f64(1e-20)
                || (v - w).abs() < S::from_f64(1e-20)
            {
                v = u;
                fv = fu;
            }
        }
    }

    // Compare with original point
    if f_x <= fx_br {
        (x.to_vec(), f_x, evals)
    } else {
        let x_best: Vec<S> = (0..n).map(|j| x[j] + xmin * d[j]).collect();
        (x_best, fx_br, evals)
    }
}

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

    // ─── Nelder-Mead tests ───

    #[test]
    fn test_nelder_mead_quadratic() {
        // min x0^2 + x1^2, optimal at (0, 0)
        let result = nelder_mead(
            |x: &[f64]| x[0] * x[0] + x[1] * x[1],
            &[5.0, 3.0],
            &NelderMeadOptions::default(),
        )
        .unwrap();
        assert!(result.converged, "did not converge: {}", result.message);
        assert!(result.x[0].abs() < 1e-4, "x0={}", result.x[0]);
        assert!(result.x[1].abs() < 1e-4, "x1={}", result.x[1]);
        assert!(result.f < 1e-8, "f={}", result.f);
    }

    #[test]
    fn test_nelder_mead_rosenbrock() {
        // Rosenbrock: min (1-x)^2 + 100*(y - x^2)^2
        let result = nelder_mead(
            |x: &[f64]| (1.0 - x[0]).powi(2) + 100.0 * (x[1] - x[0] * x[0]).powi(2),
            &[-1.0, 1.0],
            &NelderMeadOptions {
                max_iter: 50_000,
                ..Default::default()
            },
        )
        .unwrap();
        assert!(result.f < 1e-4, "f={}, x={:?}", result.f, result.x);
        assert!(
            (result.x[0] - 1.0).abs() < 0.05,
            "x0={}, expected ~1.0",
            result.x[0]
        );
    }

    #[test]
    fn test_nelder_mead_1d() {
        let result = nelder_mead(
            |x: &[f64]| (x[0] - 3.0).powi(2),
            &[10.0],
            &NelderMeadOptions::default(),
        )
        .unwrap();
        assert!(result.converged);
        assert!((result.x[0] - 3.0).abs() < 1e-4, "x={}", result.x[0]);
    }

    #[test]
    fn test_nelder_mead_beale() {
        // Beale function, min at (3, 0.5)
        let result = nelder_mead(
            |x: &[f64]| {
                (1.5 - x[0] + x[0] * x[1]).powi(2)
                    + (2.25 - x[0] + x[0] * x[1] * x[1]).powi(2)
                    + (2.625 - x[0] + x[0] * x[1].powi(3)).powi(2)
            },
            &[1.0, 1.0],
            &NelderMeadOptions {
                max_iter: 50_000,
                ..Default::default()
            },
        )
        .unwrap();
        assert!(result.f < 1e-4, "f={}", result.f);
    }

    // ─── Powell tests ───

    #[test]
    fn test_powell_quadratic() {
        let result = powell(
            |x: &[f64]| x[0] * x[0] + x[1] * x[1],
            &[5.0, 3.0],
            &PowellOptions::default(),
        )
        .unwrap();
        assert!(result.converged, "did not converge: {}", result.message);
        assert!(result.x[0].abs() < 1e-4, "x0={}", result.x[0]);
        assert!(result.x[1].abs() < 1e-4, "x1={}", result.x[1]);
    }

    #[test]
    fn test_powell_quadratic_offset() {
        // min (x-2)^2 + (y-3)^2
        let result = powell(
            |x: &[f64]| (x[0] - 2.0).powi(2) + (x[1] - 3.0).powi(2),
            &[0.0, 0.0],
            &PowellOptions::default(),
        )
        .unwrap();
        assert!(result.converged);
        assert!((result.x[0] - 2.0).abs() < 1e-4, "x0={}", result.x[0]);
        assert!((result.x[1] - 3.0).abs() < 1e-4, "x1={}", result.x[1]);
    }

    #[test]
    fn test_powell_rosenbrock() {
        let result = powell(
            |x: &[f64]| (1.0 - x[0]).powi(2) + 100.0 * (x[1] - x[0] * x[0]).powi(2),
            &[-1.0, 1.0],
            &PowellOptions {
                max_iter: 50_000,
                ..Default::default()
            },
        )
        .unwrap();
        assert!(result.f < 0.01, "f={}", result.f);
    }

    #[test]
    fn test_powell_1d() {
        let result = powell(
            |x: &[f64]| (x[0] - 7.0).powi(2),
            &[0.0],
            &PowellOptions::default(),
        )
        .unwrap();
        assert!(result.converged);
        assert!((result.x[0] - 7.0).abs() < 1e-4, "x={}", result.x[0]);
    }
}