numdiff/lib.rs
1//! [![github]](https://github.com/tamaskis/numdiff) [![crates-io]](https://crates.io/crates/numdiff) [![docs-rs]](https://docs.rs/numdiff)
2//!
3//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
4//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
5//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
6//!
7//! Automatic and numerical differentiation.
8//!
9//! # Overview
10//!
11//! This crate implements two different methods for evaluating derivatives in Rust:
12//!
13//! 1. Automatic differentiation (forward-mode using first-order dual numbers).
14//! 2. Numerical differentiation (using forward difference and central difference approximations).
15//!
16//! This crate provides generic functions (for numerical differentiation) and macros (for automatic
17//! differentiation) to evaluate various types of derivatives of the following types of functions:
18//!
19//! * Univariate, scalar-valued functions ($f:\mathbb{R}\to\mathbb{R}$)
20//! * Univariate, vector-valued functions ($\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$)
21//! * Multivariate, scalar-valued functions ($f:\mathbb{R}^{n}\to\mathbb{R}$)
22//! * Multivariate, vector-valued functions ($\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$)
23//!
24//! These functions and macros are made generic over the choice of vector representation, as long as
25//! the vector type implements the `linalg_traits::Vector` trait. See the
26//! [`linalg_traits` documentation](https://docs.rs/linalg-traits/latest/linalg_traits/) for more
27//! information.
28//!
29//! # Automatic Differentiation (Forward-Mode)
30//!
31//! ## 1st-Order Derivatives
32//!
33//! | Derivative Type | Function Type | Macro to Generate Derivative Function |
34//! | --------------- | ------------- | ------------------------------------- |
35//! | derivative | $f:\mathbb{R}\to\mathbb{R}$ | [`get_sderivative!`] |
36//! | derivative | $\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$ | [`get_vderivative!`] |
37//! | partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_spartial_derivative!`] |
38//! | partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`get_vpartial_derivative!`] |
39//! | gradient | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_gradient!`] |
40//! | directional derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_directional_derivative!`] |
41//! | Jacobian| $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`get_jacobian!`] |
42//!
43//! ## 2nd-Order Derivatives
44//!
45//! | Derivative Type | Function Type | Macro to Generate Derivative Function |
46//! | --------------- | ------------- | ------------------------------------- |
47//! | 2nd derivative | $f:\mathbb{R}\to\mathbb{R}$ | [`get_sderivative2!`] |
48//! | 2nd derivative | $\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$ | [`get_vderivative2!`] |
49//! | 2nd partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_spartial_derivative2!`] |
50//! | 2nd partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`get_vpartial_derivative2!`] |
51//! | mixed 2nd partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_mixed_spartial_derivative2!`] |
52//! | mixed 2nd partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`get_mixed_vpartial_derivative2!`] |
53//! | Hessian | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_shessian!`] |
54//! | Hessian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`get_vhessian!`] |
55//!
56//! ## Passing Runtime Parameters
57//!
58//! Many times, we want to automatically differentiate functions that can also depend on parameters
59//! defined at runtime. However, automatic differentiation is performed at compile time, so we
60//! cannot simply "capture" these parameters using closures. To solve this problem, all automatic
61//! differentiation macros expect the function being differentiated to not only accept the point at
62//! which it is differentiated, but also a runtime parameter of an arbitrary type (could be a
63//! `&[f64]`, could be a `&T` where `T` is some custom struct, etc.).
64//!
65//! Examples are included for each macro (for example, here is the example for the
66//! [`get_jacobian!`] macro):
67//! [`get_jacobian!` - Example Passing Custom Parameter Types](macro@get_jacobian#example-passing-custom-parameter-types)
68//!
69//! ## Limitations
70//!
71//! * These macros only work on functions that are generic both over the type of scalar and the type
72//! of vector.
73//! - Consequently, these macros do _not_ work on closures.
74//! * Constants (e.g. `5.0_f64`) need to be defined using `linalg_traits::Scalar::new` (e.g. if a
75//! function has the generic parameter `S: Scalar`, then instead of defining a constant number
76//! such as `5.0_f64`, we need to do `S::new(5.0)`).
77//! - This is also the case for some functions that can take constants are arguments, such as
78//! [`num_traits::Float::powf`].
79//! * When defining functions that operate on generic scalars (to make them compatible with
80//! automatic differentiation), we cannot do an assignment operation such as `1.0 += x` if
81//! `x: S` where `S: Scalar`.
82//!
83//! ## Alternatives
84//!
85//! There are already some alternative crates in the Rust ecosystem that already implement dual
86//! numbers. Originally, I intended to implement the autodifferentiation functions in this crate
87//! using one of those other dual number implementations in the backend. However, each crate had
88//! certain shortcomings that ultimately led to me providing a custom implementation of dual numbers
89//! in this crate. The alternative crates implementing dual numbers are described below.
90//!
91//! ##### [`num-dual`](https://docs.rs/num-dual/latest/num_dual/)
92//!
93//! * This crate _can_ be used to differentiate functions of generic types that implement the
94//! [`DualNum`](https://docs.rs/num-dual/latest/num_dual/trait.DualNum.html) trait. Since this
95//! trait is implemented for [`f32`] and [`f64`], it would allow us to write generic functions
96//! that can be simply evaluated using [`f64`]s, but can also be automatically differentiated if
97//! needed.
98//! * However, there are some notable shortcomings that are described below.
99//! * The [`Dual`](https://github.com/itt-ustutt/num-dual/blob/master/src/dual.rs) struct panics in
100//! its implementations of the following standard functions which are quite common in engineering:
101//! - [`num_traits::Float::floor`]
102//! - [`num_traits::Float::ceil`]
103//! - [`num_traits::Float::round`]
104//! - [`num_traits::Float::trunc`]
105//! - [`num_traits::Float::fract`]
106//! * `num-dual` has a required dependency on
107//! [`nalgebra`](https://docs.rs/nalgebra/latest/nalgebra/), which is quite a heavy dependency for
108//! those who do not need it.
109//!
110//! ##### [`autodj`](https://docs.rs/autodj/latest/autodj/)
111//!
112//! * Can only differentiate functions written using custom types, such as
113//! [`DualF64`](https://docs.rs/autodj/latest/autodj/solid/single/type.DualF64.html).
114//! * Multivariate functions, especially those with a dynamic number of variables, can be extremely
115//! clunky (see
116//! [this example](https://docs.rs/autodj/latest/autodj/index.html#dynamic-number-of-variables)).
117//!
118//! ##### [`autodiff`](https://docs.rs/autodiff/latest/autodiff/)
119//!
120//! * Can only differentiate functions written using the custom type
121//! [`FT<T>`](https://docs.rs/autodiff/latest/autodiff/forward_autodiff/type.FT.html).
122//! * Incorrect implementation of certain functions, such as [`num_traits::Float::floor`] (see the
123//! [source code](https://github.com/elrnv/autodiff/blob/master/src/forward_autodiff.rs)).
124//!
125//! # Finite Difference Methods
126//!
127//! ## Central Difference Approximations
128//!
129//! ### First-Order Derivatives
130//!
131//! | Derivative Type | Function Type | Function to Approximate Derivative |
132//! | --------------- | ------------- | ---------------------------------- |
133//! | derivative | $f:\mathbb{R}\to\mathbb{R}$ | [`central_difference::sderivative()`] |
134//! | derivative | $\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$ | [`central_difference::vderivative()`] |
135//! | partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::spartial_derivative()`] |
136//! | partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`central_difference::vpartial_derivative()`] |
137//! | gradient | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::gradient()`] |
138//! | directional derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::directional_derivative()`] |
139//! | Jacobian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`central_difference::jacobian()`] |
140//!
141//! ### Second-Order Derivatives
142//!
143//! | Derivative Type | Function Type | Function to Approximate Derivative |
144//! | --------------- | ------------- | ---------------------------------- |
145//! | Hessian | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::shessian()`] |
146//! | Hessian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`central_difference::vhessian()`] |
147//!
148//! ## Forward Difference Approximations
149//!
150//! ### First-Order Derivatives
151//!
152//! | Derivative Type | Function Type | Function to Approximate Derivative |
153//! | --------------- | ------------- | ---------------------------------- |
154//! | derivative | $f:\mathbb{R}\to\mathbb{R}$ | [`forward_difference::sderivative()`] |
155//! | derivative | $\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$ | [`forward_difference::vderivative()`] |
156//! | partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::spartial_derivative()`] |
157//! | partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`forward_difference::vpartial_derivative()`] |
158//! | gradient | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::gradient()`] |
159//! | directional derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::directional_derivative()`] |
160//! | Jacobian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`forward_difference::jacobian()`] |
161//!
162//! ### Second-Order Derivatives
163//!
164//! | Derivative Type | Function Type | Function to Approximate Derivative |
165//! | --------------- | ------------- | ---------------------------------- |
166//! | Hessian | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::shessian()`] |
167//! | Hessian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`forward_difference::vhessian()`] |
168//!
169//! ## Passing Runtime Parameters
170//!
171//! For the finite difference methods, we can simply capture any runtime parameters using closures.
172//!
173//! Examples are included for each function (for example, here is the example for the
174//! [`central_difference::jacobian()`] function):
175//! [`central_difference::jacobian()` - Example Passing Runtime Parameters](fn@central_difference::jacobian#example-passing-runtime-parameters)
176
177// Linter setup.
178#![warn(missing_docs, warnings, clippy::all, clippy::pedantic, clippy::cargo)]
179#![allow(
180 clippy::float_cmp,
181 clippy::multiple_crate_versions,
182 clippy::unreadable_literal
183)]
184
185// Module declarations.
186pub(crate) mod automatic_differentiation;
187pub mod central_difference;
188pub mod constants;
189pub mod forward_difference;
190
191// Module declarations for utils used for testing only.
192#[cfg(test)]
193pub(crate) mod test_utils;
194
195// Re-exports.
196pub use automatic_differentiation::dual::dual::Dual;
197pub use automatic_differentiation::dual::dual_vector::DualVector;
198pub use automatic_differentiation::dual::hyper_dual::HyperDual;
199pub use automatic_differentiation::dual::hyper_dual_vector::HyperDualVector;