← the late compiler
C_000237 · mathematical foundations · intermediate

Matrix Multiplication

Composing two linear transformations by taking dot products of rows with columns.

Step 1 of 4

In words

What it is, why it matters, and what it is like.

Why am I learning this?

Matrix multiplication is the single most executed operation in machine learning, and it is the reason GPUs exist. It underpins every modern AI system you will meet in this course: when a large language model predicts the next word, it is doing matrix multiplication; when your phone's voice assistant recognizes 'turn on the lights', a small model on the device is doing matrix multiplication; when a recommendation system suggests a movie, it is doing matrix multiplication. You cannot understand how any of these systems work — or debug why they fail — without this operation. Mastering it unlocks: Linear Transformations, Layers, Backpropagation, Embeddings, Attention, and the Transformer architecture. It is the arithmetic heart of everything that follows.

The idea, in plain terms

Imagine you are a shopkeeper with three products — pens, notebooks, and erasers. You have two customers who bought different quantities. Customer A bought 2 pens, 3 notebooks, and 1 eraser. Customer B bought 1 pen, 0 notebooks, and 4 erasers. You want to know how much each customer owes you, so you need to multiply the quantities by the prices (say 10 rupees per pen, 50 rupees per notebook, 5 rupees per eraser) and add them up.

This is a dot product: for each customer, you take each quantity, multiply it by the corresponding price, and add the results. Customer A owes 2×10 + 3×50 + 1×5 = 20 + 150 + 5 = 175 rupees. Customer B owes 1×10 + 0×50 + 4×5 = 10 + 0 + 20 = 30 rupees.

Now, what if you have multiple customers, and you want to do this all at once? You line up the customers as rows of a table (each row is a customer's quantities), and the prices as a column. Multiplying the table of customers by the price column gives you a new column — the amount each customer owes. That is matrix multiplication: a way to apply the same rule (prices) to many different inputs (customers) at once.

In AI, the 'customers' are data points (like images or sentences), and the 'prices' are weights the model has learned. Matrix multiplication is how the model applies its knowledge to new data.

An analogy

Think of matrix multiplication as a recipe for mixing paint. You have a shelf of primary colors: red, blue, yellow. Each paint can (a row in the first matrix) contains a recipe: how many parts of each primary to mix. For example, can 1: 30% red, 20% blue, 50% yellow. Can 2: 10% red, 60% blue, 30% yellow. Now you have a set of 'target colors' (a second matrix), each column telling you how much of each can to use. For example, target A: use 2 parts of can 1, 1 part of can 2. Target B: use 1 part of can 1, 3 parts of can 2.

The result of multiplying the recipe matrix by the mixing matrix is a new matrix where each entry tells you how much of each primary color you need for each target. For target A, you mix 2 parts of can 1 (which has 30% red) and 1 part of can 2 (10% red), so the red needed is 2×30% + 1×10% = 70% — wait, that is not right, because the parts are not percentages. Let me fix: the recipe gives the proportion of primary in each can, and the mixing matrix gives the number of parts of each can. So for target A, red = 2×0.3 + 1×0.1 = 0.7 (in some unit). Blue = 2×0.2 + 1×0.6 = 1.0. Yellow = 2×0.5 + 1×0.3 = 1.3. So target A needs 0.7 red, 1.0 blue, 1.3 yellow.

This shows how matrix multiplication composes two sets of relationships: the recipes (how each can is made from primaries) and the mixing (how each target is made from cans). The result tells you the final composition.

Where the analogy breaks down: In paint mixing, the numbers represent physical quantities that must be non-negative and add up to a sensible total. In matrix multiplication, numbers can be negative, and there is no such constraint. Also, in paint mixing, the order of operations does not matter — mixing 2 parts of can 1 and 1 part of can 2 is the same as 1 part of can 2 and 2 parts of can 1. In matrix multiplication, order matters enormously: A×B is usually not equal to B×A. This is because matrix multiplication represents the composition of transformations — the order in which you apply transformations changes the result, just as putting on socks then shoes is different from shoes then socks.

Definition

Matrix multiplication is an operation that takes two tables of numbers (matrices) and produces a new table by taking the dot product of each row of the first matrix with each column of the second matrix, provided the number of columns in the first equals the number of rows in the second.

Where this sits

You have no prior mathematics beyond arithmetic, so this page builds from scratch. The key idea you must take from arithmetic is multiplication and addition, which you have used. Matrix multiplication is a way to organize many multiplications and additions at once. It is the foundation of linear algebra, which is the branch of mathematics that studies tables of numbers and the transformations they represent. Once you master matrix multiplication, you will be ready for: linear transformations (how a matrix moves points in space), eigenvalues and eigenvectors (directions that a matrix stretches), and later, neural network layers (which are just matrix multiplications followed by a nonlinear step). The shape rules you will learn here are exactly what let you check whether a neural network architecture is valid on paper.

Signal from the Frontier

Get the next essay on mind, machine, and meaning

Essays at the intersection of AI, philosophy, and Indian governance. No promotional content.

We'll send a one-click sign-in link to confirm. No password needed.

Views expressed are personal and do not represent the Government of India or the Government of Uttarakhand.