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> class CompoundParam : public Function<T>
99{
100public:
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), ndim_p(other.ndim_p),
111 paroff_p(other.paroff_p.nelements()),
112 funpar_p(other.funpar_p.nelements()),
113 locpar_p(other.locpar_p.nelements()) {
114 for (uInt i=0; i<functionPtr_p.nelements(); ++i) {
115 functionPtr_p[i] = other.functionPtr_p[i]->clone();
116 paroff_p[i] = other.paroff_p[i];
117 }
118 for (uInt i=0; i<funpar_p.nelements(); ++i) {
119 funpar_p[i] = other.funpar_p[i];
120 locpar_p[i] = other.locpar_p[i];
121 }
122 }
123 template <class W>
125 Function<T>(other), ndim_p(other.ndim()),
126 functionPtr_p(other.nFunctions()),
127 paroff_p(other.nFunctions()),
128 funpar_p(other.nparameters()),
129 locpar_p(other.nparameters()) {
130 for (uInt i=0; i<functionPtr_p.nelements(); ++i) {
131 functionPtr_p[i] = other.function(i).cloneAD();
132 paroff_p[i] = other.parameterOffset(i);
133 }
134 for (uInt i=0; i<funpar_p.nelements(); ++i) {
135 funpar_p[i] = other.parameterFunction(i);
136 locpar_p[i] = other.parameterLocation(i);
137 }
138 }
139 template <class W>
141 Function<T>(other), ndim_p(other.ndim()),
142 functionPtr_p(other.nFunctions()),
143 paroff_p(other.nFunctions()),
144 funpar_p(other.nparameters()),
145 locpar_p(other.nparameters()) {
146 for (uInt i=0; i<functionPtr_p.nelements(); ++i) {
147 functionPtr_p[i] = other.function(i).cloneNonAD();
148 paroff_p[i] = other.parameterOffset(i);
149 }
150 for (uInt i=0; i<funpar_p.nelements(); ++i) {
151 funpar_p[i] = other.parameterFunction(i);
152 locpar_p[i] = other.parameterLocation(i);
153 }
154 }
156 // </group>
157
158 virtual ~CompoundParam();
159
160 //# Operators
161
162 //# Member functions
163 // Give name of function
164 virtual const String &name() const { static String x("compound");
165 return x; }
166
167 // Add a function to the sum. All functions must have the same
168 // <src>ndim()</src> as the first one. Returns the (zero relative) number
169 // of the function just added.
170 uInt addFunction(const Function<T> &newFunction);
171
172 // Return the number of functions in the sum.
173 uInt nFunctions() const { return functionPtr_p.nelements(); }
174
175 // Return a reference to a specific Function.
176 // <group>
177 const Function<T> &function(uInt which) const {
178 DebugAssert(nFunctions() > which, AipsError);
179 return *(functionPtr_p[which]); }
180 // </group>
181 // Get the offset in function parameterlist for function which
182 uInt parameterOffset(uInt which) const {
183 DebugAssert(nFunctions() > which, AipsError); return paroff_p[which]; }
184 // Get the function number belonging to parameter list element which
187 return funpar_p[which];
188 }
189 // Return locpar
192 return locpar_p[which];
193 }
194 // Returns the dimension of functions in the linear combination
195 virtual uInt ndim() const { return ndim_p; }
196
197private:
198 //# Data
199 // Number of dimensions of underlying functions
201
202protected:
203 //# Data
204 // Pointer to each added function
206 // Index of offset for each function to its parameters in general list
208 // Index of function belonging to parameter
210 // Index of local parameter
212
213 //# Make members of parent classes known.
214protected:
215 using Function<T>::parset_p;
216 using Function<T>::param_p;
217public:
218 using Function<T>::nparameters;
219};
220
221
222} //# NAMESPACE CASACORE - END
223
224#ifndef CASACORE_NO_AUTO_TEMPLATES
225#include <casacore/scimath/Functionals/CompoundParam.tcc>
226#endif //# CASACORE_NO_AUTO_TEMPLATES
227#endif
#define DebugAssert(expr, exception)
Definition Assert.h:183
simple 1-D array
Definition Block.h:198
size_t nelements() const
The number of elements contained in this Block<T>.
Definition Block.h:609
virtual const String & name() const
Give name of function.
Block< uInt > funpar_p
Index of function belonging to parameter.
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)
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.
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
A drop-in replacement for Block<T*>.
Definition Block.h:812
String: the storage and methods of handling collections of characters.
Definition String.h:223
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
LatticeExprNode nelements(const LatticeExprNode &expr)
1-argument function to get the number of elements in a lattice.