casacore
CompoundParam.h
Go to the documentation of this file.
1 //# CompoundParam.h: Parameters for sum of parameterized 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_COMPOUNDPARAM_H
30 #define SCIMATH_COMPOUNDPARAM_H
31 
32 #include <casacore/casa/aips.h>
33 #include <casacore/scimath/Functionals/Function.h>
34 #include <casacore/casa/BasicSL/String.h>
35 #include <casacore/casa/Containers/Block.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 // <summary> Parameters for sum of parameterized Functions
40 // </summary>
41 
42 // <use visibility=local>
43 
44 // <reviewed reviewer="tcornwel" date="1996/02/22" tests="tCompoundFunction"
45 // demos="">
46 // </reviewed>
47 //
48 // <prerequisite>
49 // <li> <linkto class="Function">Function</linkto>
50 // </prerequisite>
51 //
52 // <synopsis>
53 // This class takes an arbitrary number of Function objects, and generates
54 // a new, single function object. The parameters of the compound object
55 // are the union of all the parameters in the input objects.
56 //
57 // When CompoundFunction is evaluated, the result is the sum of
58 // all the individual function values.
59 //
60 // Note that any Function object (including another Compound object) can be
61 // part of a compound object.
62 // </synopsis>
63 //
64 // <example>
65 // Suppose for some reason we wanted the sum of <src>x^2</src> plus a gaussian.
66 // We could form it as follows:
67 // <srcblock>
68 // Polynomial<Float> x2(2);
69 // x[2] = 1.0; // x^2
70 // Gaussian1D<Float> gauss(1.0, 0.0, 1.0); // e^{-x^2}
71 // CompoundParam<Float> sum; // sum == 0.0
72 // sum.addFunction(x2); // sum == x^2
73 // sum.addFunction(gauss); // sum == x^2+e^{-x^2}
74 // sum(2.0); // == 4 + e^-4
75 // CompoundParam[0] = 2.0; // sum ==2x^2+e^{-x^2}
76 // sum(2.0); // == 8 + e^-4
77 // // Set the height of the gaussian
78 // sum[parameterOffset[1] + Gaussian1D<Float>::HEIGHT] = 2.5;
79 // </srcblock>
80 // </example>
81 
82 // <templating arg=T>
83 // <li> T should have standard numerical operators and exp() function. Current
84 // implementation only tested for real types.
85 // <li> To obtain derivatives, the derivatives should be defined.
86 // </templating>
87 
88 // <thrown>
89 // <li> AipsError if dimensions of functions added different
90 // </thrown>
91 
92 // <motivation>
93 // This class was created to allow a non-linear least squares fitter to fit a
94 // (potentially) arbitrary number of functions (typically gaussians).
95 // </motivation>
96 //
97 // <todo asof="2001/10/22">
98 // <li> Nothing I know of
99 // </todo>
100 
101 template<class T> class CompoundParam : public Function<T>
102 {
103 public:
104  //# Constructors
105  // The default constructor -- no functions, no parameters, nothing, the
106  // function operator returns a 0.
108  // Make this object a (deep) copy of other.
109  // <group>
112  Function<T>(other), ndim_p(other.ndim_p),
114  paroff_p(other.paroff_p.nelements()),
115  funpar_p(other.funpar_p.nelements()),
116  locpar_p(other.locpar_p.nelements()) {
117  for (uInt i=0; i<functionPtr_p.nelements(); ++i) {
118  functionPtr_p[i] = other.functionPtr_p[i]->clone();
119  paroff_p[i] = other.paroff_p[i];
120  }
121  for (uInt i=0; i<funpar_p.nelements(); ++i) {
122  funpar_p[i] = other.funpar_p[i];
123  locpar_p[i] = other.locpar_p[i];
124  }
125  }
126  template <class W>
128  Function<T>(other), ndim_p(other.ndim()),
129  functionPtr_p(other.nFunctions()),
130  paroff_p(other.nFunctions()),
131  funpar_p(other.nparameters()),
132  locpar_p(other.nparameters()) {
133  for (uInt i=0; i<functionPtr_p.nelements(); ++i) {
134  functionPtr_p[i] = other.function(i).cloneAD();
135  paroff_p[i] = other.parameterOffset(i);
136  }
137  for (uInt i=0; i<funpar_p.nelements(); ++i) {
138  funpar_p[i] = other.parameterFunction(i);
139  locpar_p[i] = other.parameterLocation(i);
140  }
141  }
142  template <class W>
144  Function<T>(other), ndim_p(other.ndim()),
145  functionPtr_p(other.nFunctions()),
146  paroff_p(other.nFunctions()),
147  funpar_p(other.nparameters()),
148  locpar_p(other.nparameters()) {
149  for (uInt i=0; i<functionPtr_p.nelements(); ++i) {
150  functionPtr_p[i] = other.function(i).cloneNonAD();
151  paroff_p[i] = other.parameterOffset(i);
152  }
153  for (uInt i=0; i<funpar_p.nelements(); ++i) {
154  funpar_p[i] = other.parameterFunction(i);
155  locpar_p[i] = other.parameterLocation(i);
156  }
157  }
159  // </group>
160 
161  virtual ~CompoundParam();
162 
163  //# Operators
164 
165  //# Member functions
166  // Give name of function
167  virtual const String &name() const { static String x("compound");
168  return x; }
169 
170  // Add a function to the sum. All functions must have the same
171  // <src>ndim()</src> as the first one. Returns the (zero relative) number
172  // of the function just added.
173  uInt addFunction(const Function<T> &newFunction);
174 
175  // Return the number of functions in the sum.
176  uInt nFunctions() const { return functionPtr_p.nelements(); }
177 
178  // Return a reference to a specific Function.
179  // <group>
180  const Function<T> &function(uInt which) const {
181  DebugAssert(nFunctions() > which, AipsError);
182  return *(functionPtr_p[which]); }
183  // </group>
184  // Get the offset in function parameterlist for function which
185  uInt parameterOffset(uInt which) const {
186  DebugAssert(nFunctions() > which, AipsError); return paroff_p[which]; }
187  // Get the function number belonging to parameter list element which
188  uInt parameterFunction(uInt which) const {
189  DebugAssert(nparameters() > which, AipsError);
190  return funpar_p[which];
191  }
192  // Return locpar
193  uInt parameterLocation(uInt which) const {
194  DebugAssert(nparameters() > which, AipsError);
195  return locpar_p[which];
196  }
197  // Returns the dimension of functions in the linear combination
198  virtual uInt ndim() const { return ndim_p; }
199 
200 private:
201  //# Data
202  // Number of dimensions of underlying functions
204 
205 protected:
206  //# Data
207  // Pointer to each added function
209  // Index of offset for each function to its parameters in general list
211  // Index of function belonging to parameter
213  // Index of local parameter
215 
216  //# Make members of parent classes known.
217 protected:
218  using Function<T>::parset_p;
219  using Function<T>::param_p;
220 public:
222 };
223 
224 
225 } //# NAMESPACE CASACORE - END
226 
227 #ifndef CASACORE_NO_AUTO_TEMPLATES
228 #include <casacore/scimath/Functionals/CompoundParam.tcc>
229 #endif //# CASACORE_NO_AUTO_TEMPLATES
230 #endif
#define DebugAssert(expr, exception)
Definition: Assert.h:185
size_t nelements() const
The number of elements contained in this Block<T>.
Definition: Block.h:611
virtual const String & name() const
Give name of function.
Block< uInt > funpar_p
Index of function belonging to parameter.
CompoundParam< T > & operator=(const CompoundParam< T > &other)
uInt parameterOffset(uInt which) const
Get the offset in function parameterlist for function which.
CompoundParam(const CompoundParam< T > &other)
Make this object a (deep) copy of other.
Block< uInt > paroff_p
Index of offset for each function to its parameters in general list.
CompoundParam(const CompoundParam< W > &other, Bool)
uInt parameterLocation(uInt which) const
Return locpar.
CompoundParam(const CompoundParam< W > &other)
uInt addFunction(const Function< T > &newFunction)
Add a function to the sum.
virtual uInt ndim() const
Returns the dimension of functions in the linear combination.
const Function< T > & function(uInt which) const
Return a reference to a specific Function.
CompoundParam(const CompoundParam< T > &other, Bool)
PtrBlock< Function< T > * > functionPtr_p
Pointer to each added function.
uInt ndim_p
Number of dimensions of underlying functions.
Block< uInt > locpar_p
Index of local parameter.
uInt nFunctions() const
Return the number of functions in the sum.
uInt parameterFunction(uInt which) const
Get the function number belonging to parameter list element which.
CompoundParam()
The default constructor – no functions, no parameters, nothing, the function operator returns a 0.
uInt nparameters() const
Returns the number of parameters.
Definition: Function.h:230
A drop-in replacement for Block<T*>.
Definition: Block.h:814
String: the storage and methods of handling collections of characters.
Definition: String.h:225
this file contains all the compiler specific defines
Definition: mainpage.dox:28
unsigned int uInt
Definition: aipstype.h:51
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
LatticeExprNode nelements(const LatticeExprNode &expr)
1-argument function to get the number of elements in a lattice.