Skip to main content

math_audio_test_functions/functions/
beale.rs

1//! Beale test function
2
3use ndarray::Array1;
4
5/// Beale function - 2D multimodal
6/// Global minimum: f(x) = 0 at x = (3, 0.5)
7/// Bounds: x_i in [-4.5, 4.5]
8pub 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        // Get metadata for this function
25        let metadata = get_function_metadata();
26        let meta = metadata
27            .get("beale")
28            .expect("Function beale should have metadata");
29
30        // Test 1: Verify global minima are within bounds
31        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        // Test 2: Verify function evaluates to expected values at global minima
53        let tolerance = 1e-6; // Reasonable tolerance for numerical precision
54        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        // Test 3: Basic function properties
70        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}