casacore
Loading...
Searching...
No Matches
GaussianNDParam.h
Go to the documentation of this file.
1//# GaussianNDParam.h: Multidimensional Gaussian class parameters
2//# Copyright (C) 2001,2002,2004,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_GAUSSIANNDPARAM_H
27#define SCIMATH_GAUSSIANNDPARAM_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/scimath/Functionals/Function.h>
32#include <casacore/casa/Arrays/Matrix.h>
33#include <casacore/casa/Arrays/Vector.h>
34#include <casacore/casa/BasicSL/String.h>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38// <summary> A Multi-dimensional Gaussian parameter handling. </summary>
39
40// <use visibility=local>
41
42// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tGaussianND" demos="dGaussianND">
43// </reviewed>
44
45// <prerequisite>
46// <li> <linkto class="FunctionParam">FunctionParam</linkto> class
47// <li> <linkto class="Function">Function</linkto> class
48// </prerequisite>
49
50// <synopsis>
51// A <src>GaussianND</src> is used to calculate Gaussian functions of any
52// dimension. A <linkto class=Gaussian1D> Gaussian1D </linkto> class exists
53// which is more appropriate for one dimensional Gaussian functions, and a
54// <linkto class=Gaussian2D> Gaussian2D </linkto> class exists for two
55// dimensional functions.
56//
57// A statistical description of the multi-dimensional Gaussian is used (see
58// Kendall & Stuart "The Advanced Theory of Statistics"). A Gaussian is
59// defined in terms of its height, mean (which is the location of the peak
60// value), variance, (a measure of the width of the Gaussian), and
61// covariance which skews the distribution with respect to the Axes.
62//
63// In the general description the variance and covariance are specified
64// using a covariance matrix. This is defined as (for a 4 dimensional
65// Gaussian):
66// <srcblock>
67// V = | s1*s1 r12*s1*s2 r13*s1*s3 r14*s1*s4 |
68// | r12*s1*s2 s2*s2 r23*s2*s3 r24*s2*s4 |
69// | r13*s1*s3 r23*s2*s3 s3*s3 r34*s3*s4 |
70// | r14*s1*s4 r24*s2*s4 r34*s3*s4 s4*s4 |
71// </srcblock>
72// where s1 (<src>sigma1</src>) is the standard deviation of the Gaussian with
73// respect to the first axis, and r12 (<src>rho12</src>) is the correlation
74// between the the first and second axis. The correlation MUST be between -1
75// and 1, and this class checks this as well as ensuring that the diagonal
76// is positive.
77//
78// <note role=warning> It is possible to have symmetric matrices that are of
79// the above described form (ie. symmetric with <src>-1 <= rho(ij) <=1</src>)
80// that do
81// not generate a Gaussian function. This is because the Matrix is NOT
82// positive definite (The limits on <src>rho(ij)</src> are upper limits).
83// This class
84// does check that the covariance Matrix is positive definite and will throw
85// an exception (AipsError) if it is not.</note>
86//
87// The covariance Matrix can be specified by only its upper or lower
88// triangular regions (ie. with zeros in the other triangle), otherwise it
89// MUST be symmetric.
90//
91// The Gaussian that is constructed from this covariance Matrix (V), along
92// with mean (u) and height (h) is:
93// <srcblock>
94// f(x) = h*exp( -1/2 * (x-u) * V^(-1) * (x-u))
95// </srcblock>
96// where x, and u are vectors whose length is the dimensionality of the
97// Gaussian and V^(-1) is the inverse of the covariance Matrix defined
98// above. For a two dimensional Gaussian with zero mean this expression
99// reduces to:
100// <srcblock>
101// f(x) = h*exp(-1/(2*(1-r12^2))*(x1^2/s1^2 - 2*r12*x1*x2/(s1*s2) + x2^2/s2^2))
102// </srcblock>
103//
104// The amplitude of the Gaussian can be defined in two ways, either using
105// the peak height (as is done in the constructors, and the setHeight
106// function) or using the setFlux function. The flux in this context is the
107// analytic integral of the Gaussian over all dimensions. Using the setFlux
108// function does not modify the shape of the Gaussian just its height.
109//
110// All the parameters of the Gaussian except its dimensionality can be
111// modified using the set/get functions.
112//
113// The parameter interface (see
114// <linkto class="FunctionParam">FunctionParam</linkto> class),
115// is used to provide an interface to the
116// <linkto module="Fitting"> Fitting </linkto> classes.
117// There are always 4 parameter sets.
118// <note role=warning> Note that the actual variance/covariance
119// parameters are the inverse matrix of the variance/covariance matrix given
120// by the user</note>.
121// The actual parameters are in order:
122// <ol>
123// <li> height (1 term). No assumptions on what quantity the height
124// represents, and it can be negative (enumerated by <src>HEIGHT</src>)
125// <li> mean (ndim terms) (enumerated by <src>CENTER</src>).
126// <li> variance (ndim terms). The variance is always positive, and an
127// exception (AipsError) will be thrown if you try to set a negative
128// value.
129// <li> covariance (ndim*(ndim-1)/2 terms) The order is (assuming ndim=5)
130// v12,v13,v14,v15,v23,v24,v25,v34,v35,v45. The restrictions described
131// above for the covariance (ie. -1 < r12 < +1) are enforced.
132// </ol>
133// </synopsis>
134
135// <example>
136// Construct a two dimensional Gaussian with mean=(0,1), variance=(.1,7) and
137// height = 1;
138// <srcblock>
139// uInt ndim = 2;
140// Float height = 1;
141// Vector<Float> mean(ndim); mean(0) = 0, mean(1) = 1;
142// Vector<Float> variance(ndim); variance(0) = .1, variance(1) = 7;
143// GaussianND<Float> g(ndim, height, mean, variance);
144// Vector<Float> x(ndim); x = 0;
145// cout << "g("<< x <<") = " << g(x) <<endl; // g([0,0])=1*exp(-1/2*1/7);
146// x(1)++;
147// cout << "g("<< x <<") = " <<g(x) <<endl; // g([0,1])= 1
148// cout << "Height: " << g.height() <<endl; // Height: 1
149// cout << "Flux: " << g.flux() << endl; // Flux: 2*Pi*Sqrt(.1*7)
150// cout << "Mean: " << g.mean() << endl; // Mean: [0, -1]
151// cout << "Variance: " << g.variance() <<endl; // Variance: [.1, 7]
152// cout << "Covariance: "<< g.covariance()<<endl;// Covariance: [.1, 0]
153// // [0, 7]
154// g.setFlux(1);
155// cout << "g("<< x <<") = " <<g(x) <<endl; //g([0,1])=1/(2*Pi*Sqrt(.7))
156// cout << "Height: " << g.height() <<endl; // Height: 1/(2*Pi*Sqrt(.7))
157// cout << "Flux: " << g.flux() << endl; // Flux: 1
158// cout << "Mean: " << g.mean() << endl; // Mean: [0, -1]
159// cout << "Variance: " << g.variance() <<endl; // Variance: [.1, 7]
160// cout << "Covariance: "<< g.covariance()<<endl;// Covariance: [.1, 0]
161// // [0, 7]
162// </srcblock>
163// </example>
164
165// <motivation>
166// A Gaussian Functional was needed for modeling the sky with a series of
167// components. It was later realised that it was too general and Gaussian2D
168// was written.
169// </motivation>
170
171// <templating arg=T>
172// <li> T should have standard numerical operators and exp() function. Current
173// implementation only tested for real types.
174// </templating>
175
176// <todo asof="2001/08/19">
177// <li> Nothing I know off, apart from possible optimization
178// </todo>
179
180template<class T> class GaussianNDParam : public Function<T>
181{
182public:
183 //# Enumerations
184 enum { HEIGHT=0, CENTER};
185
186 //# Constructors
187 // Constructs a Gaussian using the indicated height, mean, variance &
188 // covariance.
189 // ndim defaults to 2,
190 // mean defaults to 0,
191 // height to <src> Pi^(-ndim/2)</src> (the flux is unity)
192 // variance defaults to 1.0,
193 // covariance defaults to 0.0,
194 // <group>
200 const Vector<T> &variance);
202 const Matrix<T> &covar);
203 // </group>
204
205 // Copy constructor (deep copy)
206 // <group>
208 template <class W>
210 Function<T>(other),
211 itsDim(other.itsDim), itsFlux2Hgt(other.itsFlux2Hgt) {}
212 // </group>
213
214 // Copy assignment (deep copy)
216
217 // Destructor
219
220 //# Operators
221
222 //# Member functions
223 // Give name of function
224 virtual const String &name() const { static String x("gaussiannd");
225 return x; }
226
227 // Variable dimensionality
228 virtual uInt ndim() const { return itsDim; }
229
230 // Get or set the peak height of the Gaussian
231 // <group>
232 T height() const { return param_p[HEIGHT]; }
233 void setHeight(const T &height) { param_p[HEIGHT] = height; }
234 // </group>
235
236 // The analytical integrated area underneath the Gaussian. Use these
237 // functions as an alternative to the height functions.
238 // <group>
239 T flux() const;
240 void setFlux(const T &flux);
241 // </group>
242
243 // The center ordinate of the Gaussian
244 // <group>
246 void setMean(const Vector<T> &mean);
247 // </group>
248
249 // The FWHM of the Gaussian is <src>sqrt(8*variance*log(2))</src>.
250 // The variance MUST be positive
251 // <group>
254 // </group>
255
256 //The covariance Matrix defines the correlations between all the axes.
257 //<group>
259 void setCovariance(const Matrix<T> &covar);
260 // </group>
261
262protected:
263 //# Data
264 // dimensionality
266 // factor to convert from flux to height
268
269 //# Methods
270 // Functions to convert between internal Vector of parameters
271 // and the Covariance
272 // Matrix
273 // <group>
274 void repack(Matrix<T> &covar) const;
275 void unpack(const Matrix<T> &covar);
276 // </group>
277
278 //# Make members of parent classes known.
279protected:
280 using Function<T>::param_p;
281public:
282 using Function<T>::nparameters;
283};
284
285
286} //# NAMESPACE CASACORE - END
287
288#ifndef CASACORE_NO_AUTO_TEMPLATES
289#include <casacore/scimath/Functionals/GaussianNDParam.tcc>
290#endif //# CASACORE_NO_AUTO_TEMPLATES
291#endif
FunctionParam< T > param_p
The parameters and masks.
Definition Function.h:330
uInt nparameters() const
Returns the number of parameters.
Definition Function.h:228
Vector< T > variance() const
The FWHM of the Gaussian is sqrt(8*variance*log(2)).
void setMean(const Vector< T > &mean)
GaussianNDParam(uInt ndim, const T &height)
Matrix< T > covariance() const
The covariance Matrix defines the correlations between all the axes.
void setVariance(const Vector< T > &variance)
void setCovariance(const Matrix< T > &covar)
void setFlux(const T &flux)
GaussianNDParam(uInt ndim, const T &height, const Vector< T > &mean, const Vector< T > &variance)
void repack(Matrix< T > &covar) const
Functions to convert between internal Vector of parameters and the Covariance Matrix.
virtual const String & name() const
Give name of function.
void unpack(const Matrix< T > &covar)
GaussianNDParam(const GaussianNDParam< W > &other)
T height() const
Get or set the peak height of the Gaussian.
virtual uInt ndim() const
Variable dimensionality.
GaussianNDParam(const GaussianNDParam &other)
Copy constructor (deep copy)
T itsFlux2Hgt
factor to convert from flux to height
T flux() const
The analytical integrated area underneath the Gaussian.
GaussianNDParam(uInt ndim, const T &height, const Vector< T > &mean, const Matrix< T > &covar)
GaussianNDParam(uInt ndim, const T &height, const Vector< T > &mean)
GaussianNDParam< T > & operator=(const GaussianNDParam< T > &other)
Copy assignment (deep copy)
GaussianNDParam()
Constructs a Gaussian using the indicated height, mean, variance & covariance.
void setHeight(const T &height)
uInt itsDim
dimensionality
virtual ~GaussianNDParam()
Destructor.
Vector< T > mean() const
The center ordinate of the Gaussian.
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