Skip to content
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 Structure

A matrix's shape and constraints determine its properties. A constraint is not merely a restriction; it is a type hint for which operations exist and which can be cheap.

TypeConstraintWhy it is useful
Squaresame number of rows and columnsdeterminants and eigenvalues are defined, and a nonsingular square matrix has an inverse
Rectangularchanges dimensionality, such as projecting 1,024 dimensions to 256
Diagonalzero off the diagonalscales each axis and needs only diagonal entries for many operations
Identity ones on the diagonal, zero elsewheremultiplicative identity:
Zeroall entries are zeroadditive identity for matrices of the same shape
Symmetriccommon in covariance and energy models, with useful real eigendecomposition properties
Sparsemost entries are zerostores and computes mainly nonzero entries to save memory and work

Why You Should Care

Identity matrices appear in regularization and linear-system formulas, diagonal matrices in scaling and SVD, and symmetric matrices in covariance, graph, and energy problems. Sparse storage is what lets enormous recommendation matrices and graphs fit in memory.

A rectangular matrix has no ordinary two-sided inverse, though least-squares solutions and pseudoinverses may apply. A square matrix can also be singular and noninvertible. Shape alone never guarantees an inverse.

Matrix types are mathematical type hints. Recognizing one reveals which operations are cheap and which assumptions fail. Do not densify a sparse matrix casually, and do not feed a nonsymmetric matrix to an algorithm that assumes symmetry.

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.