Loading...
Searching...
No Matches
CompoundFunction.h
Go to the documentation of this file.
//# CompoundFunction.h: Sum of a collection of Functions
//# Copyright (C) 2001,2002,2005
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: casa-feedback@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
#ifndef SCIMATH_COMPOUNDFUNCTION_H
#define SCIMATH_COMPOUNDFUNCTION_H
//# Includes
#include <casacore/casa/aips.h>
#include <casacore/scimath/Functionals/CompoundParam.h>
#include <casacore/scimath/Functionals/Function.h>
#include <casacore/scimath/Mathematics/AutoDiff.h>
#include <casacore/scimath/Mathematics/AutoDiffMath.h>
namespace casacore { //# NAMESPACE CASACORE - BEGIN
//# Forward declarations
// <summary>
// Sum of a collection of Functions which behaves as one Function object.
// </summary>
// <use visibility=export>
// <reviewed reviewer="tcornwel" date="1996/02/22" tests="tCompoundFunction"
// demos="">
// </reviewed>
// <prerequisite>
// <li> <linkto class="Function">Function</linkto> class
// </prerequisite>
//
// <synopsis>
// This class takes an arbitrary number of Function objects, and generates
// a new, single function object. The parameters of the compound object
// are the union of all the parameters in the input objects.
//
// When CompoundFunction is evaluated, the result is the sum of
// all the individual function values.
//
// Member functions are added with the <src>addFunction()</src> method.
//
// In general the interaction with the function parameters should be through
// the overall function parameters (i.e. through the parameters of the
// <src>CompoundFunction</src>). If for any reason you want to set the
// parameters of an individual function (see e.g. the example in the
// <linkto class=Fit2D>Fit2D</linkto>), call <src>consolidate()</src> before
// abd after the actual setting.
//
// <note role=tip>
// Check <linkto class=CompoundFunction>CombiFunction</linkto> class
// for a simple linear combination of function objects </note>
// </synopsis>
//
// <example>
// Suppose for some reason we wanted the sum of <src>x^2</src> plus a gaussian.
// We could form it as follows:
// <srcblock>
// Polynomial<Float> x2(2);
// x[2] = 1.0; // x^2
// Gaussian1D<Float> gauss(1.0, 0.0, 1.0); // e^{-x^2}
// CompoundParam<Float> sum; // sum == 0.0
// sum.addFunction(x2); // sum == x^2
// sum.addFunction(gauss); // sum == x^2+e^{-x^2}
// sum(2.0); // == 4 + e^-4
// CompoundParam[0] = 2.0; // sum ==2x^2+e^{-x^2}
// sum(2.0); // == 8 + e^-4
// </srcblock>
// </example>
// <templating arg=T>
// <li> T should have standard numerical operators and exp() function. Current
// implementation only tested for real types.
// <li> To obtain derivatives, the derivatives should be defined.
// </templating>
// <thrown>
// <li> AipsError if dimensions of functions added different
// </thrown>
// <motivation>
// This class was created to allow a non-linear least squares fitter to fit a
// (potentially) arbitrary number of functions (typically Gaussians).
// </motivation>
//
// <todo asof="2001/10/22">
// <li> Nothing I know of
// </todo>
template <class T> class CompoundFunction : public CompoundParam<T>
{
public:
//# Constructors
// The default constructor -- no functions, no parameters, nothing, the
// function operator returns a 0.
CompoundFunction() : CompoundParam<T>() {}
// Make this object a (deep) copy of other. If parameters have been set
// without an intervening calculation, a <src>consolidate()</src> could
// be necessary on <em>other</em> first.
// <group>
CompoundFunction(const CompoundFunction<T> &other) :
CompoundParam<T>(other) {}
CompoundFunction(const CompoundFunction<T> &other, Bool) :
CompoundParam<T>(other, True) {}
template <class W>
CompoundFunction(const CompoundFunction<W> &other) :
CompoundParam<T>(other) {}
template <class W>
CompoundFunction(const CompoundFunction<W> &other, Bool) :
CompoundParam<T>(other, True) {}
// </group>
// Make this object a (deep) copy of other.
CompoundFunction<T> &operator=(const CompoundFunction<T> &other) {
other.fromParam_p();
CompoundParam<T>::operator=(other); return *this; }
// Destructor
virtual ~CompoundFunction() {}
//# Operators
// Evaluate the function at <src>x</src>.
virtual T eval(typename Function<T>::FunctionArg x) const;
//# Member functions
// Consolidate the parameter settings. This could be necessary if
// parameters have been set, and a copy constructor called. This is
// necessary before and after the setting of <em>local</em> parameters; i.e.
// the parameters of the individual functions.
CompoundFunction<T> &consolidate() { fromParam_p();
toParam_p(); return *this; }
// Return a copy of this object from the heap. The caller is responsible for
// deleting the pointer.
// <group>
virtual Function<T> *clone() const { fromParam_p();
return new CompoundFunction<T>(*this); }
virtual Function<typename FunctionTraits<T>::DiffType> *cloneAD() const {
return new CompoundFunction<typename FunctionTraits<T>::DiffType>(*this); }
virtual Function<typename FunctionTraits<T>::BaseType> *cloneNonAD() const {
return new CompoundFunction<typename FunctionTraits<T>::BaseType>
(*this, True); }
// </group>
private:
//# Member functions
// Copy the local parameters from general block
void fromParam_p() const;
// Make the general block from local parameters
void toParam_p();
//# Make members of parent classes known.
protected:
using CompoundParam<T>::parset_p;
using CompoundParam<T>::param_p;
using CompoundParam<T>::funpar_p;
using CompoundParam<T>::locpar_p;
using CompoundParam<T>::paroff_p;
using CompoundParam<T>::functionPtr_p;
public:
using CompoundParam<T>::nparameters;
using CompoundParam<T>::nFunctions;
using CompoundParam<T>::function;
};
#define CompoundFunction_PS CompoundFunction
// <summary> Partial <src>AutoDiff</src> specialization of CompoundFunction
// </summary>
// <synopsis>
// <note role=warning> The name <src>CompoundFunction_PS</src> is only
// for cxx2html documentation problems. Use
// <src>CompoundFunction</src> in your code.</note>
// </synopsis>
template <class T> class CompoundFunction_PS<AutoDiff<T> > :
public CompoundParam<AutoDiff<T> >
{
public:
//# Constructors
// The default constructor -- no functions, no parameters, nothing, the
// function operator returns a 0.
CompoundFunction_PS() : CompoundParam<AutoDiff<T> >() {}
// Make this object a (deep) copy of other. If parameters have been set
// without an intervening calculation, a <src>consolidate()</src> could
// be necessary on <em>other</em> first.
// <group>
CompoundFunction_PS(const CompoundFunction_PS<AutoDiff<T> > &other) :
CompoundParam<AutoDiff<T> >(other) {}
template <class W>
CompoundFunction_PS(const CompoundFunction_PS<W> &other) :
CompoundParam<AutoDiff<T> >(other) {}
// </group>
// Make this object a (deep) copy of other.
CompoundFunction_PS<AutoDiff<T> > &
operator=(const CompoundFunction_PS<AutoDiff<T> > &other) {
fromParam_p();
CompoundParam<AutoDiff<T> >::operator=(other); return *this; }
// Destructor
virtual ~CompoundFunction_PS() {}
//# Operators
// Evaluate the function and its derivatives at <src>x</src> <em>wrt</em>
// to the coefficients.
virtual AutoDiff<T>
eval(typename Function<AutoDiff<T> >::FunctionArg x) const;
//# Member functions
// Add a function to the sum. All functions must have the same
// <src>ndim()</src> as the first one. Returns the (zero relative) number
// of the function just added.
uInt addFunction(const Function<AutoDiff<T> > &newFunction);
// Consolidate the parameter settings. This could be necessary if
// parameters have been set, and a copy constructor called. This is
// necessary before and after the setting of <em>local</em> parameters; i.e.
// the parameters of the individual functions.
CompoundFunction_PS<AutoDiff<T> > &consolidate() { fromParam_p();
toParam_p(); return *this; }
// Return a copy of this object from the heap. The caller is responsible for
// deleting the pointer.
// <group>
virtual Function<AutoDiff<T> > *clone() const { fromParam_p();
return new CompoundFunction<AutoDiff<T> >(*this); }
virtual Function<typename FunctionTraits<AutoDiff<T> >::DiffType>
*cloneAD() const {
return new CompoundFunction<typename FunctionTraits<AutoDiff<T> >::DiffType>
(*this); }
virtual Function<typename FunctionTraits<AutoDiff<T> >::BaseType>
*cloneNonAD() const {
return new CompoundFunction<typename FunctionTraits<AutoDiff<T> >::BaseType>
(*this, True); }
// </group>
private:
//# Member functions
// Copy the local parameters to/from general block
void fromParam_p() const;
// Make the general block from local parameters
void toParam_p();
//# Make members of parent classes known.
protected:
using CompoundParam<AutoDiff<T> >::parset_p;
using CompoundParam<AutoDiff<T> >::param_p;
using CompoundParam<AutoDiff<T> >::funpar_p;
using CompoundParam<AutoDiff<T> >::locpar_p;
using CompoundParam<AutoDiff<T> >::paroff_p;
using CompoundParam<AutoDiff<T> >::functionPtr_p;
public:
using CompoundParam<AutoDiff<T> >::nparameters;
using CompoundParam<AutoDiff<T> >::nFunctions;
using CompoundParam<AutoDiff<T> >::function;
};
#undef CompoundFunction_PS
} //# NAMESPACE CASACORE - END
#ifndef CASACORE_NO_AUTO_TEMPLATES
#include <casacore/scimath/Functionals/CompoundFunction.tcc>
#include <casacore/scimath/Functionals/Compound2Function.tcc>
#endif //# CASACORE_NO_AUTO_TEMPLATES
#endif
CompoundFunction_PS
#define CompoundFunction_PS
Definition CompoundFunction.h:185
casacore::AutoDiff
Definition AutoDiff.h:254
casacore::CompoundFunction_PS< AutoDiff< T > >::cloneNonAD
virtual Function< typename FunctionTraits< AutoDiff< T > >::BaseType > * cloneNonAD() const
Definition CompoundFunction.h:249
casacore::CompoundFunction_PS< AutoDiff< T > >::toParam_p
void toParam_p()
Make the general block from local parameters.
casacore::CompoundFunction_PS< AutoDiff< T > >::cloneAD
virtual Function< typename FunctionTraits< AutoDiff< T > >::DiffType > * cloneAD() const
Definition CompoundFunction.h:245
casacore::CompoundFunction_PS< AutoDiff< T > >::CompoundFunction_PS
CompoundFunction_PS()
The default constructor – no functions, no parameters, nothing, the function operator returns a 0.
Definition CompoundFunction.h:202
casacore::CompoundFunction_PS< AutoDiff< T > >::CompoundFunction_PS
CompoundFunction_PS(const CompoundFunction_PS< W > &other)
Definition CompoundFunction.h:210
casacore::CompoundFunction_PS< AutoDiff< T > >::clone
virtual Function< AutoDiff< T > > * clone() const
Return a copy of this object from the heap.
Definition CompoundFunction.h:242
casacore::CompoundFunction_PS< AutoDiff< T > >::CompoundFunction_PS
CompoundFunction_PS(const CompoundFunction_PS< AutoDiff< T > > &other)
Make this object a (deep) copy of other.
Definition CompoundFunction.h:207
casacore::CompoundFunction_PS< AutoDiff< T > >::operator=
CompoundFunction_PS< AutoDiff< T > > & operator=(const CompoundFunction_PS< AutoDiff< T > > &other)
Make this object a (deep) copy of other.
Definition CompoundFunction.h:215
casacore::CompoundFunction_PS< AutoDiff< T > >::fromParam_p
void fromParam_p() const
Copy the local parameters to/from general block.
casacore::CompoundFunction_PS< AutoDiff< T > >::eval
virtual AutoDiff< T > eval(typename Function< AutoDiff< T > >::FunctionArg x) const
Evaluate the function and its derivatives at x wrt to the coefficients.
casacore::CompoundFunction_PS< AutoDiff< T > >::consolidate
CompoundFunction_PS< AutoDiff< T > > & consolidate()
Consolidate the parameter settings.
Definition CompoundFunction.h:237
casacore::CompoundFunction_PS< AutoDiff< T > >::~CompoundFunction_PS
virtual ~CompoundFunction_PS()
Destructor.
Definition CompoundFunction.h:220
casacore::CompoundFunction_PS< AutoDiff< T > >::addFunction
uInt addFunction(const Function< AutoDiff< T > > &newFunction)
Add a function to the sum.
casacore::CompoundFunction
Definition CompoundFunction.h:112
casacore::CompoundFunction::CompoundFunction
CompoundFunction(const CompoundFunction< T > &other)
Make this object a (deep) copy of other.
Definition CompoundFunction.h:122
casacore::CompoundFunction::operator=
CompoundFunction< T > & operator=(const CompoundFunction< T > &other)
Make this object a (deep) copy of other.
Definition CompoundFunction.h:134
casacore::CompoundFunction::~CompoundFunction
virtual ~CompoundFunction()
Destructor.
Definition CompoundFunction.h:139
casacore::CompoundFunction::clone
virtual Function< T > * clone() const
Return a copy of this object from the heap.
Definition CompoundFunction.h:155
casacore::CompoundFunction::cloneNonAD
virtual Function< typename FunctionTraits< T >::BaseType > * cloneNonAD() const
Definition CompoundFunction.h:159
casacore::CompoundFunction::eval
virtual T eval(typename Function< T >::FunctionArg x) const
Evaluate the function at x.
casacore::CompoundFunction::CompoundFunction
CompoundFunction()
The default constructor – no functions, no parameters, nothing, the function operator returns a 0.
Definition CompoundFunction.h:117
casacore::CompoundFunction::consolidate
CompoundFunction< T > & consolidate()
Consolidate the parameter settings.
Definition CompoundFunction.h:150
casacore::CompoundFunction::fromParam_p
void fromParam_p() const
Copy the local parameters from general block.
casacore::CompoundFunction::CompoundFunction
CompoundFunction(const CompoundFunction< T > &other, Bool)
Definition CompoundFunction.h:124
casacore::CompoundFunction::cloneAD
virtual Function< typename FunctionTraits< T >::DiffType > * cloneAD() const
Definition CompoundFunction.h:157
casacore::CompoundFunction::toParam_p
void toParam_p()
Make the general block from local parameters.
casacore::CompoundFunction::CompoundFunction
CompoundFunction(const CompoundFunction< W > &other)
Definition CompoundFunction.h:127
casacore::CompoundFunction::CompoundFunction
CompoundFunction(const CompoundFunction< W > &other, Bool)
Definition CompoundFunction.h:130
casacore::CompoundParam::funpar_p
Block< uInt > funpar_p
Index of function belonging to parameter.
Definition CompoundParam.h:209
casacore::CompoundParam::paroff_p
Block< uInt > paroff_p
Index of offset for each function to its parameters in general list.
Definition CompoundParam.h:207
casacore::CompoundParam::operator=
CompoundParam< T > & operator=(const CompoundParam< T > &other)
casacore::CompoundParam::function
const Function< T > & function(uInt which) const
Return a reference to a specific Function.
Definition CompoundParam.h:177
casacore::CompoundParam::functionPtr_p
PtrBlock< Function< T > * > functionPtr_p
Pointer to each added function.
Definition CompoundParam.h:205
casacore::CompoundParam::locpar_p
Block< uInt > locpar_p
Index of local parameter.
Definition CompoundParam.h:211
casacore::CompoundParam::nFunctions
uInt nFunctions() const
Return the number of functions in the sum.
Definition CompoundParam.h:173
casacore::CompoundParam::CompoundParam
CompoundParam()
The default constructor – no functions, no parameters, nothing, the function operator returns a 0.
casacore::Function
Definition Function.h:200
casacore::Function::param_p
FunctionParam< T > param_p
The parameters and masks.
Definition Function.h:330
casacore::Function::parset_p
Bool parset_p
Indicate parameter written.
Definition Function.h:334
casacore::Function::nparameters
uInt nparameters() const
Returns the number of parameters.
Definition Function.h:228
casacore::Function::FunctionArg
const ArgType * FunctionArg
Definition Function.h:205
casacore
this file contains all the compiler specific defines
Definition mainpage.dox:28
casacore::uInt
unsigned int uInt
Definition aipstype.h:49
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
casacore::True
const Bool True
Definition aipstype.h:41