#root-finding #root #numerical #cubic #quartic

roots

Library of well known algorithms for numerical root finding

8 releases

Uses old Rust 2015

0.0.8 Dec 21, 2022
0.0.7 Jun 17, 2021
0.0.6 Dec 22, 2019
0.0.5 Jan 17, 2019
0.0.1 Mar 23, 2015

#17 in Math

Download history 11402/week @ 2024-03-14 10926/week @ 2024-03-21 11058/week @ 2024-03-28 11637/week @ 2024-04-04 11855/week @ 2024-04-11 12161/week @ 2024-04-18 10835/week @ 2024-04-25 11687/week @ 2024-05-02 11046/week @ 2024-05-09 11638/week @ 2024-05-16 11281/week @ 2024-05-23 15345/week @ 2024-05-30 16169/week @ 2024-06-06 15883/week @ 2024-06-13 14786/week @ 2024-06-20 11250/week @ 2024-06-27

61,137 downloads per month
Used in 62 crates (23 directly)

BSD-2-Clause

155KB
3K SLoC

Library of well known algorithms for numerical root finding.

LicenseBuild StatusCrates.io

Features

Usage

extern crate roots;
use roots::Roots;
use roots::find_roots_cubic;
use roots::find_root_brent;
use roots::find_root_secant;

// Find the root of a complex function in the area determined by a simpler polynom
fn find_solution<F>(enormous_function: F, root_area_polynom:(f64,f64,f64,f64)) -> Option<f64>
  where F: Fn(f64) -> f64
{
  // de-structure polynom coefficients
  match root_area_polynom {
    (a3,a2,a1,a0) => {
      // Find root area by solving the polynom
      match find_roots_cubic(a3,a2,a1,a0) {
        // Try to find the root by one of iterative methods
        Roots::Three(roots) => {
          // Three roots found, normal case
          find_root_brent(roots[0],roots[2],enormous_function, &mut 1e-8f64).ok()
        },
        Roots::Two(roots) => {
          // Two roots found, High precision required
          find_root_brent(roots[0],roots[1],enormous_function,&mut 1e-15f64).ok()
        },
        Roots::One(roots) => {
          // One root found, Low precision is enough
          find_root_secant(roots[0]-1f64,roots[0]+1f64,enormous_function,&mut 1e-3f64).ok()
        },
        _ => None,
      }
    },
    _ => None,
  }
}

No runtime deps