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