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
134 // The myxtransform functions are defined to avoid a bug in g++-4.3.
135 // That compiler generates incorrect code when only -g is used for
136 // a std::transform with a bind1st or bind2nd for a complex<float>.
137 // So, for example, the multiplication of a std::complex<float> array and std::complex<float> scalar
138 // would fail (see g++ bug 39678).
139 // <group>
140 // sequence = scalar OP sequence
141 template<typename _InputIterator1, typename T,
142 typename _OutputIterator, typename _BinaryOperation>
143 void
144 myltransform(_InputIterator1 __first1, _InputIterator1 __last1,
145 _OutputIterator __result, T left,
146 _BinaryOperation __binary_op)
147 {
148 for ( ; __first1 != __last1; ++__first1, ++__result)
149 *__result = __binary_op(left, *__first1);
150 }
151 // sequence = sequence OP scalar
152 template<typename _InputIterator1, typename T,
153 typename _OutputIterator, typename _BinaryOperation>
154 void
155 myrtransform(_InputIterator1 __first1, _InputIterator1 __last1,
156 _OutputIterator __result, T right,
157 _BinaryOperation __binary_op)
158 {
159 for ( ; __first1 != __last1; ++__first1, ++__result)
160 *__result = __binary_op(*__first1, right);
161 }
162 // sequence OP= scalar
163 template<typename _InputIterator1, typename T,
164 typename _BinaryOperation>
165 void
166 myiptransform(_InputIterator1 __first1, _InputIterator1 __last1,
167 T right,
168 _BinaryOperation __binary_op)
169 {
170 for ( ; __first1 != __last1; ++__first1)
171 *__first1 = __binary_op(*__first1, right);
172 }
173 // </group>
174
175
176// Functions to apply a binary or unary operator to arrays.
177// They are modeled after std::transform.
178// They do not check if the shapes conform; as in std::transform the
179// user must take care that the operands conform.
180// <group>
181// Transform left and right to a result using the binary operator.
182// Result MUST be a contiguous array.
183template<typename L, typename R, typename RES, typename BinaryOperator>
184inline void arrayContTransform (const Array<L>& left, const Array<R>& right,
185 Array<RES>& result, BinaryOperator op)
186{
187 assert (result.contiguousStorage());
188 if (left.contiguousStorage() && right.contiguousStorage()) {
189 std::transform (left.cbegin(), left.cend(), right.cbegin(),
190 result.cbegin(), op);
191 } else {
192 std::transform (left.begin(), left.end(), right.begin(),
193 result.cbegin(), op);
194 }
195}
196
197// Transform left and right to a result using the binary operator.
198// Result MUST be a contiguous array.
199template<typename L, typename R, typename RES, typename BinaryOperator>
200inline void arrayContTransform (const Array<L>& left, R right,
201 Array<RES>& result, BinaryOperator op)
202{
203 assert (result.contiguousStorage());
204 if (left.contiguousStorage()) {
205 myrtransform (left.cbegin(), left.cend(),
206 result.cbegin(), right, op);
209 } else {
210 myrtransform (left.begin(), left.end(),
211 result.cbegin(), right, op);
214 }
215}
216
217// Transform left and right to a result using the binary operator.
218// Result MUST be a contiguous array.
219template<typename L, typename R, typename RES, typename BinaryOperator>
220inline void arrayContTransform (L left, const Array<R>& right,
221 Array<RES>& result, BinaryOperator op)
222{
223 assert (result.contiguousStorage());
224 if (right.contiguousStorage()) {
225 myltransform (right.cbegin(), right.cend(),
226 result.cbegin(), left, op);
229 } else {
230 myltransform (right.begin(), right.end(),
231 result.cbegin(), left, op);
234 }
235}
236
237// Transform array to a result using the unary operator.
238// Result MUST be a contiguous array.
239template<typename T, typename RES, typename UnaryOperator>
240inline void arrayContTransform (const Array<T>& arr,
241 Array<RES>& result, UnaryOperator op)
242{
243 assert (result.contiguousStorage());
244 if (arr.contiguousStorage()) {
245 std::transform (arr.cbegin(), arr.cend(), result.cbegin(), op);
246 } else {
247 std::transform (arr.begin(), arr.end(), result.cbegin(), op);
248 }
249}
250
251// Transform left and right to a result using the binary operator.
252// Result need not be a contiguous array.
253template<typename L, typename R, typename RES, typename BinaryOperator>
254void arrayTransform (const Array<L>& left, const Array<R>& right,
255 Array<RES>& result, BinaryOperator op);
256
257// Transform left and right to a result using the binary operator.
258// Result need not be a contiguous array.
259template<typename L, typename R, typename RES, typename BinaryOperator>
260void arrayTransform (const Array<L>& left, R right,
261 Array<RES>& result, BinaryOperator op);
262
263// Transform left and right to a result using the binary operator.
264// Result need not be a contiguous array.
265template<typename L, typename R, typename RES, typename BinaryOperator>
266void arrayTransform (L left, const Array<R>& right,
267 Array<RES>& result, BinaryOperator op);
268
269// Transform array to a result using the unary operator.
270// Result need not be a contiguous array.
271template<typename T, typename RES, typename UnaryOperator>
272void arrayTransform (const Array<T>& arr,
273 Array<RES>& result, UnaryOperator op);
274
275// Transform left and right to a result using the binary operator.
276// The created and returned result array is contiguous.
277template<typename T, typename BinaryOperator>
279 BinaryOperator op);
280
281// Transform left and right to a result using the binary operator.
282// The created and returned result array is contiguous.
283template<typename T, typename BinaryOperator>
284Array<T> arrayTransformResult (const Array<T>& left, T right, BinaryOperator op);
285
286// Transform left and right to a result using the binary operator.
287// The created and returned result array is contiguous.
288template<typename T, typename BinaryOperator>
289Array<T> arrayTransformResult (T left, const Array<T>& right, BinaryOperator op);
290
291// Transform array to a result using the unary operator.
292// The created and returned result array is contiguous.
293template<typename T, typename UnaryOperator>
294Array<T> arrayTransformResult (const Array<T>& arr, UnaryOperator op);
295
296// Transform left and right in place using the binary operator.
297// The result is stored in the left array (useful for e.g. the += operation).
298template<typename L, typename R, typename BinaryOperator>
299inline void arrayTransformInPlace (Array<L>& left, const Array<R>& right,
300 BinaryOperator op)
301{
302 if (left.contiguousStorage() && right.contiguousStorage()) {
303 std::transform(left.cbegin(), left.cend(), right.cbegin(), left.cbegin(), op);
304 } else {
305 std::transform(left.begin(), left.end(), right.begin(), left.begin(), op);
306 }
307}
308
309// Transform left and right in place using the binary operator.
310// The result is stored in the left array (useful for e.g. the += operation).
311template<typename L, typename R, typename BinaryOperator>
312inline void arrayTransformInPlace (Array<L>& left, R right, BinaryOperator op)
313{
314 if (left.contiguousStorage()) {
315 myiptransform (left.cbegin(), left.cend(), right, op);
317 } else {
318 myiptransform (left.begin(), left.end(), right, op);
320 }
321}
322
323// Transform the array in place using the unary operator.
324// E.g. doing <src>arrayTransformInPlace(array, Sin<T>())</src> is faster than
325// <src>array=sin(array)</src> as it does not need to create a temporary array.
326template<typename T, typename UnaryOperator>
327inline void arrayTransformInPlace (Array<T>& arr, UnaryOperator op)
328{
329 if (arr.contiguousStorage()) {
330 std::transform(arr.cbegin(), arr.cend(), arr.cbegin(), op);
331 } else {
332 std::transform(arr.begin(), arr.end(), arr.begin(), op);
333 }
334}
335// </group>
336
337//
338// Element by element arithmetic modifying left in-place. left and other
339// must be conformant.
340// <group>
341template<typename T> void operator+= (Array<T> &left, const Array<T> &other);
342template<typename T> void operator-= (Array<T> &left, const Array<T> &other);
343template<typename T> void operator*= (Array<T> &left, const Array<T> &other)
344{
345 checkArrayShapes (left, other, "*=");
346 arrayTransformInPlace (left, other, std::multiplies<T>());
347}
348
349template<typename T> void operator/= (Array<T> &left, const Array<T> &other)
350{
351 checkArrayShapes (left, other, "/=");
352 arrayTransformInPlace (left, other, std::divides<T>());
353}
354template<typename T> void operator%= (Array<T> &left, const Array<T> &other);
355template<typename T> void operator&= (Array<T> &left, const Array<T> &other);
356template<typename T> void operator|= (Array<T> &left, const Array<T> &other);
357template<typename T> void operator^= (Array<T> &left, const Array<T> &other);
358// </group>
359
360//
361// Element by element arithmetic modifying left in-place. The scalar "other"
362// behaves as if it were a conformant Array to left filled with constant values.
363// <group>
364template<typename T> void operator+= (Array<T> &left, const T &other);
365template<typename T> void operator-= (Array<T> &left, const T &other);
366template<typename T> void operator*= (Array<T> &left, const T &other)
367{
368 arrayTransformInPlace (left, other, std::multiplies<T>());
369}
370template<typename T> void operator/= (Array<T> &left, const T &other)
371{
372 arrayTransformInPlace (left, other, std::divides<T>());
373}
374template<typename T> void operator%= (Array<T> &left, const T &other);
375template<typename T> void operator&= (Array<T> &left, const T &other);
376template<typename T> void operator|= (Array<T> &left, const T &other);
377template<typename T> void operator^= (Array<T> &left, const T &other);
378// </group>
379
380// Unary arithmetic operation.
381//
382// <group>
383template<typename T> Array<T> operator+(const Array<T> &a);
384template<typename T> Array<T> operator-(const Array<T> &a);
385template<typename T> Array<T> operator~(const Array<T> &a);
386// </group>
387
388//
389// Element by element arithmetic on two arrays, returning an array.
390// <group>
391template<typename T>
392 Array<T> operator+ (const Array<T> &left, const Array<T> &right);
393template<typename T>
394 Array<T> operator- (const Array<T> &left, const Array<T> &right);
395template<typename T>
396 Array<T> operator*(const Array<T> &left, const Array<T> &right)
397{
398 checkArrayShapes (left, right, "*");
399 return arrayTransformResult (left, right, std::multiplies<T>());
400}
401template<typename T>
402 Array<T> operator/ (const Array<T> &left, const Array<T> &right);
403template<typename T>
404 Array<T> operator% (const Array<T> &left, const Array<T> &right);
405template<typename T>
406 Array<T> operator| (const Array<T> &left, const Array<T> &right);
407template<typename T>
408 Array<T> operator& (const Array<T> &left, const Array<T> &right);
409template<typename T>
410 Array<T> operator^ (const Array<T> &left, const Array<T> &right);
411// </group>
412
413//
414// Element by element arithmetic between an array and a scalar, returning
415// an array.
416// <group>
417template<typename T>
418 Array<T> operator+ (const Array<T> &left, const T &right);
419template<typename T>
420 Array<T> operator- (const Array<T> &left, const T &right);
421template<class T>
422Array<T> operator* (const Array<T> &left, const T &right)
423{
424 return arrayTransformResult (left, right, std::multiplies<T>());
425}
426template<typename T>
427 Array<T> operator/ (const Array<T> &left, const T &right);
428template<typename T>
429 Array<T> operator% (const Array<T> &left, const T &right);
430template<typename T>
431 Array<T> operator| (const Array<T> &left, const T &right);
432template<typename T>
433 Array<T> operator& (const Array<T> &left, const T &right);
434template<typename T>
435 Array<T> operator^ (const Array<T> &left, const T &right);
436// </group>
437
438//
439// Element by element arithmetic between a scalar and an array, returning
440// an array.
441// <group>
442template<typename T>
443 Array<T> operator+ (const T &left, const Array<T> &right);
444template<typename T>
445 Array<T> operator- (const T &left, const Array<T> &right);
446template<class T>
447Array<T> operator* (const T &left, const Array<T> &right)
448{
449 return arrayTransformResult (left, right, std::multiplies<T>());
450}
451
452template<typename T>
453 Array<T> operator/ (const T &left, const Array<T> &right);
454template<typename T>
455 Array<T> operator% (const T &left, const Array<T> &right);
456template<typename T>
457 Array<T> operator| (const T &left, const Array<T> &right);
458template<typename T>
459 Array<T> operator& (const T &left, const Array<T> &right);
460template<typename T>
461 Array<T> operator^ (const T &left, const Array<T> &right);
462// </group>
463
464//
465// Transcendental function that can be applied to essentially all numeric
466// types. Works on an element-by-element basis.
467// <group>
468template<typename T> Array<T> cos(const Array<T> &a);
469template<typename T> Array<T> cosh(const Array<T> &a);
470template<typename T> Array<T> exp(const Array<T> &a);
471template<typename T> Array<T> log(const Array<T> &a);
472template<typename T> Array<T> log10(const Array<T> &a);
473template<typename T> Array<T> pow(const Array<T> &a, const Array<T> &b);
474template<typename T> Array<T> pow(const T &a, const Array<T> &b);
475template<typename T> Array<T> sin(const Array<T> &a);
476template<typename T> Array<T> sinh(const Array<T> &a);
477template<typename T> Array<T> sqrt(const Array<T> &a);
478// </group>
479
480//
481// Transcendental function applied to the array on an element-by-element
482// basis. Although a template function, this does not make sense for all
483// numeric types.
484// <group>
485template<typename T> Array<T> acos(const Array<T> &a);
486template<typename T> Array<T> asin(const Array<T> &a);
487template<typename T> Array<T> atan(const Array<T> &a);
488template<typename T> Array<T> atan2(const Array<T> &y, const Array<T> &x);
489template<typename T> Array<T> atan2(const T &y, const Array<T> &x);
490template<typename T> Array<T> atan2(const Array<T> &y, const T &x);
491template<typename T> Array<T> ceil(const Array<T> &a);
492template<typename T> Array<T> fabs(const Array<T> &a);
493template<typename T> Array<T> abs(const Array<T> &a);
494template<typename T> Array<T> floor(const Array<T> &a);
495template<typename T> Array<T> round(const Array<T> &a);
496template<typename T> Array<T> sign(const Array<T> &a);
497template<typename T> Array<T> fmod(const Array<T> &a, const Array<T> &b);
498template<typename T> Array<T> fmod(const T &a, const Array<T> &b);
499template<typename T> Array<T> fmod(const Array<T> &a, const T &b);
500template<typename T> Array<T> floormod(const Array<T> &a, const Array<T> &b);
501template<typename T> Array<T> floormod(const T &a, const Array<T> &b);
502template<typename T> Array<T> floormod(const Array<T> &a, const T &b);
503template<typename T> Array<T> pow(const Array<T> &a, const T &b);
504template<typename T> Array<std::complex<T>> pow(const Array<std::complex<T>> &a, const T &b);
505template<typename T> Array<T> tan(const Array<T> &a);
506template<typename T> Array<T> tanh(const Array<T> &a);
507// N.B. fabs is deprecated. Use abs.
508template<typename T> Array<T> fabs(const Array<T> &a);
509// </group>
510
511//
512// <group>
513// Find the minimum and maximum values of an array, including their locations.
514template<typename ScalarType>
515void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos,
516 IPosition &maxPos, const Array<ScalarType> &array);
517// The array is searched at locations where the mask equals <src>valid</src>.
518// (at least one such position must exist or an exception will be thrown).
519// MaskType should be an Array of bool.
520template<typename ScalarType>
521void minMax(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos,
522 IPosition &maxPos, const Array<ScalarType> &array,
523 const Array<bool> &mask, bool valid=true);
524// The array * weight is searched
525template<typename ScalarType>
526void minMaxMasked(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos,
527 IPosition &maxPos, const Array<ScalarType> &array,
528 const Array<ScalarType> &weight);
529// </group>
530
531//
532// The "min" and "max" functions require that the type "T" have comparison
533// operators.
534// <group>
535//
536// This sets min and max to the minimum and maximum of the array to
537// avoid having to do two passes with max() and min() separately.
538template<typename T> void minMax(T &min, T &max, const Array<T> &a);
539//
540// The minimum element of the array.
541// Requires that the type "T" has comparison operators.
542template<typename T> T min(const Array<T> &a);
543// The maximum element of the array.
544// Requires that the type "T" has comparison operators.
545template<typename T> T max(const Array<T> &a);
546
547// "result" contains the maximum of "a" and "b" at each position. "result",
548// "a", and "b" must be conformant.
549template<typename T> void max(Array<T> &result, const Array<T> &a,
550 const Array<T> &b);
551// "result" contains the minimum of "a" and "b" at each position. "result",
552// "a", and "b" must be conformant.
553template<typename T> void min(Array<T> &result, const Array<T> &a,
554 const Array<T> &b);
555// Return an array that contains the maximum of "a" and "b" at each position.
556// "a" and "b" must be conformant.
557template<typename T> Array<T> max(const Array<T> &a, const Array<T> &b);
558template<typename T> Array<T> max(const T &a, const Array<T> &b);
559// Return an array that contains the minimum of "a" and "b" at each position.
560// "a" and "b" must be conformant.
561template<typename T> Array<T> min(const Array<T> &a, const Array<T> &b);
562
563// "result" contains the maximum of "a" and "b" at each position. "result",
564// and "a" must be conformant.
565template<typename T> void max(Array<T> &result, const Array<T> &a,
566 const T &b);
567template<typename T> inline void max(Array<T> &result, const T &a,
568 const Array<T> &b)
569 { max (result, b, a); }
570// "result" contains the minimum of "a" and "b" at each position. "result",
571// and "a" must be conformant.
572template<typename T> void min(Array<T> &result, const Array<T> &a,
573 const T &b);
574template<typename T> inline void min(Array<T> &result, const T &a,
575 const Array<T> &b)
576 { min (result, b, a); }
577// Return an array that contains the maximum of "a" and "b" at each position.
578template<typename T> Array<T> max(const Array<T> &a, const T &b);
579template<typename T> inline Array<T> max(const T &a, const Array<T> &b)
580 { return max(b, a); }
581// Return an array that contains the minimum of "a" and "b" at each position.
582template<typename T> Array<T> min(const Array<T> &a, const T &b);
583template<typename T> inline Array<T> min(const T &a, const Array<T> &b)
584 { return min(b, a); }
585// </group>
586
587//
588// Fills all elements of "array" with a sequence starting with "start"
589// and incrementing by "inc" for each element. The first axis varies
590// most rapidly.
591template<typename T> void indgen(Array<T> &a, T start, T inc);
592//
593// Fills all elements of "array" with a sequence starting with 0
594// and ending with nelements() - 1. The first axis varies
595// most rapidly.
596template<typename T> inline void indgen(Array<T> &a)
597 { indgen(a, T(0), T(1)); }
598//
599// Fills all elements of "array" with a sequence starting with start
600// incremented by one for each position in the array. The first axis varies
601// most rapidly.
602template<typename T> inline void indgen(Array<T> &a, T start)
603 { indgen(a, start, T(1)); }
604
605// Create a Vector of the given length and fill it with the start value
606// incremented with <code>inc</code> for each element.
607template<typename T> inline Vector<T> indgen(size_t length, T start, T inc)
608{
609 Vector<T> x(length);
610 indgen(x, start, inc);
611 return x;
612}
613
614
615// Sum of every element of the array.
616template<typename T> T sum(const Array<T> &a);
617//
618// Sum the square of every element of the array.
619template<typename T> T sumsqr(const Array<T> &a);
620//
621// Product of every element of the array. This could of course easily
622// overflow.
623template<typename T> T product(const Array<T> &a);
624
625//
626// The mean of "a" is the sum of all elements of "a" divided by the number
627// of elements of "a".
628template<typename T> T mean(const Array<T> &a);
629
630// The variance of "a" is the sum of (a(i) - mean(a))**2/(a.nelements() - ddof).
631// Similar to numpy the argument ddof (delta degrees of freedom) tells if the
632// population variance (ddof=0) or the sample variance (ddof=1) is taken.
633// The variance functions proper use ddof=1.
634// <br>Note that for a complex valued T the absolute values are used; in that way
635// the variance is equal to the sum of the variances of the real and imaginary parts.
636// Hence the imaginary part in the return value is 0.
637template<typename T> T variance(const Array<T> &a);
638template<typename T> T pvariance(const Array<T> &a, size_t ddof=0);
639// Rather than using a computed mean, use the supplied value.
640template<typename T> T variance(const Array<T> &a, T mean);
641template<typename T> T pvariance(const Array<T> &a, T mean, size_t ddof=0);
642
643// The standard deviation of "a" is the square root of its variance.
644template<typename T> T stddev(const Array<T> &a);
645template<typename T> T pstddev(const Array<T> &a, size_t ddof=0);
646template<typename T> T stddev(const Array<T> &a, T mean);
647template<typename T> T pstddev(const Array<T> &a, T mean, size_t ddof=0);
648
649//
650// The average deviation of "a" is the sum of abs(a(i) - mean(a))/N. (N.B.
651// N, not N-1 in the denominator).
652template<typename T> T avdev(const Array<T> &a);
653//
654// The average deviation of "a" is the sum of abs(a(i) - mean(a))/N. (N.B.
655// N, not N-1 in the denominator).
656// Rather than using a computed mean, use the supplied value.
657template<typename T> T avdev(const Array<T> &a,T mean);
658
659//
660// The root-mean-square of "a" is the sqrt of sum(a*a)/N.
661template<typename T> T rms(const Array<T> &a);
662
663
664// The median of "a" is a(n/2).
665// If a has an even number of elements and the switch takeEvenMean is set,
666// the median is 0.5*(a(n/2) + a((n+1)/2)).
667// According to Numerical Recipes (2nd edition) it makes little sense to take
668// the mean if the array is large enough (> 100 elements). Therefore
669// the default for takeEvenMean is false if the array has > 100 elements,
670// otherwise it is true.
671// <br>If "sorted"==true we assume the data is already sorted and we
672// compute the median directly. Otherwise the function GenSort::kthLargest
673// is used to find the median (kthLargest is about 6 times faster
674// than a full quicksort).
675// <br>Finding the median means that the array has to be (partially)
676// sorted. By default a copy will be made, but if "inPlace" is in effect,
677// the data themselves will be sorted. That should only be used if the
678// data are used not thereafter.
679// <note>The function kthLargest in class GenSortIndirect can be used to
680// obtain the index of the median in an array. </note>
681// <group>
682// TODO shouldn't take a const Array for in place sorting
683template<typename T> T median(const Array<T> &a, std::vector<T> &scratch, bool sorted,
684 bool takeEvenMean, bool inPlace=false);
685// TODO shouldn't take a const Array for in place sorting
686template<typename T> T median(const Array<T> &a, bool sorted, bool takeEvenMean,
687 bool inPlace=false)
688 { std::vector<T> scratch; return median (a, scratch, sorted, takeEvenMean, inPlace); }
689template<typename T> inline T median(const Array<T> &a, bool sorted)
690 { return median (a, sorted, (a.nelements() <= 100), false); }
691template<typename T> inline T median(const Array<T> &a)
692 { return median (a, false, (a.nelements() <= 100), false); }
693// TODO shouldn't take a const Array for in place sorting
694template<typename T> inline T medianInPlace(const Array<T> &a, bool sorted=false)
695 { return median (a, sorted, (a.nelements() <= 100), true); }
696// </group>
697
698// The median absolute deviation from the median. Interface is as for
699// the median functions
700// <group>
701// TODO shouldn't take a const Array for in place sorting
702template<typename T> T madfm(const Array<T> &a, std::vector<T> &tmp, bool sorted,
703 bool takeEvenMean, bool inPlace = false);
704// TODO shouldn't take a const Array for in place sorting
705template<typename T> T madfm(const Array<T> &a, bool sorted, bool takeEvenMean,
706 bool inPlace=false)
707 { std::vector<T> tmp; return madfm(a, tmp, sorted, takeEvenMean, inPlace); }
708template<typename T> inline T madfm(const Array<T> &a, bool sorted)
709 { return madfm(a, sorted, (a.nelements() <= 100), false); }
710template<typename T> inline T madfm(const Array<T> &a)
711 { return madfm(a, false, (a.nelements() <= 100), false); }
712// TODO shouldn't take a const Array for in place sorting
713template<typename T> inline T madfmInPlace(const Array<T> &a, bool sorted=false)
714 { return madfm(a, sorted, (a.nelements() <= 100), true); }
715// </group>
716
717// Return the fractile of an array.
718// It returns the value at the given fraction of the array.
719// A fraction of 0.5 is the same as the median, be it that no mean of
720// the two middle elements is taken if the array has an even nr of elements.
721// It uses kthLargest if the array is not sorted yet.
722// <note>The function kthLargest in class GenSortIndirect can be used to
723// obtain the index of the fractile in an array. </note>
724// TODO shouldn't take a const Array for in place sorting
725template<typename T> T fractile(const Array<T> &a, std::vector<T> &tmp, float fraction,
726 bool sorted=false, bool inPlace=false);
727// TODO shouldn't take a const Array for in place sorting
728template<typename T> T fractile(const Array<T> &a, float fraction,
729 bool sorted=false, bool inPlace=false)
730 { std::vector<T> tmp; return fractile (a, tmp, fraction, sorted, inPlace); }
731
732// Return the inter-fractile range of an array.
733// This is the full range between the bottom and the top fraction.
734// <group>
735// TODO shouldn't take a const Array for in place sorting
736template<typename T> T interFractileRange(const Array<T> &a, std::vector<T> &tmp,
737 float fraction,
738 bool sorted=false, bool inPlace=false);
739// TODO shouldn't take a const Array for in place sorting
740template<typename T> T interFractileRange(const Array<T> &a, float fraction,
741 bool sorted=false, bool inPlace=false)
742 { std::vector<T> tmp; return interFractileRange(a, tmp, fraction, sorted, inPlace); }
743// </group>
744
745// Return the inter-hexile range of an array.
746// This is the full range between the bottom sixth and the top sixth
747// of ordered array values. "The semi-interhexile range is very nearly
748// equal to the rms for a Gaussian distribution, but it is much less
749// sensitive to the tails of extended distributions." (Condon et al
750// 1998)
751// <group>
752// TODO shouldn't take a const Array for in place sorting
753template<typename T> T interHexileRange(const Array<T> &a, std::vector<T> &tmp,
754 bool sorted=false, bool inPlace=false)
755 { return interFractileRange(a, tmp, 1./6., sorted, inPlace); }
756// TODO shouldn't take a const Array for in place sorting
757template<typename T> T interHexileRange(const Array<T> &a, bool sorted=false,
758 bool inPlace=false)
759 { return interFractileRange(a, 1./6., sorted, inPlace); }
760// </group>
761
762// Return the inter-quartile range of an array.
763// This is the full range between the bottom quarter and the top
764// quarter of ordered array values.
765// <group>
766// TODO shouldn't take a const Array for in place sorting
767template<typename T> T interQuartileRange(const Array<T> &a, std::vector<T> &tmp,
768 bool sorted=false, bool inPlace=false)
769 { return interFractileRange(a, tmp, 0.25, sorted, inPlace); }
770// TODO shouldn't take a const Array for in place sorting
771template<typename T> T interQuartileRange(const Array<T> &a, bool sorted=false,
772 bool inPlace=false)
773 { return interFractileRange(a, 0.25, sorted, inPlace); }
774// </group>
775
776
777// Methods for element-by-element scaling of complex and real.
778// Note that std::complex<float> and std::complex<double> are typedefs for std::complex.
779//<group>
780template<typename T>
781void operator*= (Array<std::complex<T>> &left, const Array<T> &other)
782{
783 checkArrayShapes (left, other, "*=");
784 arrayTransformInPlace (left, other,
785 [](std::complex<T> left, T right) { return left*right; });
786}
787
788template<typename T>
789void operator*= (Array<std::complex<T>> &left, const T &other)
790{
791 arrayTransformInPlace (left, other,
792 [](std::complex<T> left, T right) { return left*right; });
793}
794
795template<typename T>
796void operator/= (Array<std::complex<T>> &left, const Array<T> &other)
797{
798 checkArrayShapes (left, other, "/=");
799 arrayTransformInPlace (left, other,
800 [](std::complex<T> left, T right) { return left/right; });
801}
802
803template<typename T>
804void operator/= (Array<std::complex<T>> &left, const T &other)
805{
806 arrayTransformInPlace (left, other,
807 [](std::complex<T> left, T right) { return left/right; });
808}
809
810template<typename T>
811Array<std::complex<T>> operator* (const Array<std::complex<T>> &left,
812 const Array<T> &right)
813{
814 checkArrayShapes (left, right, "*");
815 Array<std::complex<T>> result(left.shape());
816 arrayContTransform (left, right, result,
817 [](std::complex<T> left, T right) { return left*right; });
818 return result;
819}
820template<typename T>
821Array<std::complex<T> > operator* (const Array<std::complex<T>> &left,
822 const T &other)
823{
824 Array<std::complex<T> > result(left.shape());
825 arrayContTransform (left, other, result,
826 [](std::complex<T> left, T right) { return left*right; });
827 return result;
828}
829template<typename T>
830Array<std::complex<T> > operator*(const std::complex<T> &left,
831 const Array<T> &other)
832{
833 Array<std::complex<T> > result(other.shape());
834 arrayContTransform (left, other, result,
835 [](std::complex<T> left, T right) { return left*right; });
836 return result;
837}
838
839template<typename T>
840Array<std::complex<T>> operator/ (const Array<std::complex<T>> &left,
841 const Array<T> &right)
842{
843 checkArrayShapes (left, right, "/");
844 Array<std::complex<T>> result(left.shape());
845 arrayContTransform (left, right, result,
846 [](std::complex<T> l, T r) { return l/r; });
847 return result;
848}
849template<typename T>
850Array<std::complex<T>> operator/ (const Array<std::complex<T>> &left,
851 const T &other)
852{
853 Array<std::complex<T>> result(left.shape());
854 arrayContTransform (left, other, result,
855 [](std::complex<T> left, T right) { return left/right; });
856 return result;
857}
858template<typename T>
859Array<std::complex<T>> operator/(const std::complex<T> &left,
860 const Array<T> &other)
861{
862 Array<std::complex<T>> result(other.shape());
863 arrayContTransform (left, other, result,
864 [](std::complex<T> left, T right) { return left/right; });
865 return result;
866}
867// </group>
868
869// Returns the complex conjugate of a complex array.
870//<group>
871Array<std::complex<float>> conj(const Array<std::complex<float>> &carray);
872Array<std::complex<double>> conj(const Array<std::complex<double>> &carray);
873// Modifies rarray in place. rarray must be conformant.
874void conj(Array<std::complex<float>> &rarray, const Array<std::complex<float>> &carray);
875void conj(Array<std::complex<double>> &rarray, const Array<std::complex<double>> &carray);
876//# The following are implemented to make the compiler find the right conversion
877//# more often.
878Matrix<std::complex<float>> conj(const Matrix<std::complex<float>> &carray);
879Matrix<std::complex<double>> conj(const Matrix<std::complex<double>> &carray);
880//</group>
881
882// Form an array of complex numbers from the given real arrays.
883// Note that std::complex<float> and std::complex<double> are simply typedefs for std::complex<float>
884// and std::complex<double>, so the result is in fact one of these types.
885// <group>
886template<typename T>
888template<typename T>
889Array<std::complex<T> > makeComplex(const T &real, const Array<T>& imag);
890template<typename T>
891Array<std::complex<T> > makeComplex(const Array<T> &real, const T& imag);
892// </group>
893
894// Set the real part of the left complex array to the right real array.
895template<typename C, typename R>
896void setReal(Array<C> &carray, const Array<R> &rarray);
897
898// Set the imaginary part of the left complex array to right real array.
899template<typename C, typename R>
900void setImag(Array<C> &carray, const Array<R> &rarray);
901
902// Extracts the real part of a complex array into an array of floats.
903// <group>
904Array<float> real(const Array<std::complex<float>> &carray);
905Array<double> real(const Array<std::complex<double>> &carray);
906// Modifies rarray in place. rarray must be conformant.
907void real(Array<float> &rarray, const Array<std::complex<float>> &carray);
908void real(Array<double> &rarray, const Array<std::complex<double>> &carray);
909// </group>
910
911//
912// Extracts the imaginary part of a complex array into an array of floats.
913// <group>
914Array<float> imag(const Array<std::complex<float>> &carray);
915Array<double> imag(const Array<std::complex<double>> &carray);
916// Modifies rarray in place. rarray must be conformant.
917void imag(Array<float> &rarray, const Array<std::complex<float>> &carray);
918void imag(Array<double> &rarray, const Array<std::complex<double>> &carray);
919// </group>
920
921//
922// Extracts the amplitude (i.e. sqrt(re*re + im*im)) from an array
923// of complex numbers. N.B. this is presently called "fabs" for a single
924// complex number.
925// <group>
926Array<float> amplitude(const Array<std::complex<float>> &carray);
927Array<double> amplitude(const Array<std::complex<double>> &carray);
928// Modifies rarray in place. rarray must be conformant.
929void amplitude(Array<float> &rarray, const Array<std::complex<float>> &carray);
930void amplitude(Array<double> &rarray, const Array<std::complex<double>> &carray);
931// </group>
932
933//
934// Extracts the phase (i.e. atan2(im, re)) from an array
935// of complex numbers. N.B. this is presently called "arg"
936// for a single complex number.
937// <group>
938Array<float> phase(const Array<std::complex<float>> &carray);
939Array<double> phase(const Array<std::complex<double>> &carray);
940// Modifies rarray in place. rarray must be conformant.
941void phase(Array<float> &rarray, const Array<std::complex<float>> &carray);
942void phase(Array<double> &rarray, const Array<std::complex<double>> &carray);
943// </group>
944
945// Copy an array of complex into an array of real,imaginary pairs. The
946// first axis of the real array becomes twice as long as the complex array.
947// In the future versions which work by reference will be available; presently
948// a copy is made.
949Array<float> ComplexToReal(const Array<std::complex<float>> &carray);
950Array<double> ComplexToReal(const Array<std::complex<double>> &carray);
951// Modify the array "rarray" in place. "rarray" must be the correct shape.
952// <group>
953void ComplexToReal(Array<float> &rarray, const Array<std::complex<float>> &carray);
954void ComplexToReal(Array<double> &rarray, const Array<std::complex<double>> &carray);
955// </group>
956
957// Copy an array of real,imaginary pairs into a complex array. The first axis
958// must have an even length.
959// In the future versions which work by reference will be available; presently
960// a copy is made.
963// Modify the array "carray" in place. "carray" must be the correct shape.
964// <group>
965void RealToComplex(Array<std::complex<float>> &carray, const Array<float> &rarray);
966void RealToComplex(Array<std::complex<double>> &carray, const Array<double> &rarray);
967// </group>
968
969// Make a copy of an array of a different type; for example make an array
970// of doubles from an array of floats. Arrays to and from must be conformant
971// (same shape). Also, it must be possible to convert a scalar of type U
972// to type T.
973template<typename T, typename U>
974void convertArray(Array<T> &to, const Array<U> &from);
975
976// Returns an array where every element is squared.
977template<typename T> Array<T> square(const Array<T> &val);
978
979// Returns an array where every element is cubed.
980template<typename T> Array<T> cube(const Array<T> &val);
981
982// Helper function for expandArray using recursion for each axis.
983template<typename T>
984T* expandRecursive (int axis, const IPosition& shp, const IPosition& mult,
985 const IPosition& inSteps,
986 const T* in, T* out, const IPosition& alternate)
987{
988 if (axis == 0) {
989 if (alternate[0]) {
990 // Copy as 1,2,3 1,2,3, etc.
991 for (ssize_t j=0; j<mult[0]; ++j) {
992 const T* pin = in;
993 for (ssize_t i=0; i<shp[0]; ++i) {
994 *out++ = *pin;
995 pin += inSteps[0];
996 }
997 }
998 } else {
999 // Copy as 1,1,1 2,2,2 etc.
1000 for (ssize_t i=0; i<shp[0]; ++i) {
1001 for (ssize_t j=0; j<mult[0]; ++j) {
1002 *out++ = *in;
1003 }
1004 in += inSteps[0];
1005 }
1006 }
1007 } else {
1008 if (alternate[axis]) {
1009 for (ssize_t j=0; j<mult[axis]; ++j) {
1010 const T* pin = in;
1011 for (ssize_t i=0; i<shp[axis]; ++i) {
1012 out = expandRecursive (axis-1, shp, mult, inSteps,
1013 pin, out, alternate);
1014 pin += inSteps[axis];
1015 }
1016 }
1017 } else {
1018 for (ssize_t i=0; i<shp[axis]; ++i) {
1019 for (ssize_t j=0; j<mult[axis]; ++j) {
1020 out = expandRecursive (axis-1, shp, mult, inSteps,
1021 in, out, alternate);
1022 }
1023 in += inSteps[axis];
1024 }
1025 }
1026 }
1027 return out;
1028}
1029
1030// Expand the values of an array. The arrays can have different dimensionalities.
1031// Missing input axes have length 1; missing output axes are discarded.
1032// The length of each axis in the input array must be <= the length of the
1033// corresponding axis in the output array and divide evenly.
1034// For each axis <src>mult</src> is set to output/input.
1035// <br>The <src>alternate</src> argument determines how the values are expanded.
1036// If a row contains values '1 2 3', they can be expanded "linearly"
1037// as '1 1 2 2 3 3' or alternately as '1 2 3 1 2 3'
1038// This choice can be made for each axis; a value 0 means linearly,
1039// another value means alternately. If the length of alternate is less than
1040// the dimensionality of the output array, the missing ones default to 0.
1041template<typename T>
1042void expandArray (Array<T>& out, const Array<T>& in,
1043 const IPosition& alternate=IPosition())
1044{
1045 IPosition mult, inshp, outshp;
1046 IPosition alt = checkExpandArray (mult, inshp,
1047 in.shape(), out.shape(), alternate);
1048 Array<T> incp(in);
1049 if (in.ndim() < inshp.size()) {
1050 incp.reference (in.reform(inshp));
1051 }
1052 // Make sure output is contiguous.
1053 bool deleteIt;
1054 T* outPtr = out.getStorage (deleteIt);
1055 expandRecursive (out.ndim()-1, inshp, mult, incp.steps(),
1056 incp.data(), outPtr, alt);
1057 out.putStorage (outPtr, deleteIt);
1058}
1059
1060// Check array shapes for expandArray. It returns the alternate argument,
1061// where possibly missing values are appended (as 0).
1062// It fills in mult and inshp (with possibly missing axes of length 1).
1063// <br><code>inShape</code> defines the shape of the input array.
1064// <br><code>outShape</code> defines the shape of the output array.
1065// <br><code>alternate</code> tells per axis if value expansion uses alternation.
1066// <br><code>newInShape</code> is the input shape with new axes (of length 1) added as needed
1067// <br><code>mult</code> is the multiplication (expansion) factor per output axis
1068// Returned is the alternation per output axis; new axes have value 0 (linear expansion)
1070 const IPosition& inShape,
1071 const IPosition& outShape,
1072 const IPosition& alternate);
1073
1074
1075// </group>
1076
1077
1078} //# NAMESPACE CASACORE - END
1079
1080#include "ArrayMath.tcc"
1081
1082#endif
const IPosition & steps() const
Return steps to be made if stepping one element in a dimension.
Definition ArrayBase.h:134
size_t ndim() const
The dimensionality of this array.
Definition ArrayBase.h:96
size_t nelements() const
How many elements does this array have? Product of all axis lengths.
Definition ArrayBase.h:101
bool contiguousStorage() const
Are the array data contiguous? If they are not contiguous, getStorage (see below) needs to make a cop...
Definition ArrayBase.h:114
const IPosition & shape() const
The length of each axis.
Definition ArrayBase.h:123
T * data()
Get a pointer to the beginning of the array.
Definition Array.h:592
iterator begin()
Get the begin iterator object for any array.
Definition Array.h:844
contiter cend()
Definition Array.h:860
contiter cbegin()
Get the begin iterator object for a contiguous array.
Definition Array.h:856
iterator end()
Definition Array.h:848
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:570
this file contains all the compiler specific defines
Definition mainpage.dox:28
LatticeExprNode fractile(const LatticeExprNode &expr, const LatticeExprNode &fraction)
Determine the value of the element at the part fraction from the beginning of the given lattice.
TableExprNode operator|(const TableExprNode &left, const TableExprNode &right)
Definition ExprNode.h:1190
void checkArrayShapes(const ArrayBase &left, const ArrayBase &right, const char *name)
Definition ArrayBase.h:323
LatticeExprNode mean(const LatticeExprNode &expr)
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
void indgen(TableVector< T > &tv, T start, T inc)
Definition TabVecMath.h:398
TableExprNode operator&(const TableExprNode &left, const TableExprNode &right)
Definition ExprNode.h:1185
LatticeExprNode operator%(const LatticeExprNode &left, const LatticeExprNode &right)
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 min(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode operator-(const LatticeExprNode &expr)
TableExprNode array(const TableExprNode &values, const TableExprNodeSet &shape)
Create an array of the given shape and fill it with the values.
Definition ExprNode.h:1933
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
LatticeExprNode operator/(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
LatticeExprNode operator^(const LatticeExprNode &left, const LatticeExprNode &right)
void transformInPlace(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryOperator op)
Define a function to do a binary transform in place.
Definition Functors.h:42
LatticeExprNode median(const LatticeExprNode &expr)
void max(Array< T > &result, const T &a, const Array< T > &b)
Definition ArrayMath.h:567
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< std::complex< T > > makeComplex(const Array< T > &real, const T &imag)
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:184
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:757
Array< float > phase(const Array< std::complex< float > > &carray)
Extracts the phase (i.e.
Array< std::complex< T > > operator*(const std::complex< T > &left, const Array< T > &other)
Definition ArrayMath.h:830
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:327
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 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:607
Array< T > min(const T &a, const Array< T > &b)
Definition ArrayMath.h:583
T min(const Array< T > &a)
The minimum element of the array.
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 conj(Array< std::complex< float > > &rarray, const Array< std::complex< float > > &carray)
Modifies rarray in place.
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:753
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:686
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:166
void operator/=(Array< T > &left, const Array< T > &other)
Definition ArrayMath.h:349
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:240
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:705
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:771
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...
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:694
void arrayTransformInPlace(Array< L > &left, R right, BinaryOperator op)
Transform left and right in place using the binary operator.
Definition ArrayMath.h:312
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:299
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:144
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)
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:396
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 minMaxMasked(ScalarType &minVal, ScalarType &maxVal, IPosition &minPos, IPosition &maxPos, const Array< ScalarType > &array, const Array< ScalarType > &weight)
The array * weight is searched
Array< T > cos(const Array< T > &a)
Transcendental function that can be applied to essentially all numeric types.
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.
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:1042
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:859
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:767
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:602
void imag(Array< double > &rarray, const Array< std::complex< double > > &carray)
Array< T > atan2(const Array< T > &y, const Array< T > &x)
T stddev(const Array< T > &a)
The standard deviation of "a" is the square root of its variance.
void myrtransform(_InputIterator1 __first1, _InputIterator1 __last1, _OutputIterator __result, T right, _BinaryOperation __binary_op)
sequence = sequence OP scalar
Definition ArrayMath.h:155
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< 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)
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.
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:574
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:740
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:200
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:220
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).
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:596
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:713
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:343
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:728
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:984
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)
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)