Ocean
Ocean::AutomaticDifferentiationT< T, TNumeric > Class Template Reference

This class implements an automatic differentiation functionality. More...

Public Member Functions

 AutomaticDifferentiationT ()=default
 Creates a new differentiation object without initializing the parameters. More...
 
 AutomaticDifferentiationT (const T &value)
 Creates a new differentiation object for a given scalar value (not a constant). More...
 
 AutomaticDifferentiationT (const T &value, const T &derivative)
 Creates a new differentiation object by a given scalar and it's known derivative of the function at the specified location 'value'. More...
 
 AutomaticDifferentiationT (const T &value, const bool isVariable)
 Creates a new differentiation object by a given scalar or constant value, while a boolean state specifies whether the parameter is a scalar or a constant. More...
 
const T & derivative () const
 Returns the actual derivative of this object. More...
 
const T & value () const
 Returns the value of this object. More...
 
const T & operator() () const
 Returns the actual derivative value of this object. More...
 
AutomaticDifferentiationT< T, TNumeric > operator+ (const T &right) const
 Adds a scalar value to this differentiation object. More...
 
AutomaticDifferentiationT< T, TNumeric > & operator+= (const T &right)
 Adds a scalar value to this differentiation object. More...
 
AutomaticDifferentiationT< T, TNumeric > operator+ (const AutomaticDifferentiationT< T, TNumeric > &right) const
 Adds two differentiation objects and determines the sum derivative. More...
 
AutomaticDifferentiationT< T, TNumeric > & operator+= (const AutomaticDifferentiationT< T, TNumeric > &right)
 Adds two differentiation objects and determines the sum derivative. More...
 
AutomaticDifferentiationT< T, TNumeric > operator- (const T &right) const
 Subtracts a scalar value from this differentiation object. More...
 
AutomaticDifferentiationT< T, TNumeric > & operator-= (const T &right)
 Subtracts a scalar value from this differentiation object. More...
 
AutomaticDifferentiationT< T, TNumeric > operator- (const AutomaticDifferentiationT< T, TNumeric > &right) const
 Subtracts two differentiation objects and determines the resulting derivative. More...
 
AutomaticDifferentiationT< T, TNumeric > operator- () const
 Unary negation operator returns the negative of this differentiation object. More...
 
AutomaticDifferentiationT< T, TNumeric > & operator-= (const AutomaticDifferentiationT< T, TNumeric > &right)
 Subtracts two differentiation objects and determines the resulting derivative. More...
 
AutomaticDifferentiationT< T, TNumeric > operator* (const AutomaticDifferentiationT< T, TNumeric > &right) const
 Multiplies two differentiation objects and determines the product derivative. More...
 
AutomaticDifferentiationT< T, TNumeric > & operator*= (const AutomaticDifferentiationT< T, TNumeric > &right)
 Multiplies two differentiation objects and determines the product derivative. More...
 
AutomaticDifferentiationT< T, TNumeric > operator* (const T &right) const
 Multiplies this differentiation objects with a scalar. More...
 
AutomaticDifferentiationT< T, TNumeric > & operator*= (const T &right)
 Multiplies this differentiation objects with a scalar. More...
 
AutomaticDifferentiationT< T, TNumeric > operator/ (const AutomaticDifferentiationT< T, TNumeric > &right) const
 Divides two differentiation objects and determines the quotient derivative. More...
 
AutomaticDifferentiationT< T, TNumeric > & operator/= (const AutomaticDifferentiationT< T, TNumeric > &right)
 Divides two differentiation objects and determines the quotient derivative. More...
 
AutomaticDifferentiationT< T, TNumeric > operator/ (const T &right) const
 Divides this differentiation object by a scalar value. More...
 
AutomaticDifferentiationT< T, TNumeric > & operator/= (const T &right)
 Divides this differentiation object by a scalar value. More...
 

Static Public Member Functions

static AutomaticDifferentiationT< T, TNumeric > sin (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the sinus function. More...
 
static AutomaticDifferentiationT< T, TNumeric > cos (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the cosine function. More...
 
static AutomaticDifferentiationT< T, TNumeric > tan (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the tangent function. More...
 
static AutomaticDifferentiationT< T, TNumeric > sqrt (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the square root function. More...
 
static AutomaticDifferentiationT< T, TNumeric > sqr (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the square function. More...
 
static AutomaticDifferentiationT< T, TNumeric > exp (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the exponential function. More...
 
static AutomaticDifferentiationT< T, TNumeric > log (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the natural logarithm. More...
 
static AutomaticDifferentiationT< T, TNumeric > log2 (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the logarithm to the base 2. More...
 
static AutomaticDifferentiationT< T, TNumeric > log10 (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the logarithm to the base 10. More...
 
static AutomaticDifferentiationT< T, TNumeric > pow (const AutomaticDifferentiationT< T, TNumeric > &x, const T &y)
 Determines the derivative of the power function calculating x to the power of y. More...
 
static AutomaticDifferentiationT< T, TNumeric > abs (const AutomaticDifferentiationT< T, TNumeric > &value)
 Determines the derivative of the abs function. More...
 
static AutomaticDifferentiationT< T, TNumeric > min (const AutomaticDifferentiationT< T, TNumeric > &value, const T &second)
 Determines the derivative of the min function. More...
 
static AutomaticDifferentiationT< T, TNumeric > max (const AutomaticDifferentiationT< T, TNumeric > &value, const T &second)
 Determines the derivative of the max function. More...
 

Protected Attributes

value_ = T(0)
 The scalar value of this object. More...
 
derivative_ = T(0)
 The actual derivative of this object. More...
 

Friends

template<typename T1 , typename TNumeric1 , typename T2 >
AutomaticDifferentiationT< T1, TNumeric1 > operator+ (const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
 
template<typename T1 , typename TNumeric1 , typename T2 >
AutomaticDifferentiationT< T1, TNumeric1 > operator- (const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
 
template<typename T1 , typename TNumeric1 , typename T2 >
AutomaticDifferentiationT< T1, TNumeric1 > operator* (const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
 
template<typename T1 , typename TNumeric1 , typename T2 >
AutomaticDifferentiationT< T1, TNumeric1 > operator/ (const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
 

Detailed Description

template<typename T, typename TNumeric = NumericT<T>>
class Ocean::AutomaticDifferentiationT< T, TNumeric >

This class implements an automatic differentiation functionality.

The automatic differentiation is realized by a pair of two values using the forward mode: the actual value of a function and the corresponding derivative at this location.
Therefore, each object holds the value of x and x' for a given parameter x.
Automatic differentiation is a nice tool for fast prototyping of e.g., non-linear optimization functions.
The accuracy of the resulting derivative is almost ideal and significantly better compared to numerical differentiation.
However, in general the performance of the automatic differentiation (using the forward method) will be at least two times slower than calculating the derivative directory.
The following code snippet shows a simple example how the AutomaticDifferentiation class can be used.

// the following line determines the derivative f'(x) of f(x) = x^2 for x = 5.5
const Scalar derivative0 = automaticDerivative0.derivative();
// the following line determines the derivative of f'(x) of f(x) = x * sin(x) + 7 for x = 3
const Scalar derivative1 = automaticDerivative1.derivative();
static AutomaticDifferentiationT< T, TNumeric > sin(const AutomaticDifferentiationT< T, TNumeric > &value)
Determines the derivative of the sinus function.
Definition: AutomaticDifferentiation.h:583
AutomaticDifferentiationT< Scalar, Numeric > AutomaticDifferentiation
Definition of a scalar differentiation object using the data type of Scalar as parameter.
Definition: AutomaticDifferentiation.h:18
float Scalar
Definition of a scalar type.
Definition: Math.h:128

In the case a Jacobian matrix needs to be determined the constructor with additional boolean parameter may be used for calculations:

// we determine the 1x2 Jacobian matrix for f(x, y) = x^2 + 3y + 5
// the Jacobian will have the following layout:
// | df/dx df/dy |
Scalar jacobian[2];
Scalar x = 3; // any value for x
Scalar y = 7; // any value for y
for (unsigned int n = 0u; n < 2u; ++n)
{
const AutomaticDifferentiation d = dx + dy + 5;
jacobian[n] = d(); // or d.derivative();
}
Template Parameters
TThe data type of the scalar
TNumericThe numeric class providing access to standard mathematical functions like sin, cos, sqrt, etc.

Constructor & Destructor Documentation

◆ AutomaticDifferentiationT() [1/4]

template<typename T , typename TNumeric = NumericT<T>>
Ocean::AutomaticDifferentiationT< T, TNumeric >::AutomaticDifferentiationT ( )
default

Creates a new differentiation object without initializing the parameters.

◆ AutomaticDifferentiationT() [2/4]

template<typename T , typename TNumeric >
Ocean::AutomaticDifferentiationT< T, TNumeric >::AutomaticDifferentiationT ( const T &  value)
inlineexplicit

Creates a new differentiation object for a given scalar value (not a constant).

The derivative for the provided scalar value will be set to 1.

Parameters
valueThe scalar value defining the object

◆ AutomaticDifferentiationT() [3/4]

template<typename T , typename TNumeric >
Ocean::AutomaticDifferentiationT< T, TNumeric >::AutomaticDifferentiationT ( const T &  value,
const T &  derivative 
)
inline

Creates a new differentiation object by a given scalar and it's known derivative of the function at the specified location 'value'.

Parameters
valueThe scalar value defining the object
derivativeThe derivative of the function at location 'value', e.g., 1 for a scalar parameter, 0 for a constant

◆ AutomaticDifferentiationT() [4/4]

template<typename T , typename TNumeric >
Ocean::AutomaticDifferentiationT< T, TNumeric >::AutomaticDifferentiationT ( const T &  value,
const bool  isVariable 
)
inline

Creates a new differentiation object by a given scalar or constant value, while a boolean state specifies whether the parameter is a scalar or a constant.

Parameters
valueThe scalar or constant value defining the object
isVariableTrue, if the provided value is a scalar (with derivative 1); False, if the provided value is a constant (with derivative 0)

Member Function Documentation

◆ abs()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::abs ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the abs function.

Parameters
valueThe value for which the derivative will be determined
Returns
The resulting derivative value

◆ cos()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::cos ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the cosine function.

Parameters
valueThe value for which the derivative will be determined, in radians
Returns
The resulting derivative value

◆ derivative()

template<typename T , typename TNumeric >
const T & Ocean::AutomaticDifferentiationT< T, TNumeric >::derivative
inline

Returns the actual derivative of this object.

Returns
The object's derivative

◆ exp()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::exp ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the exponential function.

Parameters
valueThe value for which the derivative will be determined
Returns
The resulting derivative value

◆ log()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::log ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the natural logarithm.

Parameters
valueThe value for which the derivative will be determined, with value range (0, infinity)
Returns
The resulting derivative value

◆ log10()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::log10 ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the logarithm to the base 10.

Parameters
valueThe value for which the derivative will be determined, with value range (0, infinity)
Returns
The resulting derivative value

◆ log2()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::log2 ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the logarithm to the base 2.

Parameters
valueThe value for which the derivative will be determined, with value range (0, infinity)
Returns
The resulting derivative value

◆ max()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::max ( const AutomaticDifferentiationT< T, TNumeric > &  value,
const T &  second 
)
inlinestatic

Determines the derivative of the max function.

Parameters
valueThe value for which the derivative will be determined
secondThe second scalar value that will be used for maximum comparison
Returns
The resulting derivative value

◆ min()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::min ( const AutomaticDifferentiationT< T, TNumeric > &  value,
const T &  second 
)
inlinestatic

Determines the derivative of the min function.

Parameters
valueThe value for which the derivative will be determined
secondThe second scalar value that will be used for minimum comparison
Returns
The resulting derivative value

◆ operator()()

template<typename T , typename TNumeric >
const T & Ocean::AutomaticDifferentiationT< T, TNumeric >::operator()
inline

Returns the actual derivative value of this object.

Returns
The object's derivative value

◆ operator*() [1/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::operator* ( const AutomaticDifferentiationT< T, TNumeric > &  right) const
inline

Multiplies two differentiation objects and determines the product derivative.

Parameters
rightThe right differentiation object
Returns
The product derivative

◆ operator*() [2/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::operator* ( const T &  right) const
inline

Multiplies this differentiation objects with a scalar.

Parameters
rightThe right scalar value
Returns
The resulting differentiation object

◆ operator*=() [1/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > & Ocean::AutomaticDifferentiationT< T, TNumeric >::operator*= ( const AutomaticDifferentiationT< T, TNumeric > &  right)
inline

Multiplies two differentiation objects and determines the product derivative.

Parameters
rightThe right differentiation object
Returns
The reference to this object

◆ operator*=() [2/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > & Ocean::AutomaticDifferentiationT< T, TNumeric >::operator*= ( const T &  right)
inline

Multiplies this differentiation objects with a scalar.

Parameters
rightThe right scalar value
Returns
The reference to this object

◆ operator+() [1/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::operator+ ( const AutomaticDifferentiationT< T, TNumeric > &  right) const
inline

Adds two differentiation objects and determines the sum derivative.

Parameters
rightThe right differentiation object
Returns
The sum derivative

◆ operator+() [2/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::operator+ ( const T &  right) const
inline

Adds a scalar value to this differentiation object.

Parameters
rightThe scalar value
Returns
The differentiation object with added scalar

◆ operator+=() [1/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > & Ocean::AutomaticDifferentiationT< T, TNumeric >::operator+= ( const AutomaticDifferentiationT< T, TNumeric > &  right)
inline

Adds two differentiation objects and determines the sum derivative.

Parameters
rightThe right differentiation object
Returns
The reference to this object

◆ operator+=() [2/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > & Ocean::AutomaticDifferentiationT< T, TNumeric >::operator+= ( const T &  right)
inline

Adds a scalar value to this differentiation object.

Parameters
rightThe scalar value
Returns
The reference to this object

◆ operator-() [1/3]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::operator-
inline

Unary negation operator returns the negative of this differentiation object.

Returns
The negative differentiation object

◆ operator-() [2/3]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::operator- ( const AutomaticDifferentiationT< T, TNumeric > &  right) const
inline

Subtracts two differentiation objects and determines the resulting derivative.

Parameters
rightThe right differentiation object
Returns
The resulting derivative

◆ operator-() [3/3]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::operator- ( const T &  right) const
inline

Subtracts a scalar value from this differentiation object.

Parameters
rightThe scalar value
Returns
The differentiation object with subtracted scalar

◆ operator-=() [1/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > & Ocean::AutomaticDifferentiationT< T, TNumeric >::operator-= ( const AutomaticDifferentiationT< T, TNumeric > &  right)
inline

Subtracts two differentiation objects and determines the resulting derivative.

Parameters
rightThe right differentiation object
Returns
The reference to this object

◆ operator-=() [2/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > & Ocean::AutomaticDifferentiationT< T, TNumeric >::operator-= ( const T &  right)
inline

Subtracts a scalar value from this differentiation object.

Parameters
rightThe scalar value
Returns
The reference to this object

◆ operator/() [1/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::operator/ ( const AutomaticDifferentiationT< T, TNumeric > &  right) const
inline

Divides two differentiation objects and determines the quotient derivative.

Parameters
rightThe right differentiation object, while the object's value must not be zero
Returns
The quotient derivative

◆ operator/() [2/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::operator/ ( const T &  right) const
inline

Divides this differentiation object by a scalar value.

Parameters
rightThe right scalar value, must not be zero
Returns
The resulting differentiation object

◆ operator/=() [1/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > & Ocean::AutomaticDifferentiationT< T, TNumeric >::operator/= ( const AutomaticDifferentiationT< T, TNumeric > &  right)
inline

Divides two differentiation objects and determines the quotient derivative.

Parameters
rightThe right differentiation object, while the object's value must not be zero
Returns
The reference to this object

◆ operator/=() [2/2]

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > & Ocean::AutomaticDifferentiationT< T, TNumeric >::operator/= ( const T &  right)
inline

Divides this differentiation object by a scalar value.

Parameters
rightThe right scalar value, must not be zero
Returns
The reference to this object

◆ pow()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::pow ( const AutomaticDifferentiationT< T, TNumeric > &  x,
const T &  y 
)
inlinestatic

Determines the derivative of the power function calculating x to the power of y.

Parameters
xThe value for which the derivative will be determined, with value range [0, infinity)
yThe exponent, with range (-infinity, infinity)
Returns
The resulting derivative value

◆ sin()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::sin ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the sinus function.

Parameters
valueThe value for which the derivative will be determined, in radians
Returns
The resulting derivative value

◆ sqr()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::sqr ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the square function.

Parameters
valueThe value for which the derivative will be determined
Returns
The resulting derivative value

◆ sqrt()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::sqrt ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the square root function.

Parameters
valueThe value for which the derivative will be determined, with value range [0, infinity)
Returns
The resulting derivative value

◆ tan()

template<typename T , typename TNumeric >
AutomaticDifferentiationT< T, TNumeric > Ocean::AutomaticDifferentiationT< T, TNumeric >::tan ( const AutomaticDifferentiationT< T, TNumeric > &  value)
inlinestatic

Determines the derivative of the tangent function.

Parameters
valueThe value for which the derivative will be determined, in radian
Returns
The resulting derivative value

◆ value()

template<typename T , typename TNumeric >
const T & Ocean::AutomaticDifferentiationT< T, TNumeric >::value
inline

Returns the value of this object.

Returns
The object's value

Friends And Related Function Documentation

◆ operator*

template<typename T , typename TNumeric = NumericT<T>>
template<typename T1 , typename TNumeric1 , typename T2 >
AutomaticDifferentiationT<T1, TNumeric1> operator* ( const T2 &  left,
const AutomaticDifferentiationT< T1, TNumeric1 > &  right 
)
friend

◆ operator+

template<typename T , typename TNumeric = NumericT<T>>
template<typename T1 , typename TNumeric1 , typename T2 >
AutomaticDifferentiationT<T1, TNumeric1> operator+ ( const T2 &  left,
const AutomaticDifferentiationT< T1, TNumeric1 > &  right 
)
friend

◆ operator-

template<typename T , typename TNumeric = NumericT<T>>
template<typename T1 , typename TNumeric1 , typename T2 >
AutomaticDifferentiationT<T1, TNumeric1> operator- ( const T2 &  left,
const AutomaticDifferentiationT< T1, TNumeric1 > &  right 
)
friend

◆ operator/

template<typename T , typename TNumeric = NumericT<T>>
template<typename T1 , typename TNumeric1 , typename T2 >
AutomaticDifferentiationT<T1, TNumeric1> operator/ ( const T2 &  left,
const AutomaticDifferentiationT< T1, TNumeric1 > &  right 
)
friend

Field Documentation

◆ derivative_

template<typename T , typename TNumeric = NumericT<T>>
T Ocean::AutomaticDifferentiationT< T, TNumeric >::derivative_ = T(0)
protected

The actual derivative of this object.

◆ value_

template<typename T , typename TNumeric = NumericT<T>>
T Ocean::AutomaticDifferentiationT< T, TNumeric >::value_ = T(0)
protected

The scalar value of this object.


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