Code Monkey home page Code Monkey logo

rusty-rootsearch's Introduction

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));
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.