MathalyseResearch

Learn AI · Chapter 2 · Linear Algebra

Multiplying Matrices and Vectors

See how a matrix acts on a vector, how two matrices combine, which algebra rules the product obeys and which it breaks, and how a pile of equations collapses into the single line Ax = b.

Book §2.2, pages 34–36Equations 2.4–2.19About 30 minutes
Section 1

One operation, many weighted sums

How to use this page

Click any colored symbol to read what it means. Then drag the dots and commit to a guess before each answer appears.

A neural network layer turns one list of numbers into another, one weighted sum at a time.

Picture a stack of recipe cards. Card says how much of each ingredient goes into dish . The matrix is that whole stack.

the matrix that acts , what it acts on , the result

Recap: has rows and columns, and (the transpose) swaps rows and columns.

Each row is a recipe

Set the inputs with sliders

product with x1product with x2product with x3output bi
Challenge +8 XP

With Sum and difference loaded, drag the inputs until both outputs are 0 and the inputs are not all 0.

A positive weight on a positive input pushes right. A negative weight or a negative input pushes left. Sum and difference adds all three inputs in row 1 and compares the first input with the third in row 2.

Your challenge answer shows something odd: a non-zero input can produce a zero output. Keep that in mind for the module on span and rank.

Section 2

The matrix product and its shapes

The most important operation on two matrices is their product. The product of and is a third matrix , written by placing them side by side:

(2.4)
Click any coloured symbol to see what it means.

Think of it as a handshake between shapes. must have as many columns as has rows. With of shape and of shape , the result is . The inner numbers must agree and disappear.

Entry by entry, the product is defined by

(2.5)
Click any coloured symbol to see what it means.

To build , take row of and column of . Pair their -th entries, multiply each pair, and add up all products. The index runs along the shared size, which is why that size has to match.

Click any cell to choose which entry of to build. Then type the entry yourself before reading it.

Matrix product explorer

Type a number and press Check.

A: the row usedB: the column usedC: the entry produced
Challenge +8 XP

Resize the matrices so that the product is a 3 by 4 matrix.

Challenge +8 XP

Work out the highlighted entry of by hand, type it, and press Check until you get it right.

With the default sizes, 2 by 3 times 3 by 2 gives 2 by 2. Raise “Rows of B” to 4 and the product disappears: a row of has 3 numbers and a column of has 4, so they cannot be paired.

The element-wise product

The standard product is not the matrix of products of matching entries. That second operation is the element-wise product, or Hadamard product, written . Both matrices need the same shape:

Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.

Switch the explorer to its second tab and compare. There is no sum over at all. With , the matrix product has every entry equal to 2 (each entry adds two products), and the element-wise product is all ones.

Going deeper: where the element-wise product returns

Section 2.6 of the book uses it to write the effect of a diagonal matrix: . Never mix the two products up. The same two matrices give different results.

Section 3

The dot product

Two vectors and of the same length have a dot product. The book defines it as the matrix product : a row (shape ) times a column (shape ) is a 1 by 1 matrix, which is one number. Equation 2.5 with those shapes gives the formula:

Click any coloured symbol to see what it means.

This also explains the book’s remark that in a matrix product is the dot product of row of with column of . The recipe demo was a stack of dot products.

A dot product measures agreement. Entries with matching signs add to the total, and opposite signs subtract. Drag the two arrow tips.

Two vectors, one number

Set the entries with sliders
arrow x and product x1y1arrow yproduct x2y2total
Challenge +8 XP

Drag the tips until the two arrows meet at a right angle, so that the dot product is exactly 0. Neither arrow may have length 0.

Arrows pointing roughly the same way give a positive total. A right angle gives zero. Arrows pointing apart give a negative total. Equation 2.34, in the section on norms, makes the link to the angle exact.

The second bar chain multiplies the same pairs in the other order, , and ends at the same place. Every product equals , since ordinary multiplication ignores order. That is the first rule of the next section.

Section 4

Rules of the product and the transpose

The matrix product obeys two friendly rules. It is distributive and associative:

(2.6)
Click any coloured symbol to see what it means.
(2.7)
Click any coloured symbol to see what it means.

Distributive means a product can be spread over a sum. Associative means the brackets can go anywhere in a chain, so is unambiguous. The shapes still have to fit at every step.

One rule from school arithmetic does not survive. Matrix multiplication is not commutative: does not always hold, and for some shapes does not even exist. A pair of vectors is the exception, and the book records it:

(2.8)
Click any coloured symbol to see what it means.

The bench tests each rule on random matrices. It is a check, not a proof.

Test the rules on random matrices

Will AB = BA for these two?

Challenge +8 XP

On the AB versus BA tab, predict correctly three times whether the two products are equal.

Distributivity and associativity hold for every draw. The third tab is where things fail: for 2 by 2 matrices both orders exist and usually differ, and for rectangular shapes cannot be formed. A few pairs do commute, which is why guessing is a game.

Going deeper: associativity and cost

Associativity says nothing about work. For a chain the two bracketings give the same matrix with different numbers of scalar multiplications, so software chooses the bracketing on purpose. The associativity tab prints both counts. This remark is added here and is not in the book.

Order matters: a picture

Order matters because a matrix acts, and acting happens in time: socks, then shoes, is not shoes, then socks. By Equation 2.7,, so means “act with first, then ”.

Here turns the plane and stretches the horizontal direction. Drag on either picture to turn, and use the slider to stretch.

Rotate and stretch in two orders

original Fresult of AB: stretch first, then rotateresult of BA: rotate first, then stretch
Challenge +8 XP

Make the two orders give the same F while the stretch is not 1 and the rotation is more than 0.

The two results agree only in special settings: no rotation, no stretch, or a half turn (which sends every point through the origin and so commutes with any stretch). Everywhere else the orders disagree.

The transpose of a product

The transpose of a product has a simple form, with a twist: the order reverses.

(2.9)
Click any coloured symbol to see what it means.

The shapes demand the twist. If is and is , then is . The product multiplies by and lands on the same shape. The other order, , only fits if . Try the fourth tab with rectangular shapes.

Going deeper: proof of Equation 2.9

The book states 2.9 without proof. This derivation is added here and is not printed in the book.

Step one uses the definition of the transpose, step two Equation 2.5, step three swaps the indices of each factor, and step four runs Equation 2.5 backwards.

Equation 2.9 also explains Equation 2.8. The value of is one number, and one number is its own transpose. Transposing the product reverses the factors and leaves the value alone:

(2.10)
Click any coloured symbol to see what it means.

The book adds that it does not list all useful properties of the matrix product, since linear algebra is not its focus, and that many more exist.

Section 5

Systems of linear equations

The notation is now rich enough to write a whole system of linear equations in one line:

(2.11)
Click any coloured symbol to see what it means.

Here and are known, and holds the unknowns. A system reverses the first demo: the output is fixed, and you hunt for the input.

Each row of , together with its entry of , is one constraint:

(2.12–2.15)
Click any coloured symbol to see what it means.

Each line is a dot product of a row with . Written out entry by entry, the system reads

(2.16–2.19)
Click any coloured symbol to see what it means.

There are equations, one per row, and unknowns, one per column.

With two unknowns, each equation is a line in the plane of the unknowns, and a solution lies on all lines at once. Drag the point around the plane.

Every row is a line, a solution is on all of them

equation 1equation 2equation 3your xexact solution
Challenge +8 XP

On Independent rows with the third equation off, drag the point onto the crossing so both residuals read 0.00.

Challenge +8 XP

On Parallel rows, choose the right sides so that the two lines coincide: infinitely many solutions.

Two independent rows give exactly one solution, where the lines cross. A third equation misses that crossing unless it passes through it. Parallel rows either coincide, giving a whole line of solutions, or never meet, giving none. The next sections of the book explain what decides which case you get.

What to take away
  • exists when the columns of match the rows of . Its entry is the dot product of row of with column of (Eq. 2.4, 2.5).
  • The element-wise (Hadamard) product is a different operation and needs equal shapes.
  • The product is distributive and associative (2.6, 2.7), not commutative, and the transpose reverses the order: (2.9).
  • The dot product is commutative (2.8, 2.10) because it is a single number.
  • compresses equations in unknowns into one line (2.11 to 2.19). Each row is one constraint.
Final quiz

Check yourself

Questions that mix the whole module. Each one is scored, and the score feeds your mastery of this module.

Source: Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, MIT Press, 2016, Section 2.2 (pages 34–36), free to read at deeplearningbook.org. Where the book prints equation numbers, the numbers here follow the book. The order of ideas follows the book. The explanations and all demos were written for this page.