Skip to main content

math_audio_test_functions/functions/
exponential.rs

1//! Exponential test function
2
3use ndarray::Array1;
4
5/// Exponential function - unimodal function
6/// Global minimum: f(x) = -1 at x = (0, 0, ..., 0)
7/// Bounds: x_i in [-1, 1]
8pub fn exponential(x: &Array1<f64>) -> f64 {
9    let sum_squares: f64 = x.iter().map(|&xi| xi.powi(2)).sum();
10    -(-0.5 * sum_squares).exp()
11}
12#[cfg(test)]
13mod tests {
14    use super::*;
15
16    #[test]
17    fn test_exponential_known_properties() {
18        // Test some properties of the exponential function
19        use ndarray::Array1;
20
21        // Test the known global optimum
22        let x_global = Array1::from(vec![0.0, 0.0]);
23        let f_global = exponential(&x_global);
24
25        // Should be -1 at the global optimum
26        assert!(
27            (f_global + 1.0).abs() < 1e-15,
28            "Global optimum value not as expected: {}",
29            f_global
30        );
31
32        // Test that function is always negative and bounded above by -e^(-0.5*0) = -1
33        let test_points = vec![
34            vec![0.5, 0.5],
35            vec![-0.5, 0.3],
36            vec![1.0, -1.0],
37            vec![-1.0, 1.0],
38        ];
39
40        for point in test_points {
41            let x = Array1::from(point.clone());
42            let f = exponential(&x);
43
44            assert!(
45                f <= -0.0,
46                "Function should be negative at {:?}: {}",
47                point,
48                f
49            );
50            assert!(f >= -1.0, "Function should be >= -1 at {:?}: {}", point, f);
51            assert!(
52                f.is_finite(),
53                "Function should be finite at {:?}: {}",
54                point,
55                f
56            );
57        }
58
59        // Test boundary behavior
60        let x_boundary = Array1::from(vec![1.0, 1.0]);
61        let f_boundary = exponential(&x_boundary);
62        assert!(f_boundary <= 0.0, "Function at boundary should be negative");
63        assert!(
64            f_boundary.is_finite(),
65            "Function at boundary should be finite"
66        );
67    }
68}