chainrules/scalar_ad/
mod.rs

1use core::ops::{Add, Div, Mul, Neg, Sub};
2
3use num_traits::{Float, FloatConst};
4
5/// Scalar trait used by elementary AD rule helpers.
6///
7/// # Examples
8///
9/// ```rust
10/// use chainrules::ScalarAd;
11///
12/// fn takes_scalar<S: ScalarAd>(_x: S) {}
13///
14/// takes_scalar(1.0_f32);
15/// takes_scalar(1.0_f64);
16/// ```
17pub trait ScalarAd:
18    Copy
19    + PartialEq
20    + Neg<Output = Self>
21    + Add<Output = Self>
22    + Sub<Output = Self>
23    + Mul<Output = Self>
24    + Div<Output = Self>
25{
26    /// Real exponent type for `powf`.
27    type Real: Copy + Float + FloatConst;
28
29    /// Complex conjugate (identity for real scalars).
30    fn conj(self) -> Self;
31
32    /// Reciprocal.
33    fn recip(self) -> Self;
34
35    /// Cubic root.
36    fn cbrt(self) -> Self;
37
38    /// Square root.
39    fn sqrt(self) -> Self;
40
41    /// Exponential.
42    fn exp(self) -> Self;
43
44    /// `2^self`.
45    fn exp2(self) -> Self;
46
47    /// `10^self`.
48    fn exp10(self) -> Self;
49
50    /// `exp(self) - 1`.
51    fn expm1(self) -> Self;
52
53    /// Natural logarithm.
54    fn ln(self) -> Self;
55
56    /// `ln(1 + self)`.
57    fn log1p(self) -> Self;
58
59    /// Base-2 logarithm.
60    fn log2(self) -> Self;
61
62    /// Base-10 logarithm.
63    fn log10(self) -> Self;
64
65    /// Sine.
66    fn sin(self) -> Self;
67
68    /// Cosine.
69    fn cos(self) -> Self;
70
71    /// Tangent.
72    fn tan(self) -> Self;
73
74    /// Hyperbolic tangent.
75    fn tanh(self) -> Self;
76
77    /// Arc sine.
78    fn asin(self) -> Self;
79
80    /// Arc cosine.
81    fn acos(self) -> Self;
82
83    /// Arc tangent.
84    fn atan(self) -> Self;
85
86    /// Hyperbolic sine.
87    fn sinh(self) -> Self;
88
89    /// Hyperbolic cosine.
90    fn cosh(self) -> Self;
91
92    /// Area hyperbolic sine.
93    fn asinh(self) -> Self;
94
95    /// Area hyperbolic cosine.
96    fn acosh(self) -> Self;
97
98    /// Area hyperbolic tangent.
99    fn atanh(self) -> Self;
100
101    /// Absolute value.
102    fn abs(self) -> Self::Real;
103
104    /// Absolute value squared.
105    fn abs2(self) -> Self::Real;
106
107    /// Real part.
108    fn real(self) -> Self::Real;
109
110    /// Imaginary part.
111    fn imag(self) -> Self::Real;
112
113    /// Polar angle.
114    fn angle(self) -> Self::Real;
115
116    /// Power by real exponent.
117    fn powf(self, exponent: Self::Real) -> Self;
118
119    /// Power by integer exponent.
120    fn powi(self, exponent: i32) -> Self;
121
122    /// Power by same-scalar exponent.
123    fn pow(self, exponent: Self) -> Self;
124
125    /// Convert real scalar to this scalar type.
126    fn from_real(value: Self::Real) -> Self;
127
128    /// Convert signed integer to this scalar type.
129    fn from_i32(value: i32) -> Self;
130}
131
132mod complex;
133mod real;