math_audio_test_functions/functions/
exponential.rs1use ndarray::Array1;
4
5pub 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 use ndarray::Array1;
20
21 let x_global = Array1::from(vec![0.0, 0.0]);
23 let f_global = exponential(&x_global);
24
25 assert!(
27 (f_global + 1.0).abs() < 1e-15,
28 "Global optimum value not as expected: {}",
29 f_global
30 );
31
32 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 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}