Skip to content

Transpose

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

Transpose[list]
    Transposes the first two levels of list (swaps rows and columns of a matrix).
Transpose[list, {n1, n2, ...}]
    Gives the transpose of list so that level k in list is level nk in the result.
    The spec must be a permutation of {1, ..., r} where r is the depth of list.
    A repeated index (e.g. {1, 1}) selects the corresponding diagonal.
    list must be a rectangular array.

Examples

All examples below are verified against the current Mathilda build.

In[1]:= Transpose[{{a, b}, {c, d}}]
Out[1]= {{a, c}, {b, d}}

In[2]:= Transpose[{{a, b}, {c, d}}, {1, 1}]
Out[2]= {a, d}

Implementation notes

Algorithm. builtin_transpose swaps the levels of a rectangular nested-List array. It measures the array shape with get_array_dimensions (requiring depth ≥ 2 and rectangularity), then either uses the default permutation {2, 1, 3, …} (one-argument form swaps the first two levels) or the explicit permutation given as the second argument. build_transposed recursively materialises the output array by mapping each output index path back to an input index path through the permutation and copying the leaf via get_element_at. For a 2-D matrix (list of rows) this is the ordinary m[i][j] -> m[j][i] swap. Returns NULL (unevaluated) for non-rectangular or non-List inputs. ConjugateTranspose is Conjugate[Transpose[...]].

  • Protected.
  • Works only on rectangular arrays.
  • Transpose[m, {1, 1}] extracts the diagonal of a square matrix.

Attributes: Protected.

Implementation status

Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

References

Notes & additional examples

Worked examples

In[1]:= Transpose[{{1, 2, 3}, {4, 5, 6}}]
Out[1]= {{1, 4}, {2, 5}, {3, 6}}
In[1]:= Transpose[{{1, 2}, {3, 4}}]
Out[1]= {{1, 3}, {2, 4}}
In[1]:= Transpose[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {1, 1}]
Out[1]= {1, 5, 9}

The Gram matrix A^T . A of a symbolic 2x3 design matrix is symmetric by construction:

In[1]:= Transpose[{{a, b, c}, {d, e, f}}] . {{a, b, c}, {d, e, f}}
Out[1]= {{a^2 + d^2, a b + d e, a c + d f}, {a b + d e, b^2 + e^2, b c + e f}, {a c + d f, b c + e f, c^2 + f^2}}

For an antisymmetric matrix M = -M^T, the sum M + Transpose[M] vanishes:

In[1]:= m = {{0, 1, 2}, {-1, 0, 3}, {-2, -3, 0}}; m + Transpose[m]
Out[1]= {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

Notes

With one argument Transpose swaps the first two levels of a list, turning a 2x3 matrix into a 3x2 one. The optional permutation spec generalises this to arbitrary index reorderings of a rectangular array. A repeated index in the spec — {1, 1} in the third example — extracts the corresponding diagonal, here the main diagonal {1, 5, 9} of the 3x3 matrix. The spec must be a permutation of {1, ..., r} where r is the depth of the list.