casacore
Loading...
Searching...
No Matches
ArrayUtil.h
Go to the documentation of this file.
1//# ArrayUtil.h: Utility functions for arrays
2//# Copyright (C) 1995,1999,2000,2001
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_ARRAYUTIL_2_H
27#define CASA_ARRAYUTIL_2_H
28
29
30//# Includes
31#include "Vector.h"
32
33#include <regex>
34#include <string>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38// <summary>
39// Split a std::string into its elements.
40// </summary>
41
42// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil">
43
44// <prerequisite>
45// <li> <linkto class=Vector>Vector</linkto>
46// <li> std::string
47// </prerequisite>
48
49// <etymology>
50// stringToVector converts a std::string to a Vector of Strings.
51// </etymology>
52
53// <synopsis>
54// The function stringToVector splits a string into its elements
55// using the given delimiter and returns them in a <src>Vector<std::string></src>.
56// The default delimiter is a comma (,).
57// It is very useful when using a function taking a vector of strings
58// as shown in the example.
59// <p>
60// A more advanced way of splitting a string is by using a
61// regular expression as delimiter.
62// It makes it, for example, possible to treat whitespace around a comma
63// as part of the delimiter (as shown in an example below).
64// <p>
65// A string with length 0 results in a zero-length vector.
66// </synopsis>
67
68// <motivation>
69// As shown in the example, the function stringToVector makes
70// passing a Vector of Strings far easier.
71// </motivation>
72
73// <example>
74// <srcblock>
75// someFunction (stringToVector ("abc,def ,,gh"));
76// </srcblock>
77// This results in a vector with 4 elements containing the values
78// "abc", "def ", "", and "gh". The vector is passed to someFunction.
79// This is far easier than having to do it as:
80// <srcblock>
81// Vector<std::string> vector(4);
82// vector(0) = "abc";
83// vector(1) = "def ";
84// vector(2) = "";
85// vector(3) = "gh";
86// someFunction (vector);
87// </srcblock>
88//
89// The following example shows how to use a delimiter consisting of a comma
90// surrounded by possible whitespace.
91// <srcblock>
92// Vector<std::string> result = stringToVector (source, Regex(" *, *"));
93// </srcblock>
94// <example>
95
96// <group name=stringToVector>
97Vector<std::string> strToVector (const std::string& string, char delim = ',');
98Vector<std::string> strToVector (const std::string& string, const std::regex& delim);
99// </group>
100
101
102
103// <summary>
104// Concatenate two Arrays.
105// </summary>
106
107// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil">
108
109// <prerequisite>
110// <li> <linkto class=Array>Array</linkto>
111// </prerequisite>
112
113// <etymology>
114// concatenateArray concatenates two Arrays into a new Array.
115// </etymology>
116
117// <synopsis>
118// The function concatenates two Arrays into a new Array.
119// The shape of both arrays must match except for the last dimension.
120// The shape of the resulting array is equal to that of the input
121// arrays with its last dimension as the sum of both last dimensions.
122// <p>
123// An exception ArrayConformanceError is thrown when the shapes
124// do not match.
125// </synopsis>
126
127// <motivation>
128// The table system needed this function.
129// </motivation>
130
131// <example>
132// <srcblock>
133// Vector<int> vector1(5);
134// Vector<int> vector2(10);
135// indgen (vector1); // fill with values 0..4
136// indgen (vector2); // fill with values 0..9
137// Vector<int> result = concatenateVector (vector1, vector2);
138// </srcblock>
139// The example above results in a vector with length 15 and values
140// 0,1,2,3,4,0,1,2,3,4,5,6,7,8,9.
141// <p>
142// It can also be used with matrices or arrays with higher dimensionality
143// as long as all dimensions but the last one have equal length.
144// <srcblock>
145// Matrix<int> matrix1 (3,4);
146// Matrix<int> matrix2 (3,5);
147// Matrix<int> matrix3 (4,4);
148// // Concatenation of matrix1 and matrix 2 will succeed and result
149// // in a 3x9 matrix.
150// Matrix<int> matrixConc = concatenateArray (matrix1, matrix2);
151// if (matrixConc.shape() != IPosition(2,3,9)) {
152// cout << "Error in shape of concatenated matrices" << endl;
153// }
154// // Concatenation of matrix1 and matrix3 will fail, because the
155// // first dimensions have a different length (3 vs. 4).
156// try {
157// concatenateArray (matrix1, matrix2);
158// } catch (ArrayConformanceError x) {
159// cout << x.what() << endl;
160// }
161// </srcblock>
162// <example>
163
164// <group name=concatenateArray>
165template<class T>
166Array<T> concatenateArray (const Array<T>& left, const Array<T>& right);
167// </group>
168
169
170
171// <summary> Helper function for partialX functions </summary>
172// <use visibility=export>
173// <synopsis>
174// This is a specialized helper function for functions like partialSums.
175// It determines the shape of the resulting array and calculates the
176// result increments when iterating linearly through the source array.
177// It returns the first result axis which indicates the number of the first
178// contiguous collapse axes. The number of contiguous data points is
179// returned in nelemCont.
180// </synopsis>
181// <group name=partialFuncHelper>
182size_t partialFuncHelper (int& nelemCont,
183 IPosition& resultShape, IPosition& incr,
184 const IPosition& sourceShape,
185 const IPosition& collapseAxes);
186// </group>
187
188
189// <summary>
190// Reverse the order of one or more axes of an array.
191// </summary>
192
193// <use visibility=export>
194
195// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil2.cc">
196
197// <synopsis>
198// This function makes it possible to reverse one or more axes of an array by
199// swapping around the elements of each axis.
200// The resulting array is a copy of the input array with its data
201// moved around according to the new order.
202// If the order does not change, a copy is returned if the
203// <src>alwaysCopy</src> is true. Otherwise a reference of the
204// input array is returned.
205// </synopsis>
206
207// <example>
208// Reversing axis 0 of a Vector means that the Vector is reversed.
209// Reversing axis 1 of a Matrix means that its rows are reversed.
210// Reversing axis 0 of an N-dim array means that the elements of each Vector
211// in that array are reversed.
212// </example>
213
214// <group name=reverseArray>
215template<class T>
216Array<T> reverseArray (const Array<T>& array,
217 const IPosition& reversedAxes,
218 bool alwaysCopy = true);
219template<class T>
220Array<T> reverseArray (const Array<T>& array, size_t axis,
221 bool alwaysCopy = true);
222// </group>
223
224
225// <summary>
226// Reorder the axes of an array.
227// </summary>
228
229// <use visibility=export>
230
231// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil2.cc">
232
233// <synopsis>
234// This function makes it possible to reorder the axes of an array.
235// The resulting array is a copy of the input array with its data
236// moved around according to the new array order.
237// If the order does not change, a copy is returned if the
238// <src>alwaysCopy</src> is true. Otherwise a reference of the
239// input array is returned.
240// <p>
241// The <src>newAxisOrder</src> defines the new axes order.
242// Its length can be less than the dimensionality of the input array.
243// It is appended with the non-specified axes in their natural order.
244// <src>newAxisOrder(i)</src> gives the axis in the original array
245// which will now get axis <src>i</src>.
246// </synopsis>
247
248// <example>
249// <srcblock>
250// Array<int> result = reorderArray (someArray, IPosition(2,1,3));
251// </srcblock>
252// Say that someArray is a 4D array with shape [3,4,5,6].
253// The non-specified axes get appended to the axis order
254// specification [1,3] resulting in [1,3,0,2].
255// <br> This means that axis 1 gets axis 0, axis 3 gets axis 1, axis 0 gets
256// axis 2, and axis 2 gets axis 3.
257// Thus the resulting shape is [4,6,3,5] and the data are moved accordingly.
258// </example>
259
260// <motivation>
261// This function was needed for an efficient implementation of the
262// functions partialMedians and partialFractiles.
263// </motivation>
264
265// <group name=reorderArray>
266template<class T>
267Array<T> reorderArray (const Array<T>& array,
268 const IPosition& newAxisOrder,
269 bool alwaysCopy = true);
270// </group>
271
272
273// <summary>
274// Helper function for function reorderArray.
275// </summary>
276
277// <use visibility=local>
278
279// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil2.cc">
280
281// <synopsis>
282// This is a specialized helper function for function reorderArray.
283// It determines the shape of the resulting array and calculates the
284// result increments when iterating linearly through the source array.
285// It returns the number of the first non-reordered axes.
286// </synopsis>
287
288// <motivation>
289// Split off common non-templated code.
290// </motivation>
291
292// <group name=reorderArrayHelper>
293size_t reorderArrayHelper (IPosition& newShape, IPosition& incr,
294 const IPosition& shape, const IPosition& newAxisOrder);
295// </group>
296
297
298
299} //# NAMESPACE CASACORE - END
300
301#include "ArrayUtil.tcc"
302
303#endif
this file contains all the compiler specific defines
Definition mainpage.dox:28
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
Definition ExprNode.h:1991
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
Array< T > concatenateArray(const Array< T > &left, const Array< T > &right)
Helper function for partialX functions.
Definition ArrayUtil.h:182
size_t partialFuncHelper(int &nelemCont, IPosition &resultShape, IPosition &incr, const IPosition &sourceShape, const IPosition &collapseAxes)
Helper function for function reorderArray.
Definition ArrayUtil.h:293
size_t reorderArrayHelper(IPosition &newShape, IPosition &incr, const IPosition &shape, const IPosition &newAxisOrder)
Array< T > reorderArray(const Array< T > &array, const IPosition &newAxisOrder, bool alwaysCopy=true)
Reverse the order of one or more axes of an array.
Definition ArrayUtil.h:215
Array< T > reverseArray(const Array< T > &array, size_t axis, bool alwaysCopy=true)
Array< T > reverseArray(const Array< T > &array, const IPosition &reversedAxes, bool alwaysCopy=true)
Vector< std::string > strToVector(const std::string &string, char delim=',')
Vector< std::string > strToVector(const std::string &string, const std::regex &delim)