### 2.5 Transposing ¶

In a carefully designed Yorick program, array dimensions usually wind up where you need them. However, you may occasionally wind up with a five-by-seven array, instead of the seven-by-five array you wanted. Yorick has a very general `transpose` function:

    x623451 = transpose(x123456);
    x561234 = transpose(x123456, 3);
    x345612 = transpose(x123456, 5);
    x153426 = transpose(x123456, [2,5]);
    x145326 = transpose(x123456, [2,5,3,4]);
    x653124 = transpose(x123456, [2,5], [1,4,6]);

Here, `x123456` represents a six-dimensional array (hopefully these will be rare). The same array with its first and last dimensions transposed is called `x623451`; this is the default result of `transpose`. The `transpose` function can take any number of additional arguments to describe an arbitrary permutation of the indices of its first argument – the array to be transposed.

A scalar integer value, as in the second and third lines, represents a cyclic permutaion of all the dimensions; the first dimension of the input becomes the Nth dimension of the result, for an argument value of N.

An array of integer values represents a cyclic permutation of the specified dimensions. Hence, in the fifth example, `[2,5,3,4]` means that the second dimension becomes the fifth, the fifth becomes the third, the third becomes the fourth, and the fourth becomes the second. An arbitrary permutation may be built up of a number of cyclic permutations, as shown in the sixth example.

One additional problem can arise in Yorick – you may not know how many dimensions the array to be transposed has. In order to deal with this possibility, either a scalar argument or any of the numbers in a cyclic permutation list may be zero or negative to count from the last dimension. That is, 0 represents the last dimension, -1 the next to last, -2 the one before that, and so on.

Typical transposing tasks are: (1) Move the first dimension to be last, and all the others back one cyclically. (2) Move the last dimension first, and all the others forward one cyclically. (3) Transpose the first two dimensions, leaving all others fixed. (4) Transpose the final two dimensions, leaving all others fixed. These would be accomplished, in order, by the following four lines:

    x234561 = transpose(x123456, 0);
    x612345 = transpose(x123456, 2);
    x213456 = transpose(x123456, [1,2]);
    x123465 = transpose(x123456, [0,-1]);