| 1 | Library of well known algorithms for numerical root finding. |
| 2 | |
| 3 | [](https://opensource.org/licenses/BSD-2-Clause)[](https://travis-ci.com/vorot/roots)[](https://crates.io/crates/roots) |
| 4 | |
| 5 | ## Features |
| 6 | |
| 7 | - Iterative approximation: |
| 8 | - [Newton-Raphson](https://en.wikipedia.org/wiki/Newton%27s_method) method |
| 9 | - [Secant](https://en.wikipedia.org/wiki/Secant_method) method |
| 10 | - [Regula falsi](https://en.wikipedia.org/wiki/False_position_method) method (with Illinois modification) |
| 11 | - [Brent-Dekker](https://en.wikipedia.org/wiki/Brent%27s_method) method |
| 12 | - [Inverse quadratic](https://en.wikipedia.org/wiki/Inverse_quadratic_interpolation) approximation |
| 13 | - Recursive [Sturm's](https://en.wikipedia.org/wiki/Sturm%27s_theorem) method |
| 14 | - Solving polynomial equations |
| 15 | - [Linear](https://en.wikipedia.org/wiki/Linear_equation) equation (editors' choice) |
| 16 | - [Quadratic](https://en.wikipedia.org/wiki/Quadratic_equation) equation |
| 17 | - [Cubic](https://en.wikipedia.org/wiki/Cubic_function) equation |
| 18 | - [Quartic](https://en.wikipedia.org/wiki/Quartic_function) equation |
| 19 | - [Eigenvalues](https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors) method for higher-degree polynomials |
| 20 | |
| 21 | ## Usage |
| 22 | |
| 23 | ```rust |
| 24 | extern crate roots; |
| 25 | use roots::Roots; |
| 26 | use roots::find_roots_cubic; |
| 27 | use roots::find_root_brent; |
| 28 | use roots::find_root_secant; |
| 29 | |
| 30 | // Find the root of a complex function in the area determined by a simpler polynom |
| 31 | fn find_solution<F>(enormous_function: F, root_area_polynom:(f64,f64,f64,f64)) -> Option<f64> |
| 32 | where F: Fn(f64) -> f64 |
| 33 | { |
| 34 | // de-structure polynom coefficients |
| 35 | match root_area_polynom { |
| 36 | (a3,a2,a1,a0) => { |
| 37 | // Find root area by solving the polynom |
| 38 | match find_roots_cubic(a3,a2,a1,a0) { |
| 39 | // Try to find the root by one of iterative methods |
| 40 | Roots::Three(roots) => { |
| 41 | // Three roots found, normal case |
| 42 | find_root_brent(roots[0],roots[2],enormous_function, &mut 1e-8f64).ok() |
| 43 | }, |
| 44 | Roots::Two(roots) => { |
| 45 | // Two roots found, High precision required |
| 46 | find_root_brent(roots[0],roots[1],enormous_function,&mut 1e-15f64).ok() |
| 47 | }, |
| 48 | Roots::One(roots) => { |
| 49 | // One root found, Low precision is enough |
| 50 | find_root_secant(roots[0]-1f64,roots[0]+1f64,enormous_function,&mut 1e-3f64).ok() |
| 51 | }, |
| 52 | _ => None, |
| 53 | } |
| 54 | }, |
| 55 | _ => None, |
| 56 | } |
| 57 | } |
| 58 | ``` |