Matrix multiplication — the product C=AB of an m×n matrix A and an n×p matrix B: an m×p matrix with entries ci,j=∑k=1nai,kbk,j.
Inner dimensions — the number of columns of the left factor and the number of rows of the right factor. They must be equal, or the product is undefined.
Outer dimensions — the number of rows of the left factor and the number of columns of the right factor. They are the shape of the product.
Dot product — the sum of paired products of a row and a column of the same length. Every entry of C is one dot product.
Linear combination — a weighted sum of vectors. Each column of C is a linear combination of the columns of A, and each row of C is a linear combination of the rows of B.
Outer product — a column times a row: an m×p matrix of rank at most one. The product AB is the sum of n of them.
Non-commutative — AB=BA in general. The two products can differ in value, differ in shape, or one of them may not exist at all.
Getting Started with the Visualizer
Pick a case, pick a way of thinking about the product, and watch C=A×B build itself one highlighted step at a time.
• The Scenario tab holds three pills for the three scenarios, an Order toggle between A×B and B×A, and three steppers for the sizes of A and B. The rows of B are shown with a link icon because they are tied to the columns of A; a scenario can lock one stepper to 1, which shows a padlock • The Strategy tab holds four cards for the four strategies, each a different reading of the same product • The summary strip at the right of the tab bar always shows the scenario, the order, the shapes, and the strategy in play • Below the panel, the scene player animates the product. Play, step back and forward, reset, and choose a speed. The Step explanations log on the right keeps every step you have passed and opens with an unnumbered lead block describing the whole run
Every strategy produces the same C. They differ only in what is highlighted and in how many steps the product takes.
The Three Scenarios
The Scenario pills choose which shapes the product takes. They do not change the rule, only which part of the rule is on display.
• General A × B — any m×n times n×p, sizes 1 to 5 on every free stepper. This is the full picture • Matrix × vector — B is locked to a single column, so C is a single column. The product is a matrix acting on a vector • Vector × matrix — A is locked to a single row, so C is a single row. The product is a row vector acting on a matrix
The Order toggle sits beside the pills and swaps the factors. For the default shapes both orders exist, but with other sizes one of them may not, and the tool replaces the animation with a banner. That case has its own section: order and the undefined product.
General A × B: Any Compatible Pair of Shapes
The first scenario places no restriction beyond the one the definition already imposes. A is m×n, B is n×p, and every stepper from 1 to 5 is free except the rows of B, which follow the columns of A.
The tool opens here at its defaults, A2×3 and B3×2. The frozen picture below is that opening scene, before any cell has been computed.
General A × B, the opening scene
A is 2×3 and B is 3×2. The inner 3s match, so the product exists, and the outer 2 and 2 fix C at 2×2. Every cell of C still shows the dashed placeholder: nothing has been computed yet.
Two numbers are decided before a single product is formed. The inner pair, 3 and 3, must agree or nothing can be paired. The outer pair, 2 and 2, becomes the shape of C. Change the columns of A and the rows of B follow; change the rows of A or the columns of B and C changes shape with them. The compatibility rule section states this precisely.
Every cell of C is a dot product of a row of A with a column of B, so there are m×p dot products to form, each with n terms. At the defaults that is four cells with three terms each, twelve multiplications in all. The step count in the player grows with all three sizes, which is the first hint that matrix multiplication is expensive: doubling every dimension multiplies the work by eight.
The other two scenarios are this one with a stepper pinned to 1. Matrix × vector pins the columns of B and vector × matrix pins the rows of A. Nothing about the rule changes; only the shapes do.
Matrix × Vector: A Column on the Right
The second scenario locks the columns of B to 1. B is now a column vector of length n, and C is a column vector of length m: the matrix A has acted on a vector.
At the frozen step below, A is 2×3 and B is a 3×1 column. The first cell of C is being formed, and the second of its three terms is highlighted.
Matrix × vector, c1,1 at term 2 of 3
B has collapsed to a single column, so C is a single column too. Row 1 of A is highlighted against the whole of B, with the second pair, a1,2 and b2,1, raised.
This is the shape that systems of linear equations take. The system Ax=b is exactly a matrix times a column vector, with x the unknown column and b the known one. Each row of A dotted with x is one equation, which is why C has one entry per row of A.
There is a second reading worth holding onto. With B a single column, the column by column strategy has only one column to build, and that column is b1,1 times column 1 of A plus b2,1 times column 2 of A and so on. A matrix times a vector is a linear combination of the columns of the matrix, with the entries of the vector as the weights. That sentence is the foundation of the column space and of everything built on it.
Switch to vector × matrix for the mirror image, where the single row is on the left and the rows of B are combined instead.
Vector × Matrix: A Row on the Left
The third scenario locks the rows of A to 1. A is now a row vector of length n, and C is a row vector of length p: a row acting on the matrix B.
In the frozen step, A is a 1×3 row and B is 3×2. The first cell of C is under construction with its second term active, and the whole of A is the highlighted row because there is no other.
Vector × matrix, c1,1 at term 2 of 3
A is one row, so C is one row. That row is read against column 1 of B, and the second pair, a1,2 and b2,1, is raised. The remaining cell of C waits with a dashed outline.
The result has one entry per column of B, each a dot product of the row with that column. Under the row by row strategy there is a single row of C to build, and it equals a1,1 times row 1 of B plus a1,2 times row 2 of B and so on. A row vector times a matrix is a linear combination of the rows of the matrix, with the entries of the row vector as weights. This is the row space counterpart of the column picture in matrix × vector.
The two scenarios are tied together by the transpose. Transposing a product reverses the order, (AB)T=BTAT, so a row times a matrix is the transpose of the corresponding matrix times a column. Anything true of one picture has a twin in the other.
Row vectors on the left appear whenever a linear functional is applied to a matrix, in the left eigenvector equation xA=λx, and in Markov chains, where the state is a row of probabilities and each step is a multiplication by the transition matrix on the right.
Order and the Undefined Product
The Order toggle swaps the factors. A×B pairs the columns of A with the rows of B; B×A pairs the columns of B with the rows of A. Those are different requirements, and one can hold while the other fails.
When it fails, the scene player is replaced by a red banner stating the two shapes and the mismatched inner pair, and the toggle itself turns red as a warning before you press it. The still below is such a case: B is 3×3, A is 2×3, and B×A asks three columns to pair with two rows.
B × A with B 3×3 and A 2×3
The three columns of B are shaded grey and the two rows of A blue. A dot product would have to pair one of each, and 3 cannot be paired with 2, so nothing can be written where C would stand.
At the defaults, A2×3 and B3×2, both orders exist, but they are not the same product. A×B is 2×2 while B×A is 3×3. That is the most direct demonstration that matrix multiplication is not commutative: the two results cannot even be compared entry by entry because they have different shapes.
For square factors of the same size both orders exist and have the same shape, and they still differ in general. Play the row · column strategy in both orders on a 2×2 pair and watch which cells are paired: c1,2 in one order draws on row 1 of A and column 2 of B, in the other on row 1 of B and column 2 of A. Different pairs, different sums.
Whether a given order exists is decided by the compatibility rule alone. The banner links there. Pick sizes so that the columns of B equal the rows of A and the toggle turns blue again.
The Four Strategies
The Strategy tab offers four ways to build the same product. Each is a valid definition on its own, and each brings a different structure to the front.
• Row · column — one cell of C at a time, each a dot product. The textbook definition, and the slowest: m×p cells with n terms each • Column by column — one column of C at a time, each a weighted sum of the columns of A. p columns, n terms each • Row by row — one row of C at a time, each a weighted sum of the rows of B. m rows, n terms each • Sum of outer products — all of C at once, accumulated from nrank-one matrices. Marked advanced because it is the least familiar
The step counter changes with the strategy. At the defaults the row · column strategy takes 22 scenes, column by column and row by row take 12, and the sum of outer products takes 11. The result is identical every time.
Row · Column: One Cell at a Time
The first strategy is the definition made literal. To fill ci,j, take row i of A and column j of B, multiply them entry by entry, and add the n products. Then move to the next cell.
The frozen scene below is the second cell, c1,2, at its second of three terms. Row 1 of A is shaded blue and column 2 of B grey, with the active pair a1,2 and b2,2 raised.
Row · column, c1,2 at term 2 of 3
Row 1 of A in blue meets column 2 of B in grey. The active pair, a1,2 and b2,2, is raised, and the cell being written, c1,2, is solid green while the other cells of C keep their dashed outline.
The formula line under the player spells out the dot product with the current term emphasised, c1,2=a1,1b1,2+a1,2b2,2+a1,3b3,2, and the cell of C being written is solid green while the untouched cells keep a dashed outline. Cells sweep in reading order: across the first row of C, then the second.
This is the strategy to run first, because the other three are shortcuts that assume you already believe this one. It is also the one that exposes the cost. Every cell needs n multiplications, and there are m×p cells, so the product costs m×n×p multiplications in all: twelve at the defaults, and one hundred and twenty five for a pair of 5×5 matrices.
The rule is correct, and it hides what the product does. Drawn in the plane rather than tabulated, the product moves whole columns or whole rows, never single entries.
Column by Column: Combining the Columns of A
The second strategy builds C one column at a time. Column j of C is A times column j of B, and that in turn is a weighted sum of the columns of A: b1,j times column 1, plus b2,j times column 2, and so on down column j of B.
The still below is column 2 of C at its second of three terms. Column 1 of C is already complete and solid green. Column 2 of A is highlighted whole, the single entry b2,2 is the weight, and column 2 of C is the column being filled.
Column by column, column 2 of C at term 2 of 3
Column 1 of C is already solid green. Column 2 of A is highlighted whole, the single grey entry b2,2 is its weight, and column 2 of C is the column being filled.
The formula line reads col2(C)=b1,2col1(A)+b2,2col2(A)+b3,2col3(A), and this is the most consequential of the four readings. Every column of C lies in the span of the columns of A, whatever B is. That single observation is why the column space of AB sits inside the column space of A, why the rank of a product cannot exceed the rank of either factor, and why solving Ax=b means asking whether b is a combination of the columns of A.
The number of steps is p, one per column of C, each with n terms: at the defaults, two columns of three terms. Compare that with the m×p cells of the row · column view. The multiplications are the same twelve; they are simply grouped by column instead of by cell.
The matrix × vector scenario is this strategy with a single column to build.
Row by Row: Combining the Rows of B
The third strategy is the mirror of the second. Row i of C is row i of A times B, which is a weighted sum of the rows of B: ai,1 times row 1, plus ai,2 times row 2, and so on across row i of A.
The frozen scene is row 2 of C at its second term. Row 1 of C is done. The single entry a2,2 is the weight, row 2 of B is highlighted whole, and row 2 of C is the row being filled.
Row by row, row 2 of C at term 2 of 3
Row 1 of C is complete. The single blue entry a2,2 is the weight, row 2 of B is highlighted whole in grey, and row 2 of C is the row being filled.
The formula line reads row2(C)=a2,1row1(B)+a2,2row2(B)+a2,3row3(B). Every row of C lies in the span of the rows of B, whatever A is, so the row space of AB sits inside the row space of B. Together with the column reading this bounds the rank of a product from both sides.
This is also the view behind row operations. Multiplying B on the left by an elementary matrix replaces the rows of B by combinations of themselves, which is exactly what the row by row strategy shows: each new row is built from the old rows with the entries of the left factor as weights. Gaussian elimination is a sequence of such left multiplications.
There are m steps, one per row of C, each with n terms: at the defaults, two rows of three terms. Column by column grouped the same work by columns; the vector × matrix scenario is this strategy with a single row to build.
Sum of Outer Products: All of C at Once
The fourth strategy gives up on cells, rows, and columns and builds all of C in n additions. For each k from 1 to n, column k of A times row k of B is a full m×p matrix of rank at most one. Adding those n matrices gives C.
In the frozen scene, contribution 2 of 3 is being added. Column 2 of A and row 2 of B are highlighted together, and every cell of C is green because every cell receives a term from this pair.
Sum of outer products, contribution 2 of 3
Column 2 of A and row 2 of B are highlighted together. Their outer product is a full 2×2 matrix, added into every cell of C at once, which is why all of C is green rather than a single cell.
The formula line reads C=col1(A)row1(B)+col2(A)row2(B)+col3(A)row3(B). Expand any one cell of that sum and the row · column dot product reappears, because the (i,j) entry of colk(A)rowk(B) is ai,kbk,j. The four strategies are one formula with the summation performed in different orders.
The outer product view is the one that generalises. It is the shape of the singular value decomposition, A=∑kσkukvkT, of every low-rank approximation, and of the update rules in numerical linear algebra that add one rank-one matrix at a time. Recognising a product as a sum of rank-one pieces is often the first step in seeing why an algorithm is cheap.
At the defaults there are three contributions, one per inner index. The step count no longer depends on m or p at all, only on n.
Reading the Scene Player
Each scene combines highlights on A, B and C with arrows and a title, and the same colours mean the same things in every strategy.
• Blue on A marks the row, column, or cell currently being read. A raised, darker blue cell is the active entry of a pair • Grey on B marks its counterpart on the other side, with the same raised treatment for the active entry • Solid green on C marks cells that are being written or are complete; a dashed green outline marks a cell that has not been touched yet • Arrows run from the highlighted entries of A and B to the cell of C that receives their product • The title names the cell, column, row, or contribution in play, and the formula beneath it in the log shows the full expression with the active term emphasised
The Step explanations log keeps every scene you have passed. Its lead block, above step 1, describes the run as a whole and carries a short note on the active strategy and scenario; click any entry to jump the player to that scene.
What Matrix Multiplication Is
The product of an m×n matrix A and an n×p matrix B is the m×p matrix C whose entries are
ci,j=k=1∑nai,kbk,j
Each entry is a dot product of row i of A with column j of B, which is why the row must be as long as the column: the columns of A must equal the rows of B.
The definition looks arbitrary until it is read as composition. A matrix is a linear map, sending a column x to Ax. Applying B first and then A sends x to A(Bx), and the matrix that does this in one step is exactly AB. The rule for ci,j is what it has to be for (AB)x=A(Bx) to hold for every x.
Every other property follows from that one fact. Associativity is the associativity of composition. The failure of commutativity is the observation that doing two things in different orders usually gives different results. The compatibility rule is the requirement that the output of the first map can be fed into the second.
The Compatibility Rule
A×B is defined exactly when the number of columns of A equals the number of rows of B. Writing A as m×n and B as n×p, the inner ns must agree and the product is m×p.
Some pairs and their outcomes:
• 2×3 times 3×2: defined, result 2×2 • 3×2 times 2×3: defined, result 3×3 • 3×2 times 3×4: undefined, since 2=3 • 4×4 times 4×1: defined, result 4×1 • 1×4 times 4×4: defined, result 1×4
The rule is not symmetric. A×B and B×A impose different conditions, and the tool's order toggle lets you see one hold while the other fails. In the tool the rows of B are linked to the columns of A, so A×B always exists; only B×A can be undefined.
A useful way to keep the rule straight: write the shapes side by side, (m×n)(n×p), and check that the two middle numbers match. The two outer numbers are the answer.
Key Properties
Matrix multiplication keeps some of the familiar rules of arithmetic and drops others.
• Associative: (AB)C=A(BC), so a chain of products can be bracketed freely • Distributive: A(B+C)=AB+AC and (A+B)C=AC+BC • Scalars pass through: (kA)B=A(kB)=k(AB) • Identity: ImA=AIn=A for an m×n matrix A • Transpose reverses the order: (AB)T=BTAT • Not commutative: AB=BA in general, and one of the two may not exist
Two consequences catch people out. Because there is no commutativity, (A+B)2=A2+AB+BA+B2, and the middle terms do not merge. And because there are zero divisors, AB=0 does not force A=0 or B=0: a nonzero matrix can kill another nonzero matrix. Cancellation, AB=AC implying B=C, holds only when A is invertible.
The transpose rule mirrors the compatibility rule. If A is m×n and B is n×p, then BT is p×n and AT is n×m, so BTAT is the only order in which the transposes can be multiplied at all.
Worked Example
Take A as 2×3 and B as 3×2, the tool's default shapes:
A=(142536),B=791181012
The inner sizes agree, so C=AB is 2×2. Cell by cell:
c1,1=1⋅7+2⋅9+3⋅11=58,c1,2=1⋅8+2⋅10+3⋅12=64
c2,1=4⋅7+5⋅9+6⋅11=139,c2,2=4⋅8+5⋅10+6⋅12=154
AB=(5813964154)
The same numbers come out of every strategy. Column 1 of C is 7 times column 1 of A plus 9 times column 2 plus 11 times column 3: (7+18+33,28+45+66)=(58,139). Row 1 of C is 1 times row 1 of B plus 2 times row 2 plus 3 times row 3: (7+18+33,8+20+36)=(58,64). And the three outer products are
For the reverse order, BA is 3×3: it exists, but it is a different matrix of a different shape. Set the tool to 2×3 and 3×2 and run each strategy to see these four groupings animated.
Common Mistakes
A few errors recur whenever matrix multiplication is new.
• Multiplying entry by entry — ci,j is not ai,jbi,j. That operation exists, the Hadamard product, but it is a different thing and needs equal shapes • Checking the wrong pair of sizes — the inner dimensions must match, columns of the left factor against rows of the right. Matching rows to rows is the most common slip • Assuming commutativity — AB and BA are different products, and expanding (A+B)2 as A2+2AB+B2 is wrong • Reversing the transpose rule — (AB)T=BTAT, not ATBT; the shapes only work in the reversed order • Cancelling a factor — AB=AC does not give B=C unless A is invertible • Reading the result shape from the wrong ends — the product of m×n and n×p is m×p, the outer numbers, never n×n
The tool guards against the first two by construction: the rows of B are linked to the columns of A, and the undefined B×A case is shown as a banner rather than a wrong answer.
Applications
Matrix multiplication is the operation by which linear maps are composed, so it appears wherever several linear steps are chained.
Computer graphics: rotations, scalings, and translations are matrices, and applying them in sequence is a single product. Every frame of a 3D scene multiplies vertex coordinates by transformation matrices.
Systems of equations: Ax=b is a matrix times a column vector, the matrix × vector scenario, and solving it is the central problem of numerical linear algebra.
Machine learning: a layer of a neural network multiplies its input by a weight matrix. Training and inference are dominated by these products, which is why hardware is built around them.
Markov chains: the state is a row of probabilities and each time step is a multiplication by the transition matrix on the right, the vector × matrix scenario. Powers of the transition matrix give multi-step behaviour.
Graph theory: if M is the adjacency matrix of a graph, the entry (i,j) of Mk counts walks of length k from vertex i to vertex j.