math_audio_test_functions/functions/
qing.rs1use ndarray::Array1;
4
5pub fn qing(x: &Array1<f64>) -> f64 {
9 x.iter()
10 .enumerate()
11 .map(|(i, &xi)| (xi.powi(2) - (i + 1) as f64).powi(2))
12 .sum()
13}
14#[cfg(test)]
15mod tests {
16 use super::*;
17
18 #[test]
19 fn test_qing_known_properties() {
20 use ndarray::Array1;
22
23 let x_pos = Array1::from(vec![1.0, (2.0_f64).sqrt()]);
25 let f_pos = qing(&x_pos);
26 assert!(
27 f_pos.abs() < 1e-10,
28 "Positive global optimum value not as expected: {}",
29 f_pos
30 );
31
32 let x_neg = Array1::from(vec![-1.0, -(2.0_f64).sqrt()]);
33 let f_neg = qing(&x_neg);
34 assert!(
35 f_neg.abs() < 1e-10,
36 "Negative global optimum value not as expected: {}",
37 f_neg
38 );
39
40 let test_points = vec![
42 vec![0.0, 0.0],
43 vec![10.0, 10.0],
44 vec![-50.0, 30.0],
45 vec![100.0, -200.0],
46 ];
47
48 for point in test_points {
49 let x = Array1::from(point.clone());
50 let f = qing(&x);
51
52 assert!(
53 f >= 0.0,
54 "Function should be non-negative at {:?}: {}",
55 point,
56 f
57 );
58 assert!(
59 f.is_finite(),
60 "Function should be finite at {:?}: {}",
61 point,
62 f
63 );
64 }
65
66 let x_boundary = Array1::from(vec![500.0, -500.0]);
68 let f_boundary = qing(&x_boundary);
69 assert!(
70 f_boundary >= 0.0,
71 "Function at boundary should be non-negative"
72 );
73 assert!(
74 f_boundary.is_finite(),
75 "Function at boundary should be finite"
76 );
77 }
78}