TExcelTAD
Home › Linear Algebra & Matrix Theory › The Transpose

The Transpose

The transpose of a matrix turns its rows into columns. It is a simple operation with many uses: it writes dot products as matrix products, defines symmetric matrices, and appears in every gradient formula of a neural network.

Definition

The transpose of an m×nm \times n matrix A=[aij]A = [a_{ij}] is the n×mn \times m matrix ATA^T with

(AT)ij=aji(A^T)_{ij} = a_{ji}
A=[123456]AT=[142536]A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} \qquad A^T = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}

Row 1 of AA becomes column 1 of ATA^T, and so on. Geometrically, a square matrix is reflected across its main diagonal; the diagonal stays put. The transpose of a column vector is a row vector, which is why textbooks often write column vectors as [1,2,3]T[1, 2, 3]^T to save space.

Rules

Rule Comment
(AT)T=A(A^T)^T = A transposing twice restores AA
(A+B)T=AT+BT(A + B)^T = A^T + B^T
(cA)T=cAT(cA)^T = cA^T
(AB)T=BTAT(AB)^T = B^TA^T the order reverses
(A−1)T=(AT)−1(A^{-1})^T = (A^T)^{-1} for invertible AA

Proof of the reversal rule. Let AA be m×nm \times n and BB be n×pn \times p. Then

((AB)T)ij=(AB)ji=∑kajkbki=∑k(BT)ik(AT)kj=(BTAT)ij((AB)^T)_{ij} = (AB)_{ji} = \sum_k a_{jk} b_{ki} = \sum_k (B^T)_{ik} (A^T)_{kj} = (B^TA^T)_{ij}

The sizes also force the order: BTB^T is p×np \times n and ATA^T is n×mn \times m, so BTATB^TA^T is p×mp \times m, as (AB)T(AB)^T must be, while ATBTA^TB^T is usually undefined, and when it is defined (as in the example below, where p=mp = m) it has the wrong size, n×nn \times n instead of p×mp \times m.

python · try it yourself
from sympy import Matrix

A = Matrix([[1, 2, 3], [4, 5, 6]])
B = Matrix([[1, 0], [-1, 2], [3, 1]])
print("A^T =", A.T.tolist())
print("(AB)^T == B^T A^T:", (A * B).T == B.T * A.T)
print("shape of (AB)^T:", (A * B).T.shape, " shape of A^T B^T:", (A.T * B.T).shape)
A^T = [[1, 4], [2, 5], [3, 6]]
(AB)^T == B^T A^T: True
shape of (AB)^T: (2, 2)  shape of A^T B^T: (3, 3)

Dot products as matrix products

For column vectors x,y\mathbf{x}, \mathbf{y} in Rn\mathbb{R}^n, xTy\mathbf{x}^T\mathbf{y} is a 1×11 \times 1 matrix whose only entry is the dot product:

xTy=x1y1+x2y2+⋯+xnyn=x⋅yandxTx=∥x∥2\mathbf{x}^T\mathbf{y} = x_1y_1 + x_2y_2 + \cdots + x_ny_n = \mathbf{x} \cdot \mathbf{y} \qquad \text{and} \qquad \mathbf{x}^T\mathbf{x} = \lVert \mathbf{x} \rVert^2

The other order, xyT\mathbf{x}\mathbf{y}^T, is an n×nn \times n matrix called the outer product. Combining the two ideas with the reversal rule gives a useful identity: (Ax)⋅y=(Ax)Ty=xTATy=x⋅(ATy)(A\mathbf{x}) \cdot \mathbf{y} = (A\mathbf{x})^T\mathbf{y} = \mathbf{x}^TA^T\mathbf{y} = \mathbf{x} \cdot (A^T\mathbf{y}). The transpose is the matrix that "moves across" a dot product.

Symmetric and skew-symmetric matrices

A square matrix is symmetric if AT=AA^T = A (mirror image across the diagonal) and skew-symmetric if AT=−AA^T = -A (which forces zeros on the diagonal).

  • For any matrix XX, the products XTXX^TX and XXTXX^T are symmetric: (XTX)T=XT(XT)T=XTX(X^TX)^T = X^T(X^T)^T = X^TX.
  • Every square matrix splits uniquely as symmetric plus skew-symmetric: A=12(A+AT)+12(A−AT)A = \frac{1}{2}(A + A^T) + \frac{1}{2}(A - A^T).
python · try it yourself
from sympy import Matrix

X = Matrix([[1, 2], [0, 1], [3, -1]])
G = X.T * X
print("X^T X =", G.tolist(), " symmetric:", G == G.T)

A = Matrix([[4, 1, -2], [5, 0, 3], [2, 7, 1]])
S = (A + A.T) / 2
K = (A - A.T) / 2
print("S =", S.tolist())
print("K =", K.tolist())
print("S symmetric:", S == S.T, " K skew:", K == -K.T, " S + K == A:", S + K == A)
X^T X = [[10, -1], [-1, 6]]  symmetric: True
S = [[4, 3, 0], [3, 0, 5], [0, 5, 1]]
K = [[0, -2, -2], [2, 0, -2], [2, 2, 0]]
S symmetric: True  K skew: True  S + K == A: True

Symmetric matrices have especially nice eigenvalues and eigenvectors (Diagonalization of Real Symmetric Matrices).

In software

  • .T in NumPy is free. It returns a view with swapped strides; no data is copied. A row-major array viewed as transposed is laid out column-major, which is how one buffer can serve both C (row-major) and Fortran/GLSL (column-major) code. Uploading a matrix to OpenGL with the wrong convention transposes it, a classic graphics bug.
  • Gram matrices in ML. For a data matrix XX (samples×features\text{samples} \times \text{features}), XTXX^TX is features×features\text{features} \times \text{features}; after centering the columns, XTX/(n−1)X^TX/(n - 1) is the covariance matrix used by PCA, and the least-squares normal equations are XTXw=XTyX^TX\mathbf{w} = X^T\mathbf{y} (least squares).
  • Backpropagation. For a layer Y=XWY = XW, the gradients are ∂L/∂W=XT(∂L/∂Y)\partial L/\partial W = X^T(\partial L/\partial Y) and ∂L/∂X=(∂L/∂Y)WT\partial L/\partial X = (\partial L/\partial Y)W^T. The transposes are exactly what make the shapes match (Backpropagation).
  • Normals in graphics. When a mesh is transformed by MM with non-uniform scaling, surface normals must be transformed by (M−1)T(M^{-1})^T, the inverse transpose, to stay perpendicular to the surface.
python · try it yourself
import numpy as np

A = np.arange(6).reshape(2, 3)
At = A.T
print("shares memory:", np.shares_memory(A, At))
print("strides A:", A.strides, " strides A.T:", At.strides)
print("C-contiguous A.T:", At.flags["C_CONTIGUOUS"], " F-contiguous A.T:", At.flags["F_CONTIGUOUS"])
shares memory: True
strides A: (12, 4)  strides A.T: (4, 12)
C-contiguous A.T: False  F-contiguous A.T: True

Exercises

Exercise 1: Transpose by hand

Write the transpose of M=[2−105731−4]M = \begin{bmatrix} 2 & -1 & 0 & 5 \\ 7 & 3 & 1 & -4 \end{bmatrix} and give its size.

Answer

MT=[27−13015−4]M^T = \begin{bmatrix} 2 & 7 \\ -1 & 3 \\ 0 & 1 \\ 5 & -4 \end{bmatrix}, size 4×24 \times 2.

Solution

Row 1 [2,−1,0,5][2, -1, 0, 5] becomes column 1 and row 2 [7,3,1,−4][7, 3, 1, -4] becomes column 2, so MTM^T has 4 rows and 2 columns.

Exercise 2: Symmetric completion

Fill in the blanks so that [3a−140bc62]\begin{bmatrix} 3 & a & -1 \\ 4 & 0 & b \\ c & 6 & 2 \end{bmatrix} is symmetric.

Answer

a=4a = 4, b=6b = 6, c=−1c = -1.

Solution

Symmetric means aij=ajia_{ij} = a_{ji}: entry (1,2)=entry (2,1)=4\text{entry } (1,2) = \text{entry } (2,1) = 4, entry (2,3)=entry (3,2)=6\text{entry } (2,3) = \text{entry } (3,2) = 6, entry (3,1)=entry (1,3)=−1\text{entry } (3,1) = \text{entry } (1,3) = -1.

Exercise 3: Prove a product is symmetric

Show that if AA is symmetric then BTABB^TAB is symmetric for any BB of compatible size.

Answer

(BTAB)T=BTATB=BTAB(B^TAB)^T = B^TA^TB = B^TAB.

Solution
(BTAB)T=BTAT(BT)Treversal rule for three factors=BTABAT=A and (BT)T=B\begin{aligned} (B^TAB)^T &= B^T A^T (B^T)^T && \text{reversal rule for three factors} \\ &= B^T A B && A^T = A \text{ and } (B^T)^T = B \end{aligned}
python · try it yourself
from sympy import Matrix
A = Matrix([[2, 1], [1, 3]])
B = Matrix([[1, 4, 0], [2, -1, 5]])
M = B.T * A * B
print(M.tolist(), M == M.T)
[[18, 9, 35], [9, 27, 5], [35, 5, 75]] True

Exercise 4: The gradient shapes

A layer computes Y=XWY = XW with XX of shape (32, 10) and WW of shape (10, 4). The upstream gradient dYdY has shape (32, 4). Compute the shapes of XT⋅dYX^T \cdot dY and dY⋅WTdY \cdot W^T in NumPy and say which gradient each is.

Answer

XT⋅dYX^T \cdot dY is (10, 4), the gradient for WW; dY⋅WTdY \cdot W^T is (32, 10), the gradient for XX.

Solution

Each gradient must have the shape of the thing it is the gradient of, and the transposes are what make the inner sizes match.

python · try it yourself
import numpy as np
X = np.ones((32, 10)); W = np.ones((10, 4)); dY = np.ones((32, 4))
print("dW:", (X.T @ dY).shape, " W:", W.shape)
print("dX:", (dY @ W.T).shape, " X:", X.shape)
dW: (10, 4)  W: (10, 4)
dX: (32, 10)  X: (32, 10)

Test yourself

AA is 3×53 \times 5. What is the size of ATAA^T A?

  1. 3×33 \times 3
  2. 5×55 \times 5
  3. 3×53 \times 5
  4. Undefined

What is (ABC)T(ABC)^T?

  1. CTBTATC^TB^TA^T
  2. ATBTCTA^TB^TC^T
  3. CBACBA
  4. BTATCTB^TA^TC^T

Which matrix is skew-symmetric?

  1. [1221]\begin{bmatrix} 1 & 2 \\ 2 & 1 \end{bmatrix}
  2. [0330]\begin{bmatrix} 0 & 3 \\ 3 & 0 \end{bmatrix}
  3. [1−111]\begin{bmatrix} 1 & -1 \\ 1 & 1 \end{bmatrix}
  4. [0−550]\begin{bmatrix} 0 & -5 \\ 5 & 0 \end{bmatrix}