1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! `nrfind` is a crate for finding the roots of arbitrary functions whose derivatives
//! are known by the Newton-Rahpson method. 

extern crate num;
use num::Float;
use std::ops::{Div, Sub};

type RealFn<N> where N: Float + Div + Sub + PartialOrd = Fn(N) -> N;

/// Uses the Newton-Raphson method to find roots for `function` given
/// that `derivative` is the first derivative of `function`.
/// If `max_iterations` is reached without the error falling below the given
/// `acceptable_err`, `nrfind` will return `None`.
///
/// This function can operate on any type that implements `Float`. That means
/// arbitrary-precision `BigRational` types from the `num` crate are acceptable.
///
/// # Examples
/// 
/// Here, `find_root` is used to find the root of the polynomial x^3 + x^2 + 1
/// to a precision of 0.1 in 18 iterations, staring from a very
/// inaccurate initial guess.
///
/// ```
/// use nrfind::find_root;
///
/// // The function x^3 + x^2 + 1
/// fn f(x: f64) -> f64 {
///     x.powi(3) + x.powi(2) + 1.0
/// }
///
/// // The derivative of f (3x^2 + 2x)
/// fn fd(x: f64) -> f64 {
///     (3.0 * x.powi(2)) + (2.0 * x)
/// }
///
/// let result = find_root(&f, &fd, 
///                        &100.0,  // Starting guess
///                        &0.1,    // Precision
///                        18       // Iterations
///                       ).unwrap();
/// let actual = -1.4656; // The correct answer
/// let difference = (result - actual).abs();
///
/// assert!(difference < 0.1);
/// ```
/// 
pub fn find_root<N>(function: &RealFn<N>, derivative: &RealFn<N>, x0: &N, acceptable_err: &N, max_iterations: i32) -> Option<N> 
    where N: Float + Div + Sub + PartialOrd
{
    let mut current_x: N = *x0;
    let mut next_x: N;

    for _ in 0..max_iterations {
        let deviation = function(current_x) / derivative(current_x);
        next_x = current_x - deviation;
        if deviation.abs() <= *acceptable_err {
            return Some(next_x);
        }
        current_x = next_x;
    }
    return None;
}