A note added: Although the coding below is correct, it has clearly been superseeded by the following two entries contributed by acer: Entry 1 and Entry 2 in the blog entry Tip: Transpose a pair of indices of an Array.
Yesterday I wrote about a method to index an Array, using a procedure of the type `index/method`.
Below, using the same sort of procedure, a method for permuting the indices of an Array is given (please feel free to suggest improvements; probably, the else-statement may be written more concisely):
setIndexPermute := proc(permutation::'list'(posint))
`index/permute` := proc(indices::list,array::Array,value::list)
if nargs = 2 then
permutedIndices := :
for i from 1 to nops(indices) do
permutedIndices := [
array[op(permutedIndices)] := value:
An example, which performs B[j,k,i] := A[i,j,k] for all (i,j,k):
A := Array(1..3,1..3,1..3,(i,j,k)->i*j*k);
B := Array(permute,A):
Note that, of course, permutation of indices of an Array works only for Arrays where all dimensions are of equal size. These Arrays may be viewed as tensors, which they are in "Gravitation" from where the above method originates.