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//! | Hessian | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_shessian!`] |
50//! | Hessian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`get_vhessian!`] |
51//!
52//! ## Passing Runtime Parameters
53//!
54//! Many times, we want to automatically differentiate functions that can also depend on parameters
55//! defined at runtime. However, automatic differentiation is performed at compile time, so we
56//! cannot simply "capture" these parameters using closures. To solve this problem, all automatic
57//! differentiation macros expect the function being differentiated to not only accept the point at
58//! which it is differentiated, but also a runtime parameter of an arbitrary type (could be a
59//! `&[f64]`, could be a `&T` where `T` is some custom struct, etc.).
60//!
61//! Examples are included for each macro (for example, here is the example for the
62//! [`get_jacobian!`] macro):
63//! [`get_jacobian!` - Example Passing Custom Parameter Types](macro@get_jacobian#example-passing-custom-parameter-types)
64//!
65//! ## Limitations
66//!
67//! * These macros only work on functions that are generic both over the type of scalar and the type
68//! of vector.
69//! - Consequently, these macros do _not_ work on closures.
70//! * Constants (e.g. `5.0_f64`) need to be defined using `linalg_traits::Scalar::new` (e.g. if a
71//! function has the generic parameter `S: Scalar`, then instead of defining a constant number
72//! such as `5.0_f64`, we need to do `S::new(5.0)`).
73//! - This is also the case for some functions that can take constants are arguments, such as
74//! [`num_traits::Float::powf`].
75//! * When defining functions that operate on generic scalars (to make them compatible with
76//! automatic differentiation), we cannot do an assignment operation such as `1.0 += x` if
77//! `x: S` where `S: Scalar`.
78//!
79//! ## Alternatives
80//!
81//! There are already some alternative crates in the Rust ecosystem that already implement dual
82//! numbers. Originally, I intended to implement the autodifferentiation functions in this crate
83//! using one of those other dual number implementations in the backend. However, each crate had
84//! certain shortcomings that ultimately led to me providing a custom implementation of dual numbers
85//! in this crate. The alternative crates implementing dual numbers are described below.
86//!
87//! ##### [`num-dual`](https://docs.rs/num-dual/latest/num_dual/)
88//!
89//! * This crate _can_ be used to differentiate functions of generic types that implement the
90//! [`DualNum`](https://docs.rs/num-dual/latest/num_dual/trait.DualNum.html) trait. Since this
91//! trait is implemented for [`f32`] and [`f64`], it would allow us to write generic functions
92//! that can be simply evaluated using [`f64`]s, but can also be automatically differentiated if
93//! needed.
94//! * However, there are some notable shortcomings that are described below.
95//! * The [`Dual`](https://github.com/itt-ustutt/num-dual/blob/master/src/dual.rs) struct panics in
96//! its implementations of the following standard functions which are quite common in engineering:
97//! - [`num_traits::Float::floor`]
98//! - [`num_traits::Float::ceil`]
99//! - [`num_traits::Float::round`]
100//! - [`num_traits::Float::trunc`]
101//! - [`num_traits::Float::fract`]
102//! * `num-dual` has a required dependency on
103//! [`nalgebra`](https://docs.rs/nalgebra/latest/nalgebra/), which is quite a heavy dependency for
104//! those who do not need it.
105//!
106//! ##### [`autodj`](https://docs.rs/autodj/latest/autodj/)
107//!
108//! * Can only differentiate functions written using custom types, such as
109//! [`DualF64`](https://docs.rs/autodj/latest/autodj/solid/single/type.DualF64.html).
110//! * Multivariate functions, especially those with a dynamic number of variables, can be extremely
111//! clunky (see
112//! [this example](https://docs.rs/autodj/latest/autodj/index.html#dynamic-number-of-variables)).
113//!
114//! ##### [`autodiff`](https://docs.rs/autodiff/latest/autodiff/)
115//!
116//! * Can only differentiate functions written using the custom type
117//! [`FT<T>`](https://docs.rs/autodiff/latest/autodiff/forward_autodiff/type.FT.html).
118//! * Incorrect implementation of certain functions, such as [`num_traits::Float::floor`] (see the
119//! [source code](https://github.com/elrnv/autodiff/blob/master/src/forward_autodiff.rs)).
120//!
121//! # Finite Difference Methods
122//!
123//! ## Central Difference Approximations
124//!
125//! ### First-Order Derivatives
126//!
127//! | Derivative Type | Function Type | Function to Approximate Derivative |
128//! | --------------- | ------------- | ---------------------------------- |
129//! | derivative | $f:\mathbb{R}\to\mathbb{R}$ | [`central_difference::sderivative()`] |
130//! | derivative | $\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$ | [`central_difference::vderivative()`] |
131//! | partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::spartial_derivative()`] |
132//! | partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`central_difference::vpartial_derivative()`] |
133//! | gradient | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::gradient()`] |
134//! | directional derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::directional_derivative()`] |
135//! | Jacobian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`central_difference::jacobian()`] |
136//!
137//! ### Second-Order Derivatives
138//!
139//! | Derivative Type | Function Type | Function to Approximate Derivative |
140//! | --------------- | ------------- | ---------------------------------- |
141//! | Hessian | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::shessian()`] |
142//! | Hessian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`central_difference::vhessian()`] |
143//!
144//! ## Forward Difference Approximations
145//!
146//! ### First-Order Derivatives
147//!
148//! | Derivative Type | Function Type | Function to Approximate Derivative |
149//! | --------------- | ------------- | ---------------------------------- |
150//! | derivative | $f:\mathbb{R}\to\mathbb{R}$ | [`forward_difference::sderivative()`] |
151//! | derivative | $\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$ | [`forward_difference::vderivative()`] |
152//! | partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::spartial_derivative()`] |
153//! | partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`forward_difference::vpartial_derivative()`] |
154//! | gradient | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::gradient()`] |
155//! | directional derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::directional_derivative()`] |
156//! | Jacobian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`forward_difference::jacobian()`] |
157//!
158//! ### Second-Order Derivatives
159//!
160//! | Derivative Type | Function Type | Function to Approximate Derivative |
161//! | --------------- | ------------- | ---------------------------------- |
162//! | Hessian | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::shessian()`] |
163//! | Hessian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`forward_difference::vhessian()`] |
164//!
165//! ## Passing Runtime Parameters
166//!
167//! For the finite difference methods, we can simply capture any runtime parameters using closures.
168//!
169//! Examples are included for each function (for example, here is the example for the
170//! [`central_difference::jacobian()`] function):
171//! [`central_difference::jacobian()` - Example Passing Runtime Parameters](fn@central_difference::jacobian#example-passing-runtime-parameters)
172
173// Linter setup.
174#![warn(missing_docs, warnings, clippy::all, clippy::pedantic, clippy::cargo)]
175#![allow(
176 clippy::float_cmp,
177 clippy::multiple_crate_versions,
178 clippy::unreadable_literal
179)]
180
181// Module declarations.
182pub(crate) mod automatic_differentiation;
183pub mod central_difference;
184pub mod constants;
185pub mod forward_difference;
186
187// Module declarations for utils used for testing only.
188#[cfg(test)]
189pub(crate) mod test_utils;
190
191// Re-exports.
192pub use automatic_differentiation::dual::dual::Dual;
193pub use automatic_differentiation::dual::dual_vector::DualVector;
194pub use automatic_differentiation::dual::hyper_dual::HyperDual;
195pub use automatic_differentiation::dual::hyper_dual_vector::HyperDualVector;