Skip to main content

num_dual/
lib.rs

1//! Generalized, recursive, scalar and vector (hyper) dual numbers for the automatic and exact calculation of (partial) derivatives.
2//!
3//! # Example
4//! This example defines a generic scalar and a generic vector function that can be called using any (hyper-) dual number and automatically calculates derivatives.
5//! ```
6//! use num_dual::*;
7//! use nalgebra::SVector;
8//!
9//! fn foo<D: DualNum<f64>>(x: D) -> D {
10//!     x.powi(3)
11//! }
12//!
13//! fn bar<D: DualNum<f64>, const N: usize>(x: SVector<D, N>) -> D {
14//!     x.dot(&x).sqrt()
15//! }
16//!
17//! fn main() {
18//!     // Calculate a simple derivative
19//!     let (f, df) = first_derivative(foo, 5.0);
20//!     assert_eq!(f, 125.0);
21//!     assert_eq!(df, 75.0);
22//!
23//!     // Manually construct the dual number
24//!     let x = Dual64::new(5.0, 1.0);
25//!     println!("{}", foo(x));                     // 125 + 75ε
26//!
27//!     // Calculate a gradient
28//!     let (f, g) = gradient(bar, &SVector::from([4.0, 3.0]));
29//!     assert_eq!(f, 5.0);
30//!     assert_eq!(g[0], 0.8);
31//!
32//!     // Calculate a Hessian
33//!     let (f, g, h) = hessian(bar, &SVector::from([4.0, 3.0]));
34//!     println!("{h}");                            // [[0.072, -0.096], [-0.096, 0.128]]
35//!
36//!     // for x=cos(t) calculate the third derivative of foo w.r.t. t
37//!     let (f0, f1, f2, f3) = third_derivative(|t| foo(t.cos()), 1.0);
38//!     println!("{f3}");                           // 1.5836632930100278
39//! }
40//! ```
41//!
42//! # Usage
43//! There are two ways to use the data structures and functions provided in this crate:
44//! 1. (recommended) Using the provided functions for explicit ([`first_derivative`], [`gradient`], ...) and
45//!    implicit ([`implicit_derivative`], [`implicit_derivative_binary`], [`implicit_derivative_vec`]) functions.
46//! 2. (for experienced users) Using the different dual number types ([`Dual`], [`HyperDual`], [`DualVec`], ...) directly.
47//!
48//! The following examples and explanations focus on the first way.
49//!
50//! # Derivatives of explicit functions
51//! To be able to calculate the derivative of a function, it needs to be generic over the type of dual number used.
52//! Most commonly this would look like this:
53//! ```compile_fail
54//! fn foo<D: DualNum<f64> + Copy>(x: X) -> O {...}
55//! ```
56//! Of course, the function could also use single precision ([`f32`]) or be generic over the precision (`F:` [`DualNumFloat`]).
57//! For now, [`Copy`] is not a supertrait of [`DualNum`] to enable the calculation of derivatives with respect
58//! to a dynamic number of variables. However, in practice, using the [`Copy`] trait bound leads to an
59//! implementation that is more similar to one not using AD and there could be severe performance ramifications
60//! when using dynamically allocated dual numbers.
61//!
62//! The type `X` above is `D` for univariate functions, [`&OVector`](nalgebra::OVector) for multivariate
63//! functions, and `(D, D)` or `(&OVector, &OVector)` for partial derivatives. In the simplest case, the output
64//! `O` is a scalar `D`. However, it is generalized using the [`Mappable`] trait to also include types like
65//! [`Option<D>`] or [`Result<D, E>`], collections like [`Vec<D>`] or [`HashMap<K, D>`], or custom structs that
66//! implement the [`Mappable`] trait. Therefore, it is, e.g., possible to calculate the derivative of a fallible
67//! function:
68//!
69//! ```no_run
70//! # use num_dual::{DualNum, first_derivative};
71//! # type E = ();
72//! fn foo<D: DualNum<f64> + Copy>(x: D) -> Result<D, E> { todo!() }
73//!
74//! fn main() -> Result<(), E> {
75//!     let (val, deriv) = first_derivative(foo, 2.0)?;
76//!     // ...
77//!     Ok(())
78//! }
79//! ```
80//! All dual number types can contain other dual numbers as inner types. Therefore, it is also possible to
81//! use the different derivative functions inside of each other.
82//!
83//! ## Extra arguments
84//! The [`partial`] and [`partial2`] functions are used to pass additional arguments to the function, e.g.:
85//! ```no_run
86//! # use num_dual::{DualNum, first_derivative, partial};
87//! fn foo<D: DualNum<f64> + Copy>(x: D, args: &(D, D)) -> D { todo!() }
88//!
89//! fn main() {
90//!     let (val, deriv) = first_derivative(partial(foo, &(3.0, 4.0)), 5.0);
91//! }
92//! ```
93//! All types that implement the [`DualStruct`] trait can be used as additional function arguments. The
94//! only difference between using the [`partial`] and [`partial2`] functions compared to passing the extra
95//! arguments via a closure, is that the type of the extra arguments is automatically adjusted to the correct
96//! dual number type used for the automatic differentiation. Note that the following code would not compile:
97//! ```compile_fail
98//! # use num_dual::{DualNum, first_derivative};
99//! # fn foo<D: DualNum<f64> + Copy>(x: D, args: &(D, D)) -> D { todo!() }
100//! fn main() {
101//!     let (val, deriv) = first_derivative(|x| foo(x, &(3.0, 4.0)), 5.0);
102//! }
103//! ```
104//! The code created by [`partial`] essentially translates to:
105//! ```no_run
106//! # use num_dual::{DualNum, first_derivative, Dual, DualStruct};
107//! # fn foo<D: DualNum<f64> + Copy>(x: D, args: &(D, D)) -> D { todo!() }
108//! fn main() {
109//!     let (val, deriv) = first_derivative(|x| foo(x, &(Dual::from_inner(&3.0), Dual::from_inner(&4.0))), 5.0);
110//! }
111//! ```
112//!
113//! ## The [`Gradients`] trait
114//! The functions [`gradient`], [`hessian`], [`partial_hessian`] and [`jacobian`] are generic over the dimensionality
115//! of the variable vector. However, to use the functions in a generic context requires not using the [`Copy`] trait
116//! bound on the dual number type, because the dynamically sized dual numbers can by construction not implement
117//! [`Copy`]. Also, due to frequent heap allocations, the performance of the automatic differentiation could
118//! suffer significantly for dynamically sized dual numbers compared to statically sized dual numbers. The
119//! [`Gradients`] trait is introduced to overcome these limitations.
120//! ```
121//! # use num_dual::{DualNum, Gradients};
122//! # use nalgebra::{OVector, DefaultAllocator, allocator::Allocator, vector, dvector};
123//! # use approx::assert_relative_eq;
124//! fn foo<D: DualNum<f64> + Copy, N: Gradients>(x: OVector<D, N>, n: &D) -> D where DefaultAllocator: Allocator<N> {
125//!     x.dot(&x).sqrt() - n
126//! }
127//!
128//! fn main() {
129//!     let x = vector![1.0, 5.0, 5.0, 7.0];
130//!     let (f, grad) = Gradients::gradient(foo, &x, &10.0);
131//!     assert_eq!(f, 0.0);
132//!     assert_relative_eq!(grad, vector![0.1, 0.5, 0.5, 0.7]);
133//!
134//!     let x = dvector![1.0, 5.0, 5.0, 7.0];
135//!     let (f, grad) = Gradients::gradient(foo, &x, &10.0);
136//!     assert_eq!(f, 0.0);
137//!     assert_relative_eq!(grad, dvector![0.1, 0.5, 0.5, 0.7]);
138//! }
139//! ```
140//! For dynamically sized input arrays, the [`Gradients`] trait evaluates gradients or higher-order derivatives
141//! by iteratively evaluating scalar derivatives. For functions that do not rely on the [`Copy`] trait bound,
142//! only benchmarking can reveal Whether the increased performance through the avoidance of heap allocations
143//! can overcome the overhead of repeated function evaluations, i.e., if [`Gradients`] outperforms directly
144//! calling [`gradient`], [`hessian`], [`partial_hessian`] or [`jacobian`].
145//!
146//! # Derivatives of implicit functions
147//! Implicit differentiation is used to determine the derivative `dy/dx` where the output `y` is only related
148//! implicitly to the input `x` via the equation `f(x,y)=0`. Automatic implicit differentiation generalizes the
149//! idea to determining the output `y` with full derivative information. Note that the first step in calculating
150//! an implicit derivative is always determining the "real" part (i.e., neglecting all derivatives) of the equation
151//! `f(x,y)=0`. The `num-dual` library is focused on automatic differentiation and not nonlinear equation
152//! solving. Therefore, this first step needs to be done with your own custom solutions, or Rust crates for
153//! nonlinear equation solving and optimization like, e.g., [argmin](https://argmin-rs.org/).
154//!
155//! The following example implements a square root for generic dual numbers using implicit differentiation. Of
156//! course, the derivatives of the square root can also be determined explicitly using the chain rule, so the
157//! example serves mostly as illustration. `x.re()` provides the "real" part of the dual number which is a [`f64`]
158//! and therefore, we can use all the functionalities from the std library (including the square root).
159//! ```
160//! # use num_dual::{DualNum, implicit_derivative, first_derivative};
161//! fn implicit_sqrt<D: DualNum<f64> + Copy>(x: D) -> D {
162//!     implicit_derivative(|s, x| s * s - x, x.re().sqrt(), &x)
163//! }
164//!
165//! fn main() {
166//!     // sanity check, not actually calculating any derivative
167//!     assert_eq!(implicit_sqrt(25.0), 5.0);
168//!     
169//!     let (sq, deriv) = first_derivative(implicit_sqrt, 25.0);
170//!     assert_eq!(sq, 5.0);
171//!     // The derivative of sqrt(x) is 1/(2*sqrt(x)) which should evaluate to 0.1
172//!     assert_eq!(deriv, 0.1);
173//! }
174//! ```
175//! The `implicit_sqrt` or any likewise defined function is generic over the dual type `D`
176//! and can, therefore, be used anywhere as a part of an arbitrary complex computation. The functions
177//! [`implicit_derivative_binary`] and [`implicit_derivative_vec`] can be used for implicit functions
178//! with more than one variable.
179//!
180//! For implicit functions that contain complex models and a large number of parameters, the [`ImplicitDerivative`]
181//! interface might come in handy. The idea is to define the implicit function using the [`ImplicitFunction`] trait
182//! and feeding it into the [`ImplicitDerivative`] struct, which internally stores the parameters as dual numbers
183//! and their real parts. The [`ImplicitDerivative`] then provides methods for the evaluation of the real part
184//! of the residual (which can be passed to a nonlinear solver) and the implicit derivative which can be called
185//! after solving for the real part of the solution to reconstruct all the derivatives.
186//! ```
187//! # use num_dual::{ImplicitFunction, DualNum, Dual, ImplicitDerivative};
188//! struct ImplicitSqrt;
189//! impl ImplicitFunction<f64> for ImplicitSqrt {
190//!     type Parameters<D> = D;
191//!     type Variable<D> = D;
192//!     fn residual<D: DualNum<f64> + Copy>(x: D, square: &D) -> D {
193//!         *square - x * x
194//!     }
195//! }
196//!
197//! fn main() {
198//!     let x = Dual::from_re(25.0).derivative();
199//!     let func = ImplicitDerivative::new(ImplicitSqrt, x);
200//!     assert_eq!(func.residual(5.0), 0.0);
201//!     assert_eq!(x.sqrt(), func.implicit_derivative(5.0));
202//! }
203//! ```
204//!
205//! ## Combination with nonlinear solver libraries
206//! As mentioned previously, this crate does not contain any algorithms for nonlinear optimization or root finding.
207//! However, combining the capabilities of automatic differentiation with nonlinear solving can be very fruitful.
208//! Most importantly, the calculation of Jacobians or Hessians can be completely automated, if the model can be
209//! expressed within the functionalities of the [`DualNum`] trait. On top of that implicit derivatives can be of
210//! interest, if derivatives of the result of the optimization itself are relevant (e.g., in a bilevel
211//! optimization). The synergy is exploited in the [`ipopt-ad`](https://github.com/prehner/ipopt-ad) crate that
212//! turns the NLP solver [IPOPT](https://github.com/coin-or/Ipopt) into a black-box optimization algorithm (i.e.,
213//! it only requires a function that returns the values of the optimization variable and constraints), without
214//! any repercussions regarding the robustness or speed of convergence of the solver.
215//!
216//! If you are developing nonlinear optimization algorithms in Rust, feel free to reach out to us. We are happy to
217//! discuss how to enhance your algorithms with the automatic differentiation capabilities of this crate.
218
219#![warn(clippy::all)]
220#![warn(clippy::allow_attributes)]
221
222use nalgebra::allocator::Allocator;
223use nalgebra::{DefaultAllocator, Dim, OMatrix, Scalar};
224#[cfg(feature = "ndarray")]
225use ndarray::ScalarOperand;
226use num_traits::{Float, FloatConst, FromPrimitive, Inv, NumAssignOps, NumOps, Signed};
227use std::collections::HashMap;
228use std::fmt;
229use std::hash::Hash;
230use std::iter::{Product, Sum};
231
232#[macro_use]
233mod macros;
234#[macro_use]
235mod impl_derivatives;
236
237mod bessel;
238mod datatypes;
239mod explicit;
240mod implicit;
241pub use bessel::BesselDual;
242pub use datatypes::derivative::Derivative;
243pub use datatypes::dual::{Dual, Dual32, Dual64};
244pub use datatypes::dual_vec::{
245    DualDVec32, DualDVec64, DualSVec, DualSVec32, DualSVec64, DualVec, DualVec32, DualVec64,
246};
247pub use datatypes::dual2::{Dual2, Dual2_32, Dual2_64};
248pub use datatypes::dual2_vec::{
249    Dual2DVec, Dual2DVec32, Dual2DVec64, Dual2SVec, Dual2SVec32, Dual2SVec64, Dual2Vec, Dual2Vec32,
250    Dual2Vec64,
251};
252pub use datatypes::dual3::{Dual3, Dual3_32, Dual3_64};
253pub use datatypes::hyperdual::{HyperDual, HyperDual32, HyperDual64};
254pub use datatypes::hyperdual_vec::{
255    HyperDualDVec32, HyperDualDVec64, HyperDualSVec32, HyperDualSVec64, HyperDualVec,
256    HyperDualVec32, HyperDualVec64,
257};
258pub use datatypes::hyperhyperdual::{HyperHyperDual, HyperHyperDual32, HyperHyperDual64};
259pub use datatypes::real::Real;
260pub use explicit::{
261    Gradients, first_derivative, gradient, hessian, jacobian, partial, partial_hessian, partial2,
262    partial3, second_derivative, second_partial_derivative, third_derivative,
263    third_partial_derivative, third_partial_derivative_vec, zeroth_derivative,
264};
265pub use implicit::{
266    ImplicitDerivative, ImplicitFunction, implicit_derivative, implicit_derivative_binary,
267    implicit_derivative_sp, implicit_derivative_vec,
268};
269
270pub mod linalg;
271
272#[cfg(feature = "python")]
273pub mod python;
274
275#[cfg(feature = "python_macro")]
276mod python_macro;
277
278/// A generalized (hyper) dual number.
279#[cfg(feature = "ndarray")]
280pub trait DualNum<F>:
281    NumOps
282    + for<'r> NumOps<&'r Self>
283    + Signed
284    + NumOps<F>
285    + NumAssignOps
286    + NumAssignOps<F>
287    + Clone
288    + Inv<Output = Self>
289    + Sum
290    + Product
291    + FromPrimitive
292    + From<F>
293    + DualStruct<Self, F, Real = F>
294    + Mappable<Self>
295    + fmt::Display
296    + PartialOrd
297    + PartialOrd<F>
298    + fmt::Debug
299    + ScalarOperand
300    + 'static
301{
302    /// Highest derivative that can be calculated with this struct
303    const NDERIV: usize;
304
305    /// Reciprocal (inverse) of a number `1/x`
306    fn recip(&self) -> Self;
307
308    /// Power with integer exponent `x^n`
309    fn powi(&self, n: i32) -> Self;
310
311    /// Power with real exponent `x^n`
312    fn powf(&self, n: F) -> Self;
313
314    /// Square root
315    fn sqrt(&self) -> Self;
316
317    /// Cubic root
318    fn cbrt(&self) -> Self;
319
320    /// Exponential `e^x`
321    fn exp(&self) -> Self;
322
323    /// Exponential with base 2 `2^x`
324    fn exp2(&self) -> Self;
325
326    /// Exponential minus 1 `e^x-1`
327    fn exp_m1(&self) -> Self;
328
329    /// Natural logarithm
330    fn ln(&self) -> Self;
331
332    /// Logarithm with arbitrary base
333    fn log(&self, base: F) -> Self;
334
335    /// Logarithm with base 2
336    fn log2(&self) -> Self;
337
338    /// Logarithm with base 10
339    fn log10(&self) -> Self;
340
341    /// Logarithm on x plus one `ln(1+x)`
342    fn ln_1p(&self) -> Self;
343
344    /// Sine
345    fn sin(&self) -> Self;
346
347    /// Cosine
348    fn cos(&self) -> Self;
349
350    /// Tangent
351    fn tan(&self) -> Self;
352
353    /// Calculate sine and cosine simultaneously
354    fn sin_cos(&self) -> (Self, Self);
355
356    /// Arcsine
357    fn asin(&self) -> Self;
358
359    /// Arccosine
360    fn acos(&self) -> Self;
361
362    /// Arctangent
363    fn atan(&self) -> Self;
364
365    /// Arctangent
366    fn atan2(&self, other: Self) -> Self;
367
368    /// Hyperbolic sine
369    fn sinh(&self) -> Self;
370
371    /// Hyperbolic cosine
372    fn cosh(&self) -> Self;
373
374    /// Hyperbolic tangent
375    fn tanh(&self) -> Self;
376
377    /// Area hyperbolic sine
378    fn asinh(&self) -> Self;
379
380    /// Area hyperbolic cosine
381    fn acosh(&self) -> Self;
382
383    /// Area hyperbolic tangent
384    fn atanh(&self) -> Self;
385
386    /// 0th order spherical Bessel function of the first kind
387    fn sph_j0(&self) -> Self;
388
389    /// 1st order spherical Bessel function of the first kind
390    fn sph_j1(&self) -> Self;
391
392    /// 2nd order spherical Bessel function of the first kind
393    fn sph_j2(&self) -> Self;
394
395    /// Fused multiply-add
396    #[inline]
397    fn mul_add(&self, a: Self, b: Self) -> Self {
398        self.clone() * a + b
399    }
400
401    /// Power with dual exponent `x^n`
402    #[inline]
403    fn powd(&self, exp: Self) -> Self {
404        (self.ln() * exp).exp()
405    }
406}
407
408/// A generalized (hyper) dual number.
409#[cfg(not(feature = "ndarray"))]
410pub trait DualNum<F>:
411    NumOps
412    + for<'r> NumOps<&'r Self>
413    + Signed
414    + NumOps<F>
415    + NumAssignOps
416    + NumAssignOps<F>
417    + Clone
418    + Inv<Output = Self>
419    + Sum
420    + Product
421    + FromPrimitive
422    + From<F>
423    + DualStruct<Self, F, Real = F>
424    + Mappable<Self>
425    + fmt::Display
426    + PartialOrd
427    + PartialOrd<F>
428    + fmt::Debug
429    + 'static
430{
431    /// Highest derivative that can be calculated with this struct
432    const NDERIV: usize;
433
434    /// Reciprocal (inverse) of a number `1/x`
435    fn recip(&self) -> Self;
436
437    /// Power with integer exponent `x^n`
438    fn powi(&self, n: i32) -> Self;
439
440    /// Power with real exponent `x^n`
441    fn powf(&self, n: F) -> Self;
442
443    /// Square root
444    fn sqrt(&self) -> Self;
445
446    /// Cubic root
447    fn cbrt(&self) -> Self;
448
449    /// Exponential `e^x`
450    fn exp(&self) -> Self;
451
452    /// Exponential with base 2 `2^x`
453    fn exp2(&self) -> Self;
454
455    /// Exponential minus 1 `e^x-1`
456    fn exp_m1(&self) -> Self;
457
458    /// Natural logarithm
459    fn ln(&self) -> Self;
460
461    /// Logarithm with arbitrary base
462    fn log(&self, base: F) -> Self;
463
464    /// Logarithm with base 2
465    fn log2(&self) -> Self;
466
467    /// Logarithm with base 10
468    fn log10(&self) -> Self;
469
470    /// Logarithm on x plus one `ln(1+x)`
471    fn ln_1p(&self) -> Self;
472
473    /// Sine
474    fn sin(&self) -> Self;
475
476    /// Cosine
477    fn cos(&self) -> Self;
478
479    /// Tangent
480    fn tan(&self) -> Self;
481
482    /// Calculate sine and cosine simultaneously
483    fn sin_cos(&self) -> (Self, Self);
484
485    /// Arcsine
486    fn asin(&self) -> Self;
487
488    /// Arccosine
489    fn acos(&self) -> Self;
490
491    /// Arctangent
492    fn atan(&self) -> Self;
493
494    /// Arctangent
495    fn atan2(&self, other: Self) -> Self;
496
497    /// Hyperbolic sine
498    fn sinh(&self) -> Self;
499
500    /// Hyperbolic cosine
501    fn cosh(&self) -> Self;
502
503    /// Hyperbolic tangent
504    fn tanh(&self) -> Self;
505
506    /// Area hyperbolic sine
507    fn asinh(&self) -> Self;
508
509    /// Area hyperbolic cosine
510    fn acosh(&self) -> Self;
511
512    /// Area hyperbolic tangent
513    fn atanh(&self) -> Self;
514
515    /// 0th order spherical Bessel function of the first kind
516    fn sph_j0(&self) -> Self;
517
518    /// 1st order spherical Bessel function of the first kind
519    fn sph_j1(&self) -> Self;
520
521    /// 2nd order spherical Bessel function of the first kind
522    fn sph_j2(&self) -> Self;
523
524    /// Fused multiply-add
525    #[inline]
526    fn mul_add(&self, a: Self, b: Self) -> Self {
527        self.clone() * a + b
528    }
529
530    /// Power with dual exponent `x^n`
531    #[inline]
532    fn powd(&self, exp: Self) -> Self {
533        (self.ln() * exp).exp()
534    }
535}
536
537/// The underlying data type of individual derivatives. Usually f32 or f64.
538pub trait DualNumFloat:
539    Float + FloatConst + FromPrimitive + Signed + fmt::Display + fmt::Debug + Sync + Send + 'static
540{
541}
542impl<T> DualNumFloat for T where
543    T: Float
544        + FloatConst
545        + FromPrimitive
546        + Signed
547        + fmt::Display
548        + fmt::Debug
549        + Sync
550        + Send
551        + 'static
552{
553}
554
555macro_rules! impl_dual_num_float {
556    ($float:ty) => {
557        impl DualNum<$float> for $float {
558            const NDERIV: usize = 0;
559
560            fn mul_add(&self, a: Self, b: Self) -> Self {
561                <$float>::mul_add(*self, a, b)
562            }
563            fn recip(&self) -> Self {
564                <$float>::recip(*self)
565            }
566            fn powi(&self, n: i32) -> Self {
567                <$float>::powi(*self, n)
568            }
569            fn powf(&self, n: Self) -> Self {
570                <$float>::powf(*self, n)
571            }
572            fn powd(&self, n: Self) -> Self {
573                <$float>::powf(*self, n)
574            }
575            fn sqrt(&self) -> Self {
576                <$float>::sqrt(*self)
577            }
578            fn exp(&self) -> Self {
579                <$float>::exp(*self)
580            }
581            fn exp2(&self) -> Self {
582                <$float>::exp2(*self)
583            }
584            fn ln(&self) -> Self {
585                <$float>::ln(*self)
586            }
587            fn log(&self, base: Self) -> Self {
588                <$float>::log(*self, base)
589            }
590            fn log2(&self) -> Self {
591                <$float>::log2(*self)
592            }
593            fn log10(&self) -> Self {
594                <$float>::log10(*self)
595            }
596            fn cbrt(&self) -> Self {
597                <$float>::cbrt(*self)
598            }
599            fn sin(&self) -> Self {
600                <$float>::sin(*self)
601            }
602            fn cos(&self) -> Self {
603                <$float>::cos(*self)
604            }
605            fn tan(&self) -> Self {
606                <$float>::tan(*self)
607            }
608            fn asin(&self) -> Self {
609                <$float>::asin(*self)
610            }
611            fn acos(&self) -> Self {
612                <$float>::acos(*self)
613            }
614            fn atan(&self) -> Self {
615                <$float>::atan(*self)
616            }
617            fn atan2(&self, other: $float) -> Self {
618                <$float>::atan2(*self, other)
619            }
620            fn sin_cos(&self) -> (Self, Self) {
621                <$float>::sin_cos(*self)
622            }
623            fn exp_m1(&self) -> Self {
624                <$float>::exp_m1(*self)
625            }
626            fn ln_1p(&self) -> Self {
627                <$float>::ln_1p(*self)
628            }
629            fn sinh(&self) -> Self {
630                <$float>::sinh(*self)
631            }
632            fn cosh(&self) -> Self {
633                <$float>::cosh(*self)
634            }
635            fn tanh(&self) -> Self {
636                <$float>::tanh(*self)
637            }
638            fn asinh(&self) -> Self {
639                <$float>::asinh(*self)
640            }
641            fn acosh(&self) -> Self {
642                <$float>::acosh(*self)
643            }
644            fn atanh(&self) -> Self {
645                <$float>::atanh(*self)
646            }
647            fn sph_j0(&self) -> Self {
648                if self.abs() < <$float>::EPSILON {
649                    1.0 - self * self / 6.0
650                } else {
651                    self.sin() / self
652                }
653            }
654            fn sph_j1(&self) -> Self {
655                if self.abs() < <$float>::EPSILON {
656                    self / 3.0
657                } else {
658                    let sc = self.sin_cos();
659                    let rec = self.recip();
660                    (sc.0 * rec - sc.1) * rec
661                }
662            }
663            fn sph_j2(&self) -> Self {
664                if self.abs() < <$float>::EPSILON {
665                    self * self / 15.0
666                } else {
667                    let sc = self.sin_cos();
668                    let s2 = self * self;
669                    ((3.0 - s2) * sc.0 - 3.0 * self * sc.1) / (self * s2)
670                }
671            }
672        }
673    };
674}
675
676impl_dual_num_float!(f32);
677impl_dual_num_float!(f64);
678
679/// A struct that contains dual numbers. Needed for arbitrary arguments in [ImplicitFunction].
680///
681/// The trait is implemented for all dual types themselves, and common data types (tuple, vec,
682/// array, ...) and can be implemented for custom data types to achieve full flexibility.
683pub trait DualStruct<D, F> {
684    type Real;
685    type Inner;
686    fn re(&self) -> Self::Real;
687    fn from_inner(inner: &Self::Inner) -> Self;
688}
689
690/// Trait for structs used as an output of functions for which derivatives are calculated.
691///
692/// The main intention is to generalize the calculation of derivatives to fallible functions, but
693/// other use cases might also appear in the future.
694pub trait Mappable<D> {
695    type Output<O>;
696    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O>;
697}
698
699impl<D, F> DualStruct<D, F> for () {
700    type Real = ();
701    type Inner = ();
702    fn re(&self) {}
703    fn from_inner(_: &Self::Inner) -> Self {}
704}
705
706impl<D> Mappable<D> for () {
707    type Output<O> = ();
708    fn map_dual<M: FnOnce(D) -> O, O>(self, _: M) {}
709}
710
711impl DualStruct<f32, f32> for f32 {
712    type Real = f32;
713    type Inner = f32;
714    fn re(&self) -> f32 {
715        *self
716    }
717    fn from_inner(inner: &Self::Inner) -> Self {
718        *inner
719    }
720}
721
722impl Mappable<f32> for f32 {
723    type Output<O> = O;
724    fn map_dual<M: FnOnce(f32) -> O, O>(self, f: M) -> Self::Output<O> {
725        f(self)
726    }
727}
728
729impl DualStruct<f64, f64> for f64 {
730    type Real = f64;
731    type Inner = f64;
732    fn re(&self) -> f64 {
733        *self
734    }
735    fn from_inner(inner: &Self::Inner) -> Self {
736        *inner
737    }
738}
739
740impl Mappable<f64> for f64 {
741    type Output<O> = O;
742    fn map_dual<M: FnOnce(f64) -> O, O>(self, f: M) -> Self::Output<O> {
743        f(self)
744    }
745}
746
747impl<D, F, T1: DualStruct<D, F>, T2: DualStruct<D, F>> DualStruct<D, F> for (T1, T2) {
748    type Real = (T1::Real, T2::Real);
749    type Inner = (T1::Inner, T2::Inner);
750    fn re(&self) -> Self::Real {
751        let (s1, s2) = self;
752        (s1.re(), s2.re())
753    }
754    fn from_inner(re: &Self::Inner) -> Self {
755        let (r1, r2) = re;
756        (T1::from_inner(r1), T2::from_inner(r2))
757    }
758}
759
760impl<D, T1: Mappable<D>, T2: Mappable<D>> Mappable<D> for (T1, T2) {
761    type Output<O> = (T1::Output<O>, T2::Output<O>);
762    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
763        let (s1, s2) = self;
764        (s1.map_dual(&f), s2.map_dual(&f))
765    }
766}
767
768impl<D, F, T1: DualStruct<D, F>, T2: DualStruct<D, F>, T3: DualStruct<D, F>> DualStruct<D, F>
769    for (T1, T2, T3)
770{
771    type Real = (T1::Real, T2::Real, T3::Real);
772    type Inner = (T1::Inner, T2::Inner, T3::Inner);
773    fn re(&self) -> Self::Real {
774        let (s1, s2, s3) = self;
775        (s1.re(), s2.re(), s3.re())
776    }
777    fn from_inner(inner: &Self::Inner) -> Self {
778        let (r1, r2, r3) = inner;
779        (T1::from_inner(r1), T2::from_inner(r2), T3::from_inner(r3))
780    }
781}
782
783impl<D, T1: Mappable<D>, T2: Mappable<D>, T3: Mappable<D>> Mappable<D> for (T1, T2, T3) {
784    type Output<O> = (T1::Output<O>, T2::Output<O>, T3::Output<O>);
785    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
786        let (s1, s2, s3) = self;
787        (s1.map_dual(&f), s2.map_dual(&f), s3.map_dual(&f))
788    }
789}
790
791impl<D, F, T1: DualStruct<D, F>, T2: DualStruct<D, F>, T3: DualStruct<D, F>, T4: DualStruct<D, F>>
792    DualStruct<D, F> for (T1, T2, T3, T4)
793{
794    type Real = (T1::Real, T2::Real, T3::Real, T4::Real);
795    type Inner = (T1::Inner, T2::Inner, T3::Inner, T4::Inner);
796    fn re(&self) -> Self::Real {
797        let (s1, s2, s3, s4) = self;
798        (s1.re(), s2.re(), s3.re(), s4.re())
799    }
800    fn from_inner(inner: &Self::Inner) -> Self {
801        let (r1, r2, r3, r4) = inner;
802        (
803            T1::from_inner(r1),
804            T2::from_inner(r2),
805            T3::from_inner(r3),
806            T4::from_inner(r4),
807        )
808    }
809}
810
811impl<D, T1: Mappable<D>, T2: Mappable<D>, T3: Mappable<D>, T4: Mappable<D>> Mappable<D>
812    for (T1, T2, T3, T4)
813{
814    type Output<O> = (T1::Output<O>, T2::Output<O>, T3::Output<O>, T4::Output<O>);
815    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
816        let (s1, s2, s3, s4) = self;
817        (
818            s1.map_dual(&f),
819            s2.map_dual(&f),
820            s3.map_dual(&f),
821            s4.map_dual(&f),
822        )
823    }
824}
825
826impl<
827    D,
828    F,
829    T1: DualStruct<D, F>,
830    T2: DualStruct<D, F>,
831    T3: DualStruct<D, F>,
832    T4: DualStruct<D, F>,
833    T5: DualStruct<D, F>,
834> DualStruct<D, F> for (T1, T2, T3, T4, T5)
835{
836    type Real = (T1::Real, T2::Real, T3::Real, T4::Real, T5::Real);
837    type Inner = (T1::Inner, T2::Inner, T3::Inner, T4::Inner, T5::Inner);
838    fn re(&self) -> Self::Real {
839        let (s1, s2, s3, s4, s5) = self;
840        (s1.re(), s2.re(), s3.re(), s4.re(), s5.re())
841    }
842    fn from_inner(inner: &Self::Inner) -> Self {
843        let (r1, r2, r3, r4, r5) = inner;
844        (
845            T1::from_inner(r1),
846            T2::from_inner(r2),
847            T3::from_inner(r3),
848            T4::from_inner(r4),
849            T5::from_inner(r5),
850        )
851    }
852}
853
854impl<D, T1: Mappable<D>, T2: Mappable<D>, T3: Mappable<D>, T4: Mappable<D>, T5: Mappable<D>>
855    Mappable<D> for (T1, T2, T3, T4, T5)
856{
857    type Output<O> = (
858        T1::Output<O>,
859        T2::Output<O>,
860        T3::Output<O>,
861        T4::Output<O>,
862        T5::Output<O>,
863    );
864    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
865        let (s1, s2, s3, s4, s5) = self;
866        (
867            s1.map_dual(&f),
868            s2.map_dual(&f),
869            s3.map_dual(&f),
870            s4.map_dual(&f),
871            s5.map_dual(&f),
872        )
873    }
874}
875
876impl<D, F, T: DualStruct<D, F>, const N: usize> DualStruct<D, F> for [T; N] {
877    type Real = [T::Real; N];
878    type Inner = [T::Inner; N];
879    fn re(&self) -> Self::Real {
880        self.each_ref().map(|x| x.re())
881    }
882    fn from_inner(re: &Self::Inner) -> Self {
883        re.each_ref().map(T::from_inner)
884    }
885}
886
887impl<D, T: Mappable<D>, const N: usize> Mappable<D> for [T; N] {
888    type Output<O> = [T::Output<O>; N];
889    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
890        self.map(|x| x.map_dual(&f))
891    }
892}
893
894impl<D, F, T: DualStruct<D, F>> DualStruct<D, F> for Option<T> {
895    type Real = Option<T::Real>;
896    type Inner = Option<T::Inner>;
897    fn re(&self) -> Self::Real {
898        self.as_ref().map(|x| x.re())
899    }
900    fn from_inner(inner: &Self::Inner) -> Self {
901        inner.as_ref().map(|x| T::from_inner(x))
902    }
903}
904
905impl<D, T: Mappable<D>> Mappable<D> for Option<T> {
906    type Output<O> = Option<T::Output<O>>;
907    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
908        self.map(|x| x.map_dual(f))
909    }
910}
911
912impl<D, T: Mappable<D>, E> Mappable<D> for Result<T, E> {
913    type Output<O> = Result<T::Output<O>, E>;
914    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
915        self.map(|x| x.map_dual(f))
916    }
917}
918
919impl<D, F, T: DualStruct<D, F>> DualStruct<D, F> for Vec<T> {
920    type Real = Vec<T::Real>;
921    type Inner = Vec<T::Inner>;
922    fn re(&self) -> Self::Real {
923        self.iter().map(|x| x.re()).collect()
924    }
925    fn from_inner(inner: &Self::Inner) -> Self {
926        inner.iter().map(|x| T::from_inner(x)).collect()
927    }
928}
929
930impl<D, T: Mappable<D>> Mappable<D> for Vec<T> {
931    type Output<O> = Vec<T::Output<O>>;
932    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
933        self.into_iter().map(|x| x.map_dual(&f)).collect()
934    }
935}
936
937impl<D, F, T: DualStruct<D, F>, K: Clone + Eq + Hash> DualStruct<D, F> for HashMap<K, T> {
938    type Real = HashMap<K, T::Real>;
939    type Inner = HashMap<K, T::Inner>;
940    fn re(&self) -> Self::Real {
941        self.iter().map(|(k, x)| (k.clone(), x.re())).collect()
942    }
943    fn from_inner(inner: &Self::Inner) -> Self {
944        inner
945            .iter()
946            .map(|(k, x)| (k.clone(), T::from_inner(x)))
947            .collect()
948    }
949}
950
951impl<D, T: Mappable<D>, K: Eq + Hash> Mappable<D> for HashMap<K, T> {
952    type Output<O> = HashMap<K, T::Output<O>>;
953    fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
954        self.into_iter().map(|(k, x)| (k, x.map_dual(&f))).collect()
955    }
956}
957
958impl<D: DualNum<F>, F: DualNumFloat, R: Dim, C: Dim> DualStruct<D, F> for OMatrix<D, R, C>
959where
960    DefaultAllocator: Allocator<R, C>,
961    D::Inner: DualNum<F>,
962{
963    type Real = OMatrix<F, R, C>;
964    type Inner = OMatrix<D::Inner, R, C>;
965    fn re(&self) -> Self::Real {
966        self.map(|x| x.re())
967    }
968    fn from_inner(inner: &Self::Inner) -> Self {
969        inner.map(|x| D::from_inner(&x))
970    }
971}
972
973impl<D: Scalar, R: Dim, C: Dim> Mappable<Self> for OMatrix<D, R, C>
974where
975    DefaultAllocator: Allocator<R, C>,
976{
977    type Output<O> = O;
978    fn map_dual<M: Fn(Self) -> O, O>(self, f: M) -> O {
979        f(self)
980    }
981}