26#ifndef CASA_MARRAYMATH_H
27#define CASA_MARRAYMATH_H
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>
97 template<
typename T>
class MSumFunc :
public MArrayFunctorBase<T> {
102 template<
typename T>
class MSumSqrFunc :
public MArrayFunctorBase<T> {
105 T operator() (
const MArray<T>& arr)
const {
return sumsqr(arr); }
107 template<
typename T>
class MProductFunc :
public MArrayFunctorBase<T> {
112 template<
typename T>
class MMinFunc :
public MArrayFunctorBase<T> {
117 template<
typename T>
class MMaxFunc :
public MArrayFunctorBase<T> {
122 template<
typename T>
class MMeanFunc :
public MArrayFunctorBase<T> {
136 template<
typename T>
class MStddevFunc :
public MArrayFunctorBase<T> {
145 template<
typename T>
class MAvdevFunc :
public MArrayFunctorBase<T> {
150 template<
typename T>
class MRmsFunc :
public MArrayFunctorBase<T> {
155 template<
typename T>
class MMedianFunc :
public MArrayFunctorBase<T> {
158 Bool inPlace = False)
159 : itsSorted(sorted), itsTakeEvenMean(takeEvenMean), itsInPlace(inPlace) {}
162 {
return median(arr, itsSorted, itsTakeEvenMean, itsInPlace); }
171 Bool sorted = False,
Bool inPlace = False)
172 : itsFraction(fraction), itsSorted(sorted), itsInPlace(inPlace) {}
175 {
return fractile(arr, itsFraction, itsSorted, itsInPlace); }
188 const MArrayFunctorBase<T>& funcObj)
191 partialArrayMath (res, a, collapseAxes, funcObj);
194 template<
typename T,
typename RES>
198 const MArrayFunctorBase<T,RES>& funcObj)
221 for (Int64 i=0; i<nr; ++i) {
222 IPosition pos = findPos(i);
223 IPosition endPos = pos + cursorShape - 1;
224 *data[pos] = funcObj(MArray<T>(a.array()(pos,endPos), a.mask()(pos,endpos)));
230 RES* data = res.
array().data();
233 if (allTrue(miter.
array())) {
251 const MArrayFunctorBase<T>& funcObj)
257 template<
typename T,
typename RES>
261 const MArrayFunctorBase<T,RES>& funcObj)
270 RES* data = res.
array().data();
277 if (allTrue(subMask)) {
285 for (ax=0; ax<
ndim; ++ax) {
286 blc[ax] += fullBoxShape[ax];
287 if (blc[ax] <
shape[ax]) {
288 trc[ax] += fullBoxShape[ax];
289 if (trc[ax] >=
shape[ax]) {
290 trc[ax] =
shape[ax]-1;
295 trc[ax] = fullBoxShape[ax]-1;
304 template <
typename T>
307 const MArrayFunctorBase<T>& funcObj,
314 template <
typename T,
typename RES>
318 const MArrayFunctorBase<T,RES>& funcObj,
339 resa.
reference (resa(boxEnd2, resShape+boxEnd2-1));
340 resm.
reference (resm(boxEnd2, resShape+boxEnd2-1));
350 if (allTrue(subMask)) {
360 for (ax=0; ax<
ndim; ++ax) {
361 if (++pos[ax] < resShape[ax]) {
368 trc[ax] = boxEnd[ax];
715 accumulateMasked<T>(a.
array().cbegin(), a.
array().cend(),
717 accumulateMasked<T>(a.
array().begin(), a.
array().end(),
728 accumulateMasked<T>(a.
array().cbegin(), a.
array().cend(),
730 accumulateMasked<T>(a.
array().begin(), a.
array().end(),
733 return sumsqr(a.
array());
741 accumulateMasked<T>(a.
array().cbegin(), a.
array().cend(),
743 accumulateMasked<T>(a.
array().begin(), a.
array().end(),
754 accumulateMasked<T>(a.
array().cbegin(), a.
array().cend(),
756 accumulateMasked<T>(a.
array().begin(), a.
array().end(),
767 accumulateMasked<T>(a.
array().cbegin(), a.
array().cend(),
769 accumulateMasked<T>(a.
array().begin(), a.
array().end(),
779 if (nv == 0)
return T();
781 return T(
sum(a) / (1.0*nv));
788 if (nv < ddof+1)
return T();
791 accumulateMasked<T>(a.
array().cbegin(), a.
array().cend(),
793 accumulateMasked<T>(a.
array().begin(), a.
array().end(),
795 return T(
sum / (1.0*nv - ddof));
820 if (nv == 0)
return T();
823 accumulateMasked<T>(a.
array().cbegin(), a.
array().cend(),
825 accumulateMasked<T>(a.
array().begin(), a.
array().end(),
827 return T(
sum / (1.0*nv));
840 if (nv == 0)
return T();
843 accumulateMasked<T>(a.
array().cbegin(), a.
array().cend(),
845 accumulateMasked<T>(a.
array().begin(), a.
array().end(),
847 return T(
sqrt(
sum / (1.0*nv)));
855 if (a.
empty())
return T();
859 if (nv == 0)
return T();
862 return median (arr, sorted, takeEvenMean,
True);
884 if (a.
empty())
return T();
888 if (nv == 0)
return T();
905 return partialArrayMath (a, collapseAxes,
MSumFunc<T>());
939 return partialArrayMath (a, collapseAxes,
MMinFunc<T>());
950 return partialArrayMath (a, collapseAxes,
MMaxFunc<T>());
961 return partialArrayMath (a, collapseAxes,
MMeanFunc<T>());
971 return MArray<T>(partialVariances (a.
array(), collapseAxes, ddof));
1007 return partialArrayMath (a, collapseAxes,
MRmsFunc<T>());
1009 template<
typename T>
1019 takeEvenMean, inPlace));
1021 return partialArrayMath (a, collapseAxes,
1024 template<
typename T>
1034 fraction, inPlace));
1036 return partialArrayMath (a, collapseAxes,
1043 template<
typename T>
1051 SumFunc<T>(), fillEdge));
1055 template<
typename T>
1063 SumSqrFunc<T>(), fillEdge));
1067 template<
typename T>
1075 ProductFunc<T>(), fillEdge));
1079 template<
typename T>
1087 MinFunc<T>(), fillEdge));
1091 template<
typename T>
1099 MaxFunc<T>(), fillEdge));
1103 template<
typename T>
1111 MeanFunc<T>(), fillEdge));
1115 template<
typename T>
1125 VarianceFunc<T>(ddof), fillEdge));
1129 template<
typename T>
1139 StddevFunc<T>(ddof), fillEdge));
1143 template<
typename T>
1151 AvdevFunc<T>(), fillEdge));
1155 template<
typename T>
1163 RmsFunc<T>(), fillEdge));
1167 template<
typename T>
1178 MedianFunc<T>(
False, takeEvenMean,
1186 template<
typename T>
1197 FractileFunc<T>(fraction,
False,
1209 template<
typename T>
1220 template<
typename T>
1231 template<
typename T>
1242 template<
typename T>
1253 template<
typename T>
1264 template<
typename T>
1275 template<
typename T>
1287 template<
typename T>
1299 template<
typename T>
1310 template<
typename T>
1321 template<
typename T>
1331 MedianFunc<T>(
False, takeEvenMean,
1337 template<
typename T>
1347 FractileFunc<T>(fraction,
False,
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
bool contiguousStorage() const
Are the array data contiguous? If they are not contiguous, getStorage (see below) needs to make a cop...
T * data()
Get a pointer to the beginning of the array.
iterator begin()
Get the begin iterator object for any array.
contiter cbegin()
Get the begin iterator object for a contiguous array.
virtual void reference(const Array< T > &other)
After invocation, this array and other reference the same storage.
T * storage()
If you really, really, need a "raw" pointer to the beginning of the storage area this will give it to...
Int64 nvalid() const
Return the number of valid array values, thus unflagged elements.
Bool empty() const
Is the array empty?
Array< Bool > combineMask(const MArrayBase &other) const
Combine this and the other mask.
Bool isNull() const
Is the array null?
void setMask(const Array< Bool > &mask)
Set the mask.
const Array< Bool > & mask() const
Get the mask.
size_t size() const
Get the size.
Bool hasMask() const
Is there a mask?
T operator()(const MArray< T > &arr) const
MFractileFunc(Float fraction, Bool sorted=False, Bool inPlace=False)
T operator()(const MArray< T > &arr) const
T operator()(const MArray< T > &arr) const
T operator()(const MArray< T > &arr) const
T operator()(const MArray< T > &arr) const
T operator()(const MArray< T > &arr) const
T operator()(const MArray< T > &arr) const
T operator()(const MArray< T > &arr) const
Define functors to perform a reduction function on an MArray object.
T operator()(const MArray< T > &arr) const
T operator()(const MArray< T > &arr) const
MVarianceFunc(uInt ddof=0)
T operator()(const MArray< T > &arr) const
Vector< T > flatten() const
Flatten the unmasked elements of the array to a vector.
const Array< T > & array() const
Get access to the array.
void resize(const IPosition &shape, Bool useMask)
Resize the array and optionally the mask.
Iterate a const Array cursor through a const Array.
const Array< T > & array()
Return the cursor.
void next()
Move the cursor to the next position.
@ SHARE
Share means that the Array will just use the pointer (no copy), however the Array will NOT delete it ...
this file contains all the compiler specific defines
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.
LatticeExprNode exp(const LatticeExprNode &expr)
TableExprNode operator|(const TableExprNode &left, const TableExprNode &right)
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)
TableExprNode operator&(const TableExprNode &left, const TableExprNode &right)
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.
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.
TableExprNode phase(const TableExprNode &node)
The phase (i.e.
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.
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.
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)
TableExprNode square(const TableExprNode &node)
LatticeExprNode sqrt(const LatticeExprNode &expr)
T product(const TableVector< T > &tv)
TableExprNode cube(const TableExprNode &node)
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.
LatticeExprNode operator^(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode cos(const LatticeExprNode &expr)
LatticeExprNode floor(const LatticeExprNode &expr)
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)
MArray< T > sqrt(const MArray< T > &a)
MArray< T > atan2(const MArray< T > &left, const T &right)
MArray< T > floormod(const MArray< T > &left, const T &right)
MArray< T > operator^(const MArray< T > &left, const MArray< T > &right)
T medianInPlace(const MArray< T > &a, Bool sorted=False)
MArray< T > floor(const MArray< T > &a)
MArray< Double > amplitude(const MArray< DComplex > &arr)
MArray< T > fmod(const MArray< T > &left, const T &right)
MArray< T > atan2(const T &left, const MArray< T > &right)
MArray< T > max(const T &left, const MArray< T > &right)
MArray< Double > phase(const MArray< DComplex > &arr)
MArray< T > pow(const MArray< T > &a, const T &exp)
T product(const MArray< T > &a)
MArray< T > partialRmss(const MArray< T > &a, const IPosition &collapseAxes)
MArray< T > tan(const MArray< T > &a)
MArray< T > cube(const MArray< T > &a)
MArray< T > max(const MArray< T > &left, const MArray< T > &right)
void slidingArrayMath(MArray< RES > &res, const MArray< T > &array, const IPosition &halfBoxShape, const MArrayFunctorBase< T, RES > &funcObj, Bool fillEdge=True)
MArray< T > log10(const MArray< T > &a)
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.
T sum(const MArray< T > &a)
Reduce an array to a scalar using the unmasked elements only.
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 > square(const MArray< T > &a)
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 > cosh(const MArray< T > &a)
MArray< T > min(const MArray< T > &left, const T &right)
MArray< Float > phase(const MArray< Complex > &arr)
MArray< T > partialArrayMath(const MArray< T > &a, const IPosition &collapseAxes, const MArrayFunctorBase< T > &funcObj)
Do partial reduction of an MArray object.
MArray< std::complex< T > > pow(const MArray< std::complex< T > > &a, const T &exp)
MArray< T > operator%(const MArray< T > &left, const MArray< T > &right)
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.
MArray< T > operator/(const MArray< T > &left, const MArray< T > &right)
T avdev(const MArray< T > &a)
MArray< T > fmod(const T &left, const MArray< T > &right)
MArray< T > sign(const MArray< T > &a)
MArray< T > boxedArrayMath(const MArray< T > &a, const IPosition &boxShape, const MArrayFunctorBase< T > &funcObj)
MArray< T > abs(const MArray< T > &a)
MArray< T > floormod(const T &left, const MArray< T > &right)
MArray< T > boxedMeans(const MArray< T > &a, const IPosition &boxSize)
MArray< T > cos(const MArray< T > &a)
T rms(const MArray< T > &a)
MArray< T > boxedAvdevs(const MArray< T > &a, const IPosition &boxSize)
MArray< T > operator*(const MArray< T > &left, const MArray< T > &right)
MArray< T > log(const MArray< T > &a)
MArray< T > slidingSumSqrs(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
MArray< T > partialSumSqrs(const MArray< T > &a, const IPosition &collapseAxes)
MArray< T > operator&(const MArray< T > &left, const MArray< T > &right)
MArray< T > slidingArrayMath(const MArray< T > &array, const IPosition &halfBoxShape, const MArrayFunctorBase< T > &funcObj, Bool fillEdge=True)
T variance(const MArray< T > &a, T mean, uInt ddof)
T median(const MArray< T > &a, Bool sorted)
MArray< Double > imag(const MArray< DComplex > &arr)
MArray< T > acos(const MArray< T > &a)
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 > exp(const MArray< T > &a)
MArray< T > fmod(const MArray< T > &left, const MArray< T > &right)
MArray< T > boxedFractiles(const MArray< T > &a, const IPosition &boxSize, Float fraction, Bool inPlace=False)
T max(const MArray< T > &a)
MArray< T > sin(const MArray< T > &a)
Perform mathematical function on each element in an array.
MArray< T > partialMaxs(const MArray< T > &a, const IPosition &collapseAxes)
MArray< Double > real(const MArray< DComplex > &arr)
MArray< T > tanh(const MArray< T > &a)
T avdev(const MArray< T > &a, T mean)
MArray< T > min(const T &left, const MArray< T > &right)
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 > round(const MArray< T > &a)
MArray< T > slidingFractiles(const MArray< T > &a, const IPosition &halfBoxSize, Float fraction, Bool inPlace=False, Bool fillEdge=True)
MArray< T > ceil(const MArray< T > &a)
T min(const MArray< T > &a)
MArray< Float > amplitude(const MArray< Complex > &arr)
MArray< Float > imag(const MArray< Complex > &arr)
MArray< T > slidingStddevs(const MArray< T > &a, const IPosition &halfBoxSize, uInt ddof, Bool fillEdge=True)
MArray< T > partialStddevs(const MArray< T > &a, const IPosition &collapseAxes, uInt ddof)
T stddev(const MArray< T > &a, T mean, uInt ddof)
MArray< T > atan2(const MArray< T > &left, const MArray< T > &right)
MArray< T > asin(const MArray< T > &a)
MArray< T > boxedVariances(const MArray< T > &a, const IPosition &boxSize, uInt ddof)
T sumsqr(const MArray< T > &a)
MArray< T > boxedMaxs(const MArray< T > &a, const IPosition &boxSize)
T mean(const MArray< T > &a)
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 > atan(const MArray< T > &a)
MArray< T > partialMins(const MArray< T > &a, const IPosition &collapseAxes)
MArray< T > max(const MArray< T > &left, const T &right)
T median(const MArray< T > &a)
void boxedArrayMath(MArray< RES > &res, const MArray< T > &array, const IPosition &boxShape, const MArrayFunctorBase< T, RES > &funcObj)
MArray< T > operator-(const MArray< T > &left, const MArray< T > &right)
MArray< T > conj(const MArray< T > &arr)
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)
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.
MArray< T > partialMeans(const MArray< T > &a, const IPosition &collapseAxes)
MArray< T > operator|(const MArray< T > &left, const MArray< T > &right)
MArray< T > pow(const T &a, const MArray< T > &exp)
MArray< T > operator~(const MArray< T > &a)
Take the complement of the elements in an array.
MArray< T > partialVariances(const MArray< T > &a, const IPosition &collapseAxes, uInt ddof)
MArray< T > fabs(const MArray< T > &a)
MArray< T > min(const MArray< T > &left, const MArray< T > &right)
void partialArrayMath(MArray< RES > &res, const MArray< T > &a, const IPosition &collapseAxes, const MArrayFunctorBase< T, RES > &funcObj)
MArray< T > partialProducts(const MArray< T > &a, const IPosition &collapseAxes)
T variance(const MArray< T > &a, uInt ddof)
MArray< T > sinh(const MArray< T > &a)
T stddev(const MArray< T > &a, uInt ddof)
T median(const MArray< T > &a, Bool sorted, Bool takeEvenMean, Bool inPlace=False)
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)
MArray< T > slidingSums(const MArray< T > &a, const IPosition &halfBoxSize, Bool fillEdge=True)
Get sliding sums.
MArray< T > floormod(const MArray< T > &left, const MArray< T > &right)
Functor to get maximum of two values.
Functor to get minimum of two values.
Functor to add absolute diff of right and base value to left.
Functor to add squared diff of right and base value to left.
Functor to add square of right to left.