rusty-rootsearch

Crates.iorusty-rootsearch
lib.rsrusty-rootsearch
version0.4.0
sourcesrc
created_at2023-05-04 16:52:19.068353
updated_at2024-04-06 11:00:11.594448
descriptionThis package implements general one-dimensional root-finding algorithms built on the shoulders of the num_dual crate
homepage
repositoryhttps://github.com/tsmith023/rusty-rootsearch
max_upload_size
id856887
size13,361
Tommy Smith (tsmith023)

documentation

README

rusty-rootsearch

This is a small Rust-package that performs Newton's method for finding the roots of a function using the method of bisections.

It stands completely on the shoulders of the num_dual crate that implements auto-differentation for floating point numbers.

In an effort to generalise the implementation, the traits Derivable and Coercable are defined that allow the user to define their own types that can be used within the root_search, newton, and find_bisections functions. The Derivable trait is used to define the derivative of a number, which in the current implementation works well with the num_dual crate. The Coercable trait is used to convert a floating point number into the type that is derivable. This is useful for example when using the f64 type, which is not derivable, but can be converted to a Dual type exposed by the num_dual crate.

Example

use rusty_rootsearch::{root_search};
use num_dual::*;

fn find_sine_roots() {
    fn sine<D: DualNum<f32>>(x: D) -> D {
        x.sin()
    }
    let roots = root_search::<_,Dual32,f32>(&sine, -5.0, 5.0, 2000, 1000, 0.0001);
    for root in &roots.0 {
        println!("root: {}", root);
    }
    assert_eq!(roots.0.len(), 3);
    assert!(roots.0.contains(&std::f32::consts::PI));
    assert!(roots.0.contains(&(-std::f32::consts::PI)));
    assert!(roots.0.contains(&0.0));
}
Commit count: 23

cargo fmt