math_audio_test_functions/functions/
beale.rs1use ndarray::Array1;
4
5pub fn beale(x: &Array1<f64>) -> f64 {
9 let x1 = x[0];
10 let x2 = x[1];
11 (1.5 - x1 + x1 * x2).powi(2)
12 + (2.25 - x1 + x1 * x2.powi(2)).powi(2)
13 + (2.625 - x1 + x1 * x2.powi(3)).powi(2)
14}
15#[cfg(test)]
16mod tests {
17 use super::*;
18
19 #[test]
20 fn test_beale_known_properties() {
21 use crate::{FunctionMetadata, get_function_metadata};
22 use ndarray::Array1;
23
24 let metadata = get_function_metadata();
26 let meta = metadata
27 .get("beale")
28 .expect("Function beale should have metadata");
29
30 for (minimum_coords, expected_value) in &meta.global_minima {
32 assert!(
33 minimum_coords.len() >= meta.bounds.len() || meta.bounds.len() == 1,
34 "Global minimum coordinates should match bounds dimensions"
35 );
36
37 for (i, &coord) in minimum_coords.iter().enumerate() {
38 if i < meta.bounds.len() {
39 let (lower, upper) = meta.bounds[i];
40 assert!(
41 coord >= lower && coord <= upper,
42 "Global minimum coordinate {} = {} should be within bounds [{} {}]",
43 i,
44 coord,
45 lower,
46 upper
47 );
48 }
49 }
50 }
51
52 let tolerance = 1e-6; for (minimum_coords, expected_value) in &meta.global_minima {
55 let x = Array1::from_vec(minimum_coords.clone());
56 let actual_value = beale(&x);
57
58 let error = (actual_value - expected_value).abs();
59 assert!(
60 error <= tolerance,
61 "Function value at global minimum {:?} should be {}, got {}, error: {}",
62 minimum_coords,
63 expected_value,
64 actual_value,
65 error
66 );
67 }
68
69 if !meta.global_minima.is_empty() {
71 let (first_minimum, _) = &meta.global_minima[0];
72 let x = Array1::from_vec(first_minimum.clone());
73 let result = beale(&x);
74
75 assert!(
76 result.is_finite(),
77 "Function should return finite values at global minimum"
78 );
79 assert!(
80 !result.is_nan(),
81 "Function should not return NaN at global minimum"
82 );
83 }
84 }
85}