Previous: indexshift, Up: Matrices and Tensors


4.8 Swapping of Tensor Indices

— Command: indexswap tensor ...

indexswap rearranges the indices of a tensor. It is one of two generalizations of the matrix transpose operation (cf. indexshift).

indexswap takes at least one argument which is the tensor on which index swapping is to be performed. One or two additional arguments may be provided to specify the indices to be swapped. If no additional arguments are provided, the first and second indices of the tensor are swapped (equivalent the matrix transpose operation). With one additional argument, the specified index is swapped with the one following it (e.g., if 2 is specified, the second and third indices will be swapped). If two additional arguments are provided, they specify the indices to be swapped. The actual indices used will be constrained to be between 1 and the rank of the tensor.

For example, given x[a,b,c,d], the command y:indexswap(x,2,4); produces a tensor y such that y[a,b,c,d] = x[a,d,c,b]. In this example, the element that was in position [a,b,c,d] in x will be in position [a,d,c,b] in y.

Special cases: If indexswap is given a scalar (rank 0 tensor) as input, it just returns the scalar. For a vector (tensor of rank 1), indexswap transposes the 1-by-n matrix (row vector) to an n-by-1 matrix (column vector).