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