casacore
MArrayMath.h
Go to the documentation of this file.
1 //# MArrayMath.h: Mathematical operations on MArray objects
2 //# Copyright (C) 2012
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: MArrayMath.h 21262 2012-09-07 12:38:36Z gervandiepen $
27 
28 #ifndef CASA_MARRAYMATH_H
29 #define CASA_MARRAYMATH_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/tables/TaQL/MArray.h>
34 #include <casacore/tables/TaQL/MArrayMathBase.h>
35 #include <casacore/casa/Arrays/ArrayPartMath.h>
36 #include <casacore/casa/Arrays/ArrayIter.h>
37 #include <casacore/casa/BasicMath/Functors.h>
38 #include <casacore/casa/Exceptions/Error.h>
39 #include <casacore/casa/Utilities/Assert.h>
40 #include <casacore/casa/Containers/Block.h>
41 
42 namespace casacore {
43 
44  // <summary>
45  // Mathematical operations for MArray objects.
46  // </summary>
47  //
48  // <reviewed reviewer="UNKNOWN" date="" tests="tMArrayMath">
49  //
50  // <prerequisite>
51  // <li> <linkto class=MArray>MArray</linkto>
52  // </prerequisite>
53  //
54  // <synopsis>
55  // These functions perform element by element mathematical operations on
56  // optionally masked arrays and/or scalars.
57  // If two arrays are used, the arrays must conform, except for allEQ which
58  // returns False if the arrays do not conform.
59  //
60  // The functions in this file can be divided in 3 groups:
61  // <ul>
62  // <li> Full array operations like ==, near, etc.
63  // They are defined for array-array and array-scalar operations. Arrays
64  // shapes have to be conformant. They operate on all elements
65  // (also the masked ones). The result is an MArray with the same
66  // shape as the input array(s). It will have a mask if one of the
67  // operands has a mask. If both operands have a mask, the resulting
68  // mask is the OR of both masks.
69  // <li> Full reduction functions like ntrue, all, allEQ, etc.
70  // They operate on the unmasked elements only. If there are no unmasked
71  // elements, the results is 0 or True.
72  // <li> Reduction functions working on unmasked elements in parts of the
73  // input array. The result is an MArray that has a mask if the input
74  // array has a mask. An output element is masked off if its input
75  // part has no unmasked elements.
76  // The functors defined at the beginning of this file are used to
77  // operate on each part.
78  // There are 3 flavours:
79  // <ul>
80  // <li> partialXXX reduces one or more axes. E.g. one can count the
81  // number of True elements for particular array axes.
82  // The result is an array with a lower dimensionality.
83  // They can be seen as a special versions of the boxedXXX functions.
84  // <li> slidingXXX operates in a sliding window over the array. So the
85  // result is an array with the same shape as the input, although
86  // the output array is smaller if the edge is not filled.
87  // <li> boxedXXX divides the input array in boxes with the given size
88  // and operates on each box. The result is an array with the same
89  // dimensionality, but with a smaller size.
90  // If the box size does not fit integrally, the edge box is smaller.
91  // </ul>
92  // </ul>
93  // </synopsis>
94  //
95  // <group name="MArray mathematical operations">
96 
97  // Define functors to perform a reduction function on an MArray object.
98  // <group>
99  template<typename T> class MSumFunc : public MArrayFunctorBase<T> {
100  public:
101  virtual ~MSumFunc() {}
102  T operator() (const MArray<T>& arr) const { return sum(arr); }
103  };
104  template<typename T> class MSumSqrFunc : public MArrayFunctorBase<T> {
105  public:
106  virtual ~MSumSqrFunc() {}
107  T operator() (const MArray<T>& arr) const { return sumsqr(arr); }
108  };
109  template<typename T> class MProductFunc : public MArrayFunctorBase<T> {
110  public:
111  virtual ~MProductFunc() {}
112  T operator() (const MArray<T>& arr) const { return product(arr); }
113  };
114  template<typename T> class MMinFunc : public MArrayFunctorBase<T> {
115  public:
116  virtual ~MMinFunc() {}
117  T operator() (const MArray<T>& arr) const { return min(arr); }
118  };
119  template<typename T> class MMaxFunc : public MArrayFunctorBase<T> {
120  public:
121  virtual ~MMaxFunc() {}
122  T operator() (const MArray<T>& arr) const { return max(arr); }
123  };
124  template<typename T> class MMeanFunc : public MArrayFunctorBase<T> {
125  public:
126  virtual ~MMeanFunc() {}
127  T operator() (const MArray<T>& arr) const { return mean(arr); }
128  };
129  template<typename T> class MVarianceFunc : public MArrayFunctorBase<T> {
130  public:
131  explicit MVarianceFunc(uInt ddof=0)
132  : itsDdof(ddof) {}
133  virtual ~MVarianceFunc() {}
134  T operator() (const MArray<T>& arr) const { return variance(arr, itsDdof); }
135  private:
137  };
138  template<typename T> class MStddevFunc : public MArrayFunctorBase<T> {
139  public:
140  explicit MStddevFunc(uInt ddof=0)
141  : itsDdof(ddof) {}
143  T operator() (const MArray<T>& arr) const { return stddev(arr, itsDdof); }
144  private:
146  };
147  template<typename T> class MAvdevFunc : public MArrayFunctorBase<T> {
148  public:
149  virtual ~MAvdevFunc() {}
150  T operator() (const MArray<T>& arr) const { return avdev(arr); }
151  };
152  template<typename T> class MRmsFunc : public MArrayFunctorBase<T> {
153  public:
154  virtual ~MRmsFunc() {}
155  T operator() (const MArray<T>& arr) const { return rms(arr); }
156  };
157  template<typename T> class MMedianFunc : public MArrayFunctorBase<T> {
158  public:
159  explicit MMedianFunc (Bool sorted=False, Bool takeEvenMean=True,
160  Bool inPlace = False)
161  : itsSorted(sorted), itsTakeEvenMean(takeEvenMean), itsInPlace(inPlace) {}
162  virtual ~MMedianFunc() {}
163  T operator() (const MArray<T>& arr) const
164  { return median(arr, itsSorted, itsTakeEvenMean, itsInPlace); }
165  private:
169  };
170  template<typename T> class MFractileFunc : public MArrayFunctorBase<T> {
171  public:
172  explicit MFractileFunc (Float fraction,
173  Bool sorted = False, Bool inPlace = False)
174  : itsFraction(fraction), itsSorted(sorted), itsInPlace(inPlace) {}
175  virtual ~MFractileFunc() {}
176  T operator() (const MArray<T>& arr) const
177  { return fractile(arr, itsFraction, itsSorted, itsInPlace); }
178  private:
179  float itsFraction;
182  };
183 
184 
185  // Do partial reduction of an MArray object. I.e., perform the operation
186  // on a subset of the array axes (the collapse axes).
187  template<typename T>
189  const IPosition& collapseAxes,
190  const MArrayFunctorBase<T>& funcObj)
191  {
192  MArray<T> res;
193  partialArrayMath (res, a, collapseAxes, funcObj);
194  return res;
195  }
196  template<typename T, typename RES>
198  const MArray<T>& a,
199  const IPosition& collapseAxes,
200  const MArrayFunctorBase<T,RES>& funcObj)
201  {
203  // This can also be done as boxedArrayMath with a removeDegenerate thereafter.
204  //
205  // It should be possible to parallelize this loop.
206  // Determine nr of iteration steps and iterate over that as an int.
207  // Do not use Array slicing, because that is not thread-safe.
208  // Instead create ArraySTLIterator directly from Array and blc,trc,
209  // so funcObj should accept iterators instead of Array.
210  // However, ArraySTLIterator needs the sliced array, not original.
211  // Maybe keep ref of itsSteps in iterator instead of array.
212  // Hmm, tricky for median and fractile.
213  // Better to make Array copy ctor thread-safe (thus use boost shared_ptr).
214  ReadOnlyArrayIterator<T> aiter(a.array(), collapseAxes);
215  ReadOnlyArrayIterator<Bool> miter(a.mask(), collapseAxes);
216  IPosition shape(a.array().shape().removeAxes (collapseAxes));
217  /*
218  Int64 nr = 1;
219  for (uInt i=0; i<collapseAxes.size(); ++i) {
220  nr *= a.array().shape()[collapseAxes[i]];
221  }
223  for (Int64 i=0; i<nr; ++i) {
224  IPosition pos = findPos(i);
225  IPosition endPos = pos + cursorShape - 1;
226  *data[pos] = funcObj(MArray<T>(a.array()(pos,endPos), a.mask()(pos,endpos)));
227  }
228  */
230  res.resize (shape, False);
231  Array<Bool> resMask(shape);
232  RES* data = res.array().data();
233  Bool* mask = resMask.data();
234  while (!aiter.pastEnd()) {
235  if (allTrue(miter.array())) {
236  *mask++ = True;
237  *data++ = RES();
238  } else {
239  *mask++ = False;
240  *data++ = funcObj(MArray<T> (aiter.array(), miter.array()));
241  }
242  aiter.next();
243  miter.next();
244  }
245  res.setMask (resMask);
246  }
247  // </group>
248 
249 
250  template<typename T>
252  const IPosition& boxShape,
253  const MArrayFunctorBase<T>& funcObj)
254  {
255  MArray<T> res;
256  boxedArrayMath (res, a, boxShape, funcObj);
257  return res;
258  }
259  template<typename T, typename RES>
261  const MArray<T>& array,
262  const IPosition& boxShape,
263  const MArrayFunctorBase<T,RES>& funcObj)
264  {
265  AlwaysAssert (array.hasMask(), AipsError);
266  const IPosition& shape = array.shape();
267  uInt ndim = shape.size();
268  IPosition fullBoxShape, resShape;
269  fillBoxedShape (shape, boxShape, fullBoxShape, resShape);
270  res.resize (resShape, False);
271  Array<Bool> resMask(resShape);
272  RES* data = res.array().data();
273  Bool* mask = resMask.data();
274  // Loop through all data and assemble as needed.
275  IPosition blc(ndim, 0);
276  IPosition trc(fullBoxShape-1);
277  while (True) {
278  Array<Bool> subMask (array.mask()(blc,trc));
279  if (allTrue(subMask)) {
280  *data++ = RES();
281  *mask++ = True;
282  } else {
283  *data++ = funcObj (MArray<T>(array.array()(blc,trc), subMask));
284  *mask++ = False;
285  }
286  uInt ax;
287  for (ax=0; ax<ndim; ++ax) {
288  blc[ax] += fullBoxShape[ax];
289  if (blc[ax] < shape[ax]) {
290  trc[ax] += fullBoxShape[ax];
291  if (trc[ax] >= shape[ax]) {
292  trc[ax] = shape[ax]-1;
293  }
294  break;
295  }
296  blc[ax] = 0;
297  trc[ax] = fullBoxShape[ax]-1;
298  }
299  if (ax == ndim) {
300  break;
301  }
302  }
303  res.setMask (resMask);
304  }
305 
306  template <typename T>
308  const IPosition& halfBoxShape,
309  const MArrayFunctorBase<T>& funcObj,
310  Bool fillEdge=True)
311  {
312  MArray<T> res;
313  slidingArrayMath (res, array, halfBoxShape, funcObj, fillEdge);
314  return res;
315  }
316  template <typename T, typename RES>
318  const MArray<T>& array,
319  const IPosition& halfBoxShape,
320  const MArrayFunctorBase<T,RES>& funcObj,
321  Bool fillEdge=True)
322  {
323  AlwaysAssert (array.hasMask(), AipsError);
324  const IPosition& shape = array.shape();
325  uInt ndim = shape.size();
326  IPosition boxEnd, resShape;
327  Bool empty = fillSlidingShape (shape, halfBoxShape, boxEnd, resShape);
328  if (fillEdge) {
329  res.resize (shape, False);
330  res.array() = RES();
332  res.setMask (mask);
333  } else {
334  res.resize (resShape, True);
335  }
336  if (!empty) {
337  Array<RES> resa (res.array());
338  Array<Bool> resm (res.mask());
339  if (fillEdge) {
340  IPosition boxEnd2 (boxEnd/2);
341  resa.reference (resa(boxEnd2, resShape+boxEnd2-1));
342  resm.reference (resm(boxEnd2, resShape+boxEnd2-1));
343  }
344  typename Array<RES>::iterator iterarr(resa.begin());
345  typename Array<Bool>::iterator itermask(resm.begin());
346  // Loop through all data and assemble as needed.
347  IPosition blc(ndim, 0);
348  IPosition trc(boxEnd);
349  IPosition pos(ndim, 0);
350  while (True) {
351  Array<Bool> subMask (array.mask()(blc,trc));
352  if (allTrue(subMask)) {
353  *iterarr = RES();
354  *itermask = True;
355  } else {
356  *iterarr = funcObj (MArray<T>(array.array()(blc,trc), subMask));
357  *itermask = False;
358  }
359  ++iterarr;
360  ++itermask;
361  uInt ax;
362  for (ax=0; ax<ndim; ++ax) {
363  if (++pos[ax] < resShape[ax]) {
364  blc[ax]++;
365  trc[ax]++;
366  break;
367  }
368  pos(ax) = 0;
369  blc[ax] = 0;
370  trc[ax] = boxEnd[ax];
371  }
372  if (ax == ndim) {
373  break;
374  }
375  }
376  }
377  }
378 
379 
380  // Add, subtract, etc. 2 arrays or array and scalar.
381  // <group>
382  template<typename T>
383  MArray<T> operator+ (const MArray<T>& left, const MArray<T>& right)
384  { return (left.isNull() || right.isNull() ? MArray<T>() :
385  MArray<T> (left.array() + right.array(),
386  left.combineMask(right))); }
387 
388  template<typename T>
389  MArray<T> operator- (const MArray<T>& left, const MArray<T>& right)
390  { return (left.isNull() || right.isNull() ? MArray<T>() :
391  MArray<T> (left.array() - right.array(),
392  left.combineMask(right))); }
393 
394  template<typename T>
395  MArray<T> operator* (const MArray<T>& left, const MArray<T>& right)
396  { return (left.isNull() || right.isNull() ? MArray<T>() :
397  MArray<T> (left.array() * right.array(),
398  left.combineMask(right))); }
399 
400  template<typename T>
401  MArray<T> operator/ (const MArray<T>& left, const MArray<T>& right)
402  { return (left.isNull() || right.isNull() ? MArray<T>() :
403  MArray<T> (left.array() / right.array(),
404  left.combineMask(right))); }
405 
406  template<typename T>
407  MArray<T> operator% (const MArray<T>& left, const MArray<T>& right)
408  { return (left.isNull() || right.isNull() ? MArray<T>() :
409  MArray<T> (left.array() % right.array(),
410  left.combineMask(right))); }
411 
412  template<typename T>
413  MArray<T> operator& (const MArray<T>& left, const MArray<T>& right)
414  { return (left.isNull() || right.isNull() ? MArray<T>() :
415  MArray<T> (left.array() & right.array(),
416  left.combineMask(right))); }
417 
418  template<typename T>
419  MArray<T> operator| (const MArray<T>& left, const MArray<T>& right)
420  { return (left.isNull() || right.isNull() ? MArray<T>() :
421  MArray<T> (left.array() | right.array(),
422  left.combineMask(right))); }
423 
424  template<typename T>
425  MArray<T> operator^ (const MArray<T>& left, const MArray<T>& right)
426  { return (left.isNull() || right.isNull() ? MArray<T>() :
427  MArray<T> (left.array() ^ right.array(),
428  left.combineMask(right))); }
429 
430  template<typename T>
431  MArray<T> operator+ (const MArray<T>& left, const T& right)
432  { return MArray<T> (left.array() + right, left); }
433 
434  template<typename T>
435  MArray<T> operator- (const MArray<T>& left, const T& right)
436  { return MArray<T> (left.array() - right, left); }
437 
438  template<typename T>
439  MArray<T> operator* (const MArray<T>& left, const T& right)
440  { return MArray<T> (left.array() * right, left); }
441 
442  template<typename T>
443  MArray<T> operator/ (const MArray<T>& left, const T& right)
444  { return MArray<T> (left.array() / right, left); }
445 
446  template<typename T>
447  MArray<T> operator% (const MArray<T>& left, const T& right)
448  { return MArray<T> (left.array() % right, left); }
449 
450  template<typename T>
451  MArray<T> operator& (const MArray<T>& left, const T& right)
452  { return MArray<T> (left.array() & right, left); }
453 
454  template<typename T>
455  MArray<T> operator| (const MArray<T>& left, const T& right)
456  { return MArray<T> (left.array() | right, left); }
457 
458  template<typename T>
459  MArray<T> operator^ (const MArray<T>& left, const T& right)
460  { return MArray<T> (left.array() ^ right, left); }
461 
462  template<typename T>
463  MArray<T> operator+ (const T& left, const MArray<T>& right)
464  { return MArray<T> (left + right.array(), right); }
465 
466  template<typename T>
467  MArray<T> operator- (const T& left, const MArray<T>& right)
468  { return MArray<T> (left - right.array(), right); }
469 
470  template<typename T>
471  MArray<T> operator* (const T& left, const MArray<T>& right)
472  { return MArray<T> (left * right.array(), right); }
473 
474  template<typename T>
475  MArray<T> operator/ (const T& left, const MArray<T>& right)
476  { return MArray<T> (left / right.array(), right); }
477 
478  template<typename T>
479  MArray<T> operator% (const T& left, const MArray<T>& right)
480  { return MArray<T> (left % right.array(), right); }
481 
482  template<typename T>
483  MArray<T> operator& (const T& left, const MArray<T>& right)
484  { return MArray<T> (left & right.array(), right); }
485 
486  template<typename T>
487  MArray<T> operator| (const T& left, const MArray<T>& right)
488  { return MArray<T> (left | right.array(), right); }
489 
490  template<typename T>
491  MArray<T> operator^ (const T& left, const MArray<T>& right)
492  { return MArray<T> (left ^ right.array(), right); }
493  // </group>
494 
495  // Negate the elements in an array.
496  template<typename T>
498  { return MArray<T> (-a.array(), a); }
499 
500  // Take the complement of the elements in an array.
501  template<typename T>
502  MArray<T> operator~ (const MArray<T>& a)
503  { return MArray<T> (~a.array(), a); }
504 
505  // Perform mathematical function on each element in an array.
506  // <group>
507  template<typename T>
509  { return MArray<T> (sin(a.array()), a); }
510 
511  template<typename T>
513  { return MArray<T> (cos(a.array()), a); }
514 
515  template<typename T>
517  { return MArray<T> (tan(a.array()), a); }
518 
519  template<typename T>
521  { return MArray<T> (sinh(a.array()), a); }
522 
523  template<typename T>
525  { return MArray<T> (cosh(a.array()), a); }
526 
527  template<typename T>
529  { return MArray<T> (tanh(a.array()), a); }
530 
531  template<typename T>
533  { return MArray<T> (asin(a.array()), a); }
534 
535  template<typename T>
537  { return MArray<T> (acos(a.array()), a); }
538 
539  template<typename T>
541  { return MArray<T> (atan(a.array()), a); }
542 
543  template<typename T>
544  MArray<T> atan2(const MArray<T>& left, const MArray<T>& right)
545  { return (left.isNull() || right.isNull() ? MArray<T>() :
546  MArray<T> (atan2(left.array(), right.array()),
547  left.combineMask(right))); }
548 
549  template<typename T>
550  MArray<T> atan2(const MArray<T>& left, const T& right)
551  { return MArray<T> (atan2(left.array(), right), left); }
552 
553  template<typename T>
554  MArray<T> atan2(const T& left, const MArray<T>& right)
555  { return MArray<T> (atan2(left, right.array()), right); }
556 
557  template<typename T>
559  { return MArray<T> (exp(a.array()), a); }
560 
561  template<typename T>
563  { return MArray<T> (log(a.array()), a); }
564 
565  template<typename T>
567  { return MArray<T> (log10(a.array()), a); }
568 
569  template<typename T>
571  { return MArray<T> (sqrt(a.array()), a); }
572 
573  template<typename T>
575  { return MArray<T> (square(a.array()), a); }
576 
577  template<typename T>
579  { return MArray<T> (cube(a.array()), a); }
580 
581  template<typename T>
582  MArray<T> pow(const MArray<T>& a, const MArray<T>& exp)
583  { return (a.isNull() || exp.isNull() ? MArray<T>() :
584  MArray<T> (pow(a.array(), exp.array()),
585  a.combineMask(exp))); }
586 
587  template<typename T>
588  MArray<T> pow(const T& a, const MArray<T>& exp)
589  { return MArray<T> (pow(a, exp.array()), exp); }
590 
591  template<typename T>
592  MArray<T> pow(const MArray<T>& a, const Double& exp)
593  { return MArray<T> (pow(a.array(), exp), a); }
594 
595  template<typename T>
596  MArray<T> min(const MArray<T>& left, const MArray<T>& right)
597  { return (left.isNull() || right.isNull() ? MArray<T>() :
598  MArray<T> (min(left.array(), right.array()),
599  left.combineMask(right))); }
600 
601  template<typename T>
602  MArray<T> min(const MArray<T>& left, const T& right)
603  { return MArray<T> (min(left.array(), right), left); }
604 
605  template<typename T>
606  MArray<T> min(const T& left, const MArray<T>& right)
607  { return MArray<T> (min(left, right.array()), right); }
608 
609  template<typename T>
610  MArray<T> max(const MArray<T>& left, const MArray<T>& right)
611  { return (left.isNull() || right.isNull() ? MArray<T>() :
612  MArray<T> (max(left.array(), right.array()),
613  left.combineMask(right))); }
614 
615  template<typename T>
616  MArray<T> max(const MArray<T>& left, const T& right)
617  { return MArray<T> (max(left.array(), right), left); }
618 
619  template<typename T>
620  MArray<T> max(const T& left, const MArray<T>& right)
621  { return MArray<T> (max(left, right.array()), right); }
622 
623  template<typename T>
625  { return MArray<T> (ceil(a.array()), a); }
626 
627  template<typename T>
629  { return MArray<T> (floor(a.array()), a); }
630 
631  template<typename T>
633  { return MArray<T> (round(a.array()), a); }
634 
635  template<typename T>
637  { return MArray<T> (sign(a.array()), a); }
638 
639  template<typename T>
641  { return MArray<T> (abs(a.array()), a); }
642 
643  template<typename T>
645  { return MArray<T> (fabs(a.array()), a); }
646 
647  template<typename T>
648  MArray<T> fmod(const MArray<T>& left, const MArray<T>& right)
649  { return (left.isNull() || right.isNull() ? MArray<T>() :
650  MArray<T> (fmod(left.array(), right.array()),
651  left.combineMask(right))); }
652 
653  template<typename T>
654  MArray<T> fmod(const MArray<T>& left, const T& right)
655  { return MArray<T> (fmod(left.array(), right), left); }
656 
657  template<typename T>
658  MArray<T> fmod(const T& left, const MArray<T>& right)
659  { return MArray<T> (fmod(left, right.array()), right); }
660 
661  template<typename T>
662  MArray<T> floormod(const MArray<T>& left, const MArray<T>& right)
663  { return (left.isNull() || right.isNull() ? MArray<T>() :
664  MArray<T> (floormod(left.array(), right.array()),
665  left.combineMask(right))); }
666 
667  template<typename T>
668  MArray<T> floormod(const MArray<T>& left, const T& right)
669  { return MArray<T> (floormod(left.array(), right), left); }
670 
671  template<typename T>
672  MArray<T> floormod(const T& left, const MArray<T>& right)
673  { return MArray<T> (floormod(left, right.array()), right); }
674 
675  template<typename T>
677  { return MArray<T> (conj(arr.array()), arr); }
678 
679  inline MArray<Float> real(const MArray<Complex> &arr)
680  { return MArray<Float> (real(arr.array()), arr); }
681 
682  inline MArray<Float> imag(const MArray<Complex> &arr)
683  { return MArray<Float> (imag(arr.array()), arr); }
684 
686  { return MArray<Float> (amplitude(arr.array()), arr); }
687 
689  { return MArray<Float> (phase(arr.array()), arr); }
690 
692  { return MArray<Double> (real(arr.array()), arr); }
693 
695  { return MArray<Double> (imag(arr.array()), arr); }
696 
698  { return MArray<Double> (amplitude(arr.array()), arr); }
699 
701  { return MArray<Double> (phase(arr.array()), arr); }
702  // </group>
703 
704 
705  // Reduce an array to a scalar using the unmasked elements only.
706  // The result is 0 if there are no unmasked elements.
707  // <group>
708  template<typename T>
709  T sum(const MArray<T>& a)
710  {
711  if (a.hasMask()) {
712  return a.array().contiguousStorage() && a.mask().contiguousStorage() ?
713  accumulateMasked<T>(a.array().cbegin(), a.array().cend(),
714  a.mask().cbegin(), T(), std::plus<T>()) :
715  accumulateMasked<T>(a.array().begin(), a.array().end(),
716  a.mask().begin(), T(), std::plus<T>());
717  }
718  return sum(a.array());
719  }
720 
721  template<typename T>
722  T sumsqr(const MArray<T>& a)
723  {
724  if (a.hasMask()) {
725  return a.array().contiguousStorage() && a.mask().contiguousStorage() ?
726  accumulateMasked<T>(a.array().cbegin(), a.array().cend(),
727  a.mask().cbegin(), T(), SumSqr<T>()) :
728  accumulateMasked<T>(a.array().begin(), a.array().end(),
729  a.mask().begin(), T(), SumSqr<T>());
730  }
731  return sumsqr(a.array());
732  }
733 
734  template<typename T>
735  T product(const MArray<T>& a)
736  {
737  if (a.hasMask()) {
738  return a.array().contiguousStorage() && a.mask().contiguousStorage() ?
739  accumulateMasked<T>(a.array().cbegin(), a.array().cend(),
740  a.mask().cbegin(), std::multiplies<T>()) :
741  accumulateMasked<T>(a.array().begin(), a.array().end(),
742  a.mask().begin(), std::multiplies<T>());
743  }
744  return product(a.array());
745  }
746 
747  template<typename T>
748  T min(const MArray<T>& a)
749  {
750  if (a.hasMask()) {
751  return a.array().contiguousStorage() && a.mask().contiguousStorage() ?
752  accumulateMasked<T>(a.array().cbegin(), a.array().cend(),
753  a.mask().cbegin(), Min<T>()) :
754  accumulateMasked<T>(a.array().begin(), a.array().end(),
755  a.mask().begin(), Min<T>());
756  }
757  return min(a.array());
758  }
759 
760  template<typename T>
761  T max(const MArray<T>& a)
762  {
763  if (a.hasMask()) {
764  return a.array().contiguousStorage() && a.mask().contiguousStorage() ?
765  accumulateMasked<T>(a.array().cbegin(), a.array().cend(),
766  a.mask().cbegin(), Max<T>()) :
767  accumulateMasked<T>(a.array().begin(), a.array().end(),
768  a.mask().begin(), Max<T>());
769  }
770  return max(a.array());
771  }
772 
773  template<typename T>
774  T mean(const MArray<T>& a)
775  {
776  Int64 nv = a.nvalid();
777  if (nv == 0) return T();
778  if (! a.hasMask()) return mean(a.array());
779  return T(sum(a) / (1.0*nv));
780  }
781 
782  template<typename T>
783  T variance(const MArray<T>& a, T mean, uInt ddof)
784  {
785  Int64 nv = a.nvalid();
786  if (nv < ddof+1) return T();
787  if (! a.hasMask()) return pvariance(a.array(), mean, ddof);
788  T sum = a.array().contiguousStorage() && a.mask().contiguousStorage() ?
789  accumulateMasked<T>(a.array().cbegin(), a.array().cend(),
790  a.mask().cbegin(), T(), SumSqrDiff<T>(mean)) :
791  accumulateMasked<T>(a.array().begin(), a.array().end(),
792  a.mask().begin(), T(), SumSqrDiff<T>(mean));
793  return T(sum / (1.0*nv - ddof));
794  }
795 
796  template<typename T>
797  T variance(const MArray<T>& a, uInt ddof)
798  {
799  return variance(a, mean(a), ddof);
800  }
801 
802  template<typename T>
803  T stddev(const MArray<T>& a, uInt ddof)
804  {
805  return sqrt(variance(a, ddof));
806  }
807 
808  template<typename T>
809  T stddev(const MArray<T>& a, T mean, uInt ddof)
810  {
811  return sqrt(variance(a, mean, ddof));
812  }
813 
814  template<typename T>
815  T avdev(const MArray<T>& a, T mean)
816  {
817  Int64 nv = a.nvalid();
818  if (nv == 0) return T();
819  if (! a.hasMask()) return avdev(a.array(), mean);
820  T sum = a.array().contiguousStorage() && a.mask().contiguousStorage() ?
821  accumulateMasked<T>(a.array().cbegin(), a.array().cend(),
822  a.mask().cbegin(), T(), SumAbsDiff<T>(mean)) :
823  accumulateMasked<T>(a.array().begin(), a.array().end(),
824  a.mask().begin(), T(), SumAbsDiff<T>(mean));
825  return T(sum / (1.0*nv));
826  }
827 
828  template<typename T>
829  T avdev(const MArray<T>& a)
830  {
831  return avdev(a, mean(a));
832  }
833 
834  template<typename T>
835  T rms(const MArray<T>& a)
836  {
837  Int64 nv = a.nvalid();
838  if (nv == 0) return T();
839  if (! a.hasMask()) return rms(a.array());
840  T sum = a.array().contiguousStorage() && a.mask().contiguousStorage() ?
841  accumulateMasked<T>(a.array().cbegin(), a.array().cend(),
842  a.mask().cbegin(), T(), SumSqr<T>()) :
843  accumulateMasked<T>(a.array().begin(), a.array().end(),
844  a.mask().begin(), T(), SumSqr<T>());
845  return T(sqrt(sum / (1.0*nv)));
846  }
847 
848  template<typename T>
849  T median(const MArray<T> &a, Bool sorted, Bool takeEvenMean,
850  Bool inPlace=False)
851  {
852  // The normal median function needs at least one element, so shortcut.
853  if (a.empty()) return T();
854  if (! a.hasMask()) return median(a.array(), sorted, takeEvenMean, inPlace);
855  Block<T> buf(a.size());
856  Int64 nv = a.flatten (buf.storage(), buf.size());
857  if (nv == 0) return T();
858  Array<T> arr(IPosition(1, nv), buf.storage(), SHARE);
859  // Median can be taken in place.
860  return median (arr, sorted, takeEvenMean, True);
861  }
862  template<typename T>
863  inline T median(const MArray<T> &a)
864  { return median (a, False, (a.size() <= 100), False); }
865  template<typename T>
866  inline T median(const MArray<T> &a, Bool sorted)
867  { return median (a, sorted, (a.nelements() <= 100), False); }
868  template<typename T>
869  inline T medianInPlace(const MArray<T> &a, Bool sorted = False)
870  { return median (a, sorted, (a.nelements() <= 100), True); }
871 
872  // Return the fractile of an array.
873  // It returns the value at the given fraction of the array.
874  // A fraction of 0.5 is the same as the median, be it that no mean of
875  // the two middle elements is taken if the array has an even nr of elements.
876  // It uses kthLargest if the array is not sorted yet.
877  template<typename T>
878  T fractile(const MArray<T> &a, Float fraction, Bool sorted=False,
879  Bool inPlace=False)
880  {
881  // The normal fractile function needs at least one element, so shortcut.
882  if (a.empty()) return T();
883  if (! a.hasMask()) return fractile(a.array(), fraction, sorted, inPlace);
884  Block<T> buf(a.size());
885  Int64 nv = a.flatten (buf.storage(), a.size());
886  if (nv == 0) return T();
887  Array<T> arr(IPosition(1, nv), buf.storage(), SHARE);
888  return fractile (arr, fraction, sorted, True);
889  }
890  // </group>
891 
892  // Get partial sums, etc.
893  // <group>
894  template<typename T>
896  const IPosition& collapseAxes)
897  {
898  if (a.isNull()) {
899  return MArray<T>();
900  } else if (! a.hasMask()) {
901  return MArray<T>(partialSums (a.array(), collapseAxes));
902  }
903  return partialArrayMath (a, collapseAxes, MSumFunc<T>());
904  }
905  template<typename T>
907  const IPosition& collapseAxes)
908  {
909  if (a.isNull()) {
910  return MArray<T>();
911  } else if (! a.hasMask()) {
912  return MArray<T>(partialArrayMath (a.array(), collapseAxes,
913  SumSqrFunc<T>()));
914  }
915  return partialArrayMath (a, collapseAxes, MSumSqrFunc<T>());
916  }
917  template<typename T>
919  const IPosition& collapseAxes)
920  {
921  if (a.isNull()) {
922  return MArray<T>();
923  } else if (! a.hasMask()) {
924  return MArray<T>(partialProducts (a.array(), collapseAxes));
925  }
926  return partialArrayMath (a, collapseAxes, MProductFunc<T>());
927  }
928  template<typename T>
930  const IPosition& collapseAxes)
931  {
932  if (a.isNull()) {
933  return MArray<T>();
934  } else if (! a.hasMask()) {
935  return MArray<T>(partialMins (a.array(), collapseAxes));
936  }
937  return partialArrayMath (a, collapseAxes, MMinFunc<T>());
938  }
939  template<typename T>
941  const IPosition& collapseAxes)
942  {
943  if (a.isNull()) {
944  return MArray<T>();
945  } else if (! a.hasMask()) {
946  return MArray<T>(partialMaxs (a.array(), collapseAxes));
947  }
948  return partialArrayMath (a, collapseAxes, MMaxFunc<T>());
949  }
950  template<typename T>
952  const IPosition& collapseAxes)
953  {
954  if (a.isNull()) {
955  return MArray<T>();
956  } else if (! a.hasMask()) {
957  return MArray<T>(partialMeans (a.array(), collapseAxes));
958  }
959  return partialArrayMath (a, collapseAxes, MMeanFunc<T>());
960  }
961  template<typename T>
963  const IPosition& collapseAxes,
964  uInt ddof)
965  {
966  if (a.isNull()) {
967  return MArray<T>();
968  } else if (! a.hasMask()) {
969  return MArray<T>(partialVariances (a.array(), collapseAxes, ddof));
970  }
971  return partialArrayMath (a, collapseAxes, MVarianceFunc<T>(ddof));
972  }
973  template<typename T>
975  const IPosition& collapseAxes,
976  uInt ddof)
977  {
978  if (a.isNull()) {
979  return MArray<T>();
980  } else if (! a.hasMask()) {
981  return MArray<T>(partialStddevs (a.array(), collapseAxes, ddof));
982  }
983  return partialArrayMath (a, collapseAxes, MStddevFunc<T>(ddof));
984  }
985  template<typename T>
987  const IPosition& collapseAxes)
988  {
989  if (a.isNull()) {
990  return MArray<T>();
991  } else if (! a.hasMask()) {
992  return MArray<T>(partialAvdevs (a.array(), collapseAxes));
993  }
994  return partialArrayMath (a, collapseAxes, MAvdevFunc<T>());
995  }
996  template<typename T>
998  const IPosition& collapseAxes)
999  {
1000  if (a.isNull()) {
1001  return MArray<T>();
1002  } else if (! a.hasMask()) {
1003  return MArray<T>(partialRmss (a.array(), collapseAxes));
1004  }
1005  return partialArrayMath (a, collapseAxes, MRmsFunc<T>());
1006  }
1007  template<typename T>
1009  const IPosition& collapseAxes,
1010  Bool takeEvenMean=False,
1011  Bool inPlace=False)
1012  {
1013  if (a.isNull()) {
1014  return MArray<T>();
1015  } else if (! a.hasMask()) {
1016  return MArray<T>(partialMedians (a.array(), collapseAxes,
1017  takeEvenMean, inPlace));
1018  }
1019  return partialArrayMath (a, collapseAxes,
1020  MMedianFunc<T>(False, takeEvenMean, inPlace));
1021  }
1022  template<typename T>
1024  const IPosition& collapseAxes,
1025  Float fraction,
1026  Bool inPlace=False)
1027  {
1028  if (a.isNull()) {
1029  return MArray<T>();
1030  } else if (! a.hasMask()) {
1031  return MArray<T>(partialFractiles (a.array(), collapseAxes,
1032  fraction, inPlace));
1033  }
1034  return partialArrayMath (a, collapseAxes,
1035  MFractileFunc<T>(fraction, False, inPlace));
1036  }
1037  // </group>
1038 
1039  // Get sliding sums.
1040  // <group>
1041  template<typename T>
1043  const IPosition& halfBoxSize, Bool fillEdge=True)
1044  {
1045  if (a.isNull()) {
1046  return MArray<T>();
1047  } else if (! a.hasMask()) {
1048  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1049  SumFunc<T>(), fillEdge));
1050  }
1051  return slidingArrayMath (a, halfBoxSize, MSumFunc<T>(), fillEdge);
1052  }
1053  template<typename T>
1055  const IPosition& halfBoxSize, Bool fillEdge=True)
1056  {
1057  if (a.isNull()) {
1058  return MArray<T>();
1059  } else if (! a.hasMask()) {
1060  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1061  SumSqrFunc<T>(), fillEdge));
1062  }
1063  return slidingArrayMath (a, halfBoxSize, MSumSqrFunc<T>(), fillEdge);
1064  }
1065  template<typename T>
1067  const IPosition& halfBoxSize, Bool fillEdge=True)
1068  {
1069  if (a.isNull()) {
1070  return MArray<T>();
1071  } else if (! a.hasMask()) {
1072  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1073  ProductFunc<T>(), fillEdge));
1074  }
1075  return slidingArrayMath (a, halfBoxSize, MProductFunc<T>(), fillEdge);
1076  }
1077  template<typename T>
1079  const IPosition& halfBoxSize, Bool fillEdge=True)
1080  {
1081  if (a.isNull()) {
1082  return MArray<T>();
1083  } else if (! a.hasMask()) {
1084  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1085  MinFunc<T>(), fillEdge));
1086  }
1087  return slidingArrayMath (a, halfBoxSize, MMinFunc<T>(), fillEdge);
1088  }
1089  template<typename T>
1091  const IPosition& halfBoxSize, Bool fillEdge=True)
1092  {
1093  if (a.isNull()) {
1094  return MArray<T>();
1095  } else if (! a.hasMask()) {
1096  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1097  MaxFunc<T>(), fillEdge));
1098  }
1099  return slidingArrayMath (a, halfBoxSize, MMaxFunc<T>(), fillEdge);
1100  }
1101  template<typename T>
1103  const IPosition& halfBoxSize, Bool fillEdge=True)
1104  {
1105  if (a.isNull()) {
1106  return MArray<T>();
1107  } else if (! a.hasMask()) {
1108  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1109  MeanFunc<T>(), fillEdge));
1110  }
1111  return slidingArrayMath (a, halfBoxSize, MMeanFunc<T>(), fillEdge);
1112  }
1113  template<typename T>
1115  const IPosition& halfBoxSize,
1116  uInt ddof,
1117  Bool fillEdge=True)
1118  {
1119  if (a.isNull()) {
1120  return MArray<T>();
1121  } else if (! a.hasMask()) {
1122  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1123  VarianceFunc<T>(ddof), fillEdge));
1124  }
1125  return slidingArrayMath (a, halfBoxSize, MVarianceFunc<T>(ddof), fillEdge);
1126  }
1127  template<typename T>
1129  const IPosition& halfBoxSize,
1130  uInt ddof,
1131  Bool fillEdge=True)
1132  {
1133  if (a.isNull()) {
1134  return MArray<T>();
1135  } else if (! a.hasMask()) {
1136  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1137  StddevFunc<T>(ddof), fillEdge));
1138  }
1139  return slidingArrayMath (a, halfBoxSize, MStddevFunc<T>(ddof), fillEdge);
1140  }
1141  template<typename T>
1143  const IPosition& halfBoxSize, Bool fillEdge=True)
1144  {
1145  if (a.isNull()) {
1146  return MArray<T>();
1147  } else if (! a.hasMask()) {
1148  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1149  AvdevFunc<T>(), fillEdge));
1150  }
1151  return slidingArrayMath (a, halfBoxSize, MAvdevFunc<T>(), fillEdge);
1152  }
1153  template<typename T>
1155  const IPosition& halfBoxSize, Bool fillEdge=True)
1156  {
1157  if (a.isNull()) {
1158  return MArray<T>();
1159  } else if (! a.hasMask()) {
1160  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1161  RmsFunc<T>(), fillEdge));
1162  }
1163  return slidingArrayMath (a, halfBoxSize, MRmsFunc<T>(), fillEdge);
1164  }
1165  template<typename T>
1167  const IPosition& halfBoxSize,
1168  Bool takeEvenMean=False,
1169  Bool inPlace=False,
1170  Bool fillEdge=True)
1171  {
1172  if (a.isNull()) {
1173  return MArray<T>();
1174  } else if (! a.hasMask()) {
1175  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1176  MedianFunc<T>(False, takeEvenMean,
1177  inPlace),
1178  fillEdge));
1179  }
1180  return slidingArrayMath (a, halfBoxSize,
1181  MMedianFunc<T>(False, takeEvenMean, inPlace),
1182  fillEdge);
1183  }
1184  template<typename T>
1186  const IPosition& halfBoxSize,
1187  Float fraction,
1188  Bool inPlace=False,
1189  Bool fillEdge=True)
1190  {
1191  if (a.isNull()) {
1192  return MArray<T>();
1193  } else if (! a.hasMask()) {
1194  return MArray<T>(slidingArrayMath (a.array(), halfBoxSize,
1195  FractileFunc<T>(fraction, False,
1196  inPlace),
1197  fillEdge));
1198  }
1199  return slidingArrayMath (a, halfBoxSize,
1200  MFractileFunc<T>(fraction, False, inPlace),
1201  fillEdge);
1202  }
1203  // </group>
1204 
1205  // Get boxed sums.
1206  // <group>
1207  template<typename T>
1209  const IPosition& boxSize)
1210  {
1211  if (a.isNull()) {
1212  return MArray<T>();
1213  } else if (! a.hasMask()) {
1214  return MArray<T>(boxedArrayMath (a.array(), boxSize, SumFunc<T>()));
1215  }
1216  return boxedArrayMath (a, boxSize, MSumFunc<T>());
1217  }
1218  template<typename T>
1220  const IPosition& boxSize)
1221  {
1222  if (a.isNull()) {
1223  return MArray<T>();
1224  } else if (! a.hasMask()) {
1225  return MArray<T>(boxedArrayMath (a.array(), boxSize, SumSqrFunc<T>()));
1226  }
1227  return boxedArrayMath (a, boxSize, MSumSqrFunc<T>());
1228  }
1229  template<typename T>
1231  const IPosition& boxSize)
1232  {
1233  if (a.isNull()) {
1234  return MArray<T>();
1235  } else if (! a.hasMask()) {
1236  return MArray<T>(boxedArrayMath (a.array(), boxSize, ProductFunc<T>()));
1237  }
1238  return boxedArrayMath (a, boxSize, MProductFunc<T>());
1239  }
1240  template<typename T>
1242  const IPosition& boxSize)
1243  {
1244  if (a.isNull()) {
1245  return MArray<T>();
1246  } else if (! a.hasMask()) {
1247  return MArray<T>(boxedArrayMath (a.array(), boxSize, MinFunc<T>()));
1248  }
1249  return boxedArrayMath (a, boxSize, MMinFunc<T>());
1250  }
1251  template<typename T>
1253  const IPosition& boxSize)
1254  {
1255  if (a.isNull()) {
1256  return MArray<T>();
1257  } else if (! a.hasMask()) {
1258  return MArray<T>(boxedArrayMath (a.array(), boxSize, MaxFunc<T>()));
1259  }
1260  return boxedArrayMath (a, boxSize, MMaxFunc<T>());
1261  }
1262  template<typename T>
1264  const IPosition& boxSize)
1265  {
1266  if (a.isNull()) {
1267  return MArray<T>();
1268  } else if (! a.hasMask()) {
1269  return MArray<T>(boxedArrayMath (a.array(), boxSize, MeanFunc<T>()));
1270  }
1271  return boxedArrayMath (a, boxSize, MMeanFunc<T>());
1272  }
1273  template<typename T>
1275  const IPosition& boxSize,
1276  uInt ddof)
1277  {
1278  if (a.isNull()) {
1279  return MArray<T>();
1280  } else if (! a.hasMask()) {
1281  return MArray<T>(boxedArrayMath (a.array(), boxSize, VarianceFunc<T>(ddof)));
1282  }
1283  return boxedArrayMath (a, boxSize, MVarianceFunc<T>(ddof));
1284  }
1285  template<typename T>
1287  const IPosition& boxSize,
1288  uInt ddof)
1289  {
1290  if (a.isNull()) {
1291  return MArray<T>();
1292  } else if (! a.hasMask()) {
1293  return MArray<T>(boxedArrayMath (a.array(), boxSize, StddevFunc<T>(ddof)));
1294  }
1295  return boxedArrayMath (a, boxSize, MStddevFunc<T>(ddof));
1296  }
1297  template<typename T>
1299  const IPosition& boxSize)
1300  {
1301  if (a.isNull()) {
1302  return MArray<T>();
1303  } else if (! a.hasMask()) {
1304  return MArray<T>(boxedArrayMath (a.array(), boxSize, AvdevFunc<T>()));
1305  }
1306  return boxedArrayMath (a, boxSize, MAvdevFunc<T>());
1307  }
1308  template<typename T>
1310  const IPosition& boxSize)
1311  {
1312  if (a.isNull()) {
1313  return MArray<T>();
1314  } else if (! a.hasMask()) {
1315  return MArray<T>(boxedArrayMath (a.array(), boxSize, RmsFunc<T>()));
1316  }
1317  return boxedArrayMath (a, boxSize, MRmsFunc<T>());
1318  }
1319  template<typename T>
1321  const IPosition& boxSize,
1322  Bool takeEvenMean=False,
1323  Bool inPlace=False)
1324  {
1325  if (a.isNull()) {
1326  return MArray<T>();
1327  } else if (! a.hasMask()) {
1328  return MArray<T>(boxedArrayMath (a.array(), boxSize,
1329  MedianFunc<T>(False, takeEvenMean,
1330  inPlace)));
1331  }
1332  return boxedArrayMath (a, boxSize,
1333  MMedianFunc<T>(False, takeEvenMean, inPlace));
1334  }
1335  template<typename T>
1337  const IPosition& boxSize,
1338  Float fraction,
1339  Bool inPlace=False)
1340  {
1341  if (a.isNull()) {
1342  return MArray<T>();
1343  } else if (! a.hasMask()) {
1344  return MArray<T>(boxedArrayMath (a.array(), boxSize,
1345  FractileFunc<T>(fraction, False,
1346  inPlace)));
1347  }
1348  return boxedArrayMath (a, boxSize,
1349  MFractileFunc<T>(fraction, False, inPlace));
1350  }
1351  // </group>
1352 
1353  // </group>
1354 
1355 } //# end namespace
1356 
1357 #endif
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
Definition: Assert.h:157
const IPosition & shape() const
The length of each axis.
Definition: ArrayBase.h:125
bool contiguousStorage() const
Are the array data contiguous? If they are not contiguous, getStorage (see below) needs to make a cop...
Definition: ArrayBase.h:116
virtual void reference(const Array< T, Alloc > &other)
After invocation, this array and other reference the same storage.
contiter cbegin()
Get the begin iterator object for a contiguous array.
Definition: Array.h:871
contiter cend()
Definition: Array.h:875
iterator begin()
Get the begin iterator object for any array.
Definition: Array.h:859
T * data()
Get a pointer to the beginning of the array.
Definition: Array.h:604
iterator end()
Definition: Array.h:863
simple 1-D array
Definition: Block.h:200
size_t size() const
Definition: Block.h:612
T * storage()
If you really, really, need a "raw" pointer to the beginning of the storage area this will give it to...
Definition: Block.h:605
IPosition removeAxes(const IPosition &axes) const
Return an IPosition where the given axes are reoved.
Int64 nvalid() const
Return the number of valid array values, thus unflagged elements.
Definition: MArrayBase.h:132
Bool empty() const
Is the array empty?
Definition: MArrayBase.h:139
Array< Bool > combineMask(const MArrayBase &other) const
Combine this and the other mask.
size_t nelements() const
Definition: MArrayBase.h:154
Bool isNull() const
Is the array null?
Definition: MArrayBase.h:111
void setMask(const Array< Bool > &mask)
Set the mask.
size_t size() const
Get the size.
Definition: MArrayBase.h:152
const Array< Bool > & mask() const
Get the mask.
Definition: MArrayBase.h:126
Bool hasMask() const
Is there a mask?
Definition: MArrayBase.h:119
MFractileFunc(Float fraction, Bool sorted=False, Bool inPlace=False)
Definition: MArrayMath.h:172
MMedianFunc(Bool sorted=False, Bool takeEvenMean=True, Bool inPlace=False)
Definition: MArrayMath.h:159
Define functors to perform a reduction function on an MArray object.
Definition: MArrayMath.h:99
Vector< T > flatten() const
Flatten the unmasked elements of the array to a vector.
Definition: MArray.h:184
void resize(const IPosition &shape, Bool useMask)
Resize the array and optionally the mask.
Definition: MArray.h:122
const Array< T > & array() const
Get access to the array.
Definition: MArray.h:153
Iterate a const Array cursor through a const Array.
Definition: ArrayIter.h:172
const Array< T, Alloc > & array()
Return the cursor.
Definition: ArrayIter.h:204
void next()
Move the cursor to the next position.
Definition: ArrayIter.h:184
@ SHARE
Share means that the Array will just use the pointer (no copy), however the Array will NOT delete it ...
Definition: ArrayBase.h:62
int floormod(int x, int y)
this file contains all the compiler specific defines
Definition: mainpage.dox:28
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.
const Bool False
Definition: aipstype.h:44
LatticeExprNode exp(const LatticeExprNode &expr)
TableExprNode operator|(const TableExprNode &left, const TableExprNode &right)
Definition: ExprNode.h:1186
LatticeExprNode asin(const LatticeExprNode &expr)
LatticeExprNode fmod(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode acos(const LatticeExprNode &expr)
LatticeExprNode ndim(const LatticeExprNode &expr)
1-argument function to get the dimensionality of a lattice.
LatticeExprNode mean(const LatticeExprNode &expr)
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode cosh(const LatticeExprNode &expr)
LatticeExprNode atan(const LatticeExprNode &expr)
TableExprNode operator&(const TableExprNode &left, const TableExprNode &right)
Definition: ExprNode.h:1181
LatticeExprNode tanh(const LatticeExprNode &expr)
LatticeExprNode sign(const LatticeExprNode &expr)
LatticeExprNode log10(const LatticeExprNode &expr)
LatticeExprNode conj(const LatticeExprNode &expr)
LatticeExprNode operator%(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode sinh(const LatticeExprNode &expr)
LatticeExprNode sum(const LatticeExprNode &expr)
LatticeExprNode operator+(const LatticeExprNode &expr)
Global functions operating on a LatticeExprNode.
MVBaseline operator*(const RotMatrix &left, const MVBaseline &right)
Rotate a Baseline vector with rotation matrix and other multiplications.
unsigned int uInt
Definition: aipstype.h:51
LatticeExprNode stddev(const LatticeExprNode &expr)
LatticeExprNode min(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode abs(const LatticeExprNode &expr)
Numerical 1-argument functions which result in a real number regardless of input expression type.
TableExprNode amplitude(const TableExprNode &node)
The amplitude (i.e.
Definition: ExprNode.h:1440
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.
TableExprNode phase(const TableExprNode &node)
The phase (i.e.
Definition: ExprNode.h:1448
LatticeExprNode operator-(const LatticeExprNode &expr)
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
Definition: ExprNode.h:1987
LatticeExprNode tan(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:1929
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
LatticeExprNode sin(const LatticeExprNode &expr)
Numerical 1-argument functions.
LatticeExprNode operator/(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode atan2(const LatticeExprNode &left, const LatticeExprNode &right)
Numerical 2-argument functions.
LatticeExprNode variance(const LatticeExprNode &expr)
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
TableExprNode square(const TableExprNode &node)
Definition: ExprNode.h:1346
LatticeExprNode sqrt(const LatticeExprNode &expr)
float Float
Definition: aipstype.h:54
T product(const TableVector< T > &tv)
Definition: TabVecMath.h:385
TableExprNode cube(const TableExprNode &node)
Definition: ExprNode.h:1351
LatticeExprNode avdev(const LatticeExprNode &expr)
LatticeExprNode pow(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode log(const LatticeExprNode &expr)
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...
bool fillSlidingShape(const IPosition &shape, const IPosition &halfBoxSize, IPosition &boxEnd, IPosition &resultShape)
Determine the box end and shape of result for a sliding operation.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
LatticeExprNode operator^(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode cos(const LatticeExprNode &expr)
LatticeExprNode floor(const LatticeExprNode &expr)
const Bool True
Definition: aipstype.h:43
double Double
Definition: aipstype.h:55
LatticeExprNode median(const LatticeExprNode &expr)
LatticeExprNode round(const LatticeExprNode &expr)
LatticeExprNode ceil(const LatticeExprNode &expr)
LatticeExprNode real(const LatticeExprNode &expr)
LatticeExprNode imag(const LatticeExprNode &expr)
void fillBoxedShape(const IPosition &shape, const IPosition &boxShape, IPosition &fullBoxShape, IPosition &resultShape)
Helper functions for boxed and sliding functions.
TableExprNode rms(const TableExprNode &array)
Definition: ExprNode.h:1680
MArray< Double > real(const MArray< DComplex > &arr)
Definition: MArrayMath.h:691
MArray< Float > real(const MArray< Complex > &arr)
Definition: MArrayMath.h:679
MArray< T > partialSumSqrs(const MArray< T > &a, const IPosition &collapseAxes)
Definition: MArrayMath.h:906
MArray< T > pow(const MArray< T > &a, const MArray< T > &exp)
Definition: MArrayMath.h:582
MArray< T > max(const T &left, const MArray< T > &right)
Definition: MArrayMath.h:620
MArray< Double > amplitude(const MArray< DComplex > &arr)
Definition: MArrayMath.h:697
MArray< T > slidingVariances(const MArray< T > &a, const IPosition &halfBoxSize, uInt ddof, Bool fillEdge=True)
Definition: MArrayMath.h:1114
T medianInPlace(const MArray< T > &a, Bool sorted=False)
Definition: MArrayMath.h:869
MArray< T > floormod(const MArray< T > &left, const T &right)
Definition: MArrayMath.h:668
MArray< T > boxedAvdevs(const MArray< T > &a, const IPosition &boxSize)
Definition: MArrayMath.h:1298
MArray< T > min(const T &left, const MArray< T > &right)
Definition: MArrayMath.h:606
MArray< T > slidingMedians(const MArray< T > &a, const IPosition &halfBoxSize, Bool takeEvenMean=False, Bool inPlace=False, Bool fillEdge=True)
Definition: MArrayMath.h:1166
MArray< Double > phase(const MArray< DComplex > &arr)
Definition: MArrayMath.h:700
MArray< T > partialMins(const MArray< T > &a, const IPosition &collapseAxes)
Definition: MArrayMath.h:929
MArray< T > min(const MArray< T > &left, const MArray< T > &right)
Definition: MArrayMath.h:596
void slidingArrayMath(MArray< RES > &res, const MArray< T > &array, const IPosition &halfBoxShape, const MArrayFunctorBase< T, RES > &funcObj, Bool fillEdge=True)
Definition: MArrayMath.h:317
MArray< T > max(const MArray< T > &left, const MArray< T > &right)
Definition: MArrayMath.h:610
MArray< T > boxedFractiles(const MArray< T > &a, const IPosition &boxSize, Float fraction, Bool inPlace=False)
Definition: MArrayMath.h:1336
MArray< T > partialStddevs(const MArray< T > &a, const IPosition &collapseAxes, uInt ddof)
Definition: MArrayMath.h:974
T sum(const MArray< T > &a)
Reduce an array to a scalar using the unmasked elements only.
Definition: MArrayMath.h:709
MArray< T > slidingFractiles(const MArray< T > &a, const IPosition &halfBoxSize, Float fraction, Bool inPlace=False, Bool fillEdge=True)
Definition: MArrayMath.h:1185
MArray< T > slidingArrayMath(const MArray< T > &array, const IPosition &halfBoxShape, const MArrayFunctorBase< T > &funcObj, Bool fillEdge=True)
Definition: MArrayMath.h:307
MArray< T > partialMaxs(const MArray< T > &a, const IPosition &collapseAxes)
Definition: MArrayMath.h:940
MArray< T > boxedSums(const MArray< T > &a, const IPosition &boxSize)
Get boxed sums.
Definition: MArrayMath.h:1208
MArray< T > slidingMeans(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
Definition: MArrayMath.h:1102
MArray< T > floormod(const T &left, const MArray< T > &right)
Definition: MArrayMath.h:672
MArray< T > atan2(const MArray< T > &left, const T &right)
Definition: MArrayMath.h:550
MArray< Float > phase(const MArray< Complex > &arr)
Definition: MArrayMath.h:688
MArray< T > slidingProducts(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
Definition: MArrayMath.h:1066
MArray< T > slidingMaxs(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
Definition: MArrayMath.h:1090
MArray< T > floormod(const MArray< T > &left, const MArray< T > &right)
Definition: MArrayMath.h:662
MArray< T > boxedRmss(const MArray< T > &a, const IPosition &boxSize)
Definition: MArrayMath.h:1309
MArray< T > fmod(const MArray< T > &left, const T &right)
Definition: MArrayMath.h:654
MArray< T > partialMedians(const MArray< T > &a, const IPosition &collapseAxes, Bool takeEvenMean=False, Bool inPlace=False)
Definition: MArrayMath.h:1008
MArray< T > min(const MArray< T > &left, const T &right)
Definition: MArrayMath.h:602
MArray< T > boxedMeans(const MArray< T > &a, const IPosition &boxSize)
Definition: MArrayMath.h:1263
MArray< Double > imag(const MArray< DComplex > &arr)
Definition: MArrayMath.h:694
MArray< T > slidingAvdevs(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
Definition: MArrayMath.h:1142
MArray< T > boxedSumSqrs(const MArray< T > &a, const IPosition &boxSize)
Definition: MArrayMath.h:1219
MArray< T > slidingSumSqrs(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
Definition: MArrayMath.h:1054
MArray< T > fmod(const MArray< T > &left, const MArray< T > &right)
Definition: MArrayMath.h:648
MArray< T > partialMeans(const MArray< T > &a, const IPosition &collapseAxes)
Definition: MArrayMath.h:951
MArray< T > partialAvdevs(const MArray< T > &a, const IPosition &collapseAxes)
Definition: MArrayMath.h:986
MArray< T > atan2(const MArray< T > &left, const MArray< T > &right)
Definition: MArrayMath.h:544
MArray< T > slidingSums(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
Get sliding sums.
Definition: MArrayMath.h:1042
MArray< Float > imag(const MArray< Complex > &arr)
Definition: MArrayMath.h:682
T variance(const MArray< T > &a, T mean, uInt ddof)
Definition: MArrayMath.h:783
MArray< T > partialRmss(const MArray< T > &a, const IPosition &collapseAxes)
Definition: MArrayMath.h:997
MArray< T > pow(const T &a, const MArray< T > &exp)
Definition: MArrayMath.h:588
MArray< T > partialFractiles(const MArray< T > &a, const IPosition &collapseAxes, Float fraction, Bool inPlace=False)
Definition: MArrayMath.h:1023
MArray< T > fmod(const T &left, const MArray< T > &right)
Definition: MArrayMath.h:658
MArray< T > boxedMedians(const MArray< T > &a, const IPosition &boxSize, Bool takeEvenMean=False, Bool inPlace=False)
Definition: MArrayMath.h:1320
MArray< T > sin(const MArray< T > &a)
Perform mathematical function on each element in an array.
Definition: MArrayMath.h:508
MArray< T > boxedStddevs(const MArray< T > &a, const IPosition &boxSize, uInt ddof)
Definition: MArrayMath.h:1286
MArray< T > partialVariances(const MArray< T > &a, const IPosition &collapseAxes, uInt ddof)
Definition: MArrayMath.h:962
MArray< T > partialArrayMath(const MArray< T > &a, const IPosition &collapseAxes, const MArrayFunctorBase< T > &funcObj)
Do partial reduction of an MArray object.
Definition: MArrayMath.h:188
MArray< T > slidingMins(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
Definition: MArrayMath.h:1078
MArray< T > atan2(const T &left, const MArray< T > &right)
Definition: MArrayMath.h:554
MArray< Float > amplitude(const MArray< Complex > &arr)
Definition: MArrayMath.h:685
MArray< T > slidingStddevs(const MArray< T > &a, const IPosition &halfBoxSize, uInt ddof, Bool fillEdge=True)
Definition: MArrayMath.h:1128
MArray< T > boxedVariances(const MArray< T > &a, const IPosition &boxSize, uInt ddof)
Definition: MArrayMath.h:1274
MArray< T > max(const MArray< T > &left, const T &right)
Definition: MArrayMath.h:616
MArray< T > boxedArrayMath(const MArray< T > &a, const IPosition &boxShape, const MArrayFunctorBase< T > &funcObj)
Definition: MArrayMath.h:251
MArray< T > boxedMins(const MArray< T > &a, const IPosition &boxSize)
Definition: MArrayMath.h:1241
void boxedArrayMath(MArray< RES > &res, const MArray< T > &array, const IPosition &boxShape, const MArrayFunctorBase< T, RES > &funcObj)
Definition: MArrayMath.h:260
T fractile(const MArray< T > &a, Float fraction, Bool sorted=False, Bool inPlace=False)
Return the fractile of an array.
Definition: MArrayMath.h:878
MArray< T > pow(const MArray< T > &a, const Double &exp)
Definition: MArrayMath.h:592
MArray< T > partialSums(const MArray< T > &a, const IPosition &collapseAxes)
Get partial sums, etc.
Definition: MArrayMath.h:895
MArray< T > boxedMaxs(const MArray< T > &a, const IPosition &boxSize)
Definition: MArrayMath.h:1252
void partialArrayMath(MArray< RES > &res, const MArray< T > &a, const IPosition &collapseAxes, const MArrayFunctorBase< T, RES > &funcObj)
Definition: MArrayMath.h:197
MArray< T > slidingRmss(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
Definition: MArrayMath.h:1154
T median(const MArray< T > &a, Bool sorted, Bool takeEvenMean, Bool inPlace=False)
Definition: MArrayMath.h:849
MArray< T > boxedProducts(const MArray< T > &a, const IPosition &boxSize)
Definition: MArrayMath.h:1230
MArray< T > partialProducts(const MArray< T > &a, const IPosition &collapseAxes)
Definition: MArrayMath.h:918
Functor to get maximum of two values.
Definition: Functors.h:590
Functor to get minimum of two values.
Definition: Functors.h:582
Functor to add absolute diff of right and base value to left.
Definition: Functors.h:636
Functor to add squared diff of right and base value to left.
Definition: Functors.h:608
Functor to add square of right to left.
Definition: Functors.h:598