horseshoe/
numder.rs

1extern crate num_complex as nc;
2use self::nc::Complex;
3
4extern crate num_traits as nt;
5use self::nt::{cast::FromPrimitive, Num};
6
7pub trait Diff
8where
9    Self: FromPrimitive + Num + Copy,
10{
11    /// Derivate a function at a real or complex point.
12    ///
13    /// # Example
14    ///
15    /// Diff::numder(0.5, &my_function_name)
16    fn numder(z: Self, f: &Fn(Self) -> Self) -> Self {
17        let h = Self::from_f64(1e-10).unwrap();
18        (f(z + h) - f(z)) / h
19    }
20}
21
22macro_rules! impl_Diff {
23    ($($t:ty),+) => {$(
24        impl Diff for $t {}
25    )+};
26}
27
28impl_Diff!(f32, f64, Complex<f32>, Complex<f64>);