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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! Differentiation: exact automatic differentiation and finite differences.
//!
//! - [`derivative`] / [`second_derivative`] / [`partial`] — the short way to take one derivative.
//! - [`AutoDiffSingle`] / [`FiniteDifferenceSingle`] and their multi-variable siblings — the two
//! backends behind [`DerivatorSingleVariable`] and [`DerivatorMultiVariable`] (autodiff is exact).
//! - [`Jacobian`] / [`Hessian`] — derivative matrices of vector- and scalar-valued functions.
pub use ;
pub use ;
pub use ;
pub use Hessian;
pub use Jacobian;
pub use ;
use crateDiffError;
use crate;
/// The derivative of a single-variable function at a point.
///
/// For a third or higher derivative, or to choose finite differences instead,
/// use [`AutoDiffSingle`] or [`FiniteDifferenceSingle`].
///
/// # Examples
/// ```
/// use multicalc::numerical_derivative::derivative;
/// use multicalc::scalar_fn;
///
/// let function = scalar_fn!(|x| x * x * x); // f(x) = x^3
/// let point = 2.0_f64;
///
/// let slope = derivative(&function, point); // f'(2) = 3x^2 = 12
/// assert!((slope - 12.0).abs() < 1e-12);
/// ```
/// The second derivative of a single-variable function at a point.
///
/// # Examples
/// ```
/// use multicalc::numerical_derivative::second_derivative;
/// use multicalc::scalar_fn;
///
/// let function = scalar_fn!(|x| x * x * x); // f(x) = x^3
/// let point = 2.0_f32; // f32 works the same way
///
/// let bend = second_derivative(&function, point); // f''(2) = 6x = 12
/// assert!((bend - 12.0).abs() < 1e-4);
/// ```
/// One partial derivative of a multi-variable function, picked by variable index.
///
/// For a mixed or higher-order partial, use [`AutoDiffMulti`].
///
/// # Errors
/// [`DiffError::IndexOutOfRange`] if `variable_index` is not a variable of `function`. The index is
/// an ordinary number rather than part of the type, so it is checked when the code runs.
///
/// # Examples
/// ```
/// use multicalc::numerical_derivative::partial;
/// use multicalc::scalar_fn;
/// # fn main() -> Result<(), multicalc::error::DiffError> {
/// let function = scalar_fn!(|v: &[f64; 2]| v[0] * v[0] * v[1]); // g(x, y) = x^2 * y
/// let variable_index = 0; // differentiate by x
/// let point = [3.0_f64, 4.0];
///
/// let slope = partial(&function, variable_index, &point)?; // dg/dx = 2xy = 24
/// assert!((slope - 24.0).abs() < 1e-12);
/// # Ok(())
/// # }
/// ```