ArrayMath.h

Classes

Global Functions -- Mathematical operations for Arrays. (full description)
Global Functions -- Helper function for partialX functions (full description)

Mathematical operations for Arrays. (source)

Interface

void ArrayMinMaxPrintOnceDeprecated ()
void operator+= (Array<T> &left, const Array<T> &other)
void operator-= (Array<T> &left, const Array<T> &other)
void operator*= (Array<T> &left, const Array<T> &other)
void operator/= (Array<T> &left, const Array<T> &other)
void operator+= (Array<T> &left, const T &other)
void operator-= (Array<T> &left, const T &other)
void operator*= (Array<T> &left, const T &other)
void operator/= (Array<T> &left, const T &other)
Array<T> operator+(const Array<T> &a)
Array<T> operator-(const Array<T> &a)
Array<T> operator+ (const Array<T> &left, const Array<T> &right)
Array<T> operator- (const Array<T> &left, const Array<T> &right)
Array<T> operator* (const Array<T> &left, const Array<T> &right)
Array<T> operator/ (const Array<T> &left, const Array<T> &right)
Array<T> operator+ (const Array<T> &left, const T &right)
Array<T> operator- (const Array<T> &left, const T &right)
Array<T> operator* (const Array<T> &left, const T &right)
Array<T> operator/ (const Array<T> &left, const T &right)
Array<T> operator+ (const T &left, const Array<T> &right)
Array<T> operator- (const T &left, const Array<T> &right)
Array<T> operator* (const T &left, const Array<T> &right)
Array<T> operator/ (const T &left, const Array<T> &right)
Array<T> cos(const Array<T> &a)
Array<T> cosh(const Array<T> &a)
Array<T> exp(const Array<T> &a)
Array<T> log(const Array<T> &a)
Array<T> log10(const Array<T> &a)
Array<T> pow(const Array<T> &a, const Array<T> &b)
Array<T> pow(const T &a, const Array<T> &b)
Array<T> sin(const Array<T> &a)
Array<T> sinh(const Array<T> &a)
Array<T> sqrt(const Array<T> &a)
Array<T> acos(const Array<T> &a)
Array<T> asin(const Array<T> &a)
Array<T> atan(const Array<T> &a)
Array<T> atan2(const Array<T> &y, const Array<T> &x)
Array<T> ceil(const Array<T> &a)
Array<T> fabs(const Array<T> &a)
Array<T> abs(const Array<T> &a)
Array<T> floor(const Array<T> &a)
Array<T> fmod(const Array<T> &a, const Array<T> &b)
Array<T> fmod(const T &a, const Array<T> &b)
Array<T> fmod(const Array<T> &a, const T &b)
Array<T> pow(const Array<T> &a, const Double &b)
Array<T> tan(const Array<T> &a)
Array<T> tanh(const Array<T> &a)
Array<T> fabs(const Array<T> &a)
void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const IPosition<ScalarType> &array)
void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const IPosition<ScalarType> &array, const IPosition<Bool> &mask)
void minMaxMasked(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const IPosition<ScalarType> &array, const IPosition<ScalarType> &mask)
void minMax(T &min, T &max, const Array<T> &a)
inline void minMax(const Array<T> &a, T &min, T &max)
T min(const Array<T> &a)
T max(const Array<T> &a)
void max(Array<T> &result, const Array<T> &a, const Array<T> &b)
void min(Array<T> &result, const Array<T> &a, const Array<T> &b)
Array<T> max(const Array<T> &a, const Array<T> &b)
Array<T> min(const Array<T> &a, const Array<T> &b)
void max(Array<T> &result, const Array<T> &a, const T &b)
void min(Array<T> &result, const Array<T> &a, const T &b)
Array<T> max(const Array<T> &a, const T &b)
Array<T> min(const Array<T> &a, const T &b)
void indgen(Array<T> &a, T start, T inc)
void indgen(Array<T> &a)
void indgen(Array<T> &a, T start)
T sum(const Array<T> &a)
T product(const Array<T> &a)
T mean(const Array<T> &a)
T variance(const Array<T> &a)
T variance(const Array<T> &a, T mean)
T stddev(const Array<T> &a)
T stddev(const Array<T> &a, T mean)
T avdev(const Array<T> &a)
T avdev(const Array<T> &a,T mean)
inline T median(const Array<T> &a, Bool sorted = False)
inline T medianInPlace(const Array<T> &a, Bool sorted = False)
T median(const Array<T> &a, Bool sorted, Bool takeEvenMean, Bool inPlace = False)
T fractile(const Array<T> &a, Float fraction, Bool sorted = False, Bool inPlace = False)
Array<T> partialSums (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialProducts (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialMins (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialMaxs (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialMeans (const Array<T>& array, const IPosition& collapseAxes)
inline Array<T> partialVariances (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialVariances (const Array<T>& array, const IPosition& collapseAxes, const Array<T>& means)
inline Array<T> partialStddevs (const Array<T>& array, const IPosition& collapseAxes)
inline Array<T> partialStddevs (const Array<T>& array, const IPosition& collapseAxes, const Array<T>& means)
inline Array<T> partialAvdevs (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialAvdevs (const Array<T>& array, const IPosition& collapseAxes, const Array<T>& means)
Array<T> partialMedians (const Array<T>& array, const IPosition& collapseAxes, Bool takeEvenMean=False, Bool inPlace=False)
Array<T> partialFractiles (const Array<T>& array, const IPosition& collapseAxes, Float fraction, Bool inPlace=False)
Array<Complex> conj(const Array<Complex> &carray)
Array<DComplex> conj(const Array<DComplex> &carray)
Matrix<Complex> conj(const Matrix<Complex> &carray)
Matrix<DComplex> conj(const Matrix<DComplex> &carray)
Array<Float> real(const Array<Complex> &carray)
Array<Double> real(const Array<DComplex> &carray)
void real(Array<Float> &rarray, const Array<Complex> &carray)
void real(Array<Double> &rarray, const Array<DComplex> &carray)
Array<Float> imag(const Array<Complex> &carray)
Array<Double> imag(const Array<DComplex> &carray)
void imag(Array<Float> &rarray, const Array<Complex> &carray)
void imag(Array<Double> &rarray, const Array<DComplex> &carray)
Array<Float> amplitude(const Array<Complex> &carray)
Array<Double> amplitude(const Array<DComplex> &carray)
void amplitude(Array<Float> &rarray, const Array<Complex> &carray)
void amplitude(Array<Double> &rarray, const Array<DComplex> &carray)
Array<Float> phase(const Array<Complex> &carray)
Array<Double> phase(const Array<DComplex> &carray)
void phase(Array<Float> &rarray, const Array<Complex> &carray)
void phase(Array<Double> &rarray, const Array<DComplex> &carray)
Array<Float> ComplexToReal(const Array<Complex> &carray)
Array<Double> ComplexToReal(const Array<DComplex> &carray)
void ComplexToReal(Array<Float> &rarray, const Array<Complex> &carray)
void ComplexToReal(Array<Double> &rarray, const Array<DComplex> &carray)
Array<Complex> RealToComplex(const Array<Float> &rarray)
Array<DComplex> RealToComplex(const Array<Double> &rarray)
void RealToComplex(Array<Complex> &carray, const Array<Float> &rarray)
void RealToComplex(Array<DComplex> &carray, const Array<Double> &rarray)
void convertArray(Array<T> &to, const Array<U> &from)
inline Array<T> square(const Array<T> &val)
inline Array<T> cube(const Array<T> &val)

Description

Review Status

Reviewed By:
UNKNOWN
Date Reviewed:
before2004/08/25
Programs:
Tests:

Prerequisite

Etymology

This file contains global functions which perform element by element mathematical operations on arrays.

Synopsis

These functions perform element by element mathematical operations on arrays. The two arrays must conform.

Example

   Vector<Int> a(10);
   Vector<Int> b(10);
   Vector<Int> c(10);
      . . .
   c = a + b;
This example sets the elements of c to (a+b). The result of this operation is an Array.

Example

   Vector<Double> a(10);
   Vector<Double> b(10);
   Vector<Double> c(10);
      . . .
   c = atan2 (a, b);
This example sets the elements of c to atan2 (a,b). The result of this operation is an Array.

Example

   Vector<Int> a(10);
   Int result;
      . . .
   result = sum (a);
This example sums a.

Motivation

One wants to be able to perform mathematical operations on arrays.

Member Description

void ArrayMinMaxPrintOnceDeprecated ()

Function to print "deprecated" message once per program.

void operator+= (Array<T> &left, const Array<T> &other)
void operator-= (Array<T> &left, const Array<T> &other)
void operator*= (Array<T> &left, const Array<T> &other)
void operator/= (Array<T> &left, const Array<T> &other)

Element by element arithmetic modifying left in-place. left and other must be conformant.

void operator+= (Array<T> &left, const T &other)
void operator-= (Array<T> &left, const T &other)
void operator*= (Array<T> &left, const T &other)
void operator/= (Array<T> &left, const T &other)

Element by element arithmetic modifying left in-place. The scalar "other" behaves as if it were a conformant Array to left filled with constant values.

Array<T> operator+(const Array<T> &a)
Array<T> operator-(const Array<T> &a)

Unary arithmetic operation.

Array<T> operator+ (const Array<T> &left, const Array<T> &right)
Array<T> operator- (const Array<T> &left, const Array<T> &right)
Array<T> operator* (const Array<T> &left, const Array<T> &right)
Array<T> operator/ (const Array<T> &left, const Array<T> &right)

Element by element arithmetic on two arrays, returning an array.

Array<T> operator+ (const Array<T> &left, const T &right)
Array<T> operator- (const Array<T> &left, const T &right)
Array<T> operator* (const Array<T> &left, const T &right)
Array<T> operator/ (const Array<T> &left, const T &right)

Element by element arithmetic between an array and a scalar, returning an array.

Array<T> operator+ (const T &left, const Array<T> &right)
Array<T> operator- (const T &left, const Array<T> &right)
Array<T> operator* (const T &left, const Array<T> &right)
Array<T> operator/ (const T &left, const Array<T> &right)

Element by element arithmetic between a scalar and an array, returning an array.

Array<T> cos(const Array<T> &a)
Array<T> cosh(const Array<T> &a)
Array<T> exp(const Array<T> &a)
Array<T> log(const Array<T> &a)
Array<T> log10(const Array<T> &a)
Array<T> pow(const Array<T> &a, const Array<T> &b)
Array<T> pow(const T &a, const Array<T> &b)
Array<T> sin(const Array<T> &a)
Array<T> sinh(const Array<T> &a)
Array<T> sqrt(const Array<T> &a)

Transcendental function that can be applied to essentially all numeric types. Works on an element-by-element basis.

Array<T> fabs(const Array<T> &a)

Transcendental function applied to the array on an element-by-element basis. Although a template function, this does not make sense for all numeric types.

N.B. fabs is deprecated. Use abs.

Array<T> acos(const Array<T> &a)
Array<T> asin(const Array<T> &a)
Array<T> atan(const Array<T> &a)
Array<T> atan2(const Array<T> &y, const Array<T> &x)
Array<T> ceil(const Array<T> &a)
Array<T> fabs(const Array<T> &a)
Array<T> abs(const Array<T> &a)
Array<T> floor(const Array<T> &a)
Array<T> fmod(const Array<T> &a, const Array<T> &b)
Array<T> fmod(const T &a, const Array<T> &b)
Array<T> fmod(const Array<T> &a, const T &b)
Array<T> pow(const Array<T> &a, const Double &b)
Array<T> tan(const Array<T> &a)
Array<T> tanh(const Array<T> &a)

Transcendental function applied to the array on an element-by-element basis. Although a template function, this does not make sense for all numeric types.

void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const IPosition<ScalarType> &array, const IPosition<Bool> &mask)

The array is only searched at locations where the mask is True. (at least one such position must exist or an exception will be thrown). MaskType should be an Array of Bool.

void minMaxMasked(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const IPosition<ScalarType> &array, const IPosition<ScalarType> &mask)

The array * mask is searched

void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const IPosition<ScalarType> &array)

inline void minMax(const Array<T> &a, T &min, T &max)

The "min" and "max" functions require that the type "T" have comparison operators.

This version is deprecated, due to its nonstandard argument order.

T min(const Array<T> &a)

The "min" and "max" functions require that the type "T" have comparison operators.

The minimum element of the array. Requires that the type "T" has comparison operators.

T max(const Array<T> &a)

The "min" and "max" functions require that the type "T" have comparison operators.

The maximum element of the array. Requires that the type "T" has comparison operators.

void max(Array<T> &result, const Array<T> &a, const Array<T> &b)

The "min" and "max" functions require that the type "T" have comparison operators.

"result" contains the maximum of "a" and "b" at each position. "result", "a", and "b" must be conformant.

void min(Array<T> &result, const Array<T> &a, const Array<T> &b)

The "min" and "max" functions require that the type "T" have comparison operators.

"result" contains the minimum of "a" and "b" at each position. "result", "a", and "b" must be conformant.

Array<T> max(const Array<T> &a, const Array<T> &b)

The "min" and "max" functions require that the type "T" have comparison operators.

Return an array that contains the maximum of "a" and "b" at each position. "a" and "b" must be conformant.

Array<T> min(const Array<T> &a, const Array<T> &b)

The "min" and "max" functions require that the type "T" have comparison operators.

Return an array that contains the minimum of "a" and "b" at each position. "a" and "b" must be conformant.

void max(Array<T> &result, const Array<T> &a, const T &b)

The "min" and "max" functions require that the type "T" have comparison operators.

"result" contains the maximum of "a" and "b" at each position. "result", and "a" must be conformant.

void min(Array<T> &result, const Array<T> &a, const T &b)

The "min" and "max" functions require that the type "T" have comparison operators.

"result" contains the minimum of "a" and "b" at each position. "result", and "a" must be conformant.

Array<T> max(const Array<T> &a, const T &b)

The "min" and "max" functions require that the type "T" have comparison operators.

Return an array that contains the maximum of "a" and "b" at each position.

Array<T> min(const Array<T> &a, const T &b)

The "min" and "max" functions require that the type "T" have comparison operators.

Return an array that contains the minimum of "a" and "b" at each position.

void minMax(T &min, T &max, const Array<T> &a)

The "min" and "max" functions require that the type "T" have comparison operators.

void indgen(Array<T> &a, T start, T inc)

Fills all elements of "array" with a sequence starting with "start" and incrementing by "inc" for each element. The first axis varies most rapidly.

void indgen(Array<T> &a)

Fills all elements of "array" with a sequence starting with 0 and ending with nelements() - 1. The first axis varies most rapidly.

void indgen(Array<T> &a, T start)

Fills all elements of "array" with a sequence starting with start incremented by one for each position in the array. The first axis varies most rapidly.

T sum(const Array<T> &a)

Sum of every element of the array.

T product(const Array<T> &a)

Product of every element of the array. This could of course easily overflow.

T mean(const Array<T> &a)

The mean of "a" is the sum of all elements of "a" divided by the number of elements of "a".

T variance(const Array<T> &a)

The variance of "a" is the sum of (a(i) - mean(a))**2/(a.nelements() - 1). N.B. N-1, not N in the denominator).

T variance(const Array<T> &a, T mean)

The variance of "a" is the sum of (a(i) - mean(a))**2/(a.nelements() - 1). N.B. N-1, not N in the denominator). Rather than using a computed mean, use the supplied value.

T stddev(const Array<T> &a)

The standard deviation of "a" is the square root of its variance.

T stddev(const Array<T> &a, T mean)

The standard deviation of "a" is the square root of its variance. Rather than using a computed mean, use the supplied value.

T avdev(const Array<T> &a)

The average deviation of "a" is the sum of abs(a(i) - mean(a))/N. (N.B. N, not N-1 in the denominator).

T avdev(const Array<T> &a,T mean)

The average deviation of "a" is the sum of abs(a(i) - mean(a))/N. (N.B. N, not N-1 in the denominator). Rather than using a computed mean, use the supplied value.

inline T median(const Array<T> &a, Bool sorted = False)
inline T medianInPlace(const Array<T> &a, Bool sorted = False)
T median(const Array<T> &a, Bool sorted, Bool takeEvenMean, Bool inPlace = False)

The median of "a" is a(n/2). When a has an even number of elements and the switch takeEvenMean is set, the median is 0.5*(a(n/2) + a((n+1)/2)). According to Numerical Recipes (2nd edition) it makes little sense to take the mean when the array is large enough (> 100 elements). Therefore the default for takeEvenMean is False when the array has > 100 elements, otherwise it is True.
If "sorted"==True we assume the data is already sorted and we compute the median directly. Otherwise the function GenSort::kthLargest is used to find the median (kthLargest is about 6 times faster than a full quicksort).
Finding the median means that the array has to be (partially) sorted. By default a copy will be made, but if "inPlace" is in effect, the data themselves will be sorted. That should only be used if the data are used not thereafter.

T fractile(const Array<T> &a, Float fraction, Bool sorted = False, Bool inPlace = False)

Return the fractile of an array. It returns the value at the given fraction of the array. A fraction of 0.5 is the same as the median, be it that no mean of the two middle elements is taken if the array has an even nr of elements. It uses kthLargest if the array is not sorted yet.

Array<T> partialSums (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialProducts (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialMins (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialMaxs (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialMeans (const Array<T>& array, const IPosition& collapseAxes)
inline Array<T> partialVariances (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialVariances (const Array<T>& array, const IPosition& collapseAxes, const Array<T>& means)
inline Array<T> partialStddevs (const Array<T>& array, const IPosition& collapseAxes)
inline Array<T> partialStddevs (const Array<T>& array, const IPosition& collapseAxes, const Array<T>& means)
inline Array<T> partialAvdevs (const Array<T>& array, const IPosition& collapseAxes)
Array<T> partialAvdevs (const Array<T>& array, const IPosition& collapseAxes, const Array<T>& means)
Array<T> partialMedians (const Array<T>& array, const IPosition& collapseAxes, Bool takeEvenMean=False, Bool inPlace=False)
Array<T> partialFractiles (const Array<T>& array, const IPosition& collapseAxes, Float fraction, Bool inPlace=False)

The same functions as above, but determine the sum, etc. for the given axes only. The result is an array with a shape formed by the remaining axes. For example, for an array with shape [3,4,5], collapsing axis 0 results in an array with shape [4,5] containing, say, the sum for each X line. Summing for axes 0 and 2 results in an array with shape [4] containing, say, the sum for each XZ plane. ArrayLogical.h contains the functions ntrue, nfalse, partialNTrue and partialNFalse to count the number of true or false elements in an array.

Matrix<Complex> conj(const Matrix<Complex> &carray)

Returns the complex conjugate of a complex array.

Array<Complex> conj(const Array<Complex> &carray)
Array<DComplex> conj(const Array<DComplex> &carray)
Matrix<DComplex> conj(const Matrix<DComplex> &carray)

Returns the complex conjugate of a complex array.

void real(Array<Float> &rarray, const Array<Complex> &carray)

Extracts the real part of a complex array into an array of floats.

Modifies rarray in place. rarray must be conformant.

Array<Float> real(const Array<Complex> &carray)
Array<Double> real(const Array<DComplex> &carray)
void real(Array<Double> &rarray, const Array<DComplex> &carray)

Extracts the real part of a complex array into an array of floats.

void imag(Array<Float> &rarray, const Array<Complex> &carray)

Extracts the imaginary part of a complex array into an array of floats.

Modifies rarray in place. rarray must be conformant.

Array<Float> imag(const Array<Complex> &carray)
Array<Double> imag(const Array<DComplex> &carray)
void imag(Array<Double> &rarray, const Array<DComplex> &carray)

Extracts the imaginary part of a complex array into an array of floats.

void amplitude(Array<Float> &rarray, const Array<Complex> &carray)

Extracts the amplitude (i.e. sqrt(re*re + im*im)) from an array of complex numbers. N.B. this is presently called "fabs" for a single complex number.

Modifies rarray in place. rarray must be conformant.

Array<Float> amplitude(const Array<Complex> &carray)
Array<Double> amplitude(const Array<DComplex> &carray)
void amplitude(Array<Double> &rarray, const Array<DComplex> &carray)

Extracts the amplitude (i.e. sqrt(re*re + im*im)) from an array of complex numbers. N.B. this is presently called "fabs" for a single complex number.

void phase(Array<Float> &rarray, const Array<Complex> &carray)

Extracts the phase (i.e. atan2(im, re)) from an array of complex numbers. N.B. this is presently called "arg" for a single complex number.

Modifies rarray in place. rarray must be conformant.

Array<Float> phase(const Array<Complex> &carray)
Array<Double> phase(const Array<DComplex> &carray)
void phase(Array<Double> &rarray, const Array<DComplex> &carray)

Extracts the phase (i.e. atan2(im, re)) from an array of complex numbers. N.B. this is presently called "arg" for a single complex number.

Array<Float> ComplexToReal(const Array<Complex> &carray)

Copy an array of complex into an array of real,imaginary pairs. The first axis of the real array becomes twice as long as the complex array. In the future versions which work by reference will be available; presently a copy is made.

Array<Double> ComplexToReal(const Array<DComplex> &carray)

void ComplexToReal(Array<Float> &rarray, const Array<Complex> &carray)
void ComplexToReal(Array<Double> &rarray, const Array<DComplex> &carray)

Modify the array "rarray" in place. "rarray" must be the correct shape.

Array<Complex> RealToComplex(const Array<Float> &rarray)

Copy an array of real,imaginary pairs into a complex array. The first axis must have an even length. In the future versions which work by reference will be available; presently a copy is made.

Array<DComplex> RealToComplex(const Array<Double> &rarray)

void RealToComplex(Array<Complex> &carray, const Array<Float> &rarray)
void RealToComplex(Array<DComplex> &carray, const Array<Double> &rarray)

Modify the array "carray" in place. "carray" must be the correct shape.

void convertArray(Array<T> &to, const Array<U> &from)

Make a copy of an array of a different type; for example make an array of doubles from an array of floats. Arrays to and from must be conformant (same shape). Also, it must be possible to convert a scalar of type U to type T.

inline Array<T> square(const Array<T> &val)

Returns an array where every element is squared.

inline Array<T> cube(const Array<T> &val)

Returns an array where every element is cubed.


Helper function for partialX functions (source)

Interface

uInt partialFuncHelper (Int& nelemCont, IPosition& resultShape, IPosition& incr, const IPosition& sourceShape, const IPosition& collapseAxes)

Description

Synopsis

This is a specialized helper function for functions like partialSums. It determines the shape of the resulting array and calculates the result increments when iterating linearly through the source array. It returns the first result axis which indicates the number of the first contiguous collapse axes. The number of contiguous data points is returned in nelemCont.

Member Description

uInt partialFuncHelper (Int& nelemCont, IPosition& resultShape, IPosition& incr, const IPosition& sourceShape, const IPosition& collapseAxes)