``` 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
```
```//! The module with functions to test optimization algorithms.

use num::Float;

/// Paraboloid.
///
/// y = (x0 - 1)^2 + (x1 - 2)^2 + (x2 - 3)^2 ... (xn - n)^2
/// The min val is 0.0 for point (1.0, 2.0, 3.0, ... n).
///
/// ```
/// use optlib_testfunc::paraboloid;
///
/// let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
/// let value = paraboloid(&x);
///
/// assert!(value < 1e-5);
/// assert!(value >= 0.0);
/// ```
pub fn paraboloid<G: Float>(x: &Vec<G>) -> f64 {
let mut result = G::from(0.0).unwrap();
for (n, val) in x.iter().enumerate() {
result = result + (*val - (G::from(n).unwrap() + G::from(1.0).unwrap())).powi(2);
}

result.to_f64().unwrap()
}

/// The Schwefel function
///
/// # Parameters
/// Any x lies in [-500.0; 500.0].
/// For any x lies in [-500.0; 500.0] global minimum located in x' = (420.9687, 420.9687, ...).
/// f(x') = 0.
///
/// ```
/// use optlib_testfunc::schwefel;
///
/// let x = vec![420.9687, 420.9687, 420.9687, 420.9687];
/// let value = schwefel(&x);
/// assert!(value.abs() < 1e-4);
/// ```
pub fn schwefel<G: Float>(x: &Vec<G>) -> f64 {
let result = G::from(418.9829).unwrap() * G::from(x.len()).unwrap() - x.iter().fold(G::zero(), |acc, &xi| acc + xi * xi.abs().sqrt().sin());

result.to_f64().unwrap()
}

/// The Rastrigin function
///
/// # Parameters
/// Global minimum is x' = (0, 0, ...) for xn in (-5.12; +5.12)
/// f(x') = 0
///
/// ```
/// use optlib_testfunc::rastrigin;
///
/// let x = vec![0.0_f32, 0.0_f32, 0.0_f32, 0.0_f32, 0.0_f32, 0.0_f32];
/// let value = rastrigin(&x);
/// assert!(value.abs() < 1e-7);
/// ```
pub fn rastrigin<G: Float>(x: &Vec<G>) -> f64 {
let a = G::from(10.0_f64).unwrap();
let pi = G::from(3.14159265358979_f64).unwrap();
let result = a * G::from(x.len()).unwrap() +
x.iter().fold(G::zero(), |acc, &xi| acc + xi * xi - a * (G::from(2).unwrap() * pi * xi).cos());

result.to_f64().unwrap()
}

/// The Rosenbrock function
///
/// # Parameters
/// Global minimum is x' = (1, 1, ...) for xn in (-inf; +inf)
/// f(x') = 0
///
/// ```
/// use optlib_testfunc::rosenbrock;
///
/// let x = vec![1.0_f32, 1.0_f32, 1.0_f32, 1.0_f32, 1.0_f32, 1.0_f32];
/// let value = rosenbrock(&x);
/// assert!(value.abs() < 1e-7);
/// ```
pub fn rosenbrock<G: Float>(x: &Vec<G>) -> f64 {
let mut sum = G::from(0.0).unwrap();
for n in 0..x.len() - 1 {
sum = sum + G::from(100.0).unwrap() * ((x[n + 1] - x[n] * x[n]).powi(2)) + (G::from(1.0).unwrap() - x[n]).powi(2);
}

sum.to_f64().unwrap()
}
```