use nalgebra::ComplexField;
use num_traits::FromPrimitive;
pub fn derivative<N: ComplexField + FromPrimitive + Copy>(
f: impl Fn(N::RealField) -> N,
x: N::RealField,
h: N::RealField,
) -> N
where
<N as ComplexField>::RealField: FromPrimitive + Copy,
{
(f(x - h - h) + N::from_f64(8.0).unwrap() * (f(x + h) - f(x - h)) - f(x + h + h))
/ (N::from_f64(12.0).unwrap() * N::from_real(h))
}
pub fn second_derivative<N: ComplexField + FromPrimitive + Copy>(
f: impl Fn(N::RealField) -> N,
x: N::RealField,
h: N::RealField,
) -> N
where
<N as ComplexField>::RealField: FromPrimitive + Copy,
{
(f(x - h) - N::from_f64(2.0).unwrap() * f(x) + f(x + h)) / N::from_real(h.powi(2))
}