casacore
|
#include <AutoDiff.h>
Public Types | |
typedef T | value_type |
typedef value_type & | reference |
typedef const value_type & | const_reference |
typedef value_type * | iterator |
typedef const value_type * | const_iterator |
Public Member Functions | |
AutoDiff () | |
Construct a constant with a value of zero. More... | |
AutoDiff (const T &v) | |
Construct a constant with a value of v. More... | |
AutoDiff (const T &v, const uInt ndiffs, const uInt n) | |
A function f(x0,x1,...,xn,...) with a value of v. More... | |
AutoDiff (const T &v, const uInt ndiffs) | |
A function f(x0,x1,...,xn,...) with a value of v. More... | |
AutoDiff (const AutoDiff< T > &other) | |
Construct one from another. More... | |
AutoDiff (const T &v, const Vector< T > &derivs) | |
Construct a function f(x0,x1,...,xn) of a value v and a vector of derivatives derivs(0) = df/dx0, derivs(1) = df/dx1,... More... | |
~AutoDiff () | |
AutoDiff< T > & | operator= (const T &v) |
Assignment operator. More... | |
AutoDiff< T > & | operator= (const AutoDiff< T > &other) |
Assign one to another. More... | |
void | operator*= (const AutoDiff< T > &other) |
In-place mathematical operators. More... | |
void | operator/= (const AutoDiff< T > &other) |
void | operator+= (const AutoDiff< T > &other) |
void | operator-= (const AutoDiff< T > &other) |
void | operator*= (const T other) |
void | operator/= (const T other) |
void | operator+= (const T other) |
void | operator-= (const T other) |
T & | value () |
Returns the value of the function. More... | |
const T & | value () const |
const Vector< T > & | derivatives () const |
Returns a vector of the derivatives of an AutoDiff. More... | |
Vector< T > & | derivatives () |
void | derivatives (Vector< T > &res) const |
T & | derivative (uInt which) |
Returns a specific derivative. More... | |
const T & | derivative (uInt which) const |
T & | deriv (uInt which) |
const T & | deriv (uInt which) const |
uInt | nDerivatives () const |
Return total number of derivatives. More... | |
Bool | isConstant () const |
Is it a constant, i.e., with zero derivatives? More... | |
Private Attributes | |
T | val_p |
The function value. More... | |
uInt | nd_p |
The number of derivatives. More... | |
Vector< T > | grad_p |
The derivatives. More... | |
Class that computes partial derivatives by automatic differentiation.
Public interface
Class that computes partial derivatives by automatic differentiation, thus AutoDiff.
Class that computes partial derivatives by automatic differentiation. It does this by storing the value of a function and the values of its first derivatives with respect to its independent parameters. When a mathematical operation is applied to an AutoDiff object, the derivative values of the resulting new object are computed according to chain rules of differentiation.
Suppose we have a function f(x0,x1,...,xn) and its differential is
We can build a class that has the value of the function, f(x0,x1,...,xn), and the values of the derivatives, (df/dx0), (df/dx1), ..., (df/dxn) at (x0,x1,...,xn), as class members.
Now if we have another function, g(x0,x1,...,xn) and its differential is dg = (dg/dx0)*dx0 + (dg/dx1)*dx1 +... + (dg/dxn)*dxn, since
we can calculate
based on our information on
All we need to do is to define the operators and derivatives of common mathematical functions.
To be able to use the class as an automatic differentiator of a function object, we need a templated function object, i.e. an object with:
template <class T> T operator()(const T)
template <class T> T operator()(const Vector<T> &)
A simple example of such a function object could be:
A call with values will produce the function value:
In actual practice, there are a few rules to obey for the structure of the function object if you want to use the function object and its derivatives in least squares fitting procedures in the Fitting module. The major one is to view the function object having 'fixed' and 'variable' parameters. I.e., rather than viewing the function as depending on parameters a, b, x (f(a,b,x)
), the function is considered to be f(x; a,b)
, where a, b are 'fixed' parameters, and x a variable parameter. Fixed parameters should be contained in a FunctionParam container object; while the variable parameter(s) are given in the function operator()
. See Function class for details.
A Gaussian spectral profile would in general have the center frequency, the width and the amplitude as fixed parameters, and the frequency as a variable. Given a spectrum, you would solve for the fixed parameters, given spectrum values. However, in other cases the role of the parameters could be reversed. An example could be a whole stack of observed (in the laboratory) spectra at different temperatures at one frequency. In that case the width would be the variable parameter, and the frequency one of the fixed (and to be solved for)parameters.
Since the calculation of the derivatives is done with simple overloading, the calculation of second (and higher) derivatives is easy. It should be noted that higher deivatives are inefficient in the current incarnation (there is no knowledge e.g. about symmetry in the Jacobian). However, it is a very good way to get the correct answers of the derivatives. In practice actual production code will be better off with specialization of the f<AutoDiff<> >
implementation.
The AutoDiff
class is the class the user communicates with. Alias classes (AutoDiffA and AutoDiffX) exists to make it possible to have different incarnations of a templated method (e.g. a generic one and a specialized one). See the dAutoDiff
demo for an example of its use.
All operators and functions are declared in (see (file=AutoDiffMath.h)) AutoDiffMath. The output operator in (see (file=AutoDiffIO.h))AutoDiffIO.
See for an extensive example the demo program dAutoDiff. It is based on the example given above, and shows also the use of second derivatives (which is just using AutoDiff<AutoDiff<Double> >
as template argument).
The creation of the class was motivated by least-squares non-linear fit where partial derivatives of a fitted function are needed. It would be tedious to create functionals for all partial derivatives of a function.
Definition at line 257 of file AutoDiff.h.
typedef const value_type* casacore::AutoDiff< T >::const_iterator |
Definition at line 264 of file AutoDiff.h.
typedef const value_type& casacore::AutoDiff< T >::const_reference |
Definition at line 262 of file AutoDiff.h.
typedef value_type* casacore::AutoDiff< T >::iterator |
Definition at line 263 of file AutoDiff.h.
typedef value_type& casacore::AutoDiff< T >::reference |
Definition at line 261 of file AutoDiff.h.
typedef T casacore::AutoDiff< T >::value_type |
Definition at line 260 of file AutoDiff.h.
casacore::AutoDiff< T >::AutoDiff | ( | ) |
Construct a constant with a value of zero.
Zero derivatives.
casacore::AutoDiff< T >::AutoDiff | ( | const T & | v | ) |
Construct a constant with a value of v.
Zero derivatives.
casacore::AutoDiff< T >::AutoDiff | ( | const T & | v, |
const uInt | ndiffs, | ||
const uInt | n | ||
) |
A function f(x0,x1,...,xn,...) with a value of v.
The total number of derivatives is ndiffs, the nth derivative is one, and all others are zero.
casacore::AutoDiff< T >::AutoDiff | ( | const T & | v, |
const uInt | ndiffs | ||
) |
A function f(x0,x1,...,xn,...) with a value of v.
The total number of derivatives is ndiffs. All derivatives are zero.
casacore::AutoDiff< T >::AutoDiff | ( | const AutoDiff< T > & | other | ) |
Construct one from another.
casacore::AutoDiff< T >::AutoDiff | ( | const T & | v, |
const Vector< T > & | derivs | ||
) |
Construct a function f(x0,x1,...,xn) of a value v and a vector of derivatives derivs(0) = df/dx0, derivs(1) = df/dx1,...
casacore::AutoDiff< T >::~AutoDiff | ( | ) |
|
inline |
Definition at line 329 of file AutoDiff.h.
References casacore::AutoDiff< T >::grad_p.
|
inline |
Definition at line 330 of file AutoDiff.h.
References casacore::AutoDiff< T >::grad_p.
|
inline |
Returns a specific derivative.
The second set does not check for a valid which; the first set does through Vector addressing.
Definition at line 327 of file AutoDiff.h.
References casacore::AutoDiff< T >::grad_p.
|
inline |
Definition at line 328 of file AutoDiff.h.
References casacore::AutoDiff< T >::grad_p.
|
inline |
Definition at line 320 of file AutoDiff.h.
References casacore::AutoDiff< T >::grad_p.
|
inline |
Returns a vector of the derivatives of an AutoDiff.
Definition at line 319 of file AutoDiff.h.
References casacore::AutoDiff< T >::grad_p.
void casacore::AutoDiff< T >::derivatives | ( | Vector< T > & | res | ) | const |
|
inline |
Is it a constant, i.e., with zero derivatives?
Definition at line 337 of file AutoDiff.h.
References casacore::AutoDiff< T >::nd_p.
|
inline |
Return total number of derivatives.
Definition at line 334 of file AutoDiff.h.
References casacore::AutoDiff< T >::nd_p.
void casacore::AutoDiff< T >::operator*= | ( | const AutoDiff< T > & | other | ) |
In-place mathematical operators.
void casacore::AutoDiff< T >::operator*= | ( | const T | other | ) |
void casacore::AutoDiff< T >::operator+= | ( | const AutoDiff< T > & | other | ) |
void casacore::AutoDiff< T >::operator+= | ( | const T | other | ) |
void casacore::AutoDiff< T >::operator-= | ( | const AutoDiff< T > & | other | ) |
void casacore::AutoDiff< T >::operator-= | ( | const T | other | ) |
void casacore::AutoDiff< T >::operator/= | ( | const AutoDiff< T > & | other | ) |
void casacore::AutoDiff< T >::operator/= | ( | const T | other | ) |
AutoDiff<T>& casacore::AutoDiff< T >::operator= | ( | const AutoDiff< T > & | other | ) |
Assign one to another.
AutoDiff<T>& casacore::AutoDiff< T >::operator= | ( | const T & | v | ) |
Assignment operator.
Assign a constant to variable. All derivatives are zero.
Referenced by casacore::AutoDiffA< T >::operator=(), and casacore::AutoDiffX< T >::operator=().
|
inline |
Returns the value of the function.
Definition at line 313 of file AutoDiff.h.
References casacore::AutoDiff< T >::val_p.
Referenced by casacore::FunctionTraits_P< AutoDiff< T > >::getValue(), casacore::FunctionTraits_PA< AutoDiffA< T > >::getValue(), and casacore::FunctionTraits_PX< AutoDiffX< T > >::getValue().
|
inline |
Definition at line 314 of file AutoDiff.h.
References casacore::AutoDiff< T >::val_p.
|
private |
The derivatives.
Definition at line 346 of file AutoDiff.h.
Referenced by casacore::AutoDiff< T >::deriv(), casacore::AutoDiff< T >::derivative(), and casacore::AutoDiff< T >::derivatives().
|
private |
The number of derivatives.
Definition at line 344 of file AutoDiff.h.
Referenced by casacore::AutoDiff< T >::isConstant(), and casacore::AutoDiff< T >::nDerivatives().
|
private |
The function value.
Definition at line 342 of file AutoDiff.h.
Referenced by casacore::AutoDiff< T >::value().