Foundations to Frontier
Tests
A. Mathematical Foundations › A2 Linear Algebra › Matrices

Matrix Operations and Matrix Multiplication

Tables of numbers that add, scale and multiply. Matrix multiplication is the single most-executed operation in AI, and it has four equally useful interpretations.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Tables of numbers

A shop keeps a table of sales:

Apples Bananas
Week 1 2 3
Week 2 4 1

A table of numbers arranged in rows and columns is called a matrix. To get two weeks’ total of each fruit, you add matching boxes. To double every order, you double every box.

Multiplying a table by prices

Apples cost ₹10 and bananas ₹5. Week 1’s bill is 2 × 10 + 3 × 5 = 35, and week 2’s is 4 × 10 + 1 × 5 = 45. You took a row (the quantities), matched it with the prices, multiplied in pairs and added. That’s exactly how matrix multiplication works: row times column, again and again.

Row times column
A (2×3)
-2
0
3
-1
3
1
×
B (3×2)
-2
-1
1
-2
-1
1
=
C = AB (2×2), click a cell
C[1,1] = (-2)·(-2) + (0)·(1) + (3)·(-1) = 1
Each entry of C is the dot product of a row of A (orange) with a column of B (blue), so the inner sizes must match: (2×3)·(3×2) gives 2×2. Computing all of C takes m·n·p = 12 multiplications, which is why large matrix products dominate the cost of neural networks.
Why AI cares

When a chatbot reads your sentence, most of its work is this one operation, rows times columns, repeated billions of times on huge tables of numbers.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A table has 3 rows and 4 columns. How many numbers does it hold?
  2. 2. Apples ₹10, bananas ₹5. You buy 3 apples and 4 bananas. Total?
  3. 3. To get two weeks' total sales, you…

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

Shape and entries

A matrix with mm rows and nn columns has shape m×nm\times n. The entry in row ii, column jj is written aija_{ij} or AijA_{ij}.

Adding and scaling

Both are done entry by entry, and addition needs the same shape:

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

Multiplying: row times column

(AB)ij(AB)_{ij} is the dot product of row ii of AA with column jj of BB:

(1234)(5678)=(1⋅5+2⋅71⋅6+2⋅83⋅5+4⋅73⋅6+4⋅8)=(19224350)\begin{pmatrix}1&2\\3&4\end{pmatrix}\begin{pmatrix}5&6\\7&8\end{pmatrix} = \begin{pmatrix}1\cdot5+2\cdot7 & 1\cdot6+2\cdot8\\ 3\cdot5+4\cdot7 & 3\cdot6+4\cdot8\end{pmatrix} = \begin{pmatrix}19&22\\43&50\end{pmatrix}

The shape rule: (m×n)(n×p)=(m×p)(m\times n)(n\times p) = (m\times p). The inner sizes must match, and the outer sizes give the result.

The identity matrix II has 1s on the diagonal and 0s elsewhere. It does nothing: AI=IA=AAI = IA = A.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Shape of (2 × 3)(3 × 5)?
  2. 2. Can you add a 2 × 3 matrix to a 3 × 2 matrix?
  3. 3. (AB)₂₁ for A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]]?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Four ways to see AB

The same product has four interpretations, and experts switch between them constantly:

View Statement Useful for
Entries (AB)ij=airow⋅bjcol(AB)_{ij} = \mathbf a_i^{\text{row}}\cdot\mathbf b_j^{\text{col}} computing by hand, attention scores
Columns column jj of ABAB = A bjA\,\mathbf b_j, a combination of AA’s columns seeing that im⁡(AB)⊆im⁡A\operatorname{im}(AB)\subseteq\operatorname{im}A
Rows row ii of ABAB = (row ii of AA)·BB, a combination of BB’s rows processing a batch of data rows
Outer products AB=∑kakbk⊤AB = \sum_k \mathbf a_k\mathbf b_k^\top (column kk of A times row kk of B) low-rank models, the SVD, LoRA
🐍 Exercise 1: the four views of matrix multiplication
Implement AB four ways (entries, columns, rows, sum of outer products) and check they all agree with A @ B.

Rules (and non-rules)

  • ✅ Associative: (AB)C=A(BC)(AB)C = A(BC). ✅ Distributive: A(B+C)=AB+ACA(B + C) = AB + AC.
  • ❌ Not commutative: AB≠BAAB \ne BA in general. Above, BA=(23343146)BA = \begin{pmatrix}23&34\\31&46\end{pmatrix}.
  • ❌ No cancellation: AB=0AB = 0 is possible with A≠0A \ne 0 and B≠0B \ne 0. For example, (1000)(0010)=0\begin{pmatrix}1&0\\0&0\end{pmatrix}\begin{pmatrix}0&0\\1&0\end{pmatrix} = 0.
  • ⚠️ So (A+B)2=A2+AB+BA+B2(A+B)^2 = A^2 + AB + BA + B^2, not A2+2AB+B2A^2 + 2AB + B^2.

Matrix powers count paths

If AA is a graph’s adjacency matrix (Aij=1A_{ij} = 1 when there’s an edge from ii to jj), then (Ak)ij(A^k)_{ij} is the number of walks of length kk from ii to jj. If PP is a transition matrix of probabilities, then pk=p0Pk\mathbf p_k = \mathbf p_0P^k gives the distribution after kk steps. That’s the basis of Markov chains, PageRank and robot localization (T1).

In practice

Where Matrix multiplication
A batch through a dense layer Y=XW⊤+bY = XW^\top + \mathbf b: one matmul processes the whole batch
Attention QK⊤QK^\top and then (⋅)V(\cdot)V
Data science XβX\boldsymbol\beta predicts every row at once. X⊤XX^\top X appears in regression
Robotics chaining frames: WTE=WT1 1T2⋯nTE{}^{W}T_{E} = {}^{W}T_{1}\,{}^{1}T_{2}\cdots{}^{n}T_{E}
📝 Quick check (Level 2)
0/3 answered
  1. 1. Which view says AB = Σₖ (column k of A)(row k of B)?
  2. 2. (A + B)² equals A² + 2AB + B²…
  3. 3. In a triangle graph (every pair of 3 nodes connected), how many walks of length 2 go from node 1 back to node 1?

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Associativity, by indices

Theorem

(AB)C=A(BC)(AB)C = A(BC) whenever the shapes allow.

Proof. ((AB)C)il=∑k(∑jAijBjk)Ckl=∑jAij(∑kBjkCkl)=(A(BC))il((AB)C)_{il} = \sum_k\big(\sum_j A_{ij}B_{jk}\big)C_{kl} = \sum_j A_{ij}\big(\sum_k B_{jk}C_{kl}\big) = (A(BC))_{il}. The swap is allowed because finite sums can be reordered (A1-3). ∎ It’s also immediate from “matrices are maps, and multiplication is composition” (A2.1-5).

Block multiplication

Split the matrices into compatible blocks, and multiply the blocks as if they were numbers (keeping their order):

(A11A12A21A22)(B11B12B21B22)=(A11B11+A12B21A11B12+A12B22A21B11+A22B21A21B12+A22B22)\begin{pmatrix}A_{11}&A_{12}\\A_{21}&A_{22}\end{pmatrix}\begin{pmatrix}B_{11}&B_{12}\\B_{21}&B_{22}\end{pmatrix} = \begin{pmatrix}A_{11}B_{11}+A_{12}B_{21} & A_{11}B_{12}+A_{12}B_{22}\\ A_{21}B_{11}+A_{22}B_{21} & A_{21}B_{12}+A_{22}B_{22}\end{pmatrix}

This is how GPUs tile big products into small blocks that fit in fast memory, and it’s the starting point of Strassen’s trick.

Walks: a proof by induction

Theorem

For an adjacency matrix AA, (Ak)ij(A^k)_{ij} is the number of walks of length kk from ii to jj.

Proof. For k=1k = 1, it’s the definition. Assume it for kk. A walk of length k+1k+1 from ii to jj is a walk of length kk to some node ll, followed by an edge l→jl\to j. So the count is ∑l(Ak)ilAlj=(Ak+1)ij\sum_l (A^k)_{il}A_{lj} = (A^{k+1})_{ij}. ∎

The cost of multiplication

The schoolbook method for (m×n)(n×p)(m\times n)(n\times p) uses mnpmnp multiplications, so n3n^3 for square matrices. Associativity doesn’t change the answer, but it can change the cost hugely. With shapes 10×10010\times100, 100×5100\times5, 5×505\times50, computing (AB)C(AB)C costs 7,500 multiplications, while A(BC)A(BC) costs 75,000.

Strassen (1969) showed that two 2×22\times2 (block) matrices can be multiplied with 7 products instead of 8. Applied recursively, this gives O(nlog⁡27)≈O(n2.807)O(n^{\log_2 7}) \approx O(n^{2.807}). The best known exponent ω\omega has since been pushed to about 2.371 (Alman et al., 2024). Those algorithms are only faster for astronomically large matrices, though.

Exercise 3.1: Show that im(AB) ⊆ im(A), and hence rank(AB) ≤ rank(A)

Every column of ABAB is AbjA\mathbf b_j, which is in im⁡A\operatorname{im}A. So the whole column space of ABAB sits inside that of AA. Similarly, the rows of ABAB are combinations of the rows of BB, so rank⁡(AB)≤min⁡(rank⁡A,rank⁡B)\operatorname{rank}(AB)\le\min(\operatorname{rank}A, \operatorname{rank}B).

📝 Quick check (Level 3)
0/3 answered
  1. 1. How many multiplications does Strassen use for a 2 × 2 product?
  2. 2. Shapes 10×100, 100×5, 5×50. Which order is cheaper?
  3. 3. rank(AB) is at most…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Matmul is the engine of deep learning

Multiplying (m×n)(n×p)(m\times n)(n\times p) costs about 2mnp2mnp floating-point operations (FLOPs): one multiply and one add per term. Training and running large models is dominated by these products, so AI hardware is built around them: GPU tensor cores, TPU systolic arrays. Two ideas decide real speed:

  • Arithmetic intensity: FLOPs per byte moved from memory. For square n×nn\times n float32 matrices it’s about 2n3/(12n2)=n/62n^3/(12n^2) = n/6. Large matmuls are compute-bound (fast). Small ones and element-wise operations are memory-bound (slow per FLOP). That’s why kernels are fused and tiled (C5).
  • Tiling: block multiplication lets each small tile stay in fast on-chip memory while it’s reused many times. FlashAttention is a famous example of reorganizing attention’s matmuls around this idea (E12).

Machines that discover multiplication algorithms

Finding the minimum number of multiplications for small matrix products is a hard combinatorial search. Fawzi et al. (2022) framed it as a game and used deep reinforcement learning (AlphaTensor). They rediscovered Strassen-like algorithms and found new ones for some sizes, including some tuned for specific hardware. It’s a striking example of AI being used to improve the mathematics AI runs on.

🐍 Exercise 2 (research): Strassen's 7 products
Implement one level of Strassen's algorithm with 7 block products, and check it against A @ B for several sizes.

Markov chains in robotics and data science

A robot moving in a corridor whose wheel commands succeed only 80% of the time is described by a transition matrix PP. Its belief after kk steps is p0Pk\mathbf p_0P^k, the prediction step of a Bayes filter (T1). The same mathematics ranks web pages (PageRank, Page et al., 1999), models customer journeys in data science, and underlies MCMC sampling (A5). The long-run stationary distribution π=πP\boldsymbol\pi = \boldsymbol\pi P is an eigenvector problem (A2.3).

References

  1. A. Cayley (1858). “A memoir on the theory of matrices.” Philosophical Transactions of the Royal Society, 148.
  2. V. Strassen (1969). “Gaussian elimination is not optimal.” Numerische Mathematik, 13.
  3. L. Page, S. Brin, R. Motwani, T. Winograd (1999). “The PageRank citation ranking: Bringing order to the web.” Stanford InfoLab Technical Report.
  4. A. Fawzi et al. (2022). “Discovering faster matrix multiplication algorithms with reinforcement learning.” Nature, 610.
  5. T. Dao, D. Fu, S. Ermon, A. Rudra, C. Ré (2022). “FlashAttention: Fast and memory-efficient exact attention with IO-awareness.” NeurIPS.
  6. J. Alman, R. Duan, V. Vassilevska Williams, Y. Xu, Z. Xu, R. Zhou (2024). “More asymmetry yields faster matrix multiplication.” arXiv:2404.16349 (SODA 2025).
  7. G. Golub and C. Van Loan (2013). Matrix Computations, 4th ed. Johns Hopkins. Chapter 1.