Home / Linear Algebra

Matrix Multiplication

Click any cell in the result and watch exactly which row and column produced it. Every entry is a dot product — and this single operation is what every neural network layer actually does.

Shapes

rows of A (m)2
cols of A / rows of B (n)3
cols of B (p)2

Click any cell in C to see the row and column that build it.

A × B = C

click a cell in C

How That Cell Is Built

Multiplications
0
Additions
0
Output cells
0
Total ops
0

Overview

The shape rule

If A has shape m × n and B has shape n × p, then AB has shape m × p. The shared inner dimension n must match. Each output entry is the dot product of one row of A and one column of B:

(AB)ᵢⱼ = Σₖ AᵢₖBₖⱼ

For example, [1 2] multiplied by the column [3; 4] gives 1·3 + 2·4 = 11. The result is one number because the output has shape 1 × 1.

Matrix Multiplication

Each output entry is a row–column dot product; matching inner dimensions determine whether the product exists.

Products compose transformations

For column vectors, ABx means apply B first, then A. Matrix multiplication is associative, (AB)C = A(BC), but usually not commutative: AB and BA can have different shapes or values. The matrix transformation lesson shows this order geometrically.

The cost of multiplying m × n by n × p is proportional to mnp for the standard algorithm. For a chain of matrices, changing the grouping can therefore change the amount of work without changing the result. Deep-learning layers use these products to transform batches of feature vectors efficiently.

In code, NumPy uses @ for matrix multiplication and * for elementwise multiplication. Check shapes explicitly when a product fails; do not transpose matrices just to silence an error without checking what operation is intended.

Calculate a small product and check its shape

Let A=[[1,2],[3,4]] and B=[[2,0],[1,5]]. The upper-left entry of AB is the first row of A dotted with the first column of B: 1·2+2·1=4. The upper-right entry is 1·0+2·5=10. Repeating this row-column calculation gives AB=[[4,10],[10,20]]. The result is 2×2 because A's rows combine B's columns. If A is m×n and B is r×p, the product AB exists only when n=r, and its output is m×p.

Matrix multiplication is associative but usually not commutative: AB and BA can differ even when both exist. This reflects composition order: for column vectors, ABx applies B first. Elementwise multiplication is a different operation, often written A*B in array libraries; it requires compatible element shapes rather than matching inner dimensions. When an operation fails, inspect whether the intended calculation is a matrix product, transpose, or elementwise operation before changing shapes. For a chain of products, parenthesisation changes computational cost but not the result.

Try this above

Change the matrix dimensions so the inner sizes match, then alter one entry and trace which output cell changes.

Associativity means a chain A(BC) and (AB)C gives the same mathematical result, but intermediate matrix sizes can differ dramatically. For example, multiplying a 100×2 matrix by a 2×1000 matrix costs much less than first multiplying the 2×1000 matrix by a 1000×1000 matrix in a larger chain. Choosing the grouping can reduce computation without changing the function. This is called matrix-chain ordering. In floating-point arithmetic, different groupings can also produce slightly different rounding, even though the exact algebra is associative.

Sources and next steps

Predict, then reveal

About to run: set rows of A (m) to its maximum (5). Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Check yourself

0 of 2

Answer without scrolling back up.

  1. What shape results from multiplying an m × n matrix by an n × p matrix?

  2. In ABx, which matrix acts first on a column vector x?

Cheat sheet

Matrix Multiplication

Compute each entry as a row-times-column dot product and see why the shape rule and GPU parallelism make this the engine of deep learning.

MATHS · vizlearn.in/maths/matrix_multiplication.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.