Diagonal and Symmetric Matrices
Look at a right triangle and you instantly know you can reach for Pythagoras — the shape itself
tells you which shortcut to use. Matrices work the same way. A general matrix can be a mess of
n^2 unrelated numbers, but a handful of special shapes
keep coming up — in physics, statistics, computer graphics, engineering — and each shape unlocks
its own shortcut. Learning to spot these patterns at a glance saves real work later: you'll know
immediately which tricks are available before doing a single calculation.
This page is a small gallery of the most important shapes: diagonal,
symmetric, and triangular matrices. None of them require new
machinery — you already know what a matrix and a
transpose are.
What's new is learning to recognise these patterns on sight.
Diagonal matrices: the simplest of all
A diagonal matrix has non-zero entries only on the main diagonal — everywhere
else is zero:
\begin{bmatrix} a & 0 \\ 0 & d \end{bmatrix}
Multiply it by a vector and something lovely happens: the coordinates never mix. The first
component just gets scaled by a, the second by
d, completely independently of one another:
\begin{bmatrix} a & 0 \\ 0 & d \end{bmatrix}\begin{bmatrix} x \\ y \end{bmatrix}
= \begin{bmatrix} ax \\ dy \end{bmatrix}
Besides the
identity matrix
itself, this is the easiest possible matrix to work with: multiplying, raising to a
power, and inverting a diagonal matrix all reduce to doing the same simple operation to each
diagonal entry separately, one number at a time.
See it stretch the axes
Watch a diagonal matrix act on the unit square. Slider a stretches it
horizontally, slider d vertically — and the square always stays a neat
rectangle with sides parallel to the axes, because the two directions never mix. That clean,
independent stretching is the visual signature of a diagonal matrix.
Worked example: scaling a vector by a diagonal matrix
Let D = \begin{bmatrix} 3 & 0 \\ 0 & -2 \end{bmatrix} and
\mathbf{v} = \begin{bmatrix} 4 \\ 5 \end{bmatrix}. Because
D is diagonal, there's no need to run the full row-by-column
multiplication — just scale each coordinate by the matching diagonal entry:
D\mathbf{v} = \begin{bmatrix} 3 \cdot 4 \\ -2 \cdot 5 \end{bmatrix}
= \begin{bmatrix} 12 \\ -10 \end{bmatrix}
The x-coordinate tripled; the y-coordinate
doubled and flipped sign. Each axis obeys only its own diagonal entry — that's the whole
calculation, and it works the same way no matter how big the matrix gets.
function diagTimesVector(diag: number[], v: number[]): number[] {
return diag.map((d, i) => d * v[i]);
}
console.log(diagTimesVector([3, -2], [4, 5]));
console.log(diagTimesVector([1, 2, 0.5], [10, 10, 10]));
Symmetric matrices: a mirror across the diagonal
A symmetric matrix equals its own
transpose:
A = A^{\mathsf{T}}
Written entry by entry, that means a_{ij} = a_{ji} for every
i and j — whatever sits above the diagonal
is exactly copied below it, as if the diagonal were a mirror. For example:
A = \begin{bmatrix} 3 & 2 & 7 \\ 2 & 5 & 0 \\ 7 & 0 & 1 \end{bmatrix}
Notice the 2 appears twice (positions (1,2)
and (2,1)), the 7 appears twice, and so
does the 0. Only the three diagonal entries and the three entries
above the diagonal are "free" — the rest are forced copies. A symmetric matrix never needs to be
square any other way; it must be square, and it must be a mirror image of itself.
Worked example: checking symmetry directly
Is B = \begin{bmatrix} 4 & -1 \\ 3 & 6 \end{bmatrix} symmetric? Compute
the transpose by swapping rows and columns:
B^{\mathsf{T}} = \begin{bmatrix} 4 & 3 \\ -1 & 6 \end{bmatrix}
Compare entry by entry: the top-right of B is
-1, but the top-right of B^{\mathsf{T}} is
3. They disagree, so B \neq B^{\mathsf{T}}
and B is not symmetric. Fixing just that one
off-diagonal pair — replacing the 3 with
-1 — would make it symmetric. That's the whole test: transpose it,
and see if you get the same matrix back.
function isSymmetric(m: number[][]): boolean {
const n = m.length;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (m[i][j] !== m[j][i]) return false;
}
}
return true;
}
console.log(isSymmetric([[4, -1], [3, 6]])); // top-right and bottom-left disagree
console.log(isSymmetric([[4, -1], [-1, 6]])); // now they match
console.log(isSymmetric([[3, 2, 7], [2, 5, 0], [7, 0, 1]]));
Triangular matrices: zeros on one side
A matrix is upper triangular if every entry below the main diagonal is
zero, and lower triangular if every entry above it is zero:
U = \begin{bmatrix} 2 & 5 & -1 \\ 0 & 3 & 4 \\ 0 & 0 & 6 \end{bmatrix}
\qquad
L = \begin{bmatrix} 2 & 0 & 0 \\ 6 & 3 & 0 \\ 1 & -4 & 5 \end{bmatrix}
You've already met this shape without the name attached: run
Gaussian elimination
on any system of equations and the row-reduced matrix you end up with — the one in echelon form,
ready for back-substitution — is an upper triangular matrix. The zeros that elimination
painstakingly creates below the diagonal are exactly the zeros this gallery entry demands. Like
diagonal matrices, triangular matrices are cheap to compute with: solving
U\mathbf{x} = \mathbf{b} takes only back-substitution, no full
elimination required, because the zeros are already there.
Worked example: spot the type
Classify each of these matrices as diagonal, symmetric, triangular, more than one of these, or
none:
M_1 = \begin{bmatrix} 5 & 0 \\ 0 & -3 \end{bmatrix}
\quad
M_2 = \begin{bmatrix} 1 & 4 \\ 4 & 1 \end{bmatrix}
\quad
M_3 = \begin{bmatrix} 2 & 1 \\ 0 & 2 \end{bmatrix}
\quad
M_4 = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}
-
M_1: zeros off the diagonal, so it's diagonal. It's
also symmetric (a diagonal matrix always is, since swapping rows and columns changes nothing)
and both upper and lower triangular.
-
M_2: the off-diagonal entries match (both
4), so it's symmetric — but it has a non-zero entry
below the diagonal, so it is not triangular.
-
M_3: the entry below the diagonal is 0
but the entry above is 1, so it's upper triangular only —
not symmetric (1 \neq 0 when you compare the two off-diagonal spots)
and not diagonal.
-
M_4: symmetric (its own mirror image), but not diagonal or
triangular — it has non-zero entries on both sides of the diagonal.
Yes — these categories are not exclusive boxes, they can and do overlap. The clearest example is
the identity matrix
I: it is simultaneously diagonal (only the diagonal
is non-zero), symmetric (I^{\mathsf{T}} = I, trivially),
and both upper and lower triangular at once (everything off the diagonal, on
either side, is zero). Don't treat "which type is it?" as a multiple-choice question with one
right answer — check each definition separately, because a matrix can pass several tests
simultaneously.
The reverse trap is just as common: being symmetric is a genuinely useful property, but it does
not automatically make a matrix "nice" for every purpose — a symmetric matrix can
still be singular, or have negative eigenvalues, or be badly behaved numerically. What symmetry
does guarantee, always, is that its
eigenvalues are all real numbers and its eigenvectors can be chosen perpendicular to one
another
— a remarkably strong promise that general matrices simply don't come with.
Symmetric matrices aren't just a tidy mathematical curiosity — they fall out of real calculations
automatically. Take any dataset with several measured quantities (height, weight, age, say) and
compute the covariance between every pair of quantities: how much they vary
together. Stack all those pairwise covariances into a matrix, and you get the
covariance matrix.
Here's the pleasant surprise: the covariance between height and weight is, by its very
definition, the same number as the covariance between weight and height — order never mattered
in the first place. So entry (i,j) of the covariance matrix is
always equal to entry (j,i), with no extra effort required.
Every covariance matrix that has ever been computed is symmetric, automatically, just from how
covariance is defined.
That single fact is why
principal component analysis
works out so cleanly in practice: PCA diagonalizes the covariance matrix to find the directions
of greatest spread in the data, and because that matrix is guaranteed symmetric, the
diagonalization is guaranteed to succeed with real eigenvalues and perpendicular eigenvectors —
no messy complex numbers, no ambiguity about direction. A gallery entry from this very page is
doing quiet, essential work inside modern data science.