Skip to main content

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