casacore
|
#include <SparseDiff.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 | |
SparseDiff () | |
Construct a constant with a value of zero. More... | |
SparseDiff (const T &v) | |
Construct a constant with a value of v. More... | |
SparseDiff (const T &v, const uInt n) | |
A function f(x0,x1,...,xn,...) with a value of v. More... | |
SparseDiff (const T &v, const uInt n, const T &der) | |
A function f(x0,x1,...,xn,...) with a value of v. More... | |
SparseDiff (const AutoDiff< T > &other) | |
Construct from an AutoDiff. More... | |
SparseDiff (const SparseDiff< T > &other) | |
Construct one from another (deep copy) More... | |
~SparseDiff () | |
Destructor. More... | |
SparseDiff< T > & | operator= (const T &v) |
Assignment operator. More... | |
SparseDiff< T > & | operator= (const pair< uInt, T > &der) |
Assignment operator. More... | |
SparseDiff< T > & | operator= (const vector< pair< uInt, T > > &der) |
Assignment operator. More... | |
SparseDiff< T > & | operator= (const AutoDiff< T > &other) |
Assign from an Autodiff. More... | |
SparseDiff< T > & | operator= (const SparseDiff< T > &other) |
Assign one to another (deep copy) More... | |
void | operator*= (const SparseDiff< T > &other) |
Assignment operators. More... | |
void | operator/= (const SparseDiff< T > &other) |
void | operator+= (const SparseDiff< T > &other) |
void | operator-= (const SparseDiff< T > &other) |
void | operator*= (const T other) |
void | operator/= (const T other) |
void | operator+= (const T other) |
void | operator-= (const T other) |
AutoDiff< T > | toAutoDiff (uInt n) const |
Convert to an AutoDiff of length n More... | |
SparseDiffRep< T > * | theRep () |
Returns the pointer to the structure of value and derivatives. More... | |
const SparseDiffRep< T > * | theRep () const |
T & | value () |
Returns the value of the function. More... | |
const T & | value () const |
vector< pair< uInt, T > > & | derivatives () const |
Returns a vector of the derivatives of a SparseDiff. More... | |
void | derivatives (vector< pair< uInt, T > > &res) const |
const vector< pair< uInt, T > > & | grad () const |
vector< pair< uInt, T > > & | grad () |
pair< uInt, T > & | derivative (uInt which) |
Returns a specific derivative. More... | |
const pair< uInt, T > & | derivative (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... | |
void | sort () |
Sort derivative list; cater for doubles and zeroes. More... | |
Static Public Member Functions | |
static Bool | ltSort (pair< uInt, T > &lhs, pair< uInt, T > &rhs) |
Sort criterium. More... | |
Private Attributes | |
SparseDiffRep< T > * | rep_p |
Value representation. More... | |
Class that computes partial derivatives by automatic differentiation.
Public interface
Class that computes partial derivatives for some parameters by automatic differentiation, thus SparseDiff.
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 some of its independent parameters. When a mathematical operation is applied to a SparseDiff object, the derivative values of the resulting new object are computed according to the chain rules of differentiation. SparseDiff operates like the AutoDiff class, but only determines the derivatives with respect to the actual dependent variables.
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:
Note that in practice constants may be given as Double constants. 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<SparseDiff<> >
implementation.
The SparseDiff
class is the class the user communicates with. Alias classes (SparseDiffA and SparseDiffX) exist to make it possible to have different incarnations of a templated method (e.g. a generic one and a specialized one). See the dSparseDiff
demo for an example of its use.
All operators and functions are declared in (see (file=SparseDiffMath.h)) SparseDiffMath. The output operator in (see (file=SparseDiffIO.h))SparseDiffIO. The actual structure of the data block used by SparseDiff
is described in SparseDiffRep.
A SparseDiff can be constructed from an AutoDiff. toAutoDiff(n) can convert it to an AutoDiff.
See for an extensive example the demo program dSparseDiff. It is based on the example given above, and shows also the use of second derivatives (which is just using SparseDiff<SparseDiff<Double> >
as template argument).
The creation of the class was motivated by least-squares non-linear fits in cases where each individual condition equation depends only on a fraction of the fixed parameters (e.g. self-calibration where only pairs of antennas are present per equation), and hence only a few 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 279 of file SparseDiff.h.
typedef const value_type* casacore::SparseDiff< T >::const_iterator |
Definition at line 286 of file SparseDiff.h.
typedef const value_type& casacore::SparseDiff< T >::const_reference |
Definition at line 284 of file SparseDiff.h.
typedef value_type* casacore::SparseDiff< T >::iterator |
Definition at line 285 of file SparseDiff.h.
typedef value_type& casacore::SparseDiff< T >::reference |
Definition at line 283 of file SparseDiff.h.
typedef T casacore::SparseDiff< T >::value_type |
Definition at line 282 of file SparseDiff.h.
casacore::SparseDiff< T >::SparseDiff | ( | ) |
Construct a constant with a value of zero.
Zero derivatives.
casacore::SparseDiff< T >::SparseDiff | ( | const T & | v | ) |
Construct a constant with a value of v.
Zero derivatives.
casacore::SparseDiff< T >::SparseDiff | ( | const T & | v, |
const uInt | n | ||
) |
A function f(x0,x1,...,xn,...) with a value of v.
The nth derivative is one, and all other derivatives are zero.
casacore::SparseDiff< T >::SparseDiff | ( | const T & | v, |
const uInt | n, | ||
const T & | der | ||
) |
A function f(x0,x1,...,xn,...) with a value of v.
The nth derivative is der, and all other derivatives are zero.
casacore::SparseDiff< T >::SparseDiff | ( | const AutoDiff< T > & | other | ) |
Construct from an AutoDiff.
casacore::SparseDiff< T >::SparseDiff | ( | const SparseDiff< T > & | other | ) |
Construct one from another (deep copy)
casacore::SparseDiff< T >::~SparseDiff | ( | ) |
Destructor.
|
inline |
Returns a specific derivative.
No check for a valid which.
Definition at line 366 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
|
inline |
Definition at line 367 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
vector<pair<uInt, T> >& casacore::SparseDiff< T >::derivatives | ( | ) | const |
Returns a vector of the derivatives of a SparseDiff.
void casacore::SparseDiff< T >::derivatives | ( | vector< pair< uInt, T > > & | res | ) | const |
|
inline |
Definition at line 361 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
|
inline |
Definition at line 360 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
|
inline |
Is it a constant, i.e., with zero derivatives?
Definition at line 375 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
|
static |
Sort criterium.
|
inline |
Return total number of derivatives.
Definition at line 372 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
void casacore::SparseDiff< T >::operator*= | ( | const SparseDiff< T > & | other | ) |
Assignment operators.
|
inline |
Definition at line 333 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p, and casacore::SparseDiff< T >::value().
void casacore::SparseDiff< T >::operator+= | ( | const SparseDiff< T > & | other | ) |
|
inline |
Definition at line 337 of file SparseDiff.h.
References casacore::SparseDiff< T >::value().
void casacore::SparseDiff< T >::operator-= | ( | const SparseDiff< T > & | other | ) |
|
inline |
Definition at line 338 of file SparseDiff.h.
References casacore::SparseDiff< T >::value().
void casacore::SparseDiff< T >::operator/= | ( | const SparseDiff< T > & | other | ) |
|
inline |
Definition at line 335 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p, and casacore::SparseDiff< T >::value().
SparseDiff<T>& casacore::SparseDiff< T >::operator= | ( | const AutoDiff< T > & | other | ) |
Assign from an Autodiff.
SparseDiff<T>& casacore::SparseDiff< T >::operator= | ( | const pair< uInt, T > & | der | ) |
Assignment operator.
Add a gradient to variable.
SparseDiff<T>& casacore::SparseDiff< T >::operator= | ( | const SparseDiff< T > & | other | ) |
Assign one to another (deep copy)
SparseDiff<T>& casacore::SparseDiff< T >::operator= | ( | const T & | v | ) |
Assignment operator.
Assign a constant to variable.
Referenced by casacore::SparseDiffA< T >::operator=(), and casacore::SparseDiffX< T >::operator=().
SparseDiff<T>& casacore::SparseDiff< T >::operator= | ( | const vector< pair< uInt, T > > & | der | ) |
Assignment operator.
Assign gradients to variable.
void casacore::SparseDiff< T >::sort | ( | ) |
Sort derivative list; cater for doubles and zeroes.
|
inline |
Returns the pointer to the structure of value and derivatives.
Definition at line 346 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
|
inline |
Definition at line 347 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
AutoDiff<T> casacore::SparseDiff< T >::toAutoDiff | ( | uInt | n | ) | const |
Convert to an AutoDiff of length n
|
inline |
Returns the value of the function.
Definition at line 352 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
Referenced by casacore::SparseDiff< T >::operator*=(), casacore::SparseDiff< T >::operator+=(), casacore::SparseDiff< T >::operator-=(), and casacore::SparseDiff< T >::operator/=().
|
inline |
Definition at line 353 of file SparseDiff.h.
References casacore::SparseDiff< T >::rep_p.
|
private |
Value representation.
Definition at line 386 of file SparseDiff.h.
Referenced by casacore::SparseDiff< T >::derivative(), casacore::SparseDiff< T >::grad(), casacore::SparseDiff< T >::isConstant(), casacore::SparseDiff< T >::nDerivatives(), casacore::SparseDiff< T >::operator*=(), casacore::SparseDiff< T >::operator/=(), casacore::SparseDiff< T >::theRep(), and casacore::SparseDiff< T >::value().