geomc 1.0
A c++ linear algebra template library
Public Types | Public Member Functions | Public Attributes | Friends | Related Functions | List of all members
Dual< T, DP > Class Template Reference

Class implementing the dual numbers, whose arithmetic operations perform a simultaneous calculation of the first derivative. More...

#include <geomc/function/Dual.h>

Public Types

typedef detail::Discontinuity< T, DP > discontinuity_policy
 

Public Member Functions

 Dual ()
 Construct a dual with 0 value and 0 derivative.
 
 Dual (T x)
 Construct a dual with value x and 0 derivative.
 
 Dual (T x, T dx)
 Construct a dual with value x and derivative dx.
 
template<DiscontinuityPolicy DP1>
 Dual (const Dual< T, DP1 > &d)
 
Dual< T > & operator*= (const Dual< T > &d)
 Multiply and assign.
 
template<typename U >
std::enable_if< std::is_scalar< U >::value, Dual< T > & >::type operator*= (U s)
 Multiply and assign.
 
Dual< T > & operator/= (const Dual< T > &d)
 Divide and assign.
 
template<typename U >
std::enable_if< std::is_scalar< U >::value, Dual< T > & >::type operator/= (U s)
 Divide and assign.
 
Dual< T > & operator+= (const Dual< T > &d)
 Add and assign.
 
Dual< T > & operator-= (const Dual< T > &d)
 Subtract and assign.
 
Dual< T > operator- () const
 Negation.
 
T & operator[] (index_t idx)
 Indexing. More...
 
operator[] (index_t idx) const
 Indexing. More...
 
bool operator== (const Dual< T > &d)
 Equality of primal component.
 
bool operator!= (const Dual< T > &d)
 Inequality of primal component.
 
bool operator>= (const Dual< T > &d)
 Greater-or-equal-to comparison of primal component.
 
bool operator<= (const Dual< T > &d)
 Less-or-equal-to comparison of primal component.
 
bool operator> (const Dual< T > &d)
 Greater-than comparison of primal component.
 
bool operator< (const Dual< T > &d)
 Less-than comparison of primal component.
 
template<typename U >
 operator Dual< U > () const
 

Public Attributes

x
 Real (primal) component.
 
dx
 Dual (epsilon) component.
 

Friends

Dual< T > operator* (const Dual< T > &d1, const Dual< T > &d2)
 Multiplication.
 
template<typename U >
std::enable_if< std::is_scalar< U >::value, Dual< T > >::type operator* (const Dual< T > &d1, U s)
 Dual-scalar multiplication.
 
template<typename U >
std::enable_if< std::is_scalar< U >::value, Dual< T > >::type operator* (U s, const Dual< T > &d1)
 Scalar-dual multiplication.
 
Dual< T > operator/ (const Dual< T > &d1, const Dual< T > &d2)
 Division.
 
template<typename U >
std::enable_if< std::is_scalar< U >::value, Dual< T > >::type operator/ (const Dual< T > &d, U s)
 Dual / scalar division.
 
template<typename U >
std::enable_if< std::is_scalar< U >::value, Dual< T > >::type operator/ (U s, const Dual< T > &d)
 Scalar / dual division.
 
Dual< T > operator+ (const Dual< T > &d1, const Dual< T > &d2)
 Addition.
 
Dual< T > operator- (const Dual< T > &d1, const Dual< T > &d2)
 Subtraction.
 

Related Functions

(Note that these are not member functions.)

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 > cos (const geom::Dual< T, P > &d)
 Cosine function.
 
template<typename T , geom::DiscontinuityPolicy P>
geom::Dual< T, P > tan (const geom::Dual< T, P > &d)
 Tangent 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 > acos (const geom::Dual< T, P > &d)
 Arccos (inverse cosine) 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 > exp (const geom::Dual< T, P > &d)
 Exponential (ex) function.
 
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::Dual< T > pow (const geom::Dual< T > &base, U 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 > sqrt (const geom::Dual< T, P > &d)
 Square root.
 
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 > ceil (const geom::Dual< T, P > &d)
 Ceiling 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 > min (const geom::Dual< T, P > &d1, const geom::Dual< T, P > &d2)
 Minimum.
 
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 > fma (geom::Dual< T, P > a, geom::Dual< T, P > b, geom::Dual< T, P > c)
 Fused multiply-add. More...
 

Detailed Description

template<typename T, DiscontinuityPolicy DP = DiscontinuityPolicy::Right>
class geom::Dual< T, DP >

Class implementing the dual numbers, whose arithmetic operations perform a simultaneous calculation of the first derivative.

Overview

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.

Use

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.

Equality

Duals are compared only using their primal value, so that they behave just as bare numbers do. This means that two Duals with different epsilons may compare equal! If you need exact binary equality, then explicitly check that both xs and dxs are equal.

Template Parameters
TThe numerical type of the number.
DPThe DiscontinuityPolicy for handling discontinuous derivatives.

Member Function Documentation

◆ operator[]() [1/2]

T & operator[] ( index_t  idx)
inline

Indexing.

Real value is element 0, epsilon value is element 1.

◆ operator[]() [2/2]

T operator[] ( index_t  idx) const
inline

Indexing.

Real value is element 0, epsilon value is element 1.

Friends And Related Function Documentation

◆ fma()

geom::Dual< T, P > fma ( geom::Dual< T, P >  a,
geom::Dual< T, P >  b,
geom::Dual< T, P >  c 
)
related

Fused multiply-add.

Compute a * b + c.


The documentation for this class was generated from the following file: