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 nalgebra_macros;
236#[macro_use]
237mod impl_derivatives;
238
239mod bessel;
240mod datatypes;
241mod explicit;
242mod implicit;
243pub use bessel::BesselDual;
244pub use datatypes::derivative::Derivative;
245pub use datatypes::dual::{Dual, Dual32, Dual64};
246pub use datatypes::dual_vec::{
247 DualDVec32, DualDVec64, DualSVec, DualSVec32, DualSVec64, DualVec, DualVec32, DualVec64,
248};
249pub use datatypes::dual2::{Dual2, Dual2_32, Dual2_64};
250pub use datatypes::dual2_vec::{
251 Dual2DVec, Dual2DVec32, Dual2DVec64, Dual2SVec, Dual2SVec32, Dual2SVec64, Dual2Vec, Dual2Vec32,
252 Dual2Vec64,
253};
254pub use datatypes::dual3::{Dual3, Dual3_32, Dual3_64};
255pub use datatypes::hyperdual::{HyperDual, HyperDual32, HyperDual64};
256pub use datatypes::hyperdual_vec::{
257 HyperDualDVec32, HyperDualDVec64, HyperDualSVec32, HyperDualSVec64, HyperDualVec,
258 HyperDualVec32, HyperDualVec64,
259};
260pub use datatypes::hyperhyperdual::{HyperHyperDual, HyperHyperDual32, HyperHyperDual64};
261pub use datatypes::real::Real;
262pub use explicit::{
263 Gradients, first_derivative, gradient, hessian, jacobian, partial, partial_hessian, partial2,
264 partial3, second_derivative, second_partial_derivative, third_derivative,
265 third_partial_derivative, third_partial_derivative_vec, zeroth_derivative,
266};
267pub use implicit::{
268 ImplicitDerivative, ImplicitFunction, implicit_derivative, implicit_derivative_binary,
269 implicit_derivative_sp, implicit_derivative_vec,
270};
271
272pub mod linalg;
273
274#[cfg(feature = "python")]
275pub mod python;
276
277#[cfg(feature = "python_macro")]
278mod python_macro;
279
280/// A generalized (hyper) dual number.
281#[cfg(feature = "ndarray")]
282pub trait DualNum<F>:
283 NumOps
284 + for<'r> NumOps<&'r Self>
285 + Signed
286 + NumOps<F>
287 + NumAssignOps
288 + NumAssignOps<F>
289 + Clone
290 + Inv<Output = Self>
291 + Sum
292 + Product
293 + FromPrimitive
294 + From<F>
295 + DualStruct<Self, F, Real = F>
296 + Mappable<Self>
297 + fmt::Display
298 + PartialOrd
299 + PartialOrd<F>
300 + fmt::Debug
301 + ScalarOperand
302 + 'static
303{
304 /// Highest derivative that can be calculated with this struct
305 const NDERIV: usize;
306
307 /// Reciprocal (inverse) of a number `1/x`
308 fn recip(&self) -> Self;
309
310 /// Power with integer exponent `x^n`
311 fn powi(&self, n: i32) -> Self;
312
313 /// Power with real exponent `x^n`
314 fn powf(&self, n: F) -> Self;
315
316 /// Square root
317 fn sqrt(&self) -> Self;
318
319 /// Cubic root
320 fn cbrt(&self) -> Self;
321
322 /// Exponential `e^x`
323 fn exp(&self) -> Self;
324
325 /// Exponential with base 2 `2^x`
326 fn exp2(&self) -> Self;
327
328 /// Exponential minus 1 `e^x-1`
329 fn exp_m1(&self) -> Self;
330
331 /// Natural logarithm
332 fn ln(&self) -> Self;
333
334 /// Logarithm with arbitrary base
335 fn log(&self, base: F) -> Self;
336
337 /// Logarithm with base 2
338 fn log2(&self) -> Self;
339
340 /// Logarithm with base 10
341 fn log10(&self) -> Self;
342
343 /// Logarithm on x plus one `ln(1+x)`
344 fn ln_1p(&self) -> Self;
345
346 /// Sine
347 fn sin(&self) -> Self;
348
349 /// Cosine
350 fn cos(&self) -> Self;
351
352 /// Tangent
353 fn tan(&self) -> Self;
354
355 /// Calculate sine and cosine simultaneously
356 fn sin_cos(&self) -> (Self, Self);
357
358 /// Arcsine
359 fn asin(&self) -> Self;
360
361 /// Arccosine
362 fn acos(&self) -> Self;
363
364 /// Arctangent
365 fn atan(&self) -> Self;
366
367 /// Arctangent
368 fn atan2(&self, other: Self) -> Self;
369
370 /// Hyperbolic sine
371 fn sinh(&self) -> Self;
372
373 /// Hyperbolic cosine
374 fn cosh(&self) -> Self;
375
376 /// Hyperbolic tangent
377 fn tanh(&self) -> Self;
378
379 /// Area hyperbolic sine
380 fn asinh(&self) -> Self;
381
382 /// Area hyperbolic cosine
383 fn acosh(&self) -> Self;
384
385 /// Area hyperbolic tangent
386 fn atanh(&self) -> Self;
387
388 /// 0th order spherical Bessel function of the first kind
389 fn sph_j0(&self) -> Self;
390
391 /// 1st order spherical Bessel function of the first kind
392 fn sph_j1(&self) -> Self;
393
394 /// 2nd order spherical Bessel function of the first kind
395 fn sph_j2(&self) -> Self;
396
397 /// Fused multiply-add
398 #[inline]
399 fn mul_add(&self, a: Self, b: Self) -> Self {
400 self.clone() * a + b
401 }
402
403 /// Power with dual exponent `x^n`
404 #[inline]
405 fn powd(&self, exp: Self) -> Self {
406 (self.ln() * exp).exp()
407 }
408}
409
410/// A generalized (hyper) dual number.
411#[cfg(not(feature = "ndarray"))]
412pub trait DualNum<F>:
413 NumOps
414 + for<'r> NumOps<&'r Self>
415 + Signed
416 + NumOps<F>
417 + NumAssignOps
418 + NumAssignOps<F>
419 + Clone
420 + Inv<Output = Self>
421 + Sum
422 + Product
423 + FromPrimitive
424 + From<F>
425 + DualStruct<Self, F, Real = F>
426 + Mappable<Self>
427 + fmt::Display
428 + PartialOrd
429 + PartialOrd<F>
430 + fmt::Debug
431 + 'static
432{
433 /// Highest derivative that can be calculated with this struct
434 const NDERIV: usize;
435
436 /// Reciprocal (inverse) of a number `1/x`
437 fn recip(&self) -> Self;
438
439 /// Power with integer exponent `x^n`
440 fn powi(&self, n: i32) -> Self;
441
442 /// Power with real exponent `x^n`
443 fn powf(&self, n: F) -> Self;
444
445 /// Square root
446 fn sqrt(&self) -> Self;
447
448 /// Cubic root
449 fn cbrt(&self) -> Self;
450
451 /// Exponential `e^x`
452 fn exp(&self) -> Self;
453
454 /// Exponential with base 2 `2^x`
455 fn exp2(&self) -> Self;
456
457 /// Exponential minus 1 `e^x-1`
458 fn exp_m1(&self) -> Self;
459
460 /// Natural logarithm
461 fn ln(&self) -> Self;
462
463 /// Logarithm with arbitrary base
464 fn log(&self, base: F) -> Self;
465
466 /// Logarithm with base 2
467 fn log2(&self) -> Self;
468
469 /// Logarithm with base 10
470 fn log10(&self) -> Self;
471
472 /// Logarithm on x plus one `ln(1+x)`
473 fn ln_1p(&self) -> Self;
474
475 /// Sine
476 fn sin(&self) -> Self;
477
478 /// Cosine
479 fn cos(&self) -> Self;
480
481 /// Tangent
482 fn tan(&self) -> Self;
483
484 /// Calculate sine and cosine simultaneously
485 fn sin_cos(&self) -> (Self, Self);
486
487 /// Arcsine
488 fn asin(&self) -> Self;
489
490 /// Arccosine
491 fn acos(&self) -> Self;
492
493 /// Arctangent
494 fn atan(&self) -> Self;
495
496 /// Arctangent
497 fn atan2(&self, other: Self) -> Self;
498
499 /// Hyperbolic sine
500 fn sinh(&self) -> Self;
501
502 /// Hyperbolic cosine
503 fn cosh(&self) -> Self;
504
505 /// Hyperbolic tangent
506 fn tanh(&self) -> Self;
507
508 /// Area hyperbolic sine
509 fn asinh(&self) -> Self;
510
511 /// Area hyperbolic cosine
512 fn acosh(&self) -> Self;
513
514 /// Area hyperbolic tangent
515 fn atanh(&self) -> Self;
516
517 /// 0th order spherical Bessel function of the first kind
518 fn sph_j0(&self) -> Self;
519
520 /// 1st order spherical Bessel function of the first kind
521 fn sph_j1(&self) -> Self;
522
523 /// 2nd order spherical Bessel function of the first kind
524 fn sph_j2(&self) -> Self;
525
526 /// Fused multiply-add
527 #[inline]
528 fn mul_add(&self, a: Self, b: Self) -> Self {
529 self.clone() * a + b
530 }
531
532 /// Power with dual exponent `x^n`
533 #[inline]
534 fn powd(&self, exp: Self) -> Self {
535 (self.ln() * exp).exp()
536 }
537}
538
539/// A generalized (hyper) dual number that has a static size.
540pub trait DualNumCopy<F>: DualNum<F> + Copy + Send + Sync {}
541impl<T: DualNum<F> + Copy + Send + Sync, F> DualNumCopy<F> for T {}
542
543/// The underlying data type of individual derivatives. Usually f32 or f64.
544pub trait DualNumFloat:
545 Float + FloatConst + FromPrimitive + Signed + fmt::Display + fmt::Debug + Sync + Send + 'static
546{
547}
548impl<T> DualNumFloat for T where
549 T: Float
550 + FloatConst
551 + FromPrimitive
552 + Signed
553 + fmt::Display
554 + fmt::Debug
555 + Sync
556 + Send
557 + 'static
558{
559}
560
561macro_rules! impl_dual_num_float {
562 ($float:ty) => {
563 impl DualNum<$float> for $float {
564 const NDERIV: usize = 0;
565
566 fn mul_add(&self, a: Self, b: Self) -> Self {
567 <$float>::mul_add(*self, a, b)
568 }
569 fn recip(&self) -> Self {
570 <$float>::recip(*self)
571 }
572 fn powi(&self, n: i32) -> Self {
573 <$float>::powi(*self, n)
574 }
575 fn powf(&self, n: Self) -> Self {
576 <$float>::powf(*self, n)
577 }
578 fn powd(&self, n: Self) -> Self {
579 <$float>::powf(*self, n)
580 }
581 fn sqrt(&self) -> Self {
582 <$float>::sqrt(*self)
583 }
584 fn exp(&self) -> Self {
585 <$float>::exp(*self)
586 }
587 fn exp2(&self) -> Self {
588 <$float>::exp2(*self)
589 }
590 fn ln(&self) -> Self {
591 <$float>::ln(*self)
592 }
593 fn log(&self, base: Self) -> Self {
594 <$float>::log(*self, base)
595 }
596 fn log2(&self) -> Self {
597 <$float>::log2(*self)
598 }
599 fn log10(&self) -> Self {
600 <$float>::log10(*self)
601 }
602 fn cbrt(&self) -> Self {
603 <$float>::cbrt(*self)
604 }
605 fn sin(&self) -> Self {
606 <$float>::sin(*self)
607 }
608 fn cos(&self) -> Self {
609 <$float>::cos(*self)
610 }
611 fn tan(&self) -> Self {
612 <$float>::tan(*self)
613 }
614 fn asin(&self) -> Self {
615 <$float>::asin(*self)
616 }
617 fn acos(&self) -> Self {
618 <$float>::acos(*self)
619 }
620 fn atan(&self) -> Self {
621 <$float>::atan(*self)
622 }
623 fn atan2(&self, other: $float) -> Self {
624 <$float>::atan2(*self, other)
625 }
626 fn sin_cos(&self) -> (Self, Self) {
627 <$float>::sin_cos(*self)
628 }
629 fn exp_m1(&self) -> Self {
630 <$float>::exp_m1(*self)
631 }
632 fn ln_1p(&self) -> Self {
633 <$float>::ln_1p(*self)
634 }
635 fn sinh(&self) -> Self {
636 <$float>::sinh(*self)
637 }
638 fn cosh(&self) -> Self {
639 <$float>::cosh(*self)
640 }
641 fn tanh(&self) -> Self {
642 <$float>::tanh(*self)
643 }
644 fn asinh(&self) -> Self {
645 <$float>::asinh(*self)
646 }
647 fn acosh(&self) -> Self {
648 <$float>::acosh(*self)
649 }
650 fn atanh(&self) -> Self {
651 <$float>::atanh(*self)
652 }
653 fn sph_j0(&self) -> Self {
654 if self.abs() < <$float>::EPSILON {
655 1.0 - self * self / 6.0
656 } else {
657 self.sin() / self
658 }
659 }
660 fn sph_j1(&self) -> Self {
661 if self.abs() < <$float>::EPSILON {
662 self / 3.0
663 } else {
664 let sc = self.sin_cos();
665 let rec = self.recip();
666 (sc.0 * rec - sc.1) * rec
667 }
668 }
669 fn sph_j2(&self) -> Self {
670 if self.abs() < <$float>::EPSILON {
671 self * self / 15.0
672 } else {
673 let sc = self.sin_cos();
674 let s2 = self * self;
675 ((3.0 - s2) * sc.0 - 3.0 * self * sc.1) / (self * s2)
676 }
677 }
678 }
679 };
680}
681
682impl_dual_num_float!(f32);
683impl_dual_num_float!(f64);
684
685/// A struct that contains dual numbers. Needed for arbitrary arguments in [ImplicitFunction].
686///
687/// The trait is implemented for all dual types themselves, and common data types (tuple, vec,
688/// array, ...) and can be implemented for custom data types to achieve full flexibility.
689pub trait DualStruct<D, F> {
690 type Real;
691 type Inner;
692 fn re(&self) -> Self::Real;
693 fn from_inner(inner: &Self::Inner) -> Self;
694}
695
696/// Trait for structs used as an output of functions for which derivatives are calculated.
697///
698/// The main intention is to generalize the calculation of derivatives to fallible functions, but
699/// other use cases might also appear in the future.
700pub trait Mappable<D> {
701 type Output<O>;
702 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O>;
703}
704
705impl<D, F> DualStruct<D, F> for () {
706 type Real = ();
707 type Inner = ();
708 fn re(&self) {}
709 fn from_inner(_: &Self::Inner) -> Self {}
710}
711
712impl<D> Mappable<D> for () {
713 type Output<O> = ();
714 fn map_dual<M: FnOnce(D) -> O, O>(self, _: M) {}
715}
716
717impl DualStruct<f32, f32> for f32 {
718 type Real = f32;
719 type Inner = f32;
720 fn re(&self) -> f32 {
721 *self
722 }
723 fn from_inner(inner: &Self::Inner) -> Self {
724 *inner
725 }
726}
727
728impl Mappable<f32> for f32 {
729 type Output<O> = O;
730 fn map_dual<M: FnOnce(f32) -> O, O>(self, f: M) -> Self::Output<O> {
731 f(self)
732 }
733}
734
735impl DualStruct<f64, f64> for f64 {
736 type Real = f64;
737 type Inner = f64;
738 fn re(&self) -> f64 {
739 *self
740 }
741 fn from_inner(inner: &Self::Inner) -> Self {
742 *inner
743 }
744}
745
746impl Mappable<f64> for f64 {
747 type Output<O> = O;
748 fn map_dual<M: FnOnce(f64) -> O, O>(self, f: M) -> Self::Output<O> {
749 f(self)
750 }
751}
752
753impl<D, F, T1: DualStruct<D, F>, T2: DualStruct<D, F>> DualStruct<D, F> for (T1, T2) {
754 type Real = (T1::Real, T2::Real);
755 type Inner = (T1::Inner, T2::Inner);
756 fn re(&self) -> Self::Real {
757 let (s1, s2) = self;
758 (s1.re(), s2.re())
759 }
760 fn from_inner(re: &Self::Inner) -> Self {
761 let (r1, r2) = re;
762 (T1::from_inner(r1), T2::from_inner(r2))
763 }
764}
765
766impl<D, T1: Mappable<D>, T2: Mappable<D>> Mappable<D> for (T1, T2) {
767 type Output<O> = (T1::Output<O>, T2::Output<O>);
768 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
769 let (s1, s2) = self;
770 (s1.map_dual(&f), s2.map_dual(&f))
771 }
772}
773
774impl<D, F, T1: DualStruct<D, F>, T2: DualStruct<D, F>, T3: DualStruct<D, F>> DualStruct<D, F>
775 for (T1, T2, T3)
776{
777 type Real = (T1::Real, T2::Real, T3::Real);
778 type Inner = (T1::Inner, T2::Inner, T3::Inner);
779 fn re(&self) -> Self::Real {
780 let (s1, s2, s3) = self;
781 (s1.re(), s2.re(), s3.re())
782 }
783 fn from_inner(inner: &Self::Inner) -> Self {
784 let (r1, r2, r3) = inner;
785 (T1::from_inner(r1), T2::from_inner(r2), T3::from_inner(r3))
786 }
787}
788
789impl<D, T1: Mappable<D>, T2: Mappable<D>, T3: Mappable<D>> Mappable<D> for (T1, T2, T3) {
790 type Output<O> = (T1::Output<O>, T2::Output<O>, T3::Output<O>);
791 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
792 let (s1, s2, s3) = self;
793 (s1.map_dual(&f), s2.map_dual(&f), s3.map_dual(&f))
794 }
795}
796
797impl<D, F, T1: DualStruct<D, F>, T2: DualStruct<D, F>, T3: DualStruct<D, F>, T4: DualStruct<D, F>>
798 DualStruct<D, F> for (T1, T2, T3, T4)
799{
800 type Real = (T1::Real, T2::Real, T3::Real, T4::Real);
801 type Inner = (T1::Inner, T2::Inner, T3::Inner, T4::Inner);
802 fn re(&self) -> Self::Real {
803 let (s1, s2, s3, s4) = self;
804 (s1.re(), s2.re(), s3.re(), s4.re())
805 }
806 fn from_inner(inner: &Self::Inner) -> Self {
807 let (r1, r2, r3, r4) = inner;
808 (
809 T1::from_inner(r1),
810 T2::from_inner(r2),
811 T3::from_inner(r3),
812 T4::from_inner(r4),
813 )
814 }
815}
816
817impl<D, T1: Mappable<D>, T2: Mappable<D>, T3: Mappable<D>, T4: Mappable<D>> Mappable<D>
818 for (T1, T2, T3, T4)
819{
820 type Output<O> = (T1::Output<O>, T2::Output<O>, T3::Output<O>, T4::Output<O>);
821 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
822 let (s1, s2, s3, s4) = self;
823 (
824 s1.map_dual(&f),
825 s2.map_dual(&f),
826 s3.map_dual(&f),
827 s4.map_dual(&f),
828 )
829 }
830}
831
832impl<
833 D,
834 F,
835 T1: DualStruct<D, F>,
836 T2: DualStruct<D, F>,
837 T3: DualStruct<D, F>,
838 T4: DualStruct<D, F>,
839 T5: DualStruct<D, F>,
840> DualStruct<D, F> for (T1, T2, T3, T4, T5)
841{
842 type Real = (T1::Real, T2::Real, T3::Real, T4::Real, T5::Real);
843 type Inner = (T1::Inner, T2::Inner, T3::Inner, T4::Inner, T5::Inner);
844 fn re(&self) -> Self::Real {
845 let (s1, s2, s3, s4, s5) = self;
846 (s1.re(), s2.re(), s3.re(), s4.re(), s5.re())
847 }
848 fn from_inner(inner: &Self::Inner) -> Self {
849 let (r1, r2, r3, r4, r5) = inner;
850 (
851 T1::from_inner(r1),
852 T2::from_inner(r2),
853 T3::from_inner(r3),
854 T4::from_inner(r4),
855 T5::from_inner(r5),
856 )
857 }
858}
859
860impl<D, T1: Mappable<D>, T2: Mappable<D>, T3: Mappable<D>, T4: Mappable<D>, T5: Mappable<D>>
861 Mappable<D> for (T1, T2, T3, T4, T5)
862{
863 type Output<O> = (
864 T1::Output<O>,
865 T2::Output<O>,
866 T3::Output<O>,
867 T4::Output<O>,
868 T5::Output<O>,
869 );
870 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
871 let (s1, s2, s3, s4, s5) = self;
872 (
873 s1.map_dual(&f),
874 s2.map_dual(&f),
875 s3.map_dual(&f),
876 s4.map_dual(&f),
877 s5.map_dual(&f),
878 )
879 }
880}
881
882impl<D, F, T: DualStruct<D, F>, const N: usize> DualStruct<D, F> for [T; N] {
883 type Real = [T::Real; N];
884 type Inner = [T::Inner; N];
885 fn re(&self) -> Self::Real {
886 self.each_ref().map(|x| x.re())
887 }
888 fn from_inner(re: &Self::Inner) -> Self {
889 re.each_ref().map(T::from_inner)
890 }
891}
892
893impl<D, T: Mappable<D>, const N: usize> Mappable<D> for [T; N] {
894 type Output<O> = [T::Output<O>; N];
895 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
896 self.map(|x| x.map_dual(&f))
897 }
898}
899
900impl<D, F, T: DualStruct<D, F>> DualStruct<D, F> for Option<T> {
901 type Real = Option<T::Real>;
902 type Inner = Option<T::Inner>;
903 fn re(&self) -> Self::Real {
904 self.as_ref().map(|x| x.re())
905 }
906 fn from_inner(inner: &Self::Inner) -> Self {
907 inner.as_ref().map(|x| T::from_inner(x))
908 }
909}
910
911impl<D, T: Mappable<D>> Mappable<D> for Option<T> {
912 type Output<O> = Option<T::Output<O>>;
913 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
914 self.map(|x| x.map_dual(f))
915 }
916}
917
918impl<D, T: Mappable<D>, E> Mappable<D> for Result<T, E> {
919 type Output<O> = Result<T::Output<O>, E>;
920 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
921 self.map(|x| x.map_dual(f))
922 }
923}
924
925impl<D, F, T: DualStruct<D, F>> DualStruct<D, F> for Vec<T> {
926 type Real = Vec<T::Real>;
927 type Inner = Vec<T::Inner>;
928 fn re(&self) -> Self::Real {
929 self.iter().map(|x| x.re()).collect()
930 }
931 fn from_inner(inner: &Self::Inner) -> Self {
932 inner.iter().map(|x| T::from_inner(x)).collect()
933 }
934}
935
936impl<D, T: Mappable<D>> Mappable<D> for Vec<T> {
937 type Output<O> = Vec<T::Output<O>>;
938 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
939 self.into_iter().map(|x| x.map_dual(&f)).collect()
940 }
941}
942
943impl<D, F, T: DualStruct<D, F>, K: Clone + Eq + Hash> DualStruct<D, F> for HashMap<K, T> {
944 type Real = HashMap<K, T::Real>;
945 type Inner = HashMap<K, T::Inner>;
946 fn re(&self) -> Self::Real {
947 self.iter().map(|(k, x)| (k.clone(), x.re())).collect()
948 }
949 fn from_inner(inner: &Self::Inner) -> Self {
950 inner
951 .iter()
952 .map(|(k, x)| (k.clone(), T::from_inner(x)))
953 .collect()
954 }
955}
956
957impl<D, T: Mappable<D>, K: Eq + Hash> Mappable<D> for HashMap<K, T> {
958 type Output<O> = HashMap<K, T::Output<O>>;
959 fn map_dual<M: Fn(D) -> O, O>(self, f: M) -> Self::Output<O> {
960 self.into_iter().map(|(k, x)| (k, x.map_dual(&f))).collect()
961 }
962}
963
964impl<D: DualNum<F>, F: DualNumFloat, R: Dim, C: Dim> DualStruct<D, F> for OMatrix<D, R, C>
965where
966 DefaultAllocator: Allocator<R, C>,
967 D::Inner: DualNum<F>,
968{
969 type Real = OMatrix<F, R, C>;
970 type Inner = OMatrix<D::Inner, R, C>;
971 fn re(&self) -> Self::Real {
972 self.map(|x| x.re())
973 }
974 fn from_inner(inner: &Self::Inner) -> Self {
975 inner.map(|x| D::from_inner(&x))
976 }
977}
978
979impl<D: Scalar, R: Dim, C: Dim> Mappable<Self> for OMatrix<D, R, C>
980where
981 DefaultAllocator: Allocator<R, C>,
982{
983 type Output<O> = O;
984 fn map_dual<M: Fn(Self) -> O, O>(self, f: M) -> O {
985 f(self)
986 }
987}