| 1 | // Copyright 2015 Mikhail Vorotilov. See the COPYRIGHT |
| 2 | // file at the top-level directory of this distribution and at |
| 3 | // http://rust-lang.org/COPYRIGHT. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | // option. This file may not be copied, modified, or distributed |
| 9 | // except according to those terms. |
| 10 | |
| 11 | use super::super::FloatType; |
| 12 | use super::Convergency; |
| 13 | use super::SearchError; |
| 14 | |
| 15 | /// Arrange two points so that the greatest value is first |
| 16 | fn arrange<F: FloatType>(a: F, ya: F, b: F, yb: F) -> (F, F, F, F) { |
| 17 | if ya.abs() > yb.abs() { |
| 18 | (a, ya, b, yb) |
| 19 | } else { |
| 20 | (b, yb, a, ya) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /// Find a root of the function f(x) = 0 using the Brent method. |
| 25 | /// |
| 26 | /// Pro |
| 27 | /// |
| 28 | /// + Fast |
| 29 | /// + Robust |
| 30 | /// + No need for derivative function |
| 31 | /// |
| 32 | /// Contra |
| 33 | /// |
| 34 | /// - Complicated |
| 35 | /// - Needs initial bracketing |
| 36 | /// |
| 37 | /// # Failures |
| 38 | /// ## NoBracketing |
| 39 | /// Initial values do not bracket the root. |
| 40 | /// ## NoConvergency |
| 41 | /// Algorithm cannot find a root within the given number of iterations. |
| 42 | /// # Examples |
| 43 | /// |
| 44 | /// ``` |
| 45 | /// use roots::SimpleConvergency; |
| 46 | /// use roots::find_root_brent; |
| 47 | /// |
| 48 | /// let f = |x| { 1f64*x*x - 1f64 }; |
| 49 | /// let mut convergency = SimpleConvergency { eps:1e-15f64, max_iter:30 }; |
| 50 | /// |
| 51 | /// let root1 = find_root_brent(10f64, 0f64, &f, &mut convergency); |
| 52 | /// // Returns approximately Ok(1); |
| 53 | /// |
| 54 | /// let root2 = find_root_brent(-10f64, 0f64, &f, &mut 1e-15f64); |
| 55 | /// // Returns approximately Ok(-1); |
| 56 | /// ``` |
| 57 | pub fn find_root_brent<F, Func>(a: F, b: F, mut f: Func, convergency: &mut dyn Convergency<F>) -> Result<F, SearchError> |
| 58 | where |
| 59 | F: FloatType, |
| 60 | Func: FnMut(F) -> F, |
| 61 | { |
| 62 | let (mut a, mut ya, mut b, mut yb) = arrange(a, f(a), b, f(b)); |
| 63 | if ya * yb > F::zero() { |
| 64 | return Err(SearchError::NoBracketing); |
| 65 | } |
| 66 | |
| 67 | let (mut c, mut yc, mut d) = (a, ya, a); |
| 68 | let mut flag = true; |
| 69 | |
| 70 | let _2 = F::from(2i16); |
| 71 | let _3 = F::from(3i16); |
| 72 | let _4 = F::from(4i16); |
| 73 | |
| 74 | let mut iter = 0; |
| 75 | loop { |
| 76 | if convergency.is_root_found(ya) { |
| 77 | return Ok(a); |
| 78 | } |
| 79 | if convergency.is_root_found(yb) { |
| 80 | return Ok(b); |
| 81 | } |
| 82 | if convergency.is_converged(a, b) { |
| 83 | return Ok(c); |
| 84 | } |
| 85 | let mut s = if (ya != yc) && (yb != yc) { |
| 86 | a * yb * yc / ((ya - yb) * (ya - yc)) + b * ya * yc / ((yb - ya) * (yb - yc)) + c * ya * yb / ((yc - ya) * (yc - yb)) |
| 87 | } else { |
| 88 | b - yb * (b - a) / (yb - ya) |
| 89 | }; |
| 90 | |
| 91 | let cond1 = (s - b) * (s - (_3 * a + b) / _4) > F::zero(); |
| 92 | let cond2 = flag && (s - b).abs() >= (b - c).abs() / _2; |
| 93 | let cond3 = !flag && (s - b).abs() >= (c - d).abs() / _2; |
| 94 | let cond4 = flag && convergency.is_converged(b, c); |
| 95 | let cond5 = !flag && convergency.is_converged(c, d); |
| 96 | |
| 97 | if cond1 || cond2 || cond3 || cond4 || cond5 { |
| 98 | s = (a + b) / _2; |
| 99 | flag = true; |
| 100 | } else { |
| 101 | flag = false; |
| 102 | } |
| 103 | |
| 104 | let ys = f(s); |
| 105 | d = c; |
| 106 | c = b; |
| 107 | yc = yb; |
| 108 | if ya * ys < F::zero() { |
| 109 | // Root bracketed between a ans s |
| 110 | match arrange(a, f(a), s, ys) { |
| 111 | (_a, _ya, _b, _yb) => { |
| 112 | a = _a; |
| 113 | ya = _ya; |
| 114 | b = _b; |
| 115 | yb = _yb; |
| 116 | } |
| 117 | } |
| 118 | } else { |
| 119 | // Root bracketed between s ans b |
| 120 | match arrange(s, ys, b, f(b)) { |
| 121 | (_a, _ya, _b, _yb) => { |
| 122 | a = _a; |
| 123 | ya = _ya; |
| 124 | b = _b; |
| 125 | yb = _yb; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | iter = iter + 1; |
| 131 | if convergency.is_iteration_limit_reached(iter) { |
| 132 | return Err(SearchError::NoConvergency); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | #[cfg(test)] |
| 138 | mod test { |
| 139 | use super::super::*; |
| 140 | use super::*; |
| 141 | |
| 142 | #[test] |
| 143 | fn test_find_root_brent() { |
| 144 | let f = |x| 1f64 * x * x - 1f64; |
| 145 | let mut conv = debug_convergency::DebugConvergency::new(1e-15f64, 30); |
| 146 | |
| 147 | conv.reset(); |
| 148 | assert_float_eq!(1e-15f64, find_root_brent(10f64, 0f64, &f, &mut conv).ok().unwrap(), 1f64); |
| 149 | assert_eq!(10, conv.get_iter_count()); |
| 150 | |
| 151 | conv.reset(); |
| 152 | assert_float_eq!(1e-15f64, find_root_brent(-10f64, 0f64, &f, &mut conv).ok().unwrap(), -1f64); |
| 153 | assert_eq!(10, conv.get_iter_count()); |
| 154 | |
| 155 | conv.reset(); |
| 156 | assert_eq!(find_root_brent(10f64, 20f64, &f, &mut conv), Err(SearchError::NoBracketing)); |
| 157 | assert_eq!(0, conv.get_iter_count()); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn test_find_root_brent_simple() { |
| 162 | let f = |x| 1f64 * x * x - 1f64; |
| 163 | |
| 164 | assert_float_eq!(1e-15f64, find_root_brent(10f64, 0f64, &f, &mut 1e-15f64).ok().unwrap(), 1f64); |
| 165 | |
| 166 | assert_float_eq!( |
| 167 | 1e-15f64, |
| 168 | find_root_brent(-10f64, 0f64, &f, &mut 1e-15f64).ok().unwrap(), |
| 169 | -1f64 |
| 170 | ); |
| 171 | |
| 172 | assert_eq!( |
| 173 | find_root_brent(10f64, 20f64, &f, &mut 1e-15f64), |
| 174 | Err(SearchError::NoBracketing) |
| 175 | ); |
| 176 | } |
| 177 | } |