/// Recursive ternary search algorithm for finding maximum of unimodal function
Recursive ternary search algorithm for finding maximum of unimodal function
Recursive ternary search algorithm for finding maximum of unimodal function
pub fn ternary_search_max_rec(
    f: fn(f32) -> f32,
    start: f32,
    end: f32,
    absolute_precision: f32,
) -> f32 {
    if (end - start).abs() >= absolute_precision {
        let mid1 = start + (end - start) / 3.0;
        let mid2 = end - (end - start) / 3.0;
        let r1 = f(mid1);
        let r2 = f(mid2);
        if r1 < r2 {
            return ternary_search_max_rec(f, mid1, end, absolute_precision);
        } else if r1 > r2 {
            return ternary_search_max_rec(f, start, mid2, absolute_precision);
        } else {
            return ternary_search_max_rec(f, mid1, mid2, absolute_precision);
        }
    }
    f(start)
}
pub fn ternary_search_max_rec ( f : fn ( f32 ) -> f32 , start : f32 , end : f32 , absolute_precision : f32 , ) -> f32 { if ( end - start ) . abs ( ) >= absolute_precision { let mid1 = start + ( end - start ) / 3.0 ; let mid2 = end - ( end - start ) / 3.0 ; let r1 = f ( mid1 ) ; let r2 = f ( mid2 ) ; if r1 < r2 { return ternary_search_max_rec ( f , mid1 , end , absolute_precision ) ; } else if r1 > r2 { return ternary_search_max_rec ( f , start , mid2 , absolute_precision ) ; } else { return ternary_search_max_rec ( f , mid1 , mid2 , absolute_precision ) ; } } f ( start ) }
Recursive ternary search algorithm for finding maximum of unimodal function
Recursive ternary search algorithm for finding maximum of unimodal function
f
fn(f32) -> f32
start
f32
end
f32
absolute_precision
f32