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