AI State of the Art
Math/Linear Algebra/11 min
Section 315

Matrix Operations: Multiplication, Transpose, Inverse

Listen as a podcast β†’

Introduction: matrices do the work

Matrices aren't just tables of numbers. They are transformation machines. When a neural network receives an input and produces an output, it's a series of matrix operations doing the work.

Understanding these operations means understanding what actually happens in each layer of a model.

Addition and subtraction

The simplest operation. You add matrices element by element. The two matrices must have the same size.

If A,B∈RmΓ—n\mathbf{A}, \mathbf{B} \in \mathbb{R}^{m \times n}:

(A+B)ij=aij+bij(\mathbf{A} + \mathbf{B})_{ij} = a_{ij} + b_{ij}

Concrete example:

(1234)+(5678)=(681012)\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} + \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix} = \begin{pmatrix} 6 & 8 \\ 10 & 12 \end{pmatrix}

In ML, addition appears everywhere: adding the bias b\mathbf{b} to the output of a linear layer, adding residual connections in Transformers.

Scalar multiplication

You multiply every element by the same number:

(Ξ±A)ij=Ξ±β‹…aij(\alpha \mathbf{A})_{ij} = \alpha \cdot a_{ij}

When you multiply weights by a regularization factor, or when you apply a learning rate to the gradient, that's scalar multiplication.

The matrix product: the king of operations

This is the most important operation in deep learning. It's at the heart of every linear layer, every attention mechanism, every transformation.

Definition

To multiply A∈RmΓ—p\mathbf{A} \in \mathbb{R}^{m \times p} by B∈RpΓ—n\mathbf{B} \in \mathbb{R}^{p \times n}, the number of columns of A must equal the number of rows of B. The result is a matrix C∈RmΓ—n\mathbf{C} \in \mathbb{R}^{m \times n}:

cij=βˆ‘k=1paikβ‹…bkjc_{ij} = \sum_{k=1}^{p} a_{ik} \cdot b_{kj}

Each element cijc_{ij} is the dot product of the ii-th row of A\mathbf{A} with the jj-th column of B\mathbf{B}.

Three matrices drawn as grids of empty boxes inside brackets. In the first, 4 rows by 3 columns, the three boxes of the second row are shaded grey and the heading reads Row 2. In the second, 3 rows by 2 columns, the three boxes of the first column are shaded grey and the heading reads Column 1. An equals sign leads to the third matrix, 4 rows by 2 columns, where a single box is shaded grey, the one in the second row and first column, headed Element (2,1).
The diagram shows where each entry of the result comes from: row 2 of the first matrix meets column 1 of the second, and their dot product fills box (2,1). It also makes clear why the dimensions must match, the row and the column need the same number of boxes.Source : University of South Carolina, Math 344, Matrix Operations

Example

(1234)Γ—(5678)=(1β‹…5+2β‹…71β‹…6+2β‹…83β‹…5+4β‹…73β‹…6+4β‹…8)=(19224350)\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \times \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix} = \begin{pmatrix} 1 \cdot 5 + 2 \cdot 7 & 1 \cdot 6 + 2 \cdot 8 \\ 3 \cdot 5 + 4 \cdot 7 & 3 \cdot 6 + 4 \cdot 8 \end{pmatrix} = \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}

Important properties

  • Not commutative: ABβ‰ BA\mathbf{AB} \neq \mathbf{BA} in general. Order matters.
  • Associative: (AB)C=A(BC)(\mathbf{AB})\mathbf{C} = \mathbf{A}(\mathbf{BC})
  • Distributive: A(B+C)=AB+AC\mathbf{A}(\mathbf{B} + \mathbf{C}) = \mathbf{AB} + \mathbf{AC}

Why it's fundamental in ML

A linear layer (or dense, or fully connected) in a neural network performs exactly:

y=Wx+b\mathbf{y} = \mathbf{W}\mathbf{x} + \mathbf{b}

Where:

  • x∈Rn\mathbf{x} \in \mathbb{R}^n is the input vector
  • W∈RpΓ—n\mathbf{W} \in \mathbb{R}^{p \times n} is the weight matrix
  • b∈Rp\mathbf{b} \in \mathbb{R}^p is the bias vector
  • y∈Rp\mathbf{y} \in \mathbb{R}^p is the output vector

It's a matrix-vector product followed by an addition. The matrix W\mathbf{W} encodes the learned transformation: it decides which combinations of inputs are relevant.

For a batch of BB examples, you stack the vectors into a matrix X∈RBΓ—n\mathbf{X} \in \mathbb{R}^{B \times n} and compute Y=XWT+b\mathbf{Y} = \mathbf{X}\mathbf{W}^T + \mathbf{b}. That's a single matrix product that processes the entire batch at once. That's why GPUs, optimized for massive matrix multiplication, are so important.

The attention mechanism

In a Transformer, attention is computed by:

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \text{softmax}\left(\frac{\mathbf{Q}\mathbf{K}^T}{\sqrt{d_k}}\right)\mathbf{V}

It's a cascade of matrix products. QKT\mathbf{Q}\mathbf{K}^T computes the similarity scores between all pairs of tokens. Multiplication by V\mathbf{V} produces the weighted output. Everything relies on matrix multiplication.

The transpose

Definition

The transpose of A∈RmΓ—n\mathbf{A} \in \mathbb{R}^{m \times n} is the matrix AT∈RnΓ—m\mathbf{A}^T \in \mathbb{R}^{n \times m} obtained by swapping rows and columns:

(AT)ij=aji(\mathbf{A}^T)_{ij} = a_{ji}

Concretely, the first row of A\mathbf{A} becomes the first column of AT\mathbf{A}^T.

(123456)T=(142536)\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}^T = \begin{pmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{pmatrix}

Properties

  • (AT)T=A(\mathbf{A}^T)^T = \mathbf{A}
  • (A+B)T=AT+BT(\mathbf{A} + \mathbf{B})^T = \mathbf{A}^T + \mathbf{B}^T
  • (AB)T=BTAT(\mathbf{AB})^T = \mathbf{B}^T\mathbf{A}^T, note, the order reverses

Uses in ML

The transpose appears constantly:

  • The dot product of two vectors is written xTy=βˆ‘ixiyi\mathbf{x}^T\mathbf{y} = \sum_i x_i y_i
  • In the attention formula, QKT\mathbf{Q}\mathbf{K}^T transposes the keys to compute similarities
  • The gradient of the loss with respect to a linear layer's weights involves the transpose of the activations

The inverse of a matrix

Definition

For a square matrix A∈RnΓ—n\mathbf{A} \in \mathbb{R}^{n \times n}, the inverse Aβˆ’1\mathbf{A}^{-1} is the matrix such that:

AAβˆ’1=Aβˆ’1A=I\mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}

It's the analog of division for matrices. If Ax=b\mathbf{A}\mathbf{x} = \mathbf{b}, then x=Aβˆ’1b\mathbf{x} = \mathbf{A}^{-1}\mathbf{b}.

When does it exist?

The inverse doesn't always exist. A matrix is invertible (or nonsingular) if and only if:

  • Its determinant is nonzero
  • Its columns are linearly independent
  • It has no zero eigenvalue

If the matrix is not invertible, it's called singular. Intuitively, it "crushes" information in one direction, it's impossible to go back.

In ML

You often avoid computing the inverse directly (it's expensive and numerically unstable). But the concept is central:

  • Linear regression: the analytical solution is w=(XTX)βˆ’1XTy\mathbf{w} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}. In practice, more numerically stable methods are used.
  • Second-order optimization: Newton's methods use the inverse of the Hessian matrix.
  • Multivariate Gaussian distribution: involves the inverse of the covariance matrix.

The determinant

Intuitive definition

The determinant of a square matrix is a scalar that measures how the matrix deforms space.

For a 2Γ—22 \times 2 matrix:

det⁑(abcd)=adβˆ’bc\det \begin{pmatrix} a & b \\ c & d \end{pmatrix} = ad - bc

Geometric meaning

The determinant indicates:

  • Magnitude: ∣det⁑(A)∣|\det(\mathbf{A})| is the factor by which volumes are multiplied under the transformation A\mathbf{A}. A determinant of 2 means areas double.
  • Sign: a negative determinant means the transformation reverses orientation (like a mirror).
  • Zero: if det⁑(A)=0\det(\mathbf{A}) = 0, the matrix crushes space into a lower dimension. Information is lost. The matrix is not invertible.
In a two-axis coordinate system, a pink parallelogram starts at the origin (0,0). Its vertices are labelled (a,b), (c,d) and (a+c, b+d). The expression ad minus bc is written across the pink area.
The two columns of the matrix, the vectors (a,b) and (c,d), form the sides of a parallelogram. Its area is exactly ad minus bc, which is the determinant. If the two vectors are aligned, the parallelogram collapses, the area is zero, and the matrix is not invertible.Source : Wikimedia Commons, Area parallellogram as determinant

In ML

The determinant appears in:

  • Normalizing flows: generative models that transform a simple distribution into a complex one. The log-determinant of the Jacobian is at the heart of the likelihood computation.
  • Multivariate Gaussian distribution: the density involves det⁑(Ξ£)\det(\mathbf{\Sigma}), the determinant of the covariance matrix.

The linear layer: putting it all together

Let's recap. A linear layer in a neural network performs:

y=Wx+b\mathbf{y} = \mathbf{W}\mathbf{x} + \mathbf{b}

That's:

  1. A matrix product (Wx\mathbf{W}\mathbf{x}), the learned transformation
  2. An addition (+b+ \mathbf{b}), the offset
  3. Followed by a nonlinear activation function

Stacking multiple layers means chaining matrix multiplications interspersed with nonlinearities. The entire network is a composition of matrix transformations.

That's why deep learning is fundamentally applied linear algebra. And that's why mastering these operations isn't optional, it means understanding what your model actually does.

Where this is used. Matrix multiplication and the transpose are literally the whole arithmetic of self-attention.