MathalyseResearch

Learn AI · Chapter 2 · Linear Algebra

Special Kinds of Matrices and Vectors

Learn to recognize diagonal, symmetric and orthogonal matrices, and unit and orthogonal vectors, and see why each shape makes arithmetic cheap or geometry simple.

Book §2.6, pages 40–42Equations 2.35–2.38About 30 minutes
Section 1

Why special shapes matter

How to use this page

Click any colored symbol to see what it means. Then drag, slide and type.

A general × matrix is numbers, and every operation touches all of them. A special matrix is a fixed spanner that does fewer jobs, faster.

, matrices diagonal entries , vectors an angle
  • Diagonal matrices scale each coordinate on its own.
  • Symmetric matrices equal their own transpose.
  • Unit and orthogonal vectors are length 1 and at a right angle.
  • Orthogonal matrices rotate or mirror space without stretching it.

The demo counts every multiplication or division on a real × matrix. Move .

Cost of arithmetic by matrix shape

general matrixdiagonal matrixorthogonal matrix
Challenge +8 XP

Find a size where inverting a general matrix costs over 100,000 operations while the diagonal inverse still costs under 100.

Before you leave this demo, guess. If you double , how much more does the general inverse cost?

Predict the growth

Pick a guess. The demo then counts the real operations for the current and for .

A general product costs multiplications, a diagonal one . A general inverse by Gauss-Jordan costs , a diagonal one divisions, an orthogonal one nothing (Section 5).

Going deeper

The Gauss-Jordan count is a standard fact added here, not taken from the book. Each of the pivot columns normalizes one row and clears the others, and every row has entries, so the total is .

The next module shows why these shapes matter so much: a symmetric matrix can be written as an orthogonal matrix, times a diagonal one, times the transpose of that orthogonal matrix.

Section 2

Diagonal matrices

Think of a mixing desk with one volume knob per channel. No knob touches another channel. A matrix is diagonal if and only if it is zero everywhere except the main diagonal, from top left to bottom right:

Click any coloured symbol to see what it means.

The identity matrix is one. To build a square diagonal matrix from a vector , the book writes : the entries of go on the diagonal.

A diagonal matrix only scales. Output entry is , so each coordinate of gets its own factor. The symbol is the element-wise product: multiply two equally long vectors entry by entry.

Click any coloured symbol to see what it means.

Only multiplications, not . Drag the round handle , move the knobs and , and aim the violet output at the orange target.

A diagonal matrix scales each axis alone

input, drag the handle outputoriginal shapetarget
Click any coloured symbol to see what it means.
Challenge +8 XP

Choose the two knobs so the violet output point lands on the orange target ring (within 0.12). Try a negative entry.

A knob of 1.5 stretches the shape sideways. A negative knob mirrors it. A knob between 0 and 1 compresses. A knob at 0 flattens the shape onto a line, and a flattened axis cannot be restored. That is the rule for the inverse, the book’s second claim:

Click any coloured symbol to see what it means.

The inverse exists only if every diagonal entry is non-zero, otherwise it divides by zero. Tick the checkbox: the dark outline returns to the start. Then push a knob to 0 and the inverse is gone.

Going deeper

The book adds a design remark. A general learning algorithm can often be derived for arbitrary matrices. Forcing some of them to be diagonal gives a cheaper but less descriptive algorithm, since a diagonal matrix cannot mix coordinates.

Rectangular diagonal matrices

A diagonal matrix need not be square. A rectangular diagonal matrix still has for . It has no inverse, but multiplying is still cheap. scales each coordinate and then pads or trims the length. A tall appends zeros. A wide drops the last entries of .

Rectangular diagonal matrices

Click any coloured symbol to see what it means.

In the wide tab, move . Nothing in the output changes: that lost information is why these matrices have no inverse.

Section 3

Symmetric matrices

The transpose flips a matrix over its main diagonal: entry moves to . A matrix is symmetric if flipping changes nothing:

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

Entry by entry, : the upper right triangle is a mirror image of the lower left. Only of the numbers are free. This count is added here and is not printed in the book.

Symmetric matrices arise from functions of two arguments that ignore their order. The book’s example is a table of distances: is the distance from point to point , and the way back is equally long. Drag the four points, then switch on a one-way cost (think uphill walking) and watch the mirror break.

Distances give a symmetric matrix

Challenge +8 XP

Switch on the one-way cost, then drag the four points so the matrix becomes symmetric again. (Hint: what makes the climb vanish?)

The orange cells mark every pair that disagrees. Symmetry belongs to the function that produces the entries. It can be lost as easily as it is gained.

Section 4

Unit and orthogonal vectors

A unit vector has length one, that is, unit norm. The L2 norm is the ordinary length of . Divide any non-zero vector by its own length and you get a unit vector with the same direction, a pure compass heading.

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

Two vectors and are orthogonal if their dot product vanishes:

Click any coloured symbol to see what it means.

By Equation 2.34, . For non-zero vectors the product is zero exactly when : a 90 degree angle. If both also have length 1, they are orthonormal.

Drag and until the readout says orthonormal. The dashed circle has radius 1.

Drag two vectors to a right angle

Challenge +8 XP

Drag the tip of to point 30° above the horizontal. Then make and orthonormal: both tips on the dashed circle (within 0.05), and a right angle (within 3°).

In , at most non-zero vectors can be mutually orthogonal. Guess what that means in the plane.

Predict: three at a right angle?

Can three non-zero vectors in the plane be mutually orthogonal? Pick, and the demo above adds the third vector so you can try.

Going deeper

This explanation is added here and is not printed in the book. After and take the two perpendicular directions, any other direction has a non-zero part along one of them. The zero vector is orthogonal to everything, which is why the book requires non-zero norm.

Section 5

Orthogonal matrices

An orthogonal matrix is square, with mutually orthonormal rows and mutually orthonormal columns. Think of the two axes of a picture frame: turn the frame and the axes stay perpendicular and of unit length.

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

Entry of is the dot product of column with column of . For the identity, every column needs dot product 0 with the others and 1 with itself. The second equality says the same for rows.

The inverse undoes a matrix, so says directly that the transpose undoes :

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

Hence the inverse is almost free: a relabeling of entries, no arithmetic, the third bar of the cost demo. The book’s right inverse (Eq. 2.29) equals the left one for square matrices. Module 2-4 teaches this, so it is not repeated here.

Rotations and reflections

In the plane every orthogonal matrix is a rotation or a reflection (a rotation plus a mirror flip). This classification is added here and is not printed in the book. Turn the F and watch the two arrows, the matrix columns.

Rotations and reflections

originalafter after then its transpose
Click any coloured symbol to see what it means.

The arrows stay at length 1 and 90 degrees, and the transpose brings the shape back. A determinant is one number that a square matrix produces: the matrix with rows (2, 0) and (0, 5) has determinant 10. This definition is added here and is not printed in this section of the book. Every rotation here has determinant +1, and every reflection has −1.

Challenge +8 XP

Rotate the F so the first column (teal arrow) points straight up: a rotation by exactly 90°, no flip.

The warning in the definition

The book stresses this: the rows and the columns must be orthonormal, not merely orthogonal. There is no special name for orthogonal vectors of unequal length. Here a rotation has its second column stretched by . The columns stay at right angles, yet as soon as the transpose no longer undoes the matrix.

Orthogonal is not enough: unit length matters

originalafter after then the transpose
Click any coloured symbol to see what it means.

For the product is diagonal with entries 1 and . The transpose over- or under-corrects. Only would work: with its second row divided by . Remember: orthonormal columns, then the transpose is the inverse.

Challenge +8 XP

Make orthogonal again: bring the stretch back to 1, so the dark outline lies exactly on the gray original.

What to take away
  • A diagonal matrix scales each coordinate separately, at a cost of operations. Its inverse exists only when every diagonal entry is non-zero.
  • A rectangular diagonal matrix pads the result with zeros or drops trailing entries. It has no inverse.
  • A symmetric matrix satisfies , as distance tables do.
  • A unit vector has norm 1. Two vectors are orthogonal when , and orthonormal if also unit. At most non-zero vectors in are mutually orthogonal.
  • An orthogonal matrix has orthonormal rows and columns, so and . It rotates or reflects space without changing lengths.
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.6 (pages 40–42), 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.