10
$\begingroup$

It is very likely that it is my lack of math skills that is showing up here. However, I think Transpose is such an important function that I need to master it. So I am going to ask because I do not understand the information given in the documentation.

Transpose[list,{Subscript[n, 1], Subscript[n, 2], …}] transposes list so that the k-th level in list is the Subscript[n, k]-th level in the result.

Would anyone be able to provide a simple example and point out in a matrix what is going on? I am in particular struggling with the nth and k-th level. How does that work?

I want to master Transpose[list, {…}]. Please explain the case where the k-th and n-th level are to be transposed, since this is where I am struggling.

If this is considered mathematical question, and therefore not in the right place, I would appreciate a comment so that I could delete the post.

$\endgroup$
4
  • $\begingroup$ There are copious examples in the Scope section of the function in the documentation. Look at the results (the pattern of indexes in particular) of those results, should be clear. $\endgroup$
    – ciao
    Apr 13, 2014 at 0:15
  • $\begingroup$ As a mathematical question, "What is the transpose?" is actually pretty complex. In short, it's a canonical isomorphism between $V\otimes W^*$ and $V^*\otimes W$ that exists whenever $V,W$ are Hilbert spaces. $\endgroup$ Apr 13, 2014 at 6:07
  • 2
    $\begingroup$ Transpose[$m, {3, 2, 1}] === Flatten[$m, {{3}, {2}, {1}}], so you might find this discussion of Flatten useful. $\endgroup$
    – WReach
    Apr 13, 2014 at 14:54
  • 3
    $\begingroup$ Does this answer your question? Using Transpose with a list as the second argument $\endgroup$
    – xzczd
    Jan 12 at 12:15

2 Answers 2

16
$\begingroup$

Here is a visualization of the 3 dimensional case. A part of the tensor is indexed by

`tensor[[l1, l2, l3]]`

where l1, l2, l3 are the indices to levels 1, 2, 3 respectively. Transposing switches how the values are indexed. For example, if new = Transpose[old, {2, 3, 1}], then new[[l3, l1, l2]] == old[[l1, l2, l3]] or new[[l1, l2, l3]] == old[[l2, l3, l1]]. The first equality corresponds to how the result is described in Transpose.

In the visualization below, the colors are transposed according to the permutation labeling the graphics. Level 1 corresponds to hue, level 2 to saturation, and level 3 to brightness. The upper left is the identity permutation and corresponds to the original tensor. The labels on the axes correspond to the level in the original tensor.

tensor = Table[{i, j, k}, {i, 4}, {j, 4}, {k, 4}];
cf[i_, j_, k_] := Hue[(i - 1)/4, j/4, (k + 3)/9];
g[p_] := Graphics3D[{
    PointSize[0.1],
    Point[Flatten[tensor, 2], 
     VertexColors -> cf @@@ Flatten[Transpose[tensor, p], 2]]
    },
   PlotRange -> {{0.8, 4.2}, {0.8, 4.2}, {0.8, 4.2}},
   PlotLabel -> p,
   Axes -> True, Ticks -> None,
   AxesLabel -> Ordering@p
   ];

GraphicsGrid[Partition[Table[g[p], {p, Permutations[{1, 2, 3}]}], 3]]

Mathematica graphics

I hope that this example will help with understanding how higher dimensional tensors are transposed.

$\endgroup$
2
  • $\begingroup$ Thanks. That's very helpful. In your example, new[[l3, l1, l2]] == old[[l1, l2, l3]] or new[[l1, l2, l3]] == old[[l2, l3, l1]] , do you have an easy way to know the arrangement {l2,l3,l1} given {l3,l1,l2}? For example, I have a tensor with dimension {l1,l2,l3,l4}, and I want to have new[[l2, l3, l1,l4]] == old[[l1, l2, l3,l4]], what should I put in Transpose[old, ]? $\endgroup$ Sep 17, 2015 at 5:01
  • 1
    $\begingroup$ @xslittlegrass, It's a question of permuting the levels. For your second question FindPermutation[{l1, l2, l3, l4}, {l2, l3, l1, l4}] // PermutationList[#, 4] & will return what you need. For the first, in {l3,l1,l2}, l1 went to position 2; so the permutation start with {2,..}; next l2 went to position 3, so the permutation becomes {2, 3,...}; finally, l3 went to position 1, so the argument to Transpose is/was {2, 3, 1}. $\endgroup$
    – Michael E2
    Sep 17, 2015 at 10:47
2
$\begingroup$

This is more of a math question, but in the spirit of being helpful:

I think if you run this code and look at the colors of each matrix you might understand better what transpose does.

    m = Table[Graphics[{RGBColor[0, .33 i, .33 j], Disk[]}], {i, 1, 3},
     {j,1,3}] // MatrixForm;
    mT = Transpose@Table[Graphics[{RGBColor[0, .33 i, .33 j], Disk[]}],
     {i, 1, 3}, {j, 1, 3}] // MatrixForm;
    Row[{m, mT}]

Transpose basically reflects elements across the diagonal.

Notice how the first column of the original matrix is the same as the first row of the transposed matrix.

$\endgroup$
1
  • 4
    $\begingroup$ I think the OP had in mind the syntax with the second argument of Transpose, which allows complex array reshuffles for arrays of higher dimensions, rather than the (trivial) single-arg Transpose. $\endgroup$ Apr 13, 2014 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.