geomc 1.0
A c++ linear algebra template library
|
Class implementing the dual numbers, whose arithmetic operations perform a simultaneous calculation of the first derivative. More...
#include <geomc/function/functiondetail/DualImpl.h>
Public Types | |
typedef detail::discontinuity_impl< T, Dp > | discontinuity_policy |
Public Member Functions | |
constexpr | Dual () |
Construct a Dual default value and 0 derivative. | |
constexpr | Dual (const Dual &d)=default |
Copy-construct a Dual. | |
template<DiscontinuityPolicy DP1> | |
constexpr | Dual (const Dual< T, DP1 > &d) |
Construct a Dual from one with a different discontinuity policy. | |
constexpr | Dual (T x) |
Construct a Dual with value x and 0 derivative. | |
constexpr | Dual (T x, T dx) |
Construct a Dual with value x and derivative dx . | |
template<typename U> requires std::convertible_to<T,U> | |
operator Dual< U > () const | |
template<typename U, DiscontinuityPolicy P2 = Dp> requires std::convertible_to<T,U> | |
operator U () const | |
bool | operator!= (const Dual< T, Dp > &d) |
Inequality of primal component. | |
Dual< T, Dp > & | operator++ () |
Pre-increment. | |
Dual< T, Dp > | operator++ (int) |
Post-increment. | |
Dual< T, Dp > & | operator-- () |
Pre-decrement. | |
Dual< T, Dp > | operator-- (int) |
Post-decrement. | |
bool | operator< (const Dual< T, Dp > &d) |
Less-than comparison of primal component. | |
bool | operator<= (const Dual< T, Dp > &d) |
Less-or-equal-to comparison of primal component. | |
constexpr Dual< T, Dp > & | operator= (const Dual< T, Dp > &other)=default |
constexpr Dual< T, Dp > & | operator= (T other) |
bool | operator== (const Dual< T, Dp > &d) |
Equality of primal component. | |
bool | operator> (const Dual< T, Dp > &d) |
Greater-than comparison of primal component. | |
bool | operator>= (const Dual< T, Dp > &d) |
Greater-or-equal-to comparison of primal component. | |
Public Attributes | |
T | dx |
Dual (epsilon) component. | |
T | x |
Real (primal) component. | |
Related Symbols | |
(Note that these are not member symbols.) | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | abs (const geom::Dual< T, P > &d) |
Absolute value. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | acos (const geom::Dual< T, P > &d) |
Arccos (inverse cosine) function. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | asin (const geom::Dual< T, P > &d) |
Arcsin (inverse sine) function. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | atan (const geom::Dual< T, P > &d) |
Arctan (inverse tangent) function. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | atan2 (const geom::Dual< T, P > &y, const geom::Dual< T, P > &x) |
Arctangent of y/x in the range [-pi, pi] . | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | ceil (const geom::Dual< T, P > &d) |
Ceiling function. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | cos (const geom::Dual< T, P > &d) |
Cosine function. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | exp (const geom::Dual< T, P > &d) |
Exponential (ex) function. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | floor (const geom::Dual< T, P > &d) |
Floor function. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | fma (geom::Dual< T, P > a, geom::Dual< T, P > b, geom::Dual< T, P > c) |
Fused multiply-add. | |
template<typename T, geom::DiscontinuityPolicy Dp> | |
geom::Dual< T, Dp > | log (const geom::Dual< T, Dp > &d) |
Returns the natural logarithm of d . | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | max (const geom::Dual< T, P > &d1, const geom::Dual< T, P > &d2) |
Maximum. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | min (const geom::Dual< T, P > &d1, const geom::Dual< T, P > &d2) |
Minimum. | |
template<typename T, typename U> | |
geom::Dual< T > | pow (const geom::Dual< T > &base, U xp) |
Returns base raised to exponent xp . | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | pow (const geom::Dual< T, P > &base, const geom::Dual< T, P > &xp) |
Returns base raised to exponent xp . | |
template<typename T, typename U, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | pow (U base, const geom::Dual< T, P > &xp) |
Returns base raised to exponent xp . | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | sin (const geom::Dual< T, P > &d) |
Sine function. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | sqrt (const geom::Dual< T, P > &d) |
Square root. | |
template<typename T, geom::DiscontinuityPolicy P> | |
geom::Dual< T, P > | tan (const geom::Dual< T, P > &d) |
Tangent function. | |
Class implementing the dual numbers, whose arithmetic operations perform a simultaneous calculation of the first derivative.
Dual numbers implicitly compute and store the first derivative of any arbitrary function by extending the reals to include a new nonzero element ε whose square is zero. Thus duals have the form (a + bε); where a
may be thought of as the function value, and b
its first derivative at a
. This technique is commonly known as Automatic Differentiation, or AD.
Fundamental operations on duals (including addition, multiplication, division, and any number of other primitive mathematical functions) implicitly compute and keep track of the derivative by performing the chain and product rules in-place. Efficiency and accuracy is very good (easily better than either symbolic or numerical differentiation), adding only a constant factor to arithmetic operations.
In general, Duals should behave exactly as bare numbers in code.
It is safe to convert a constant value to a Dual (and this will be done implicitly by setting the epsilon component to zero). It is unsafe to convert a Dual number to a bare value, as this forgets the derivative information. Therefore if the Dual is to be used as a constant, it must be cast explicitly, or the primal component must be extracted.
The value of an arbitrary function may be differentiated with respect to its input if and only if those inputs, and all the calculations that depend on those inputs, are performed entirely on Dual numbers. In other words, a function is differentiable if it uses Duals "all the way to the bottom" of its calculation. If ever the primal is extracted and used in some sub-calculation apart from its epsilon component, then the resultant derivative information will be invalid.
The epsilon components of a function's input or output together represent the components of a directional derivative.
Duals are compared only using their primal value, so that they behave just as bare numbers do. This means that two Dual
s with different epsilons may compare equal! If you need exact binary equality, then explicitly check that both x
s and dx
s are equal.
T | The numerical type of the number. |
DP | The DiscontinuityPolicy for handling discontinuous derivatives. |
|
Fused multiply-add.
Compute a * b + c
.