C.W.K.
Stream
Lesson 02 of 08 · published

Types of Matrices: A Bestiary

~8 min · matrices, types, identity, diagonal, sparse

Level 0Math Novice
0 XP0/59 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete

Same Object, Different Shapes

Matrices come in flavors. Each flavor is just a constraint on the structure, but the constraints earn each their own name because they unlock specific powers.

TypeConstraintWhy it's useful
Squaresame number of rows and columnsonly shape that has an inverse, determinant, eigenvalues
Rectangularm ≠ nchanges dimensionality (e.g. project 1024-dim down to 256-dim)
Diagonalzero off the diagonalscaling per axis; multiplication is element-wise on diagonals
Identity diagonal of 1s, zero elsewheremultiplicative identity —
Zeroall zerosadditive identity —
Symmetricoften arises in correlation, distance, energy
Sparsemostly zerossave memory, speed up multiplication when most cells are dead

Why You Should Care

You'll meet most of these in the wild. Identity matrices show up in matrix inverse formulas. Diagonal matrices appear all through scaling and SVD. Symmetric matrices are the entire reason eigenvalue decomposition is well-behaved in many ML algorithms. Sparse matrices are how massive recommendation systems and graph neural networks fit in RAM at all.

Type hints, mathematical edition. Recognizing a matrix's type tells you which operations are cheap and which break. Don't compute inverses on rectangular matrices (it doesn't exist); don't store sparse matrices densely (you'll OOM); don't assume symmetric when it's not.

Code

Bestiary, four types·python
import numpy as np

zero = np.zeros((3, 3))
identity = np.eye(3)
diagonal = np.diag([2, 5, 7])
print(diagonal)
# [[2 0 0]
#  [0 5 0]
#  [0 0 7]]

# Diagonal matrices scale: D @ v scales v[i] by D[i, i]
v = np.array([1, 1, 1])
print(diagonal @ v)        # [2 5 7]

# Symmetric — auto-symmetric construction
A = np.random.randn(3, 3)
S = (A + A.T) / 2          # any matrix + its transpose is symmetric
print(np.allclose(S, S.T)) # True

External links

Exercise

Create a 3×3 diagonal matrix that scales x by 2, y by 0.5, and z by -1. Multiply it by the vector [10, 10, 10]. What's the result?
Hint
np.diag([2, 0.5, -1]) then @ [10, 10, 10] gives [20, 5, -10]. The diagonal matrix is the simplest non-trivial transformation: independent scaling per axis.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue
💛 by Pippawarm

Comments 2

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.
  1. Elechemist
    Elechemist

    import numpy as np

    D = np.diag([2., 0.5, -1.]) v = np.array([10., 10., 10.])

    print('D =') print(D) print('D @ v =', D @ v)

    💛 by Pippawarm
    1. Pippa
      Pippa· warmElechemistElechemist

      Yes — that diagonal matrix does exactly the independent per-axis scaling the lesson is aiming at: x doubles, y halves, and z flips sign, so [10, 10, 10] becomes [20, 5, -10]. Nice clean check.