casacore
Loading...
Searching...
No Matches
ArrayMath.h
Go to the documentation of this file.
1// # ArrayMath.h: ArrayMath: Simple mathematics done on an entire array.
2// # Copyright (C) 1993,1994,1995,1996,1998,1999,2001,2003
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: 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_ARRAYMATH_2_H
27#define CASA_ARRAYMATH_2_H
28
29#include "Array.h"
30
31#include <algorithm>
32#include <cassert>
33#include <functional>
34#include <numeric>
35
36namespace casacore { // # NAMESPACE CASACORE - BEGIN
37
38// <summary>
39// Mathematical operations for Arrays.
40// </summary>
41// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArray">
42//
43// <prerequisite>
44// <li> <linkto class=Array>Array</linkto>
45// </prerequisite>
46//
47// <etymology>
48// This file contains global functions which perform element by element
49// mathematical operations on arrays.
50// </etymology>
51//
52// <synopsis>
53// These functions perform element by element mathematical operations on
54// arrays. The two arrays must conform.
55//
56// Furthermore it defines functions a la std::transform to transform one or
57// two arrays by means of a unary or binary operator. All math and logical
58// operations on arrays can be expressed by means of these transform functions.
59// <br>It also defines an in-place transform function because for non-trivial
60// iterators it works faster than a transform where the result is an iterator
61// on the same data object as the left operand.
62// <br>The transform functions distinguish between contiguous and non-contiguous
63// arrays because iterating through a contiguous array can be done in a faster
64// way.
65// <br> Similar to the standard transform function these functions do not check
66// if the shapes match. The user is responsible for that.
67// </synopsis>
68//
69// <example>
70// <srcblock>
71// Vector<int> a(10);
72// Vector<int> b(10);
73// Vector<int> c(10);
74// . . .
75// c = a + b;
76// </srcblock>
77// This example sets the elements of c to (a+b). It checks if a and b have the
78// same shape.
79// The result of this operation is an Array.
80// </example>
81//
82// <example>
83// <srcblock>
84// c = arrayTransformResult (a, b, std::plus<double>());
85// </srcblock>
86// This example does the same as the previous example, but expressed using
87// the transform function (which, in fact, is used by the + operator above).
88// However, it is not checked if the shapes match.
89// </example>
90
91// <example>
92// <srcblock>
93// arrayContTransform (a, b, c, std::plus<double>());
94// </srcblock>
95// This example does the same as the previous example, but is faster because
96// the result array already exists and does not need to be allocated.
97// Note that the caller must be sure that c is contiguous.
98// </example>
99
100// <example>
101// <srcblock>
102// Vector<double> a(10);
103// Vector<double> b(10);
104// Vector<double> c(10);
105// . . .
106// c = atan2 (a, b);
107// </srcblock>
108// This example sets the elements of c to atan2 (a,b).
109// The result of this operation is an Array.
110// </example>
111//
112// <example>
113// <srcblock>
114// Vector<int> a(10);
115// int result;
116// . . .
117// result = sum (a);
118// </srcblock>
119// This example sums a.
120// </example>
121//
122// <motivation>
123// One wants to be able to perform mathematical operations on arrays.
124// </motivation>
125//
126// <linkfrom anchor="Array mathematical operations" classes="Array Vector Matrix Cube">
127// <here>Array mathematical operations</here> -- Mathematical operations for
128// Arrays.
129// </linkfrom>
130//
131// <group name="Array mathematical operations">
133// The myxtransform functions are defined to avoid a bug in g++-4.3.
134// That compiler generates incorrect code when only -g is used for
135// a std::transform with a bind1st or bind2nd for a complex<float>.
136// So, for example, the multiplication of a std::complex<float> array and std::complex<float> scalar
137// would fail (see g++ bug 39678).
138// <group>
139// sequence = scalar OP sequence
140template <typename _InputIterator1, typename T, typename _OutputIterator, typename _BinaryOperation>
141void myltransform(_InputIterator1 __first1, _InputIterator1 __last1, _OutputIterator __result,
142 T left, _BinaryOperation __binary_op) {
143 for (; __first1 != __last1; ++__first1, ++__result) *__result = __binary_op(left, *__first1);
144}
145// sequence = sequence OP scalar
146template <typename _InputIterator1, typename T, typename _OutputIterator, typename _BinaryOperation>
147void myrtransform(_InputIterator1 __first1, _InputIterator1 __last1, _OutputIterator __result,
148 T right, _BinaryOperation __binary_op) {
149 for (; __first1 != __last1; ++__first1, ++__result) *__result = __binary_op(*__first1, right);
150}
151// sequence OP= scalar
152template <typename _InputIterator1, typename T, typename _BinaryOperation>
153void myiptransform(_InputIterator1 __first1, _InputIterator1 __last1, T right,
154 _BinaryOperation __binary_op) {
155 for (; __first1 != __last1; ++__first1) *__first1 = __binary_op(*__first1, right);
156}
157// </group>
158
159// Functions to apply a binary or unary operator to arrays.
160// They are modeled after std::transform.
161// They do not check if the shapes conform; as in std::transform the
162// user must take care that the operands conform.
163// <group>
164// Transform left and right to a result using the binary operator.
165// Result MUST be a contiguous array.
166template <typename L, typename R, typename RES, typename BinaryOperator>
167inline void arrayContTransform(const Array<L> &left, const Array<R> &right, Array<RES> &result,
168 BinaryOperator op) {
169 assert(result.contiguousStorage());
170 if (left.contiguousStorage() && right.contiguousStorage()) {
171 std::transform(left.cbegin(), left.cend(), right.cbegin(), result.cbegin(), op);
172 } else {
173 std::transform(left.begin(), left.end(), right.begin(), result.cbegin(), op);
174 }
175}
176
177// Transform left and right to a result using the binary operator.
178// Result MUST be a contiguous array.
179template <typename L, typename R, typename RES, typename BinaryOperator>
180inline void arrayContTransform(const Array<L> &left, R right, Array<RES> &result,
181 BinaryOperator op) {
182 assert(result.contiguousStorage());
183 if (left.contiguousStorage()) {
184 myrtransform(left.cbegin(), left.cend(), result.cbegin(), right, op);
187 } else {
188 myrtransform(left.begin(), left.end(), result.cbegin(), right, op);
191 }
192}
193
194// Transform left and right to a result using the binary operator.
195// Result MUST be a contiguous array.
196template <typename L, typename R, typename RES, typename BinaryOperator>
197inline void arrayContTransform(L left, const Array<R> &right, Array<RES> &result,
198 BinaryOperator op) {
199 assert(result.contiguousStorage());
200 if (right.contiguousStorage()) {
201 myltransform(right.cbegin(), right.cend(), result.cbegin(), left, op);
204 } else {
205 myltransform(right.begin(), right.end(), result.cbegin(), left, op);
208 }
209}
210
211// Transform array to a result using the unary operator.
212// Result MUST be a contiguous array.
213template <typename T, typename RES, typename UnaryOperator>
214inline void arrayContTransform(const Array<T> &arr, Array<RES> &result, UnaryOperator op) {
215 assert(result.contiguousStorage());
216 if (arr.contiguousStorage()) {
217 std::transform(arr.cbegin(), arr.cend(), result.cbegin(), op);
218 } else {
219 std::transform(arr.begin(), arr.end(), result.cbegin(), op);
220 }
221}
222
223// Transform left and right to a result using the binary operator.
224// Result need not be a contiguous array.
225template <typename L, typename R, typename RES, typename BinaryOperator>
226void arrayTransform(const Array<L> &left, const Array<R> &right, Array<RES> &result,
227 BinaryOperator op);
228
229// Transform left and right to a result using the binary operator.
230// Result need not be a contiguous array.
231template <typename L, typename R, typename RES, typename BinaryOperator>
232void arrayTransform(const Array<L> &left, R right, Array<RES> &result, BinaryOperator op);
233
234// Transform left and right to a result using the binary operator.
235// Result need not be a contiguous array.
236template <typename L, typename R, typename RES, typename BinaryOperator>
237void arrayTransform(L left, const Array<R> &right, Array<RES> &result, BinaryOperator op);
238
239// Transform array to a result using the unary operator.
240// Result need not be a contiguous array.
241template <typename T, typename RES, typename UnaryOperator>
242void arrayTransform(const Array<T> &arr, Array<RES> &result, UnaryOperator op);
243
244// Transform left and right to a result using the binary operator.
245// The created and returned result array is contiguous.
246template <typename T, typename BinaryOperator>
247Array<T> arrayTransformResult(const Array<T> &left, const Array<T> &right, BinaryOperator op);
248
249// Transform left and right to a result using the binary operator.
250// The created and returned result array is contiguous.
251template <typename T, typename BinaryOperator>
252Array<T> arrayTransformResult(const Array<T> &left, T right, BinaryOperator op);
253
254// Transform left and right to a result using the binary operator.
255// The created and returned result array is contiguous.
256template <typename T, typename BinaryOperator>
257Array<T> arrayTransformResult(T left, const Array<T> &right, BinaryOperator op);
258
259// Transform array to a result using the unary operator.
260// The created and returned result array is contiguous.
261template <typename T, typename UnaryOperator>
262Array<T> arrayTransformResult(const Array<T> &arr, UnaryOperator op);
263
264// Transform left and right in place using the binary operator.
265// The result is stored in the left array (useful for e.g. the += operation).
266template <typename L, typename R, typename BinaryOperator>
267inline void arrayTransformInPlace(Array<L> &left, const Array<R> &right, BinaryOperator op) {
268 if (left.contiguousStorage() && right.contiguousStorage()) {
269 std::transform(left.cbegin(), left.cend(), right.cbegin(), left.cbegin(), op);
270 } else {
271 std::transform(left.begin(), left.end(), right.begin(), left.begin(), op);
272 }
273}
274
275// Transform left and right in place using the binary operator.
276// The result is stored in the left array (useful for e.g. the += operation).
277template <typename L, typename R, typename BinaryOperator>
278inline void arrayTransformInPlace(Array<L> &left, R right, BinaryOperator op) {
279 if (left.contiguousStorage()) {
280 myiptransform(left.cbegin(), left.cend(), right, op);
282 } else {
283 myiptransform(left.begin(), left.end(), right, op);
285 }
286}
287
288// Transform the array in place using the unary operator.
289// E.g. doing <src>arrayTransformInPlace(array, Sin<T>())</src> is faster than
290// <src>array=sin(array)</src> as it does not need to create a temporary array.
291template <typename T, typename UnaryOperator>
292inline void arrayTransformInPlace(Array<T> &arr, UnaryOperator op) {
293 if (arr.contiguousStorage()) {
294 std::transform(arr.cbegin(), arr.cend(), arr.cbegin(), op);
295 } else {
296 std::transform(arr.begin(), arr.end(), arr.begin(), op);
297 }
298}
299// </group>
300
301//
302// Element by element arithmetic modifying left in-place. left and other
303// must be conformant.
304// <group>
305template <typename T>
306void operator+=(Array<T> &left, const Array<T> &other);
307template <typename T>
308void operator-=(Array<T> &left, const Array<T> &other);
309template <typename T>
310void operator*=(Array<T> &left, const Array<T> &other) {
311 checkArrayShapes(left, other, "*=");
312 arrayTransformInPlace(left, other, std::multiplies<T>());
313}
314
315template <typename T>
316void operator/=(Array<T> &left, const Array<T> &other) {
317 checkArrayShapes(left, other, "/=");
318 arrayTransformInPlace(left, other, std::divides<T>());
319}
320template <typename T>
321void operator%=(Array<T> &left, const Array<T> &other);
322template <typename T>
323void operator&=(Array<T> &left, const Array<T> &other);
324template <typename T>
325void operator|=(Array<T> &left, const Array<T> &other);
326template <typename T>
327void operator^=(Array<T> &left, const Array<T> &other);
328// </group>
329
330//
331// Element by element arithmetic modifying left in-place. The scalar "other"
332// behaves as if it were a conformant Array to left filled with constant values.
333// <group>
334template <typename T>
335void operator+=(Array<T> &left, const T &other);
336template <typename T>
337void operator-=(Array<T> &left, const T &other);
338template <typename T>
339void operator*=(Array<T> &left, const T &other) {
340 arrayTransformInPlace(left, other, std::multiplies<T>());
341}
342template <typename T>
343void operator/=(Array<T> &left, const T &other) {
344 arrayTransformInPlace(left, other, std::divides<T>());
345}
346template <typename T>
347void operator%=(Array<T> &left, const T &other);
348template <typename T>
349void operator&=(Array<T> &left, const T &other);
350template <typename T>
351void operator|=(Array<T> &left, const T &other);
352template <typename T>
353void operator^=(Array<T> &left, const T &other);
354// </group>
355
356// Unary arithmetic operation.
357//
358// <group>
359template <typename T>
361template <typename T>
363template <typename T>
365// </group>
366
367//
368// Element by element arithmetic on two arrays, returning an array.
369// <group>
370template <typename T>
371Array<T> operator+(const Array<T> &left, const Array<T> &right);
372template <typename T>
373Array<T> operator-(const Array<T> &left, const Array<T> &right);
374template <typename T>
375Array<T> operator*(const Array<T> &left, const Array<T> &right) {
376 checkArrayShapes(left, right, "*");
377 return arrayTransformResult(left, right, std::multiplies<T>());
378}
379template <typename T>
380Array<T> operator/(const Array<T> &left, const Array<T> &right);
381template <typename T>
382Array<T> operator%(const Array<T> &left, const Array<T> &right);
383template <typename T>
384Array<T> operator|(const Array<T> &left, const Array<T> &right);
385template <typename T>
386Array<T> operator&(const Array<T> &left, const Array<T> &right);
387template <typename T>
388Array<T> operator^(const Array<T> &left, const Array<T> &right);
389// </group>
390
391//
392// Element by element arithmetic between an array and a scalar, returning
393// an array.
394// <group>
395template <typename T>
396Array<T> operator+(const Array<T> &left, const T &right);
397template <typename T>
398Array<T> operator-(const Array<T> &left, const T &right);
399template <class T>
400Array<T> operator*(const Array<T> &left, const T &right) {
401 return arrayTransformResult(left, right, std::multiplies<T>());
402}
403template <typename T>
404Array<T> operator/(const Array<T> &left, const T &right);
405template <typename T>
406Array<T> operator%(const Array<T> &left, const T &right);
407template <typename T>
408Array<T> operator|(const Array<T> &left, const T &right);
409template <typename T>
410Array<T> operator&(const Array<T> &left, const T &right);
411template <typename T>
412Array<T> operator^(const Array<T> &left, const T &right);
413// </group>
414
415//
416// Element by element arithmetic between a scalar and an array, returning
417// an array.
418// <group>
419template <typename T>
420Array<T> operator+(const T &left, const Array<T> &right);
421template <typename T>
422Array<T> operator-(const T &left, const Array<T> &right);
423template <class T>
424Array<T> operator*(const T &left, const Array<T> &right) {
425 return arrayTransformResult(left, right, std::multiplies<T>());
426}
427
428template <typename T>
429Array<T> operator/(const T &left, const Array<T> &right);
430template <typename T>
431Array<T> operator%(const T &left, const Array<T> &right);
432template <typename T>
433Array<T> operator|(const T &left, const Array<T> &right);
434template <typename T>
435Array<T> operator&(const T &left, const Array<T> &right);
436template <typename T>
437Array<T> operator^(const T &left, const Array<T> &right);
438// </group>
439
440//
441// Transcendental function that can be applied to essentially all numeric
442// types. Works on an element-by-element basis.
443// <group>
444template <typename T>
446template <typename T>
448template <typename T>
450template <typename T>
452template <typename T>
454template <typename T>
455Array<T> pow(const Array<T> &a, const Array<T> &b);
456template <typename T>
457Array<T> pow(const T &a, const Array<T> &b);
458template <typename T>
460template <typename T>
462template <typename T>
464// </group>
465
466//
467// Transcendental function applied to the array on an element-by-element
468// basis. Although a template function, this does not make sense for all
469// numeric types.
470// <group>
471template <typename T>
473template <typename T>
475template <typename T>
477template <typename T>
478Array<T> atan2(const Array<T> &y, const Array<T> &x);
479template <typename T>
480Array<T> atan2(const T &y, const Array<T> &x);
481template <typename T>
482Array<T> atan2(const Array<T> &y, const T &x);
483template <typename T>
485template <typename T>
487template <typename T>
489template <typename T>
491template <typename T>
493template <typename T>
495template <typename T>
496Array<T> fmod(const Array<T> &a, const Array<T> &b);
497template <typename T>
498Array<T> fmod(const T &a, const Array<T> &b);
499template <typename T>
500Array<T> fmod(const Array<T> &a, const T &b);
501template <typename T>
502Array<T> floormod(const Array<T> &a, const Array<T> &b);
503template <typename T>
504Array<T> floormod(const T &a, const Array<T> &b);
505template <typename T>
506Array<T> floormod(const Array<T> &a, const T &b);
507template <typename T>
508Array<T> pow(const Array<T> &a, const T &b);
509template <typename T>
510Array<std::complex<T>> pow(const Array<std::complex<T>> &a, const T &b);
511template <typename T>
513template <typename T>
515// N.B. fabs is deprecated. Use abs.
516template <typename T>
518// </group>
519
520//
521// <group>
522// Find the minimum and maximum values of an array, including their locations.
523template <typename ScalarType>
524void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos,
525 const Array<ScalarType> &array);
526// The array is searched at locations where the mask equals <src>valid</src>.
527// (at least one such position must exist or an exception will be thrown).
528// MaskType should be an Array of bool.
529template <typename ScalarType>
530void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos,
531 const Array<ScalarType> &array, const Array<bool> &mask, bool valid = true);
532// The array * weight is searched
533template <typename ScalarType>
534void minMaxMasked(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos,
535 const Array<ScalarType> &array, const Array<ScalarType> &weight);
536// </group>
537
538//
539// The "min" and "max" functions require that the type "T" have comparison
540// operators.
541// <group>
542//
543// This sets min and max to the minimum and maximum of the array to
544// avoid having to do two passes with max() and min() separately.
545template <typename T>
546void minMax(T &min, T &max, const Array<T> &a);
547//
548// The minimum element of the array.
549// Requires that the type "T" has comparison operators.
550template <typename T>
551T min(const Array<T> &a);
552// The maximum element of the array.
553// Requires that the type "T" has comparison operators.
554template <typename T>
555T max(const Array<T> &a);
556
557// "result" contains the maximum of "a" and "b" at each position. "result",
558// "a", and "b" must be conformant.
559template <typename T>
560void max(Array<T> &result, const Array<T> &a, const Array<T> &b);
561// "result" contains the minimum of "a" and "b" at each position. "result",
562// "a", and "b" must be conformant.
563template <typename T>
564void min(Array<T> &result, const Array<T> &a, const Array<T> &b);
565// Return an array that contains the maximum of "a" and "b" at each position.
566// "a" and "b" must be conformant.
567template <typename T>
568Array<T> max(const Array<T> &a, const Array<T> &b);
569template <typename T>
570Array<T> max(const T &a, const Array<T> &b);
571// Return an array that contains the minimum of "a" and "b" at each position.
572// "a" and "b" must be conformant.
573template <typename T>
574Array<T> min(const Array<T> &a, const Array<T> &b);
575
576// "result" contains the maximum of "a" and "b" at each position. "result",
577// and "a" must be conformant.
578template <typename T>
579void max(Array<T> &result, const Array<T> &a, const T &b);
580template <typename T>
581inline void max(Array<T> &result, const T &a, const Array<T> &b) {
582 max(result, b, a);
583}
584// "result" contains the minimum of "a" and "b" at each position. "result",
585// and "a" must be conformant.
586template <typename T>
587void min(Array<T> &result, const Array<T> &a, const T &b);
588template <typename T>
589inline void min(Array<T> &result, const T &a, const Array<T> &b) {
590 min(result, b, a);
591}
592// Return an array that contains the maximum of "a" and "b" at each position.
593template <typename T>
594Array<T> max(const Array<T> &a, const T &b);
595template <typename T>
596inline Array<T> max(const T &a, const Array<T> &b) {
597 return max(b, a);
598}
599// Return an array that contains the minimum of "a" and "b" at each position.
600template <typename T>
601Array<T> min(const Array<T> &a, const T &b);
602template <typename T>
603inline Array<T> min(const T &a, const Array<T> &b) {
604 return min(b, a);
605}
606// </group>
607
608//
609// Fills all elements of "array" with a sequence starting with "start"
610// and incrementing by "inc" for each element. The first axis varies
611// most rapidly.
612template <typename T>
613void indgen(Array<T> &a, T start, T inc);
614//
615// Fills all elements of "array" with a sequence starting with 0
616// and ending with nelements() - 1. The first axis varies
617// most rapidly.
618template <typename T>
619inline void indgen(Array<T> &a) {
620 indgen(a, T(0), T(1));
621}
622//
623// Fills all elements of "array" with a sequence starting with start
624// incremented by one for each position in the array. The first axis varies
625// most rapidly.
626template <typename T>
627inline void indgen(Array<T> &a, T start) {
628 indgen(a, start, T(1));
629}
630
631// Create a Vector of the given length and fill it with the start value
632// incremented with <code>inc</code> for each element.
633template <typename T>
634inline Vector<T> indgen(size_t length, T start, T inc) {
635 Vector<T> x(length);
636 indgen(x, start, inc);
637 return x;
638}
639
640// Sum of every element of the array.
641template <typename T>
642T sum(const Array<T> &a);
643//
644// Sum the square of every element of the array.
645template <typename T>
646T sumsqr(const Array<T> &a);
647//
648// Product of every element of the array. This could of course easily
649// overflow.
650template <typename T>
651T product(const Array<T> &a);
652
653//
654// The mean of "a" is the sum of all elements of "a" divided by the number
655// of elements of "a".
656template <typename T>
657T mean(const Array<T> &a);
658
659// The variance of "a" is the sum of (a(i) - mean(a))**2/(a.nelements() - ddof).
660// Similar to numpy the argument ddof (delta degrees of freedom) tells if the
661// population variance (ddof=0) or the sample variance (ddof=1) is taken.
662// The variance functions proper use ddof=1.
663// <br>Note that for a complex valued T the absolute values are used; in that way
664// the variance is equal to the sum of the variances of the real and imaginary parts.
665// Hence the imaginary part in the return value is 0.
666template <typename T>
667T variance(const Array<T> &a);
668template <typename T>
669T pvariance(const Array<T> &a, size_t ddof = 0);
670// Rather than using a computed mean, use the supplied value.
671template <typename T>
672T variance(const Array<T> &a, T mean);
673template <typename T>
674T pvariance(const Array<T> &a, T mean, size_t ddof = 0);
675
676// The standard deviation of "a" is the square root of its variance.
677template <typename T>
678T stddev(const Array<T> &a);
679template <typename T>
680T pstddev(const Array<T> &a, size_t ddof = 0);
681template <typename T>
682T stddev(const Array<T> &a, T mean);
683template <typename T>
684T pstddev(const Array<T> &a, T mean, size_t ddof = 0);
685
686//
687// The average deviation of "a" is the sum of abs(a(i) - mean(a))/N. (N.B.
688// N, not N-1 in the denominator).
689template <typename T>
690T avdev(const Array<T> &a);
691//
692// The average deviation of "a" is the sum of abs(a(i) - mean(a))/N. (N.B.
693// N, not N-1 in the denominator).
694// Rather than using a computed mean, use the supplied value.
695template <typename T>
696T avdev(const Array<T> &a, T mean);
697
698//
699// The root-mean-square of "a" is the sqrt of sum(a*a)/N.
700template <typename T>
701T rms(const Array<T> &a);
702
703// The median of "a" is a(n/2).
704// If a has an even number of elements and the switch takeEvenMean is set,
705// the median is 0.5*(a(n/2) + a((n+1)/2)).
706// According to Numerical Recipes (2nd edition) it makes little sense to take
707// the mean if the array is large enough (> 100 elements). Therefore
708// the default for takeEvenMean is false if the array has > 100 elements,
709// otherwise it is true.
710// <br>If "sorted"==true we assume the data is already sorted and we
711// compute the median directly. Otherwise the function GenSort::kthLargest
712// is used to find the median (kthLargest is about 6 times faster
713// than a full quicksort).
714// <br>Finding the median means that the array has to be (partially)
715// sorted. By default a copy will be made, but if "inPlace" is in effect,
716// the data themselves will be sorted. That should only be used if the
717// data are used not thereafter.
718// <note>The function kthLargest in class GenSortIndirect can be used to
719// obtain the index of the median in an array. </note>
720// <group>
721// TODO shouldn't take a const Array for in place sorting
722template <typename T>
723T median(const Array<T> &a, std::vector<T> &scratch, bool sorted, bool takeEvenMean,
724 bool inPlace = false);
725// TODO shouldn't take a const Array for in place sorting
726template <typename T>
727T median(const Array<T> &a, bool sorted, bool takeEvenMean, bool inPlace = false) {
728 std::vector<T> scratch;
729 return median(a, scratch, sorted, takeEvenMean, inPlace);
730}
731template <typename T>
732inline T median(const Array<T> &a, bool sorted) {
733 return median(a, sorted, (a.nelements() <= 100), false);
734}
735template <typename T>
736inline T median(const Array<T> &a) {
737 return median(a, false, (a.nelements() <= 100), false);
738}
739// TODO shouldn't take a const Array for in place sorting
740template <typename T>
741inline T medianInPlace(const Array<T> &a, bool sorted = false) {
742 return median(a, sorted, (a.nelements() <= 100), true);
743}
744// </group>
745
746// The median absolute deviation from the median. Interface is as for
747// the median functions
748// <group>
749// TODO shouldn't take a const Array for in place sorting
750template <typename T>
751T madfm(const Array<T> &a, std::vector<T> &tmp, bool sorted, bool takeEvenMean,
752 bool inPlace = false);
753// TODO shouldn't take a const Array for in place sorting
754template <typename T>
755T madfm(const Array<T> &a, bool sorted, bool takeEvenMean, bool inPlace = false) {
756 std::vector<T> tmp;
757 return madfm(a, tmp, sorted, takeEvenMean, inPlace);
758}
759template <typename T>
760inline T madfm(const Array<T> &a, bool sorted) {
761 return madfm(a, sorted, (a.nelements() <= 100), false);
762}
763template <typename T>
764inline T madfm(const Array<T> &a) {
765 return madfm(a, false, (a.nelements() <= 100), false);
766}
767// TODO shouldn't take a const Array for in place sorting
768template <typename T>
769inline T madfmInPlace(const Array<T> &a, bool sorted = false) {
770 return madfm(a, sorted, (a.nelements() <= 100), true);
771}
772// </group>
773
774// Return the fractile of an array.
775// It returns the value at the given fraction of the array.
776// A fraction of 0.5 is the same as the median, be it that no mean of
777// the two middle elements is taken if the array has an even nr of elements.
778// It uses kthLargest if the array is not sorted yet.
779// <note>The function kthLargest in class GenSortIndirect can be used to
780// obtain the index of the fractile in an array. </note>
781// TODO shouldn't take a const Array for in place sorting
782template <typename T>
783T fractile(const Array<T> &a, std::vector<T> &tmp, float fraction, bool sorted = false,
784 bool inPlace = false);
785// TODO shouldn't take a const Array for in place sorting
786template <typename T>
787T fractile(const Array<T> &a, float fraction, bool sorted = false, bool inPlace = false) {
788 std::vector<T> tmp;
789 return fractile(a, tmp, fraction, sorted, inPlace);
790}
791
792// Return the inter-fractile range of an array.
793// This is the full range between the bottom and the top fraction.
794// <group>
795// TODO shouldn't take a const Array for in place sorting
796template <typename T>
797T interFractileRange(const Array<T> &a, std::vector<T> &tmp, float fraction, bool sorted = false,
798 bool inPlace = false);
799// TODO shouldn't take a const Array for in place sorting
800template <typename T>
801T interFractileRange(const Array<T> &a, float fraction, bool sorted = false, bool inPlace = false) {
802 std::vector<T> tmp;
803 return interFractileRange(a, tmp, fraction, sorted, inPlace);
804}
805// </group>
806
807// Return the inter-hexile range of an array.
808// This is the full range between the bottom sixth and the top sixth
809// of ordered array values. "The semi-interhexile range is very nearly
810// equal to the rms for a Gaussian distribution, but it is much less
811// sensitive to the tails of extended distributions." (Condon et al
812// 1998)
813// <group>
814// TODO shouldn't take a const Array for in place sorting
815template <typename T>
816T interHexileRange(const Array<T> &a, std::vector<T> &tmp, bool sorted = false,
817 bool inPlace = false) {
818 return interFractileRange(a, tmp, 1. / 6., sorted, inPlace);
819}
820// TODO shouldn't take a const Array for in place sorting
821template <typename T>
822T interHexileRange(const Array<T> &a, bool sorted = false, bool inPlace = false) {
823 return interFractileRange(a, 1. / 6., sorted, inPlace);
824}
825// </group>
826
827// Return the inter-quartile range of an array.
828// This is the full range between the bottom quarter and the top
829// quarter of ordered array values.
830// <group>
831// TODO shouldn't take a const Array for in place sorting
832template <typename T>
833T interQuartileRange(const Array<T> &a, std::vector<T> &tmp, bool sorted = false,
834 bool inPlace = false) {
835 return interFractileRange(a, tmp, 0.25, sorted, inPlace);
836}
837// TODO shouldn't take a const Array for in place sorting
838template <typename T>
839T interQuartileRange(const Array<T> &a, bool sorted = false, bool inPlace = false) {
840 return interFractileRange(a, 0.25, sorted, inPlace);
841}
842// </group>
843
844// Methods for element-by-element scaling of complex and real.
845// Note that std::complex<float> and std::complex<double> are typedefs for std::complex.
846//<group>
847template <typename T>
848void operator*=(Array<std::complex<T>> &left, const Array<T> &other) {
849 checkArrayShapes(left, other, "*=");
850 arrayTransformInPlace(left, other, [](std::complex<T> left, T right) { return left * right; });
851}
852
853template <typename T>
854void operator*=(Array<std::complex<T>> &left, const T &other) {
855 arrayTransformInPlace(left, other, [](std::complex<T> left, T right) { return left * right; });
856}
857
858template <typename T>
859void operator/=(Array<std::complex<T>> &left, const Array<T> &other) {
860 checkArrayShapes(left, other, "/=");
861 arrayTransformInPlace(left, other, [](std::complex<T> left, T right) { return left / right; });
862}
863
864template <typename T>
865void operator/=(Array<std::complex<T>> &left, const T &other) {
866 arrayTransformInPlace(left, other, [](std::complex<T> left, T right) { return left / right; });
867}
868
869template <typename T>
870Array<std::complex<T>> operator*(const Array<std::complex<T>> &left, const Array<T> &right) {
871 checkArrayShapes(left, right, "*");
872 Array<std::complex<T>> result(left.shape());
873 arrayContTransform(left, right, result,
874 [](std::complex<T> left, T right) { return left * right; });
875 return result;
876}
877template <typename T>
878Array<std::complex<T>> operator*(const Array<std::complex<T>> &left, const T &other) {
879 Array<std::complex<T>> result(left.shape());
880 arrayContTransform(left, other, result,
881 [](std::complex<T> left, T right) { return left * right; });
882 return result;
883}
884template <typename T>
885Array<std::complex<T>> operator*(const std::complex<T> &left, const Array<T> &other) {
886 Array<std::complex<T>> result(other.shape());
887 arrayContTransform(left, other, result,
888 [](std::complex<T> left, T right) { return left * right; });
889 return result;
890}
891
892template <typename T>
893Array<std::complex<T>> operator/(const Array<std::complex<T>> &left, const Array<T> &right) {
894 checkArrayShapes(left, right, "/");
895 Array<std::complex<T>> result(left.shape());
896 arrayContTransform(left, right, result, [](std::complex<T> l, T r) { return l / r; });
897 return result;
898}
899template <typename T>
900Array<std::complex<T>> operator/(const Array<std::complex<T>> &left, const T &other) {
901 Array<std::complex<T>> result(left.shape());
902 arrayContTransform(left, other, result,
903 [](std::complex<T> left, T right) { return left / right; });
904 return result;
905}
906template <typename T>
907Array<std::complex<T>> operator/(const std::complex<T> &left, const Array<T> &other) {
908 Array<std::complex<T>> result(other.shape());
909 arrayContTransform(left, other, result,
910 [](std::complex<T> left, T right) { return left / right; });
911 return result;
912}
913// </group>
914
915// Returns the complex conjugate of a complex array.
916//<group>
917Array<std::complex<float>> conj(const Array<std::complex<float>> &carray);
918Array<std::complex<double>> conj(const Array<std::complex<double>> &carray);
919// Modifies rarray in place. rarray must be conformant.
920void conj(Array<std::complex<float>> &rarray, const Array<std::complex<float>> &carray);
921void conj(Array<std::complex<double>> &rarray, const Array<std::complex<double>> &carray);
922// # The following are implemented to make the compiler find the right conversion
923// # more often.
924Matrix<std::complex<float>> conj(const Matrix<std::complex<float>> &carray);
925Matrix<std::complex<double>> conj(const Matrix<std::complex<double>> &carray);
926//</group>
927
928// Form an array of complex numbers from the given real arrays.
929// Note that std::complex<float> and std::complex<double> are simply typedefs for
930// std::complex<float> and std::complex<double>, so the result is in fact one of these types.
931// <group>
932template <typename T>
934template <typename T>
936template <typename T>
938// </group>
939
940// Set the real part of the left complex array to the right real array.
941template <typename C, typename R>
942void setReal(Array<C> &carray, const Array<R> &rarray);
943
944// Set the imaginary part of the left complex array to right real array.
945template <typename C, typename R>
946void setImag(Array<C> &carray, const Array<R> &rarray);
947
948// Extracts the real part of a complex array into an array of floats.
949// <group>
950Array<float> real(const Array<std::complex<float>> &carray);
951Array<double> real(const Array<std::complex<double>> &carray);
952// Modifies rarray in place. rarray must be conformant.
953void real(Array<float> &rarray, const Array<std::complex<float>> &carray);
954void real(Array<double> &rarray, const Array<std::complex<double>> &carray);
955// </group>
956
957//
958// Extracts the imaginary part of a complex array into an array of floats.
959// <group>
960Array<float> imag(const Array<std::complex<float>> &carray);
961Array<double> imag(const Array<std::complex<double>> &carray);
962// Modifies rarray in place. rarray must be conformant.
963void imag(Array<float> &rarray, const Array<std::complex<float>> &carray);
964void imag(Array<double> &rarray, const Array<std::complex<double>> &carray);
965// </group>
966
967//
968// Extracts the amplitude (i.e. sqrt(re*re + im*im)) from an array
969// of complex numbers. N.B. this is presently called "fabs" for a single
970// complex number.
971// <group>
972Array<float> amplitude(const Array<std::complex<float>> &carray);
973Array<double> amplitude(const Array<std::complex<double>> &carray);
974// Modifies rarray in place. rarray must be conformant.
975void amplitude(Array<float> &rarray, const Array<std::complex<float>> &carray);
976void amplitude(Array<double> &rarray, const Array<std::complex<double>> &carray);
977// </group>
978
979//
980// Extracts the phase (i.e. atan2(im, re)) from an array
981// of complex numbers. N.B. this is presently called "arg"
982// for a single complex number.
983// <group>
984Array<float> phase(const Array<std::complex<float>> &carray);
985Array<double> phase(const Array<std::complex<double>> &carray);
986// Modifies rarray in place. rarray must be conformant.
987void phase(Array<float> &rarray, const Array<std::complex<float>> &carray);
988void phase(Array<double> &rarray, const Array<std::complex<double>> &carray);
989// </group>
990
991// Copy an array of complex into an array of real,imaginary pairs. The
992// first axis of the real array becomes twice as long as the complex array.
993// In the future versions which work by reference will be available; presently
994// a copy is made.
995Array<float> ComplexToReal(const Array<std::complex<float>> &carray);
996Array<double> ComplexToReal(const Array<std::complex<double>> &carray);
997// Modify the array "rarray" in place. "rarray" must be the correct shape.
998// <group>
999void ComplexToReal(Array<float> &rarray, const Array<std::complex<float>> &carray);
1000void ComplexToReal(Array<double> &rarray, const Array<std::complex<double>> &carray);
1001// </group>
1002
1003// Copy an array of real,imaginary pairs into a complex array. The first axis
1004// must have an even length.
1005// In the future versions which work by reference will be available; presently
1006// a copy is made.
1009// Modify the array "carray" in place. "carray" must be the correct shape.
1010// <group>
1011void RealToComplex(Array<std::complex<float>> &carray, const Array<float> &rarray);
1012void RealToComplex(Array<std::complex<double>> &carray, const Array<double> &rarray);
1013// </group>
1014
1015// Make a copy of an array of a different type; for example make an array
1016// of doubles from an array of floats. Arrays to and from must be conformant
1017// (same shape). Also, it must be possible to convert a scalar of type U
1018// to type T.
1019template <typename T, typename U>
1020void convertArray(Array<T> &to, const Array<U> &from);
1021
1022// Returns an array where every element is squared.
1023template <typename T>
1025
1026// Returns an array where every element is cubed.
1027template <typename T>
1029
1030// Helper function for expandArray using recursion for each axis.
1031template <typename T>
1032T *expandRecursive(int axis, const IPosition &shp, const IPosition &mult, const IPosition &inSteps,
1033 const T *in, T *out, const IPosition &alternate) {
1034 if (axis == 0) {
1035 if (alternate[0]) {
1036 // Copy as 1,2,3 1,2,3, etc.
1037 for (ssize_t j = 0; j < mult[0]; ++j) {
1038 const T *pin = in;
1039 for (ssize_t i = 0; i < shp[0]; ++i) {
1040 *out++ = *pin;
1041 pin += inSteps[0];
1042 }
1043 }
1044 } else {
1045 // Copy as 1,1,1 2,2,2 etc.
1046 for (ssize_t i = 0; i < shp[0]; ++i) {
1047 for (ssize_t j = 0; j < mult[0]; ++j) {
1048 *out++ = *in;
1049 }
1050 in += inSteps[0];
1051 }
1052 }
1053 } else {
1054 if (alternate[axis]) {
1055 for (ssize_t j = 0; j < mult[axis]; ++j) {
1056 const T *pin = in;
1057 for (ssize_t i = 0; i < shp[axis]; ++i) {
1058 out = expandRecursive(axis - 1, shp, mult, inSteps, pin, out, alternate);
1059 pin += inSteps[axis];
1060 }
1061 }
1062 } else {
1063 for (ssize_t i = 0; i < shp[axis]; ++i) {
1064 for (ssize_t j = 0; j < mult[axis]; ++j) {
1065 out = expandRecursive(axis - 1, shp, mult, inSteps, in, out, alternate);
1066 }
1067 in += inSteps[axis];
1068 }
1069 }
1070 }
1071 return out;
1072}
1073
1074// Expand the values of an array. The arrays can have different dimensionalities.
1075// Missing input axes have length 1; missing output axes are discarded.
1076// The length of each axis in the input array must be <= the length of the
1077// corresponding axis in the output array and divide evenly.
1078// For each axis <src>mult</src> is set to output/input.
1079// <br>The <src>alternate</src> argument determines how the values are expanded.
1080// If a row contains values '1 2 3', they can be expanded "linearly"
1081// as '1 1 2 2 3 3' or alternately as '1 2 3 1 2 3'
1082// This choice can be made for each axis; a value 0 means linearly,
1083// another value means alternately. If the length of alternate is less than
1084// the dimensionality of the output array, the missing ones default to 0.
1085template <typename T>
1086void expandArray(Array<T> &out, const Array<T> &in, const IPosition &alternate = IPosition()) {
1087 IPosition mult, inshp, outshp;
1088 IPosition alt = checkExpandArray(mult, inshp, in.shape(), out.shape(), alternate);
1089 Array<T> incp(in);
1090 if (in.ndim() < inshp.size()) {
1091 incp.reference(in.reform(inshp));
1092 }
1093 // Make sure output is contiguous.
1094 bool deleteIt;
1095 T *outPtr = out.getStorage(deleteIt);
1096 expandRecursive(out.ndim() - 1, inshp, mult, incp.steps(), incp.data(), outPtr, alt);
1097 out.putStorage(outPtr, deleteIt);
1098}
1099
1100// Check array shapes for expandArray. It returns the alternate argument,
1101// where possibly missing values are appended (as 0).
1102// It fills in mult and inshp (with possibly missing axes of length 1).
1103// <br><code>inShape</code> defines the shape of the input array.
1104// <br><code>outShape</code> defines the shape of the output array.
1105// <br><code>alternate</code> tells per axis if value expansion uses alternation.
1106// <br><code>newInShape</code> is the input shape with new axes (of length 1) added as needed
1107// <br><code>mult</code> is the multiplication (expansion) factor per output axis
1108// Returned is the alternation per output axis; new axes have value 0 (linear expansion)
1109IPosition checkExpandArray(IPosition &mult, IPosition &newInShape, const IPosition &inShape,
1110 const IPosition &outShape, const IPosition &alternate);
1111
1112// </group>
1113
1114} // namespace casacore
1115
1116#include "ArrayMath.tcc"
1117
1118#endif
const IPosition & steps() const
Return steps to be made if stepping one element in a dimension.
Definition ArrayBase.h:126
size_t ndim() const
The dimensionality of this array.
Definition ArrayBase.h:94
size_t nelements() const
How many elements does this array have?
Definition ArrayBase.h:98
bool contiguousStorage() const
Are the array data contiguous?
Definition ArrayBase.h:108
const IPosition & shape() const
The length of each axis.
Definition ArrayBase.h:116
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 cend()
Definition Array.h:824
contiter cbegin()
Get the begin iterator object for a contiguous array.
Definition Array.h:822
iterator end()
Definition Array.h:816
void putStorage(T *&storage, bool deleteAndCopy)
putStorage() is normally called after a call to getStorage() (cf).
virtual void reference(const Array< T > &other)
After invocation, this array and other reference the same storage.
Array< T > reform(const IPosition &shape) const
It is occasionally useful to have an array which access the same storage appear to have a different s...
T * getStorage(bool &deleteIt)
Generally use of this should be shunned, except to use a FORTRAN routine or something similar.
size_t size() const
Definition IPosition.h:552
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.
void checkArrayShapes(const ArrayBase &left, const ArrayBase &right, const char *name)
Definition ArrayBase.h:301
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
void indgen(TableVector< T > &tv, T start, T inc)
Definition TabVecMath.h:402
T * array
The actual storage.
Definition Block.h:689
LatticeExprNode min(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
void transformInPlace(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryOperator op)
Define a function to do a binary transform in place.
Definition Functors.h:41
LatticeExprNode median(const LatticeExprNode &expr)
void max(Array< T > &result, const T &a, const Array< T > &b)
Definition ArrayMath.h:581
Array< std::complex< T > > operator/(const Array< std::complex< T > > &left, const Array< T > &right)
Definition ArrayMath.h:893
Array< T > operator/(const Array< T > &left, const Array< T > &right)
Array< T > operator+(const Array< T > &a)
Unary arithmetic operation.
Array< double > amplitude(const Array< std::complex< double > > &carray)
Array< double > imag(const Array< std::complex< double > > &carray)
Array< T > arrayTransformResult(const Array< T > &left, T right, BinaryOperator op)
Transform left and right to a result using the binary operator.
Array< T > operator/(const T &left, const Array< T > &right)
Array< std::complex< T > > makeComplex(const Array< T > &real, const T &imag)
Array< T > operator*(const T &left, const Array< T > &right)
Definition ArrayMath.h:424
void ComplexToReal(Array< float > &rarray, const Array< std::complex< float > > &carray)
Modify the array "rarray" in place.
void imag(Array< float > &rarray, const Array< std::complex< float > > &carray)
Modifies rarray in place.
void arrayContTransform(const Array< L > &left, const Array< R > &right, Array< RES > &result, BinaryOperator op)
Functions to apply a binary or unary operator to arrays.
Definition ArrayMath.h:167
Array< std::complex< T > > operator*(const Array< std::complex< T > > &left, const Array< T > &right)
Definition ArrayMath.h:870
T interHexileRange(const Array< T > &a, bool sorted=false, bool inPlace=false)
TODO shouldn't take a const Array for in place sorting.
Definition ArrayMath.h:822
Array< std::complex< T > > operator*(const Array< std::complex< T > > &left, const T &other)
Definition ArrayMath.h:878
Array< float > phase(const Array< std::complex< float > > &carray)
Extracts the phase (i.e.
Array< T > operator-(const Array< T > &left, const T &right)
Array< std::complex< T > > operator*(const std::complex< T > &left, const Array< T > &other)
Definition ArrayMath.h:885
Array< float > real(const Array< std::complex< float > > &carray)
Extracts the real part of a complex array into an array of floats.
void arrayTransformInPlace(Array< T > &arr, UnaryOperator op)
Transform the array in place using the unary operator.
Definition ArrayMath.h:292
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".
void RealToComplex(Array< std::complex< double > > &carray, const Array< double > &rarray)
Array< T > operator%(const T &left, const Array< T > &right)
Array< T > operator|(const Array< T > &left, const Array< T > &right)
Vector< T > indgen(size_t length, T start, T inc)
Create a Vector of the given length and fill it with the start value incremented with inc for each el...
Definition ArrayMath.h:634
Array< T > operator|(const Array< T > &left, const T &right)
Array< T > min(const T &a, const Array< T > &b)
Definition ArrayMath.h:603
T min(const Array< T > &a)
The minimum element of the array.
Array< T > operator-(const T &left, const Array< T > &right)
Array< double > ComplexToReal(const Array< std::complex< double > > &carray)
void min(Array< T > &result, const Array< T > &a, const Array< T > &b)
"result" contains the minimum of "a" and "b" at each position.
void operator*=(Array< std::complex< T > > &left, const T &other)
Definition ArrayMath.h:854
void conj(Array< std::complex< float > > &rarray, const Array< std::complex< float > > &carray)
Modifies rarray in place.
Array< T > operator|(const T &left, const Array< T > &right)
T interHexileRange(const Array< T > &a, std::vector< T > &tmp, bool sorted=false, bool inPlace=false)
Return the inter-hexile range of an array.
Definition ArrayMath.h:816
T median(const Array< T > &a, bool sorted, bool takeEvenMean, bool inPlace=false)
TODO shouldn't take a const Array for in place sorting.
Definition ArrayMath.h:727
T variance(const Array< T > &a)
The variance of "a" is the sum of (a(i) - mean(a))**2/(a.nelements() - ddof).
Array< T > floormod(const Array< T > &a, const T &b)
T madfm(const Array< T > &a, std::vector< T > &tmp, bool sorted, bool takeEvenMean, bool inPlace=false)
The median absolute deviation from the median.
T avdev(const Array< T > &a)
The average deviation of "a" is the sum of abs(a(i) - mean(a))/N.
void myiptransform(_InputIterator1 __first1, _InputIterator1 __last1, T right, _BinaryOperation __binary_op)
sequence OP= scalar
Definition ArrayMath.h:153
void operator/=(Array< T > &left, const Array< T > &other)
Definition ArrayMath.h:316
T fractile(const Array< T > &a, std::vector< T > &tmp, float fraction, bool sorted=false, bool inPlace=false)
Return the fractile of an array.
T interFractileRange(const Array< T > &a, std::vector< T > &tmp, float fraction, bool sorted=false, bool inPlace=false)
Return the inter-fractile range of an array.
void operator%=(Array< T > &left, const Array< T > &other)
Array< double > phase(const Array< std::complex< double > > &carray)
Array< T > fmod(const Array< T > &a, const T &b)
void arrayContTransform(const Array< T > &arr, Array< RES > &result, UnaryOperator op)
Transform array to a result using the unary operator.
Definition ArrayMath.h:214
void arrayTransform(const Array< L > &left, R right, Array< RES > &result, BinaryOperator op)
Transform left and right to a result using the binary operator.
T madfm(const Array< T > &a, bool sorted, bool takeEvenMean, bool inPlace=false)
TODO shouldn't take a const Array for in place sorting.
Definition ArrayMath.h:755
void operator|=(Array< T > &left, const Array< T > &other)
Array< T > arrayTransformResult(const Array< T > &left, const Array< T > &right, BinaryOperator op)
Transform left and right to a result using the binary operator.
void amplitude(Array< double > &rarray, const Array< std::complex< double > > &carray)
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 fl...
T interQuartileRange(const Array< T > &a, bool sorted=false, bool inPlace=false)
TODO shouldn't take a const Array for in place sorting.
Definition ArrayMath.h:839
void arrayTransform(const Array< T > &arr, Array< RES > &result, UnaryOperator op)
Transform array to a result using the unary operator.
Array< std::complex< float > > conj(const Array< std::complex< float > > &carray)
Returns the complex conjugate of a complex array.
void setReal(Array< C > &carray, const Array< R > &rarray)
Set the real part of the left complex array to the right real array.
void ComplexToReal(Array< double > &rarray, const Array< std::complex< double > > &carray)
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 eac...
Array< T > operator+(const T &left, const Array< T > &right)
Element by element arithmetic between a scalar and an array, returning an array.
void operator+=(Array< T > &left, const Array< T > &other)
Element by element arithmetic modifying left in-place.
void real(Array< float > &rarray, const Array< std::complex< float > > &carray)
Modifies rarray in place.
Array< T > pow(const Array< T > &a, const Array< T > &b)
Array< T > arrayTransformResult(const Array< T > &arr, UnaryOperator op)
Transform array to a result using the unary operator.
T medianInPlace(const Array< T > &a, bool sorted=false)
TODO shouldn't take a const Array for in place sorting.
Definition ArrayMath.h:741
void arrayTransformInPlace(Array< L > &left, R right, BinaryOperator op)
Transform left and right in place using the binary operator.
Definition ArrayMath.h:278
IPosition checkExpandArray(IPosition &mult, IPosition &newInShape, const IPosition &inShape, const IPosition &outShape, const IPosition &alternate)
Check array shapes for expandArray.
Array< T > arrayTransformResult(T left, const Array< T > &right, BinaryOperator op)
Transform left and right to a result using the binary operator.
Array< float > amplitude(const Array< std::complex< float > > &carray)
Extracts the amplitude (i.e.
Array< T > atan2(const T &y, const Array< T > &x)
void arrayTransformInPlace(Array< L > &left, const Array< R > &right, BinaryOperator op)
Transform left and right in place using the binary operator.
Definition ArrayMath.h:267
Array< T > operator*(const Array< T > &left, const T &right)
Definition ArrayMath.h:400
T sumsqr(const Array< T > &a)
Sum the square of every element of the array.
void myltransform(_InputIterator1 __first1, _InputIterator1 __last1, _OutputIterator __result, T left, _BinaryOperation __binary_op)
The myxtransform functions are defined to avoid a bug in g++-4.3.
Definition ArrayMath.h:141
Array< float > imag(const Array< std::complex< float > > &carray)
Extracts the imaginary part of a complex array into an array of floats.
void phase(Array< double > &rarray, const Array< std::complex< double > > &carray)
Array< double > real(const Array< std::complex< double > > &carray)
Matrix< std::complex< float > > conj(const Matrix< std::complex< float > > &carray)
Array< std::complex< T > > pow(const Array< std::complex< T > > &a, const T &b)
void operator/=(Array< std::complex< T > > &left, const Array< T > &other)
Definition ArrayMath.h:859
Array< T > min(const Array< T > &a, const T &b)
Return an array that contains the minimum of "a" and "b" at each position.
Matrix< std::complex< double > > conj(const Matrix< std::complex< double > > &carray)
void minMax(T &min, T &max, const Array< T > &a)
The "min" and "max" functions require that the type "T" have comparison operators.
Array< T > operator*(const Array< T > &left, const Array< T > &right)
Definition ArrayMath.h:375
Array< T > acos(const Array< T > &a)
Transcendental function applied to the array on an element-by-element basis.
Array< std::complex< float > > RealToComplex(const Array< float > &rarray)
Copy an array of real,imaginary pairs into a complex array.
Array< T > max(const Array< T > &a, const T &b)
Return an array that contains the maximum of "a" and "b" at each position.
void max(Array< T > &result, const Array< T > &a, const Array< T > &b)
"result" contains the maximum of "a" and "b" at each position.
Array< T > operator%(const Array< T > &left, const Array< T > &right)
void operator/=(Array< std::complex< T > > &left, const T &other)
Definition ArrayMath.h:865
void minMaxMasked(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const Array< ScalarType > &array, const Array< ScalarType > &weight)
The array * weight is searched.
Array< T > operator%(const Array< T > &left, const T &right)
Array< T > cos(const Array< T > &a)
Transcendental function that can be applied to essentially all numeric types.
void operator*=(Array< std::complex< T > > &left, const Array< T > &other)
Methods for element-by-element scaling of complex and real.
Definition ArrayMath.h:848
void operator&=(Array< T > &left, const Array< T > &other)
T avdev(const Array< T > &a, T mean)
The average deviation of "a" is the sum of abs(a(i) - mean(a))/N.
Array< T > operator^(const T &left, const Array< T > &right)
T pvariance(const Array< T > &a, T mean, size_t ddof=0)
void expandArray(Array< T > &out, const Array< T > &in, const IPosition &alternate=IPosition())
Expand the values of an array.
Definition ArrayMath.h:1086
void operator-=(Array< T > &left, const Array< T > &other)
Array< std::complex< T > > operator/(const std::complex< T > &left, const Array< T > &other)
Definition ArrayMath.h:907
T interQuartileRange(const Array< T > &a, std::vector< T > &tmp, bool sorted=false, bool inPlace=false)
Return the inter-quartile range of an array.
Definition ArrayMath.h:833
void arrayTransform(const Array< L > &left, const Array< R > &right, Array< RES > &result, BinaryOperator op)
Transform left and right to a result using the binary operator.
Array< T > atan2(const Array< T > &y, const T &x)
Array< T > max(const Array< T > &a, const Array< T > &b)
Return an array that contains the maximum of "a" and "b" at each position.
void indgen(Array< T > &a, T start)
Fills all elements of "array" with a sequence starting with start incremented by one for each positio...
Definition ArrayMath.h:627
void imag(Array< double > &rarray, const Array< std::complex< double > > &carray)
Array< T > atan2(const Array< T > &y, const Array< T > &x)
Array< T > operator+(const Array< T > &left, const Array< T > &right)
Element by element arithmetic on two arrays, returning an array.
T stddev(const Array< T > &a)
The standard deviation of "a" is the square root of its variance.
Array< T > operator^(const Array< T > &left, const T &right)
void myrtransform(_InputIterator1 __first1, _InputIterator1 __last1, _OutputIterator __result, T right, _BinaryOperation __binary_op)
sequence = sequence OP scalar
Definition ArrayMath.h:147
void max(Array< T > &result, const Array< T > &a, const T &b)
"result" contains the maximum of "a" and "b" at each position.
T sum(const Array< T > &a)
Sum of every element of the array.
void phase(Array< float > &rarray, const Array< std::complex< float > > &carray)
Modifies rarray in place.
Array< T > operator-(const Array< T > &left, const Array< T > &right)
Array< std::complex< T > > makeComplex(const Array< T > &real, const Array< T > &imag)
Form an array of complex numbers from the given real arrays.
Array< std::complex< double > > RealToComplex(const Array< double > &rarray)
Array< T > operator+(const Array< T > &left, const T &right)
Element by element arithmetic between an array and a scalar, returning an array.
void RealToComplex(Array< std::complex< float > > &carray, const Array< float > &rarray)
Modify the array "carray" in place.
T max(const Array< T > &a)
The maximum element of the array.
Array< T > square(const Array< T > &val)
Returns an array where every element is squared.
Array< std::complex< T > > operator/(const Array< std::complex< T > > &left, const T &other)
Definition ArrayMath.h:900
T pstddev(const Array< T > &a, T mean, size_t ddof=0)
T product(const Array< T > &a)
Product of every element of the array.
void arrayTransform(L left, const Array< R > &right, Array< RES > &result, BinaryOperator op)
Transform left and right to a result using the binary operator.
Array< T > floormod(const T &a, const Array< T > &b)
void min(Array< T > &result, const T &a, const Array< T > &b)
Definition ArrayMath.h:589
T interFractileRange(const Array< T > &a, float fraction, bool sorted=false, bool inPlace=false)
TODO shouldn't take a const Array for in place sorting.
Definition ArrayMath.h:801
Array< T > operator/(const Array< T > &left, const T &right)
Array< T > operator&(const Array< T > &left, const Array< T > &right)
void arrayContTransform(const Array< L > &left, R right, Array< RES > &result, BinaryOperator op)
Transform left and right to a result using the binary operator.
Definition ArrayMath.h:180
void arrayContTransform(L left, const Array< R > &right, Array< RES > &result, BinaryOperator op)
Transform left and right to a result using the binary operator.
Definition ArrayMath.h:197
T median(const Array< T > &a, std::vector< T > &scratch, bool sorted, bool takeEvenMean, bool inPlace=false)
The median of "a" is a(n/2).
void operator+=(Array< T > &left, const T &other)
Element by element arithmetic modifying left in-place.
Array< T > floormod(const Array< T > &a, const Array< T > &b)
Array< std::complex< T > > makeComplex(const T &real, const Array< T > &imag)
void indgen(Array< T > &a)
Fills all elements of "array" with a sequence starting with 0 and ending with nelements() - 1.
Definition ArrayMath.h:619
T variance(const Array< T > &a, T mean)
Rather than using a computed mean, use the supplied value.
Array< T > min(const Array< T > &a, const Array< T > &b)
Return an array that contains the minimum of "a" and "b" at each position.
void operator^=(Array< T > &left, const Array< T > &other)
T madfmInPlace(const Array< T > &a, bool sorted=false)
TODO shouldn't take a const Array for in place sorting.
Definition ArrayMath.h:769
void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const Array< ScalarType > &array)
Find the minimum and maximum values of an array, including their locations.
void operator*=(Array< T > &left, const Array< T > &other)
Definition ArrayMath.h:310
T fractile(const Array< T > &a, float fraction, bool sorted=false, bool inPlace=false)
TODO shouldn't take a const Array for in place sorting.
Definition ArrayMath.h:787
void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const Array< ScalarType > &array, const Array< bool > &mask, bool valid=true)
The array is searched at locations where the mask equals valid.
void min(Array< T > &result, const Array< T > &a, const T &b)
"result" contains the minimum of "a" and "b" at each position.
void amplitude(Array< float > &rarray, const Array< std::complex< float > > &carray)
Modifies rarray in place.
Array< T > cube(const Array< T > &val)
Returns an array where every element is cubed.
void real(Array< double > &rarray, const Array< std::complex< double > > &carray)
Array< std::complex< double > > conj(const Array< std::complex< double > > &carray)
T * expandRecursive(int axis, const IPosition &shp, const IPosition &mult, const IPosition &inSteps, const T *in, T *out, const IPosition &alternate)
Helper function for expandArray using recursion for each axis.
Definition ArrayMath.h:1032
Array< float > ComplexToReal(const Array< std::complex< float > > &carray)
Copy an array of complex into an array of real,imaginary pairs.
Array< T > fmod(const Array< T > &a, const Array< T > &b)
void setImag(Array< C > &carray, const Array< R > &rarray)
Set the imaginary part of the left complex array to right real array.
Array< T > operator^(const Array< T > &left, const Array< T > &right)
Array< T > operator&(const T &left, const Array< T > &right)
T rms(const Array< T > &a)
The root-mean-square of "a" is the sqrt of sum(a*a)/N.
void conj(Array< std::complex< double > > &rarray, const Array< std::complex< double > > &carray)
Array< T > fmod(const T &a, const Array< T > &b)
Array< T > operator&(const Array< T > &left, const T &right)