casacore
Loading...
Searching...
No Matches
MArrayUtil.h
Go to the documentation of this file.
1// # MArrayUtil.h: Utility functions for MArrays
2// # Copyright (C) 2012
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_MARRAYUTIL_H
27#define CASA_MARRAYUTIL_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Arrays/ArrayUtil.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// <summary>
36// Reorder the axes of the data in an MArray object
37// </summary>
38
39// <use visibility=export>
40
41// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tMArrayUtil.cc">
42
43// <synopsis>
44// This function makes it possible to reorder the axes of an MArray.
45// Both the data and the optional mask are reordered.
46// The resulting array is a copy of the input array with its data
47// moved around according to the new array order.
48// If the order does not change, a copy is returned if the
49// <src>alwaysCopy</src> is true. Otherwise a reference of the
50// input array is returned.
51// <p>
52// The <src>newAxisOrder</src> defines the new axes order.
53// Its length can be less than the dimensionality of the input array.
54// It is appended with the non-specified axes in their natural order.
55// <src>newAxisOrder(i)</src> gives the axis in the original array
56// which will now get axis <src>i</src>.
57// </synopsis>
58
59// <example>
60// <srcblock>
61// MArray<Int> result = reorderArray (someArray, IPosition(2,1,3));
62// </srcblock>
63// Say that someArray is a 4D array with shape [3,4,5,6].
64// The non-specified axes get appended to the axis order
65// specification [1,3] resulting in [1,3,0,2].
66// <br> This means that axis 1 gets axis 0, axis 3 gets axis 1, axis 0 gets
67// axis 2, and axis 2 gets axis 3.
68// Thus the resulting shape is [4,6,3,5] and the data are moved accordingly.
69// </example>
70
71// <group name=reorderMArray>
72template <class T>
73MArray<T> reorderArray(const MArray<T>& array, const IPosition& newAxisOrder,
74 Bool alwaysCopy = True) {
75 return (array.isNull()
76 ? MArray<T>()
77 : (array.hasMask()
78 ? MArray<T>(reorderArray(array.array(), newAxisOrder, alwaysCopy),
79 reorderArray(array.mask(), newAxisOrder, alwaysCopy))
80 : MArray<T>(reorderArray(array.array(), newAxisOrder, alwaysCopy))));
81}
82// </group>
83
84// <summary>
85// Reverse the order of one or more axes of an MArray.
86// </summary>
87
88// <use visibility=export>
89
90// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tMArrayUtil.cc">
91
92// <synopsis>
93// This function makes it possible to reverse one or more axes of an MArray by
94// swapping around the elements of each axis.
95// Both the data and the optional mask are reversed.
96// The resulting array is a copy of the input array with its data
97// moved around according to the new order.
98// If the order does not change, a copy is returned if the
99// <src>alwaysCopy</src> is true. Otherwise a reference of the
100// input array is returned.
101// </synopsis>
102
103// <example>
104// Reversing axis 0 of a Vector means that the Vector is reversed.
105// Reversing axis 1 of a Matrix means that its rows are reversed.
106// Reversing axis 0 of an N-dim array means that the elements of each Vector
107// in that array are reversed.
108// </example>
109
110// <group name=reverseMArray>
111template <class T>
112MArray<T> reverseArray(const MArray<T>& array, const IPosition& reversedAxes,
113 Bool alwaysCopy = True) {
114 return (array.isNull()
115 ? MArray<T>()
116 : (array.hasMask()
117 ? MArray<T>(reverseArray(array.array(), reversedAxes, alwaysCopy),
118 reverseArray(array.mask(), reversedAxes, alwaysCopy))
119 : MArray<T>(reverseArray(array.array(), reversedAxes, alwaysCopy))));
120}
121// </group>
122
123} // namespace casacore
124
125#endif
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
T * array
The actual storage.
Definition Block.h:689
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
MArray< T > reorderArray(const MArray< T > &array, const IPosition &newAxisOrder, Bool alwaysCopy=True)
Definition MArrayUtil.h:74
Reverse the order of one or more axes of an MArray.
Definition MArrayUtil.h:111
MArray< T > reverseArray(const MArray< T > &array, const IPosition &reversedAxes, Bool alwaysCopy=True)
Definition MArrayUtil.h:113