ArrayTools
DataTranspose
manipulate single dimension of a Matrix, Vector, or Array as a flat Matrix and transpose
Calling Sequence
Parameters
Description
Thread Safety
Examples
DataTranspose(A, rows, cols, dim)
A
-
rectangular storage Matrix, Vector, or Array of any data type and ordering
rows
integer; number of rows viewing dimension dim as data storage for a row-major Matrix (or alternatively the number of columns for a column-major Matrix)
cols
integer; number of columns viewing dimension dim as data storage for a row-major Matrix (or alternatively the number of rows for a column-major Matrix)
dim
(optional) non-negative integer; dimension to transpose; assumed to be 1 if not specified
The DataTranspose command performs an in-place transpose with respect to a single dimension of the input Matrix, Vector, or Array as though the data for that dimension represents the storage for a Matrix. The input Matrix, Vector, or Array must have rectangular (dense) storage.
To understand how this works, you must know how data for row and column-major Matrices is stored. For a description of the two possibilities, see Fortran_order and C_order.
This is simplest to understand for a Vector. You can view the input data as storage for a rows x cols C_order Matrix. On output, the data in the Vector is re-arranged so it can be viewed as the storage for a cols x rows C_order Matrix that holds the transpose of the original Matrix.
For example, the 6 element Vector
V := Vector[row]([1,2,3,4,5,6]);
V≔123456
is treated as a Matrix
M := Matrix([[1,2,3],[4,5,6]],order=C_order);
M≔123456
and the vector data after the transpose
ArrayTools:-DataTranspose(V,2,3), V;
142536
can be seen as storage for the transpose.
M := Matrix([[1,4],[2,5],[3,6]],order=C_order);
M≔142536
For Matrices and Arrays with more than one dimension, the specified dimension dim is transformed in the same manner as described for a Vector, but this is done for all values of the indices for the other dimensions.
For example, the Matrix
V := Matrix([[ 1, 2, 3, 4, 5, 6], [ 7, 8, 9,10,11,12]],order=C_order);
V≔123456789101112
when transformed with respect to the second dimension looks like
ArrayTools:-DataTranspose(V,2,3,2), V;
142536710811912
which is the same as for the vector example, but applied twice, once for each row of the Matrix.
If the dimension dim is not specified, then it is assumed to be 1.
Note: For the dimension being transformed, the number of rows times the number of cols specified cannot exceed the number of entries in that dimension.
This command can be used in combination with Alias to construct the transpose of a non-square Matrix directly using the same storage of the original Matrix (an in-place transpose). See the following examples.
This function is part of the ArrayTools package, so it can be used in the short form DataTranspose(..) only after executing the command with(ArrayTools). However, it can always be accessed through the long form of the command by using ArrayTools[DataTranspose](..).
The DataTranspose command is thread safe as of Maple 2023, provided that the rtable A is not shared between threads.
For more information on thread safety, see index/threadsafe.
with⁡ArrayTools:
A demonstration of how to use DataTranspose to compute the transpose of a non-square C_order Matrix in-place
M≔Matrix⁡4,3,i,j↦10⋅i+j,order=C_order
M≔111213212223313233414243
Generate a 1-D representation of the Matrix.
MV≔Alias⁡M,12,row
MV≔111213212223313233414243
Transpose the data.
DataTranspose⁡MV,4,3,MV
112131411222324213233343
Alias as a 3 x 4 Matrix
Alias⁡M,3,4
Note: The data of the original Matrix has been changed.
Matrix⁡4,3,i,j↦10⋅i+j,order=C_order,M
111213212223313233414243,112131411222324213233343
A non-square Fortran_order Matrix is similar, but the call to DataTranspose requires the dimensions be reversed.
M≔Matrix⁡4,3,i,j↦10⋅i+j,order=Fortran_order
MV≔112131411222324213233343
DataTranspose⁡MV,3,4,MV
111213212223313233414243
See Also
ArrayTools[Alias]
C_order
Fortran_order
interface
Matrix
Vector
Download Help Document