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>
113 public:
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>
124 template <class W>
126 template <class W>
128 // </group>
129 // Make this object a (deep) copy of other.
131 other.fromParam_p();
133 return *this;
134 }
135
136 // Destructor
137 virtual ~CompoundFunction() {}
138
139 // # Operators
140 // Evaluate the function at <src>x</src>.
141 virtual T eval(typename Function<T>::FunctionArg x) const;
142
143 // # Member functions
144 // Consolidate the parameter settings. This could be necessary if
145 // parameters have been set, and a copy constructor called. This is
146 // necessary before and after the setting of <em>local</em> parameters; i.e.
147 // the parameters of the individual functions.
149 fromParam_p();
150 toParam_p();
151 return *this;
152 }
153 // Return a copy of this object from the heap. The caller is responsible for
154 // deleting the pointer.
155 // <group>
156 virtual Function<T> *clone() const {
157 fromParam_p();
158 return new CompoundFunction<T>(*this);
159 }
166 // </group>
167
168 private:
169 // # Member functions
170 // Copy the local parameters from general block
171 void fromParam_p() const;
172 // Make the general block from local parameters
173 void toParam_p();
174
175 // # Make members of parent classes known.
176 protected:
178 using CompoundParam<T>::param_p;
183
184 public:
188};
189
190#define CompoundFunction_PS CompoundFunction
191
192// <summary> Partial <src>AutoDiff</src> specialization of CompoundFunction
193// </summary>
194
195// <synopsis>
196// <note role=warning> The name <src>CompoundFunction_PS</src> is only
197// for cxx2html documentation problems. Use
198// <src>CompoundFunction</src> in your code.</note>
199// </synopsis>
200
201template <class T>
202class CompoundFunction_PS<AutoDiff<T>> : public CompoundParam<AutoDiff<T>> {
203 public:
204 // # Constructors
205 // The default constructor -- no functions, no parameters, nothing, the
206 // function operator returns a 0.
208 // Make this object a (deep) copy of other. If parameters have been set
209 // without an intervening calculation, a <src>consolidate()</src> could
210 // be necessary on <em>other</em> first.
213 : CompoundParam<AutoDiff<T>>(other) {}
214 template <class W>
216 // </group>
217 // Make this object a (deep) copy of other.
219 fromParam_p();
220 CompoundParam<AutoDiff<T>>::operator=(other);
221 return *this;
222 }
223
224 // Destructor
225 virtual ~CompoundFunction_PS() {}
226
227 // # Operators
228 // Evaluate the function and its derivatives at <src>x</src> <em>wrt</em>
229 // to the coefficients.
230 virtual AutoDiff<T> eval(typename Function<AutoDiff<T>>::FunctionArg x) const;
231
232 // # Member functions
233 // Add a function to the sum. All functions must have the same
234 // <src>ndim()</src> as the first one. Returns the (zero relative) number
235 // of the function just added.
236 uInt addFunction(const Function<AutoDiff<T>> &newFunction);
237 // Consolidate the parameter settings. This could be necessary if
238 // parameters have been set, and a copy constructor called. This is
239 // necessary before and after the setting of <em>local</em> parameters; i.e.
240 // the parameters of the individual functions.
242 fromParam_p();
243 toParam_p();
244 return *this;
245 }
246 // Return a copy of this object from the heap. The caller is responsible for
247 // deleting the pointer.
248 // <group>
249 virtual Function<AutoDiff<T>> *clone() const {
250 fromParam_p();
251 return new CompoundFunction<AutoDiff<T>>(*this);
253 virtual Function<typename FunctionTraits<AutoDiff<T>>::DiffType> *cloneAD() const {
254 return new CompoundFunction<typename FunctionTraits<AutoDiff<T>>::DiffType>(*this);
256 virtual Function<typename FunctionTraits<AutoDiff<T>>::BaseType> *cloneNonAD() const {
257 return new CompoundFunction<typename FunctionTraits<AutoDiff<T>>::BaseType>(*this, True);
258 }
259 // </group>
260
261 private:
262 // # Member functions
263 // Copy the local parameters to/from general block
264 void fromParam_p() const;
265 // Make the general block from local parameters
266 void toParam_p();
267
268 // # Make members of parent classes known.
269 protected:
276
277 public:
281};
282
283#undef CompoundFunction_PS
284
285} // namespace casacore
286
287#ifndef CASACORE_NO_AUTO_TEMPLATES
288#include <casacore/scimath/Functionals/CompoundFunction.tcc>
289#include <casacore/scimath/Functionals/Compound2Function.tcc>
290#endif // # CASACORE_NO_AUTO_TEMPLATES
291#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< Function< T > * > functionPtr_p
Pointer to each added function.
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.
Block< uInt > locpar_p
Index of local parameter.
uInt nFunctions() const
Return the number of functions in the sum.
CompoundParam()
The default constructor – no functions, no parameters, nothing, the function operator returns a 0.
FunctionParam< T > param_p
The parameters and masks.
Definition Function.h:337
Bool parset_p
Indicate parameter written.
Definition Function.h:341
uInt nparameters() const
Returns the number of parameters.
Definition Function.h:226
const ArgType * FunctionArg
Definition Function.h:204
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
virtual Function< typename FunctionTraits< T >::DiffType > * cloneAD() const
Definition Polynomial.h:126
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
virtual Function< typename FunctionTraits< T >::BaseType > * cloneNonAD() const
Definition Polynomial.h:129