casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: aips2-request@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 //#
27 //# $Id$
28 
29 #ifndef SCIMATH_COMPOUNDFUNCTION_H
30 #define SCIMATH_COMPOUNDFUNCTION_H
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
38 
39 namespace casacore { //# NAMESPACE CASACORE - BEGIN
40 
41 //# Forward declarations
42 
43 // <summary>
44 // Sum of a collection of Functions which behaves as one Function object.
45 // </summary>
46 
47 // <use visibility=export>
48 
49 // <reviewed reviewer="tcornwel" date="1996/02/22" tests="tCompoundFunction"
50 // demos="">
51 // </reviewed>
52 
53 // <prerequisite>
54 // <li> <linkto class="Function">Function</linkto> class
55 // </prerequisite>
56 //
57 // <synopsis>
58 // This class takes an arbitrary number of Function objects, and generates
59 // a new, single function object. The parameters of the compound object
60 // are the union of all the parameters in the input objects.
61 //
62 // When CompoundFunction is evaluated, the result is the sum of
63 // all the individual function values.
64 //
65 // Member functions are added with the <src>addFunction()</src> method.
66 //
67 // In general the interaction with the function parameters should be through
68 // the overall function parameters (i.e. through the parameters of the
69 // <src>CompoundFunction</src>). If for any reason you want to set the
70 // parameters of an individual function (see e.g. the example in the
71 // <linkto class=Fit2D>Fit2D</linkto>), call <src>consolidate()</src> before
72 // abd after the actual setting.
73 //
74 // <note role=tip>
75 // Check <linkto class=CompoundFunction>CombiFunction</linkto> class
76 // for a simple linear combination of function objects </note>
77 // </synopsis>
78 //
79 // <example>
80 // Suppose for some reason we wanted the sum of <src>x^2</src> plus a gaussian.
81 // We could form it as follows:
82 // <srcblock>
83 // Polynomial<Float> x2(2);
84 // x[2] = 1.0; // x^2
85 // Gaussian1D<Float> gauss(1.0, 0.0, 1.0); // e^{-x^2}
86 // CompoundParam<Float> sum; // sum == 0.0
87 // sum.addFunction(x2); // sum == x^2
88 // sum.addFunction(gauss); // sum == x^2+e^{-x^2}
89 // sum(2.0); // == 4 + e^-4
90 // CompoundParam[0] = 2.0; // sum ==2x^2+e^{-x^2}
91 // sum(2.0); // == 8 + e^-4
92 // </srcblock>
93 // </example>
94 
95 // <templating arg=T>
96 // <li> T should have standard numerical operators and exp() function. Current
97 // implementation only tested for real types.
98 // <li> To obtain derivatives, the derivatives should be defined.
99 // </templating>
100 
101 // <thrown>
102 // <li> AipsError if dimensions of functions added different
103 // </thrown>
104 
105 // <motivation>
106 // This class was created to allow a non-linear least squares fitter to fit a
107 // (potentially) arbitrary number of functions (typically Gaussians).
108 // </motivation>
109 //
110 // <todo asof="2001/10/22">
111 // <li> Nothing I know of
112 // </todo>
113 
114 template <class T> class CompoundFunction : public CompoundParam<T>
115 {
116 public:
117  //# Constructors
118  // The default constructor -- no functions, no parameters, nothing, the
119  // function operator returns a 0.
121  // Make this object a (deep) copy of other. If parameters have been set
122  // without an intervening calculation, a <src>consolidate()</src> could
123  // be necessary on <em>other</em> first.
124  // <group>
126  CompoundParam<T>(other) {}
128  CompoundParam<T>(other, True) {}
129  template <class W>
131  CompoundParam<T>(other) {}
132  template <class W>
134  CompoundParam<T>(other, True) {}
135  // </group>
136  // Make this object a (deep) copy of other.
138  other.fromParam_p();
139  CompoundParam<T>::operator=(other); return *this; }
140 
141  // Destructor
142  virtual ~CompoundFunction() {}
143 
144  //# Operators
145  // Evaluate the function at <src>x</src>.
146  virtual T eval(typename Function<T>::FunctionArg x) const;
147 
148  //# Member functions
149  // Consolidate the parameter settings. This could be necessary if
150  // parameters have been set, and a copy constructor called. This is
151  // necessary before and after the setting of <em>local</em> parameters; i.e.
152  // the parameters of the individual functions.
154  toParam_p(); return *this; }
155  // Return a copy of this object from the heap. The caller is responsible for
156  // deleting the pointer.
157  // <group>
158  virtual Function<T> *clone() const { fromParam_p();
159  return new CompoundFunction<T>(*this); }
164  (*this, True); }
165  // </group>
166 
167 private:
168  //# Member functions
169  // Copy the local parameters from general block
170  void fromParam_p() const;
171  // Make the general block from local parameters
172  void toParam_p();
173 
174  //# Make members of parent classes known.
175 protected:
182 public:
186 };
187 
188 #define CompoundFunction_PS CompoundFunction
189 
190 // <summary> Partial <src>AutoDiff</src> specialization of CompoundFunction
191 // </summary>
192 
193 // <synopsis>
194 // <note role=warning> The name <src>CompoundFunction_PS</src> is only
195 // for cxx2html documentation problems. Use
196 // <src>CompoundFunction</src> in your code.</note>
197 // </synopsis>
199 template <class T> class CompoundFunction_PS<AutoDiff<T> > :
200 public CompoundParam<AutoDiff<T> >
201 {
202  public:
203  //# Constructors
204  // The default constructor -- no functions, no parameters, nothing, the
205  // function operator returns a 0.
207  // Make this object a (deep) copy of other. If parameters have been set
208  // without an intervening calculation, a <src>consolidate()</src> could
209  // be necessary on <em>other</em> first.
210  // <group>
212  CompoundParam<AutoDiff<T> >(other) {}
213  template <class W>
214  CompoundFunction_PS(const CompoundFunction_PS<W> &other) :
215  CompoundParam<AutoDiff<T> >(other) {}
216  // </group>
217  // Make this object a (deep) copy of other.
218  CompoundFunction_PS<AutoDiff<T> > &
219  operator=(const CompoundFunction_PS<AutoDiff<T> > &other) {
220  fromParam_p();
221  CompoundParam<AutoDiff<T> >::operator=(other); return *this; }
222 
223  // Destructor
224  virtual ~CompoundFunction_PS() {}
225 
226  //# Operators
227  // Evaluate the function and its derivatives at <src>x</src> <em>wrt</em>
228  // to the coefficients.
229  virtual AutoDiff<T>
230  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.
241  CompoundFunction_PS<AutoDiff<T> > &consolidate() { fromParam_p();
242  toParam_p(); return *this; }
243  // Return a copy of this object from the heap. The caller is responsible for
244  // deleting the pointer.
245  // <group>
246  virtual Function<AutoDiff<T> > *clone() const { fromParam_p();
247  return new CompoundFunction<AutoDiff<T> >(*this); }
249  *cloneAD() const {
251  (*this); }
253  *cloneNonAD() const {
255  (*this, True); }
256  // </group>
257 
258 private:
259  //# Member functions
260  // Copy the local parameters to/from general block
261  void fromParam_p() const;
262  // Make the general block from local parameters
263  void toParam_p();
264 
265  //# Make members of parent classes known.
266 protected:
267  using CompoundParam<AutoDiff<T> >::parset_p;
268  using CompoundParam<AutoDiff<T> >::param_p;
269  using CompoundParam<AutoDiff<T> >::funpar_p;
270  using CompoundParam<AutoDiff<T> >::locpar_p;
271  using CompoundParam<AutoDiff<T> >::paroff_p;
272  using CompoundParam<AutoDiff<T> >::functionPtr_p;
273 public:
274  using CompoundParam<AutoDiff<T> >::nparameters;
275  using CompoundParam<AutoDiff<T> >::nFunctions;
276  using CompoundParam<AutoDiff<T> >::function;
277 };
278 
279 #undef CompoundFunction_PS
280 
281 
282 } //# NAMESPACE CASACORE - END
283 
284 #ifndef CASACORE_NO_AUTO_TEMPLATES
285 #include <casacore/scimath/Functionals/CompoundFunction.tcc>
286 #include <casacore/scimath/Functionals/Compound2Function.tcc>
287 #endif //# CASACORE_NO_AUTO_TEMPLATES
288 #endif
virtual Function< T > * clone() const
Return a copy of this object from the heap.
CompoundFunction(const CompoundFunction< W > &other)
void fromParam_p() const
Copy the local parameters from general block.
CompoundFunction< T > & consolidate()
Consolidate the parameter settings.
#define CompoundFunction_PS
PtrHolder< T > & operator=(const PtrHolder< T > &other)
virtual Function< typename FunctionTraits< T >::DiffType > * cloneAD() const
CompoundFunction(const CompoundFunction< T > &other)
Make this object a (deep) copy of other.
virtual ~CompoundFunction()
Destructor.
CompoundFunction(const CompoundFunction< T > &other, Bool)
CompoundFunction< T > & operator=(const CompoundFunction< T > &other)
Make this object a (deep) copy of other.
void toParam_p()
Make the general block from local parameters.
Numerical functional interface class.
Definition: GenericL2Fit.h:46
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
Class that computes partial derivatives by automatic differentiation.
Definition: AutoDiff.h:257
CompoundFunction()
The default constructor – no functions, no parameters, nothing, the function operator returns a 0...
CompoundParam< T > & operator=(const CompoundParam< T > &other)
Parameters for sum of parameterized Functions.
virtual Function< typename FunctionTraits< T >::BaseType > * cloneNonAD() const
virtual T eval(typename Function< T >::FunctionArg x) const
Evaluate the function at x.
const Bool True
Definition: aipstype.h:43
Sum of a collection of Functions which behaves as one Function object.
CompoundFunction(const CompoundFunction< W > &other, Bool)
unsigned int uInt
Definition: aipstype.h:51