casacore
Loading...
Searching...
No Matches
CompoundFunction.h
Go to the documentation of this file.
1//# CompoundFunction.h: Sum of a collection of Functions
2//# Copyright (C) 2001,2002,2005
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef SCIMATH_COMPOUNDFUNCTION_H
27#define SCIMATH_COMPOUNDFUNCTION_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/scimath/Functionals/CompoundParam.h>
32#include <casacore/scimath/Functionals/Function.h>
33#include <casacore/scimath/Mathematics/AutoDiff.h>
34#include <casacore/scimath/Mathematics/AutoDiffMath.h>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38//# Forward declarations
39
40// <summary>
41// Sum of a collection of Functions which behaves as one Function object.
42// </summary>
43
44// <use visibility=export>
45
46// <reviewed reviewer="tcornwel" date="1996/02/22" tests="tCompoundFunction"
47// demos="">
48// </reviewed>
49
50// <prerequisite>
51// <li> <linkto class="Function">Function</linkto> class
52// </prerequisite>
53//
54// <synopsis>
55// This class takes an arbitrary number of Function objects, and generates
56// a new, single function object. The parameters of the compound object
57// are the union of all the parameters in the input objects.
58//
59// When CompoundFunction is evaluated, the result is the sum of
60// all the individual function values.
61//
62// Member functions are added with the <src>addFunction()</src> method.
63//
64// In general the interaction with the function parameters should be through
65// the overall function parameters (i.e. through the parameters of the
66// <src>CompoundFunction</src>). If for any reason you want to set the
67// parameters of an individual function (see e.g. the example in the
68// <linkto class=Fit2D>Fit2D</linkto>), call <src>consolidate()</src> before
69// abd after the actual setting.
70//
71// <note role=tip>
72// Check <linkto class=CompoundFunction>CombiFunction</linkto> class
73// for a simple linear combination of function objects </note>
74// </synopsis>
75//
76// <example>
77// Suppose for some reason we wanted the sum of <src>x^2</src> plus a gaussian.
78// We could form it as follows:
79// <srcblock>
80// Polynomial<Float> x2(2);
81// x[2] = 1.0; // x^2
82// Gaussian1D<Float> gauss(1.0, 0.0, 1.0); // e^{-x^2}
83// CompoundParam<Float> sum; // sum == 0.0
84// sum.addFunction(x2); // sum == x^2
85// sum.addFunction(gauss); // sum == x^2+e^{-x^2}
86// sum(2.0); // == 4 + e^-4
87// CompoundParam[0] = 2.0; // sum ==2x^2+e^{-x^2}
88// sum(2.0); // == 8 + e^-4
89// </srcblock>
90// </example>
91
92// <templating arg=T>
93// <li> T should have standard numerical operators and exp() function. Current
94// implementation only tested for real types.
95// <li> To obtain derivatives, the derivatives should be defined.
96// </templating>
97
98// <thrown>
99// <li> AipsError if dimensions of functions added different
100// </thrown>
101
102// <motivation>
103// This class was created to allow a non-linear least squares fitter to fit a
104// (potentially) arbitrary number of functions (typically Gaussians).
105// </motivation>
106//
107// <todo asof="2001/10/22">
108// <li> Nothing I know of
109// </todo>
110
111template <class T> class CompoundFunction : public CompoundParam<T>
112{
113public:
114 //# Constructors
115 // The default constructor -- no functions, no parameters, nothing, the
116 // function operator returns a 0.
118 // Make this object a (deep) copy of other. If parameters have been set
119 // without an intervening calculation, a <src>consolidate()</src> could
120 // be necessary on <em>other</em> first.
121 // <group>
123 CompoundParam<T>(other) {}
125 CompoundParam<T>(other, True) {}
126 template <class W>
128 CompoundParam<T>(other) {}
129 template <class W>
131 CompoundParam<T>(other, True) {}
132 // </group>
133 // Make this object a (deep) copy of other.
135 other.fromParam_p();
136 CompoundParam<T>::operator=(other); return *this; }
137
138 // Destructor
139 virtual ~CompoundFunction() {}
140
141 //# Operators
142 // Evaluate the function at <src>x</src>.
143 virtual T eval(typename Function<T>::FunctionArg x) const;
144
145 //# Member functions
146 // Consolidate the parameter settings. This could be necessary if
147 // parameters have been set, and a copy constructor called. This is
148 // necessary before and after the setting of <em>local</em> parameters; i.e.
149 // the parameters of the individual functions.
151 toParam_p(); return *this; }
152 // Return a copy of this object from the heap. The caller is responsible for
153 // deleting the pointer.
154 // <group>
155 virtual Function<T> *clone() const { fromParam_p();
156 return new CompoundFunction<T>(*this); }
162 // </group>
163
164private:
165 //# Member functions
166 // Copy the local parameters from general block
167 void fromParam_p() const;
168 // Make the general block from local parameters
169 void toParam_p();
170
171 //# Make members of parent classes known.
172protected:
174 using CompoundParam<T>::param_p;
179public:
183};
184
185#define CompoundFunction_PS CompoundFunction
186
187// <summary> Partial <src>AutoDiff</src> specialization of CompoundFunction
188// </summary>
189
190// <synopsis>
191// <note role=warning> The name <src>CompoundFunction_PS</src> is only
192// for cxx2html documentation problems. Use
193// <src>CompoundFunction</src> in your code.</note>
194// </synopsis>
196template <class T> class CompoundFunction_PS<AutoDiff<T> > :
197public CompoundParam<AutoDiff<T> >
198{
199 public:
200 //# Constructors
201 // The default constructor -- no functions, no parameters, nothing, the
202 // function operator returns a 0.
204 // Make this object a (deep) copy of other. If parameters have been set
205 // without an intervening calculation, a <src>consolidate()</src> could
206 // be necessary on <em>other</em> first.
207 // <group>
209 CompoundParam<AutoDiff<T> >(other) {}
210 template <class W>
211 CompoundFunction_PS(const CompoundFunction_PS<W> &other) :
212 CompoundParam<AutoDiff<T> >(other) {}
213 // </group>
214 // Make this object a (deep) copy of other.
215 CompoundFunction_PS<AutoDiff<T> > &
217 fromParam_p();
218 CompoundParam<AutoDiff<T> >::operator=(other); return *this; }
219
220 // Destructor
221 virtual ~CompoundFunction_PS() {}
222
223 //# Operators
224 // Evaluate the function and its derivatives at <src>x</src> <em>wrt</em>
225 // to the coefficients.
227 eval(typename Function<AutoDiff<T> >::FunctionArg x) const;
228
229 //# Member functions
230// Add a function to the sum. All functions must have the same
231 // <src>ndim()</src> as the first one. Returns the (zero relative) number
232 // of the function just added.
233 uInt addFunction(const Function<AutoDiff<T> > &newFunction);
234 // Consolidate the parameter settings. This could be necessary if
235 // parameters have been set, and a copy constructor called. This is
236 // necessary before and after the setting of <em>local</em> parameters; i.e.
237 // the parameters of the individual functions.
238 CompoundFunction_PS<AutoDiff<T> > &consolidate() { fromParam_p();
239 toParam_p(); return *this; }
240 // Return a copy of this object from the heap. The caller is responsible for
241 // deleting the pointer.
242 // <group>
243 virtual Function<AutoDiff<T> > *clone() const { fromParam_p();
244 return new CompoundFunction<AutoDiff<T> >(*this); }
246 *cloneAD() const {
248 (*this); }
250 *cloneNonAD() const {
252 (*this, True); }
253 // </group>
254
255private:
256 //# Member functions
257 // Copy the local parameters to/from general block
258 void fromParam_p() const;
259 // Make the general block from local parameters
260 void toParam_p();
261
262 //# Make members of parent classes known.
263protected:
264 using CompoundParam<AutoDiff<T> >::parset_p;
265 using CompoundParam<AutoDiff<T> >::param_p;
266 using CompoundParam<AutoDiff<T> >::funpar_p;
267 using CompoundParam<AutoDiff<T> >::locpar_p;
268 using CompoundParam<AutoDiff<T> >::paroff_p;
269 using CompoundParam<AutoDiff<T> >::functionPtr_p;
270public:
271 using CompoundParam<AutoDiff<T> >::nparameters;
272 using CompoundParam<AutoDiff<T> >::nFunctions;
273 using CompoundParam<AutoDiff<T> >::function;
274};
275
276#undef CompoundFunction_PS
277
278
279} //# NAMESPACE CASACORE - END
280
281#ifndef CASACORE_NO_AUTO_TEMPLATES
282#include <casacore/scimath/Functionals/CompoundFunction.tcc>
283#include <casacore/scimath/Functionals/Compound2Function.tcc>
284#endif //# CASACORE_NO_AUTO_TEMPLATES
285#endif
#define CompoundFunction_PS
virtual Function< typename FunctionTraits< AutoDiff< T > >::BaseType > * cloneNonAD() const
void toParam_p()
Make the general block from local parameters.
virtual Function< typename FunctionTraits< AutoDiff< T > >::DiffType > * cloneAD() const
CompoundFunction_PS()
The default constructor – no functions, no parameters, nothing, the function operator returns a 0.
CompoundFunction_PS(const CompoundFunction_PS< W > &other)
virtual Function< AutoDiff< T > > * clone() const
Return a copy of this object from the heap.
CompoundFunction_PS(const CompoundFunction_PS< AutoDiff< T > > &other)
Make this object a (deep) copy of other.
CompoundFunction_PS< AutoDiff< T > > & operator=(const CompoundFunction_PS< AutoDiff< T > > &other)
Make this object a (deep) copy of other.
void fromParam_p() const
Copy the local parameters to/from general block.
virtual AutoDiff< T > eval(typename Function< AutoDiff< T > >::FunctionArg x) const
Evaluate the function and its derivatives at x wrt to the coefficients.
CompoundFunction_PS< AutoDiff< T > > & consolidate()
Consolidate the parameter settings.
uInt addFunction(const Function< AutoDiff< T > > &newFunction)
Add a function to the sum.
CompoundFunction(const CompoundFunction< T > &other)
Make this object a (deep) copy of other.
CompoundFunction< T > & operator=(const CompoundFunction< T > &other)
Make this object a (deep) copy of other.
virtual ~CompoundFunction()
Destructor.
virtual Function< T > * clone() const
Return a copy of this object from the heap.
virtual Function< typename FunctionTraits< T >::BaseType > * cloneNonAD() const
virtual T eval(typename Function< T >::FunctionArg x) const
Evaluate the function at x.
CompoundFunction()
The default constructor – no functions, no parameters, nothing, the function operator returns a 0.
CompoundFunction< T > & consolidate()
Consolidate the parameter settings.
void fromParam_p() const
Copy the local parameters from general block.
CompoundFunction(const CompoundFunction< T > &other, Bool)
virtual Function< typename FunctionTraits< T >::DiffType > * cloneAD() const
void toParam_p()
Make the general block from local parameters.
CompoundFunction(const CompoundFunction< W > &other)
CompoundFunction(const CompoundFunction< W > &other, Bool)
Block< uInt > funpar_p
Index of function belonging to parameter.
Block< uInt > paroff_p
Index of offset for each function to its parameters in general list.
CompoundParam< T > & operator=(const CompoundParam< T > &other)
const Function< T > & function(uInt which) const
Return a reference to a specific Function.
PtrBlock< Function< T > * > functionPtr_p
Pointer to each added function.
Block< uInt > locpar_p
Index of local parameter.
uInt nFunctions() const
Return the number of functions in the sum.
FunctionParam< T > param_p
The parameters and masks.
Definition Function.h:330
Bool parset_p
Indicate parameter written.
Definition Function.h:334
uInt nparameters() const
Returns the number of parameters.
Definition Function.h:228
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
PtrHolder< T > & operator=(const PtrHolder< T > &other)