casa
$Rev:20696$
|
00001 //# ArrayUtil.h: Utility functions for arrays 00002 //# Copyright (C) 1995,1999,2000,2001 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: ArrayUtil.h 21285 2012-11-14 15:36:59Z gervandiepen $ 00027 00028 #ifndef CASA_ARRAYUTIL_H 00029 #define CASA_ARRAYUTIL_H 00030 00031 00032 //# Includes 00033 #include <casa/aips.h> 00034 #include <casa/Arrays/Vector.h> 00035 #include <casa/BasicSL/String.h> 00036 00037 namespace casa { //# NAMESPACE CASA - BEGIN 00038 00039 //# Forward Declarations 00040 class Regex; 00041 00042 // <summary> 00043 // Split a String into its elements. 00044 // </summary> 00045 00046 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil"> 00047 00048 // <prerequisite> 00049 // <li> <linkto class=Vector>Vector</linkto> 00050 // <li> <linkto class=String>String</linkto> 00051 // </prerequisite> 00052 00053 // <etymology> 00054 // stringToVector converts a String to a Vector of Strings. 00055 // </etymology> 00056 00057 // <synopsis> 00058 // The function stringToVector splits a string into its elements 00059 // using the given delimiter and returns them in a <src>Vector<String></src>. 00060 // The default delimiter is a comma (,). 00061 // It is very useful when using a function taking a vector of strings 00062 // as shown in the example. 00063 // <p> 00064 // A more advanced way of splitting a string is by using a 00065 // <linkto class=Regex>regular expression</linkto> as delimiter. 00066 // It makes it, for example, possible to treat whitespace around a comma 00067 // as part of the delimiter (as shown in an example below). 00068 // <p> 00069 // A string with length 0 results in a zero-length vector. 00070 // </synopsis> 00071 00072 // <motivation> 00073 // As shown in the example, the function stringToVector makes 00074 // passing a Vector of Strings far easier. 00075 // </motivation> 00076 00077 // <example> 00078 // <srcblock> 00079 // someFunction (stringToVector ("abc,def ,,gh")); 00080 // </srcblock> 00081 // This results in a vector with 4 elements containing the values 00082 // "abc", "def ", "", and "gh". The vector is passed to someFunction. 00083 // This is far easier than having to do it as: 00084 // <srcblock> 00085 // Vector<String> vector(4); 00086 // vector(0) = "abc"; 00087 // vector(1) = "def "; 00088 // vector(2) = ""; 00089 // vector(3) = "gh"; 00090 // someFunction (vector); 00091 // </srcblock> 00092 // 00093 // The following example shows how to use a delimiter consisting of a comma 00094 // surrounded by possible whitespace. 00095 // <srcblock> 00096 // Vector<String> result = stringToVector (source, Regex(" *, *")); 00097 // </srcblock> 00098 // <example> 00099 00100 // <group name=stringToVector> 00101 Vector<String> stringToVector (const String& string, char delim = ','); 00102 Vector<String> stringToVector (const String& string, const Regex& delim); 00103 // </group> 00104 00105 00106 00107 // <summary> 00108 // Concatenate two Arrays. 00109 // </summary> 00110 00111 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil"> 00112 00113 // <prerequisite> 00114 // <li> <linkto class=Array>Array</linkto> 00115 // </prerequisite> 00116 00117 // <etymology> 00118 // concatenateArray concatenates two Arrays into a new Array. 00119 // </etymology> 00120 00121 // <synopsis> 00122 // The function concatenates two Arrays into a new Array. 00123 // The shape of both arrays must match except for the last dimension. 00124 // The shape of the resulting array is equal to that of the input 00125 // arrays with its last dimension as the sum of both last dimensions. 00126 // <p> 00127 // An exception ArrayConformanceError is thrown when the shapes 00128 // do not match. 00129 // </synopsis> 00130 00131 // <motivation> 00132 // The table system needed this function. 00133 // </motivation> 00134 00135 // <example> 00136 // <srcblock> 00137 // Vector<Int> vector1(5); 00138 // Vector<Int> vector2(10); 00139 // indgen (vector1); // fill with values 0..4 00140 // indgen (vector2); // fill with values 0..9 00141 // Vector<Int> result = concatenateVector (vector1, vector2); 00142 // </srcblock> 00143 // The example above results in a vector with length 15 and values 00144 // 0,1,2,3,4,0,1,2,3,4,5,6,7,8,9. 00145 // <p> 00146 // It can also be used with matrices or arrays with higher dimensionality 00147 // as long as all dimensions but the last one have equal length. 00148 // <srcblock> 00149 // Matrix<Int> matrix1 (3,4); 00150 // Matrix<Int> matrix2 (3,5); 00151 // Matrix<Int> matrix3 (4,4); 00152 // // Concatenation of matrix1 and matrix 2 will succeed and result 00153 // // in a 3x9 matrix. 00154 // Matrix<Int> matrixConc = concatenateArray (matrix1, matrix2); 00155 // if (matrixConc.shape() != IPosition(2,3,9)) { 00156 // cout << "Error in shape of concatenated matrices" << endl; 00157 // } 00158 // // Concatenation of matrix1 and matrix3 will fail, because the 00159 // // first dimensions have a different length (3 vs. 4). 00160 // try { 00161 // concatenateArray (matrix1, matrix2); 00162 // } catch (ArrayConformanceError x) { 00163 // cout << x.getMesg() << endl; 00164 // } 00165 // </srcblock> 00166 // <example> 00167 00168 // <group name=concatenateArray> 00169 template<class T> 00170 Array<T> concatenateArray (const Array<T>& left, const Array<T>& right); 00171 // </group> 00172 00173 00174 00175 // <summary> Helper function for partialX functions </summary> 00176 // <use visibility=export> 00177 // <synopsis> 00178 // This is a specialized helper function for functions like partialSums. 00179 // It determines the shape of the resulting array and calculates the 00180 // result increments when iterating linearly through the source array. 00181 // It returns the first result axis which indicates the number of the first 00182 // contiguous collapse axes. The number of contiguous data points is 00183 // returned in nelemCont. 00184 // </synopsis> 00185 // <group name=partialFuncHelper> 00186 uInt partialFuncHelper (Int& nelemCont, 00187 IPosition& resultShape, IPosition& incr, 00188 const IPosition& sourceShape, 00189 const IPosition& collapseAxes); 00190 // </group> 00191 00192 00193 // <summary> 00194 // Reverse the order of one or more axes of an array. 00195 // </summary> 00196 00197 // <use visibility=export> 00198 00199 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil2.cc"> 00200 00201 // <synopsis> 00202 // This function makes it possible to reverse one or more axes of an array by 00203 // swapping around the elements of each axis. 00204 // The resulting array is a copy of the input array with its data 00205 // moved around according to the new order. 00206 // If the order does not change, a copy is returned if the 00207 // <src>alwaysCopy</src> is true. Otherwise a reference of the 00208 // input array is returned. 00209 // <p> 00210 // Reversing axis 0 means that its elements are reversed. 00211 // Reversing axis 1 means that the 00212 // </synopsis> 00213 00214 // <example> 00215 // Reversing axis 0 of a Vector means that the Vector is reversed. 00216 // Reversing axis 1 of a Matrix means that its rows are reversed. 00217 // Reversing axis 0 of an N-dim array means that the elements of each Vector 00218 // in that array are reversed. 00219 // Reversing axis 1 of a Matrix means that its columns are reversed. 00220 // </example> 00221 00222 // <group name=reverseArray> 00223 template<class T> 00224 Array<T> reverseArray (const Array<T>& array, 00225 const IPosition& reversedAxes, 00226 Bool alwaysCopy = True); 00227 template<class T> 00228 Array<T> reverseArray (const Array<T>& array, uInt axis, 00229 Bool alwaysCopy = True); 00230 // </group> 00231 00232 00233 // <summary> 00234 // Reorder the axes of an array. 00235 // </summary> 00236 00237 // <use visibility=export> 00238 00239 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil2.cc"> 00240 00241 // <synopsis> 00242 // This function makes it possible to reorder the axes of an array. 00243 // The resulting array is a copy of the input array with its data 00244 // moved around according to the new array order. 00245 // If the order does not change, a copy is returned if the 00246 // <src>alwaysCopy</src> is true. Otherwise a reference of the 00247 // input array is returned. 00248 // <p> 00249 // The <src>newAxisOrder</src> defines the new axes order. 00250 // Its length can be less than the dimensionality of the input array. 00251 // It is appended with the non-specified axes in their natural order. 00252 // <src>newAxisOrder(i)</src> gives the axis in the original array 00253 // which will now get axis <src>i</src>. 00254 // </synopsis> 00255 00256 // <example> 00257 // <srcblock> 00258 // Array<Int> result = reorderArray (someArray, IPosition(2,1,3)); 00259 // </srcblock> 00260 // Say that someArray is a 4D array with shape [3,4,5,6]. 00261 // The non-specified axes get appended to the axis order 00262 // specification [1,3] resulting in [1,3,0,2]. 00263 // <br> This means that axis 1 gets axis 0, axis 3 gets axis 1, axis 0 gets 00264 // axis 2, and axis 2 gets axis 3. 00265 // Thus the resulting shape is [4,6,3,5] and the data are moved accordingly. 00266 // </example> 00267 00268 // <motivation> 00269 // This function was needed for an efficient implementation of the 00270 // functions partialMedians and partialFractiles. 00271 // </motivation> 00272 00273 // <group name=reorderArray> 00274 template<class T> 00275 Array<T> reorderArray (const Array<T>& array, 00276 const IPosition& newAxisOrder, 00277 Bool alwaysCopy = True); 00278 // </group> 00279 00280 00281 // <summary> 00282 // Helper function for function reorderArray. 00283 // </summary> 00284 00285 // <use visibility=local> 00286 00287 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil2.cc"> 00288 00289 // <synopsis> 00290 // This is a specialized helper function for function reorderArray. 00291 // It determines the shape of the resulting array and calculates the 00292 // result increments when iterating linearly through the source array. 00293 // It returns the number of the first non-reordered axes. 00294 // </synopsis> 00295 00296 // <motivation> 00297 // Split off common non-templated code. 00298 // </motivation> 00299 00300 // <group name=reorderArrayHelper> 00301 uInt reorderArrayHelper (IPosition& newShape, IPosition& incr, 00302 const IPosition& shape, const IPosition& newAxisOrder); 00303 // </group> 00304 00305 00306 00307 } //# NAMESPACE CASA - END 00308 00309 #ifndef CASACORE_NO_AUTO_TEMPLATES 00310 #include <casa/Arrays/ArrayUtil.tcc> 00311 #endif //# CASACORE_NO_AUTO_TEMPLATES 00312 #endif