complex_newton 0.1.0

newton's method for finding solutions of complex equations
Documentation
use num::Complex;

fn d(
    f: fn(Complex<f64>) -> Complex<f64>,
    x: Complex<f64>,
    a: Complex<f64>,
    o: usize,
) -> Complex<f64> {
    if o == 1 {
        (f(x + a) - f(x - a)) / (2.0 * a)
    } else {
        (d(f, x + a, a, o - 1) - d(f, x - a, a, o - 1)) / a
    }
}

pub fn newton(
    f: fn(Complex<f64>) -> Complex<f64>,
    mut x: Complex<f64>,
    a: Complex<f64>,
) -> Complex<f64> {
    let mut y = f(x);

    let mut i = 0.0;
    while y.norm() > a.norm() {
        i += 1.0;
        let lr = 1.0 - 0.001 * i;

        y = f(x);
        let d = lr * y / d(f, x, a, 1);
        x -= d;

        // println!("{}", d);
        // if d.abs() <= a {
        //     return newton_min(f, x, a);
        // }
    }

    x
}

pub fn newton_min(
    f: fn(Complex<f64>) -> Complex<f64>,
    mut x: Complex<f64>,
    a: Complex<f64>,
) -> Complex<f64> {
    let mut y = d(f, x, a, 1);

    while y.norm() > a.norm() {
        let lr = -0.001 * x + 1.0;

        y = d(f, x, a, 1);
        let d = lr * y / d(f, x, a, 2);
        x -= d;
    }

    x
}