casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArrayPartMath.h
Go to the documentation of this file.
1 //# ArrayPartMath.h: mathematics done on an array parts.
2 //# Copyright (C) 1993,1994,1995,1996,1998,1999,2001,2003
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 //# $Id: ArrayPartMath.h 21262 2012-09-07 12:38:36Z gervandiepen $
27 
28 #ifndef CASA_ARRAYPARTMATH_H
29 #define CASA_ARRAYPARTMATH_H
30 
31 #include <casacore/casa/aips.h>
34 
35 namespace casacore { //# NAMESPACE CASACORE - BEGIN
36 
37 // <summary>
38 // Mathematical and logical operations for Array parts.
39 // </summary>
40 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArray">
41 //
42 // <prerequisite>
43 // <li> <linkto class=Array>Array</linkto>
44 // </prerequisite>
45 //
46 // <etymology>
47 // This file contains global functions which perform part by part
48 // mathematical or logical operations on arrays.
49 // </etymology>
50 //
51 // <synopsis>
52 // These functions perform chunk by chunk mathematical operations on
53 // arrays.
54 // In particular boxed and sliding operations are possible. E.g. to calculate
55 // the median in sliding windows making it possible to subtract the background
56 // in an image.
57 //
58 // The operations to be performed are defined by means of functors that
59 // reduce an array subset to a scalar. Those functors are wrappers for
60 // ArrayMath and ArrayLogical functions like sum, median, and ntrue.
61 //
62 // The <src>partialXX</src> functions are a special case of the
63 // <src>BoxedArrayMath</src> function.
64 // They reduce one or more entire axes which can be done in a faster way than
65 // the more general <src>boxedArrayMath</src> function.
66 // </synopsis>
67 //
68 // <example>
69 // <srcblock>
70 // Array<Double> data(...);
71 // Array<Double> means = partialMeans (data, IPosition(2,0,1));
72 // </srcblock>
73 // This example calculates the mean of each plane in the data array.
74 // </example>
75 //
76 // <example>
77 // <srcblock>
78 // IPosition shp = data.shape();
79 // Array<Double> means = boxedArrayMath (data, IPosition(2,shp[0],shp[1]),
80 // SumFunc<Double>());
81 // </srcblock>
82 // does the same as the first example.
83 // Note that in this example the box is formed by the entire axes, but it
84 // could also be a subset of it to average, say, boxes of 5*5 elements.
85 // </example>
86 //
87 // <linkfrom anchor="Array mathematical operations" classes="Array Vector Matrix Cube">
88 // <here>Array mathematical operations</here> -- Mathematical operations for
89 // Arrays.
90 // </linkfrom>
91 //
92 // <group name="Array partial operations">
93 
94 
95 // Determine the sum, product, etc. for the given axes only.
96 // The result is an array with a shape formed by the remaining axes.
97 // For example, for an array with shape [3,4,5], collapsing axis 0
98 // results in an array with shape [4,5] containing, say, the sum for
99 // each X line.
100 // Summing for axes 0 and 2 results in an array with shape [4] containing,
101 // say, the sum for each XZ plane.
102 // <note>
103 // ArrayLogical.h contains the functions ntrue, nfalse, partialNTrue and
104 // partialNFalse to count the number of true or false elements in an array.
105 // </note>
106 // <group>
107 template<class T> Array<T> partialSums (const Array<T>& array,
108  const IPosition& collapseAxes);
109 template<class T> Array<T> partialSumSqrs (const Array<T>& array,
110  const IPosition& collapseAxes);
111 template<class T> Array<T> partialProducts (const Array<T>& array,
112  const IPosition& collapseAxes);
113 template<class T> Array<T> partialMins (const Array<T>& array,
114  const IPosition& collapseAxes);
115 template<class T> Array<T> partialMaxs (const Array<T>& array,
116  const IPosition& collapseAxes);
117 template<class T> Array<T> partialMeans (const Array<T>& array,
118  const IPosition& collapseAxes);
119 template<class T> inline Array<T> partialVariances (const Array<T>& array,
120  const IPosition& collapseAxes,
121  uInt ddof=1)
122 {
123  return partialVariances (array, collapseAxes,
124  partialMeans (array, collapseAxes), ddof);
125 }
126 template<class T> Array<T> partialVariances (const Array<T>& array,
127  const IPosition& collapseAxes,
128  const Array<T>& means);
129 template<class T> Array<T> partialVariances (const Array<T>& array,
130  const IPosition& collapseAxes,
131  const Array<T>& means,
132  uInt ddof);
133 template<class T> Array<std::complex<T>> partialVariances (const Array<std::complex<T>>& array,
134  const IPosition& collapseAxes,
135  const Array<std::complex<T>>& means,
136  uInt ddof);
137 template<class T> inline Array<T> partialStddevs (const Array<T>& array,
138  const IPosition& collapseAxes,
139  uInt ddof=1)
140 {
141  return sqrt (partialVariances (array, collapseAxes,
142  partialMeans (array, collapseAxes), ddof));
143 }
144 template<class T> inline Array<T> partialStddevs (const Array<T>& array,
145  const IPosition& collapseAxes,
146  const Array<T>& means,
147  uInt ddof=1)
148 {
149  return sqrt (partialVariances (array, collapseAxes, means, ddof));
150 }
151 template<class T> inline Array<T> partialAvdevs (const Array<T>& array,
152  const IPosition& collapseAxes)
153 {
154  return partialAvdevs (array, collapseAxes,
155  partialMeans (array, collapseAxes));
156 }
157 template<class T> Array<T> partialAvdevs (const Array<T>& array,
158  const IPosition& collapseAxes,
159  const Array<T>& means);
160 template<class T> Array<T> partialRmss (const Array<T>& array,
161  const IPosition& collapseAxes);
162 template<class T> Array<T> partialMedians (const Array<T>& array,
163  const IPosition& collapseAxes,
164  Bool takeEvenMean=False,
165  Bool inPlace=False);
166 template<class T> Array<T> partialMadfms (const Array<T>& array,
167  const IPosition& collapseAxes,
168  Bool takeEvenMean=False,
169  Bool inPlace=False);
170 template<class T> Array<T> partialFractiles (const Array<T>& array,
171  const IPosition& collapseAxes,
172  Float fraction,
173  Bool inPlace=False);
174 template<class T> Array<T> partialInterFractileRanges (const Array<T>& array,
175  const IPosition& collapseAxes,
176  Float fraction,
177  Bool inPlace=False);
179  const IPosition& collapseAxes,
180  Bool inPlace=False)
181  { return partialInterFractileRanges (array, collapseAxes, 1./6., inPlace); }
183  const IPosition& collapseAxes,
184  Bool inPlace=False)
185  { return partialInterFractileRanges (array, collapseAxes, 0.25, inPlace); }
186 // </group>
187 
188 
189 
190  // Define functors to perform a reduction function on an Array object.
191  // Use virtual functions instead of templates to avoid code bloat
192  // in partialArrayMath, etc.
193  template<typename T> class SumFunc : public ArrayFunctorBase<T> {
194  public:
195  virtual ~SumFunc() {}
196  virtual T operator() (const Array<T>& arr) const { return sum(arr); }
197  };
198  template<typename T> class SumSqrFunc : public ArrayFunctorBase<T> {
199  public:
200  virtual ~SumSqrFunc() {}
201  virtual T operator() (const Array<T>& arr) const { return sumsqr(arr); }
202  };
203  template<typename T> class ProductFunc : public ArrayFunctorBase<T> {
204  public:
205  virtual ~ProductFunc() {}
206  virtual T operator() (const Array<T>& arr) const { return product(arr); }
207  };
208  template<typename T> class MinFunc : public ArrayFunctorBase<T> {
209  public:
210  virtual ~MinFunc() {}
211  virtual T operator() (const Array<T>& arr) const { return min(arr); }
212  };
213  template<typename T> class MaxFunc : public ArrayFunctorBase<T> {
214  public:
215  virtual ~MaxFunc() {}
216  virtual T operator() (const Array<T>& arr) const { return max(arr); }
217  };
218  template<typename T> class MeanFunc : public ArrayFunctorBase<T> {
219  public:
220  virtual ~MeanFunc() {}
221  virtual T operator() (const Array<T>& arr) const { return mean(arr); }
222  };
223  template<typename T> class VarianceFunc : public ArrayFunctorBase<T> {
224  public:
225  explicit VarianceFunc (uInt ddof)
226  : itsDdof(ddof) {}
227  virtual ~VarianceFunc() {}
228  virtual T operator() (const Array<T>& arr) const { return pvariance(arr, itsDdof); }
229  private:
231  };
232  template<typename T> class StddevFunc : public ArrayFunctorBase<T> {
233  public:
234  explicit StddevFunc (uInt ddof)
235  : itsDdof(ddof) {}
236  virtual ~StddevFunc() {}
237  virtual T operator() (const Array<T>& arr) const { return pstddev(arr, itsDdof); }
238  private:
240  };
241  template<typename T> class AvdevFunc : public ArrayFunctorBase<T> {
242  public:
243  virtual ~AvdevFunc() {}
244  virtual T operator() (const Array<T>& arr) const { return avdev(arr); }
245  };
246  template<typename T> class RmsFunc : public ArrayFunctorBase<T> {
247  public:
248  virtual ~RmsFunc() {}
249  virtual T operator() (const Array<T>& arr) const { return rms(arr); }
250  };
251  template<typename T> class MedianFunc : public ArrayFunctorBase<T> {
252  public:
253  explicit MedianFunc (Bool sorted=False, Bool takeEvenMean=True,
254  Bool inPlace = False)
255  : itsSorted(sorted), itsTakeEvenMean(takeEvenMean), itsInPlace(inPlace) {}
256  virtual ~MedianFunc() {}
257  virtual T operator() (const Array<T>& arr) const
258  { return median(arr, itsTmp, itsSorted, itsTakeEvenMean, itsInPlace); }
259  private:
263  mutable Block<T> itsTmp;
264  };
265  template<typename T> class MadfmFunc : public ArrayFunctorBase<T> {
266  public:
267  explicit MadfmFunc(Bool sorted = False, Bool takeEvenMean = True,
268  Bool inPlace = False)
269  : itsSorted(sorted), itsTakeEvenMean(takeEvenMean), itsInPlace(inPlace) {}
270  virtual ~MadfmFunc() {}
271  virtual T operator()(const Array<T>& arr) const
272  { return madfm(arr, itsTmp, itsSorted, itsTakeEvenMean, itsInPlace); }
273  private:
278  };
279  template<typename T> class FractileFunc : public ArrayFunctorBase<T> {
280  public:
281  explicit FractileFunc (Float fraction,
282  Bool sorted = False, Bool inPlace = False)
283  : itsFraction(fraction), itsSorted(sorted), itsInPlace(inPlace) {}
284  virtual ~FractileFunc() {}
285  virtual T operator() (const Array<T>& arr) const
286  { return fractile(arr, itsTmp, itsFraction, itsSorted, itsInPlace); }
287  private:
288  float itsFraction;
291  mutable Block<T> itsTmp;
292  };
293  template<typename T> class InterFractileRangeFunc {
294  public:
295  explicit InterFractileRangeFunc(Float fraction,
296  Bool sorted = False, Bool inPlace = False)
297  : itsFraction(fraction), itsSorted(sorted), itsInPlace(inPlace) {}
299  virtual T operator()(const Array<T>& arr) const
300  { return interFractileRange(arr, itsTmp, itsFraction,
301  itsSorted, itsInPlace); }
302  private:
303  float itsFraction;
307  };
308  template<typename T> class InterHexileRangeFunc: public InterFractileRangeFunc<T> {
309  public:
310  explicit InterHexileRangeFunc(Bool sorted = False, Bool inPlace = False)
311  : InterFractileRangeFunc<T> (1./6., sorted, inPlace)
312  {}
314  };
315  template<typename T> class InterQuartileRangeFunc: public InterFractileRangeFunc<T> {
316  public:
317  explicit InterQuartileRangeFunc(Bool sorted = False, Bool inPlace = False)
318  : InterFractileRangeFunc<T> (0.25, sorted, inPlace)
319  {}
321  };
322 
323 
324 
325  // Do partial reduction of an Array object. I.e., perform the operation
326  // on a subset of the array axes (the collapse axes).
327  template<typename T>
329  const IPosition& collapseAxes,
330  const ArrayFunctorBase<T>& funcObj)
331  {
332  Array<T> res;
333  partialArrayMath (res, a, collapseAxes, funcObj);
334  return res;
335  }
336  template<typename T, typename RES>
337  void partialArrayMath (Array<RES>& res,
338  const Array<T>& a,
339  const IPosition& collapseAxes,
340  const ArrayFunctorBase<T,RES>& funcObj);
341 
342 
343 // Apply the given ArrayMath reduction function objects
344 // to each box in the array.
345 // <example>
346 // Downsample an array by taking the median of every [25,25] elements.
347 // <srcblock>
348 // Array<Float> downArr = boxedArrayMath(in, IPosition(2,25,25),
349 // MedianFunc<Float>());
350 // </srcblock>
351 // </example>
352 // The dimensionality of the array can be larger than the box; in that
353 // case the missing axes of the box are assumed to have length 1.
354 // A box axis length <= 0 means the full array axis.
355  template<typename T>
356  inline Array<T> boxedArrayMath (const Array<T>& a,
357  const IPosition& boxSize,
358  const ArrayFunctorBase<T>& funcObj)
359  {
360  Array<T> res;
361  boxedArrayMath (res, a, boxSize, funcObj);
362  return res;
363  }
364  template<typename T, typename RES>
365  void boxedArrayMath (Array<RES>&,
366  const Array<T>& array,
367  const IPosition& boxSize,
368  const ArrayFunctorBase<T,RES>& funcObj);
369 
370 // Apply for each element in the array the given ArrayMath reduction function
371 // object to the box around that element. The full box is 2*halfBoxSize + 1.
372 // It can be used for arrays and boxes of any dimensionality; missing
373 // halfBoxSize values are set to 0.
374 // <example>
375 // Determine for each element in the array the median of a box
376 // with size [51,51] around that element:
377 // <srcblock>
378 // Array<Float> medians = slidingArrayMath(in, IPosition(2,25,25),
379 // MedianFunc<Float>());
380 // </srcblock>
381 // This is a potentially expensive operation. On a high-end PC it took
382 // appr. 27 seconds to get the medians for an array of [1000,1000] using
383 // a halfBoxSize of [50,50].
384 // </example>
385 // <br>The fillEdge argument determines how the edge is filled where
386 // no full boxes can be made. True means it is set to zero; False means
387 // that the edge is removed, thus the output array is smaller than the
388 // input array.
389 // <note> This brute-force method of determining the medians outperforms
390 // all kinds of smart implementations. For a vector it is about as fast
391 // as class <linkto class=MedianSlider>MedianSlider</linkto>, for a 2D array
392 // it is much, much faster.
393 // </note>
394  template<typename T>
396  const IPosition& halfBoxSize,
397  const ArrayFunctorBase<T>& funcObj,
398  Bool fillEdge=True)
399  {
400  Array<T> res;
401  slidingArrayMath (res, a, halfBoxSize, funcObj, fillEdge);
402  return res;
403  }
404  template<typename T, typename RES>
405  void slidingArrayMath (Array<RES>& res,
406  const Array<T>& array,
407  const IPosition& halfBoxSize,
408  const ArrayFunctorBase<T,RES>& funcObj,
409  Bool fillEdge=True);
410 
411 // </group>
412 
413 // <group>
414 // Helper functions for boxed and sliding functions.
415 // Determine full box shape and shape of result for a boxed operation.
416 void fillBoxedShape (const IPosition& shape, const IPosition& boxShape,
417  IPosition& fullBoxShape, IPosition& resultShape);
418 // Determine the box end and shape of result for a sliding operation.
419 // It returns False if the result is empty.
420 Bool fillSlidingShape (const IPosition& shape, const IPosition& halfBoxSize,
421  IPosition& boxEnd, IPosition& resultShape);
422 // </group>
423 
424 } //# NAMESPACE CASACORE - END
425 
426 #ifndef CASACORE_NO_AUTO_TEMPLATES
427 #include <casacore/casa/Arrays/ArrayPartMath.tcc>
428 #endif //# CASACORE_NO_AUTO_TEMPLATES
429 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
Define functors to perform a reduction function on an Array object.
TableExprNode means(const TableExprNode &array, const TableExprNodeSet &collapseAxes)
Definition: ExprNode.h:1699
LatticeExprNode median(const LatticeExprNode &expr)
TableExprNode array(const TableExprNode &values, const TableExprNodeSet &shape)
Create an array of the given shape and fill it with the values.
Definition: ExprNode.h:1886
LatticeExprNode sum(const LatticeExprNode &expr)
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
Array< T > partialInterQuartileRanges(const Array< T > &array, const IPosition &collapseAxes, Bool inPlace=False)
LatticeExprNode fractile(const LatticeExprNode &expr, const LatticeExprNode &fraction)
Determine the value of the element at the part fraction from the beginning of the given lattice...
InterFractileRangeFunc(Float fraction, Bool sorted=False, Bool inPlace=False)
void fillBoxedShape(const IPosition &shape, const IPosition &boxShape, IPosition &fullBoxShape, IPosition &resultShape)
Helper functions for boxed and sliding functions.
Array< T > partialVariances(const Array< T > &array, const IPosition &collapseAxes, uInt ddof=1)
MedianFunc(Bool sorted=False, Bool takeEvenMean=True, Bool inPlace=False)
Array< T > partialStddevs(const Array< T > &array, const IPosition &collapseAxes, uInt ddof=1)
LatticeExprNode min(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode avdev(const LatticeExprNode &expr)
Array< T > boxedArrayMath(const Array< T > &a, const IPosition &boxSize, const ArrayFunctorBase< T > &funcObj)
Apply the given ArrayMath reduction function objects to each box in the array.
Array< T > partialArrayMath(const Array< T > &a, const IPosition &collapseAxes, const ArrayFunctorBase< T > &funcObj)
Do partial reduction of an Array object.
Bool fillSlidingShape(const IPosition &shape, const IPosition &halfBoxSize, IPosition &boxEnd, IPosition &resultShape)
Determine the box end and shape of result for a sliding operation.
LatticeExprNode sqrt(const LatticeExprNode &expr)
Basic class for math on Array objects.
Definition: ArrayMathBase.h:57
TableExprNode product(const TableExprNode &array)
Definition: ExprNode.h:1607
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
MaskedArray< T > boxedArrayMath(const MaskedArray< T > &array, const IPosition &boxSize, const FuncType &funcObj)
Apply the given ArrayMath reduction function objects to each box in the array.
float Float
Definition: aipstype.h:54
MadfmFunc(Bool sorted=False, Bool takeEvenMean=True, Bool inPlace=False)
const Bool False
Definition: aipstype.h:44
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape...
Definition: ExprNode.h:1944
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
FractileFunc(Float fraction, Bool sorted=False, Bool inPlace=False)
simple 1-D array
Array< T > partialAvdevs(const Array< T > &array, const IPosition &collapseAxes)
virtual casacore::Bool operator()(Flux< casacore::Double > &value, Flux< casacore::Double > &error, const casacore::MFrequency &mfreq, const casacore::Bool updatecoeffs)=0
Array< T > slidingArrayMath(const Array< T > &a, const IPosition &halfBoxSize, const ArrayFunctorBase< T > &funcObj, Bool fillEdge=True)
Apply for each element in the array the given ArrayMath reduction function object to the box around t...
LatticeExprNode mean(const LatticeExprNode &expr)
TableExprNode rms(const TableExprNode &array)
Definition: ExprNode.h:1637
Array< T > slidingArrayMath(const MaskedArray< T > &array, const IPosition &halfBoxSize, const FuncType &funcObj, Bool fillEdge=True)
Apply for each element in the array the given ArrayMath reduction function object to the box around t...
Array< T > partialInterHexileRanges(const Array< T > &array, const IPosition &collapseAxes, Bool inPlace=False)
const Bool True
Definition: aipstype.h:43
unsigned int uInt
Definition: aipstype.h:51
Array< T > partialStddevs(const Array< T > &array, const IPosition &collapseAxes, const Array< T > &means, uInt ddof=1)
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42