bacon_sci/differentiate/mod.rs
1/* This file is part of bacon.
2 * Copyright (c) Wyatt Campbell.
3 *
4 * See repository LICENSE for information.
5 */
6
7use nalgebra::ComplexField;
8use num_traits::FromPrimitive;
9
10/// Numerically find the derivative of a function at a point.
11///
12/// Given a function of a real field to a complex field, an input point x, and a
13/// step size h, calculate the derivative of f at x using the five-point method.
14///
15/// Making h too small will lead to round-off error.
16pub fn derivative<N: ComplexField + FromPrimitive + Copy>(
17 f: impl Fn(N::RealField) -> N,
18 x: N::RealField,
19 h: N::RealField,
20) -> N
21where
22 <N as ComplexField>::RealField: FromPrimitive + Copy,
23{
24 (f(x - h - h) + N::from_f64(8.0).unwrap() * (f(x + h) - f(x - h)) - f(x + h + h))
25 / (N::from_f64(12.0).unwrap() * N::from_real(h))
26}
27
28/// Numerically find the second derivative of a function at a point.
29///
30/// Given a function of a real field to a complex field, an input point x, and a
31/// step size h, calculate the second derivative of f at x using the five-point
32/// method.
33///
34/// Making h too small will lead to round-off error.
35pub fn second_derivative<N: ComplexField + FromPrimitive + Copy>(
36 f: impl Fn(N::RealField) -> N,
37 x: N::RealField,
38 h: N::RealField,
39) -> N
40where
41 <N as ComplexField>::RealField: FromPrimitive + Copy,
42{
43 (f(x - h) - N::from_f64(2.0).unwrap() * f(x) + f(x + h)) / N::from_real(h.powi(2))
44}