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//! | Derivative Type | Function Type | Macro to Generate Derivative Function |
32//! | --------------- | ------------- | ---------------------------------- |
33//! | derivative | $f:\mathbb{R}\to\mathbb{R}$ | [`get_sderivative!`] |
34//! | derivative | $\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$ | [`get_vderivative!`] |
35//! | partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_spartial_derivative!`] |
36//! | partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`get_vpartial_derivative!`] |
37//! | gradient | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_gradient!`] |
38//! | directional derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`get_directional_derivative!`] |
39//! | Jacobian| $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`get_jacobian!`] |
40//!
41//! ## Passing Runtime Parameters
42//!
43//! Many times, we want to automatically differentiate functions that can also depend on parameters
44//! defined at runtime. However, automatic differentiation is performed at compile time, so we
45//! cannot simply "capture" these parameters using closures. To solve this problem, all automatic
46//! differentiation macros expect the function to not only accept the point at which it is
47//! differentiated, but also a vector of runtime parameters that can be modified at runtime.
48//!
49//! ## Limitations
50//!
51//! * These macros only work on functions that are generic both over the type of scalar and the type
52//! of vector.
53//! * Consequently, these macros do _not_ work on closures.
54//! - Currently, this also means we can't "pass extra parameters" to a function.
55//! * Constants (e.g. `5.0_f64`) need to be defined using `linalg_traits::Scalar::new` (e.g. if a
56//! function has the generic parameter `S: Scalar`, then instead of defining a constant number
57//! such as `5.0_f64`, we need to do `S::new(5.0)`).
58//! - This is also the case for some functions that can take constants are arguments, such as
59//! [`num_traits::Float::powf`].
60//! * When defining functions that operate on generic scalars (to make them compatible with
61//! automatic differentiation), we cannot do an assignment operation such as `1.0 += x` if
62//! `x: S` where `S: Scalar`.
63//!
64//! ## Alternatives
65//!
66//! There are already some alternative crates in the Rust ecosystem that already implement dual
67//! numbers. Originally, I intended to implement the autodifferentiation functions in this crate
68//! using one of those other dual number implementations in the backend. However, each crate had
69//! certain shortcomings that ultimately led to me providing a custom implementation of dual numbers
70//! in this crate. The alternative crates implementing dual numbers are described below.
71//!
72//! ##### [`num-dual`](https://docs.rs/num-dual/latest/num_dual/)
73//!
74//! * This crate _can_ be used to differentiate functions of generic types that implement the
75//! [`DualNum`](https://docs.rs/num-dual/latest/num_dual/trait.DualNum.html) trait. Since this
76//! trait is implemented for [`f32`] and [`f64`], it would allow us to write generic functions
77//! that can be simply evaluated using [`f64`]s, but can also be automatically differentiated if
78//! needed.
79//! * However, there are some notable shortcomings that are described below.
80//! * The [`Dual`](https://github.com/itt-ustutt/num-dual/blob/master/src/dual.rs) struct panics in
81//! its implementations of the following standard functions which are quite common in engineering:
82//! - [`num_traits::Float::floor`]
83//! - [`num_traits::Float::ceil`]
84//! - [`num_traits::Float::round`]
85//! - [`num_traits::Float::trunc`]
86//! - [`num_traits::Float::fract`]
87//! * `num-dual` has a required dependency on
88//! [`nalgebra`](https://docs.rs/nalgebra/latest/nalgebra/), which is quite a heavy dependency for
89//! those who do not need it.
90//!
91//! ##### [`autodj`](https://docs.rs/autodj/latest/autodj/)
92//!
93//! * Can only differentiate functions written using custom types, such as
94//! [`DualF64`](https://docs.rs/autodj/latest/autodj/solid/single/type.DualF64.html).
95//! * Multivariate functions, especially those with a dynamic number of variables, can be extremely
96//! clunky (see
97//! [this example](https://docs.rs/autodj/latest/autodj/index.html#dynamic-number-of-variables)).
98//!
99//! ##### [`autodiff`](https://docs.rs/autodiff/latest/autodiff/)
100//!
101//! * Can only differentiate functions written using the custom type
102//! [`FT<T>`](https://docs.rs/autodiff/latest/autodiff/forward_autodiff/type.FT.html).
103//! * Incorrect implementation of certain functions, such as [`num_traits::Float::floor`] (see the
104//! [source code](https://github.com/elrnv/autodiff/blob/master/src/forward_autodiff.rs)).
105//!
106//! # Central Difference Approximations
107//!
108//! | Derivative Type | Function Type | Function to Approximate Derivative |
109//! | --------------- | ------------- | ---------------------------------- |
110//! | derivative | $f:\mathbb{R}\to\mathbb{R}$ | [`central_difference::sderivative()`] |
111//! | derivative | $\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$ | [`central_difference::vderivative()`] |
112//! | partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::spartial_derivative()`] |
113//! | partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`central_difference::vpartial_derivative()`] |
114//! | gradient | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::gradient()`] |
115//! | directional derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::directional_derivative()`] |
116//! | Jacobian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`central_difference::jacobian()`] |
117//! | Hessian | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`central_difference::shessian()`] |
118//! | Hessian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`central_difference::vhessian()`] |
119//!
120//! # Forward Difference Approximations
121//!
122//! | Derivative Type | Function Type | Function to Approximate Derivative |
123//! | --------------- | ------------- | ---------------------------------- |
124//! | derivative | $f:\mathbb{R}\to\mathbb{R}$ | [`forward_difference::sderivative()`] |
125//! | derivative | $\mathbf{f}:\mathbb{R}\to\mathbb{R}^{m}$ | [`forward_difference::vderivative()`] |
126//! | partial derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::spartial_derivative()`] |
127//! | partial derivative | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`forward_difference::vpartial_derivative()`] |
128//! | gradient | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::gradient()`] |
129//! | directional derivative | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::directional_derivative()`] |
130//! | Jacobian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`forward_difference::jacobian()`] |
131//! | Hessian | $f:\mathbb{R}^{n}\to\mathbb{R}$ | [`forward_difference::shessian()`] |
132//! | Hessian | $\mathbf{f}:\mathbb{R}^{n}\to\mathbb{R}^{m}$ | [`forward_difference::vhessian()`] |
133
134// Linter setup.
135#![warn(missing_docs)]
136
137// Module declarations.
138pub(crate) mod automatic_differentiation;
139pub mod central_difference;
140pub mod constants;
141pub mod forward_difference;
142
143// Module declarations for utils used for testing only.
144#[cfg(test)]
145pub(crate) mod test_utils;
146
147// Re-exports.
148pub use automatic_differentiation::dual::Dual;
149pub use automatic_differentiation::dual_vector::DualVector;