Free preview

61 lessons

Essential Linear Algebra for ML

Free preview

Essential Linear Algebra for ML · 61 lessons

Master the linear algebra that powers modern ML

No surprise gaps

Actually remember it

Skip what you know

One subscription. All learning paths included.

Our content is best on a larger screen

Symmetric matrices

What is a symmetric matrix?

Explanation

A symmetric matrix is a square matrix that is equal to its own transpose. In other words, if AA is a symmetric matrix, then we have:

A=AT.A = A^T.

This means that the entry in row ii and column jj is the same as the entry in row jj and column ii, for all ii and jj. That is, Aij=AjiA_{ij} = A_{ji}.

For example, a 2×22 \times 2 matrix A=[abcd]A = \begin{bmatrix} a & b \\ c & d \end{bmatrix} is symmetric if b=cb = c. So, a symmetric 2×22 \times 2 matrix looks like:

[abbd].\begin{bmatrix} a & b \\ b & d \end{bmatrix}.

Similarly, a 3×33 \times 3 matrix A=[abcdefghi]A = \begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix} is symmetric if b=db=d, c=gc=g, and f=hf=h. It looks like:

[abcbefcfi].\begin{bmatrix} a & b & c \\ b & e & f \\ c & f & i \end{bmatrix}.

The elements along the main diagonal (from top-left to bottom-right) can have any value, but the elements mirrored across this diagonal must be identical.

Example

Determine which of the following matrices are symmetric:

A=[2334],B=[1234],C=[517182720].A = \begin{bmatrix} 2 & 3 \\ 3 & 4 \end{bmatrix}, \quad B = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad C = \begin{bmatrix} 5 & 1 & 7 \\ 1 & 8 & -2 \\ 7 & -2 & 0 \end{bmatrix}.

Solution

Let's check each matrix in turn:

AT=[2334]=AA^T = \begin{bmatrix} 2 & 3 \\ 3 & 4 \end{bmatrix} = A, so AA is symmetric.

BT=[1324]BB^T = \begin{bmatrix} 1 & 3 \\ 2 & 4 \end{bmatrix} \neq B, so BB is not symmetric.

CT=CC^T = C, so CC is symmetric.

Practice questions

3 questions

Consider the following 4×44 \times 4 matrices:

A=[2457406856327821],B=[3125147625805619]A = \begin{bmatrix} 2 & 4 & 5 & 7 \\ 4 & 0 & 6 & 8 \\ 5 & 6 & 3 & 2 \\ 7 & 8 & 2 & 1 \end{bmatrix}, \quad B = \begin{bmatrix} 3 & 1 & 2 & 5 \\ 1 & 4 & 7 & 6 \\ 2 & 5 & 8 & 0 \\ 5 & 6 & 1 & 9 \end{bmatrix}

Which of the following is true?

Select the correct answer:

+ 2 more questions

Orthogonal eigenvectors of symmetric matrices

Explanation

Symmetric matrices have two fundamental properties:

1. All eigenvalues are real numbers.

If AA is symmetric, then every solution λ\lambda of det(AλI)=0\det(A - \lambda I) = 0 is a real number. This means we never have to deal with complex eigenvalues when working with symmetric matrices.

2. The eigenvectors can be chosen to be orthonormal.

If AA is a symmetric matrix with two different eigenvalues λ1λ2\lambda_1 \neq \lambda_2, and corresponding eigenvectors v1\vec{v}_1 and v2\vec{v}_2, then these eigenvectors are guaranteed to be orthogonal:

v1v2=0.\vec{v}_1 \cdot \vec{v}_2 = 0.

This means that eigenvectors associated with distinct eigenvalues always point in perpendicular directions. Even when eigenvalues are repeated, it is still possible to select eigenvectors that are mutually orthogonal.

Finally, by normalising each eigenvector to have length 11, we can obtain an orthonormal set of eigenvectors {v^1,v^2,}\{\hat{\vec{v}}_1, \hat{\vec{v}}_2, \dots\} satisfying

v^iv^j={1if i=j,0if ij.\hat{\vec{v}}_i \cdot \hat{\vec{v}}_j = \begin{cases} 1 & \text{if } i = j, \\ 0 & \text{if } i \neq j. \end{cases}

In other words, every eigenvector in the set has unit length, and any two different eigenvectors are perpendicular (i.e. their dot product is 00). Note we use the "hat" notation v^\hat{\vec{v}} above a vector v\vec{v} to denote that it's a unit vector.

Together, these properties make symmetric matrices especially well-behaved. Their eigenvectors form a clean, perpendicular coordinate system, and each eigenvector points along a direction that the matrix simply stretches or compresses.

Because the eigenvectors are orthogonal, these directions do not overlap or interfere with each other, which is why symmetric matrices are so useful in practice.

Visualisation

Have a play with the visualisation below.

1234−1−2−3−41234−1−2−3−4
λ1=3.62,v^1=[0.530.85]
λ2=1.38,v^2=[0.850.53]
v^1v^2=0.00
A=[2113]
v^1
v^2

The blue and red lines show the eigenvector directions of the symmetric matrix A. The arrows on these lines are the orthonormal eigenvectors (unit length). Notice their dot product is always zero.

ML Context

Symmetric matrices are everywhere in data science, for example:

  • Covariance matrices: These describe how features vary together in a dataset. They are always symmetric. Their orthonormal eigenvectors give the principal components in PCA, and the eigenvalues tell us how much variance each component explains.
  • Principal Component Analysis (PCA): PCA works because the covariance matrix has orthonormal eigenvectors. This ensures that the principal components are independent directions of variation, making dimensionality reduction clean and interpretable.
  • Graphs and networks: The adjacency matrix of an undirected graph is symmetric. Its eigenvalues and eigenvectors reveal structural properties such as connectivity and community structure.
  • Optimisation: Many optimisation problems (e.g. quadratic programming) involve symmetric matrices. Their well-behaved eigenstructure guarantees stability and makes analysis tractable.

In short: whenever we see a symmetric matrix in ML, we can rely on its eigenvalues being real and its eigenvectors forming a neat, orthonormal coordinate system.

Example

Consider the symmetric matrix

A=[4224].A = \begin{bmatrix} 4 & 2 \\ 2 & 4 \end{bmatrix}.

The eigenvalues of AA are λ1=6\lambda_1 = 6 and λ2=2\lambda_2 = 2, with corresponding eigenvectors

v1=[11],v2=[11].\vec{v}_1 = \begin{bmatrix} 1 \\ 1 \end{bmatrix}, \quad \vec{v}_2 = \begin{bmatrix} -1 \\ 1 \end{bmatrix}.
  1. Check that v1\vec{v}_1 and v2\vec{v}_2 are orthogonal.
  2. Normalise them to form an orthonormal set of eigenvectors {u^1,u^2}\{\hat{\vec{u}}_1, \hat{\vec{u}}_2 \}.

Solution

1. Check orthogonality

v1v2=(1)(1)+(1)(1)=1+1=0.\vec{v}_1 \cdot \vec{v}_2 = (1)(-1) + (1)(1) = -1 + 1 = 0.

So the eigenvectors are orthogonal.

2. Normalise each eigenvector

The length of v1\vec{v}_1 is:

v1=12+12=2.|\vec{v}_1| = \sqrt{1^2 + 1^2} = \sqrt{2}.

So the unit vector, which we'll call u^1\hat{\vec{u}}_1, is given by:

u^1=12[11].\hat{\vec{u}}_1 = \dfrac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ 1 \end{bmatrix}.

The length of v2\vec{v}_2 is:

v2=(1)2+12=2.|\vec{v}_2| = \sqrt{(-1)^2 + 1^2} = \sqrt{2}.

So the unit vector, which we'll call u^2\hat{\vec{u}}_2, is:

u^2=12[11].\hat{\vec{u}}_2 = \dfrac{1}{\sqrt{2}} \begin{bmatrix} -1 \\ 1 \end{bmatrix}.

The orthonormal set of eigenvectors, {u^1,u^2}\{\hat{\vec{u}}_1, \hat{\vec{u}}_2 \}, of AA is therefore:

{12[11],12[11]}.\left\{ \dfrac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ 1 \end{bmatrix}, \dfrac{1}{\sqrt{2}} \begin{bmatrix} -1 \\ 1 \end{bmatrix}\right\}.

Practice questions

3 questions

Suppose a symmetric matrix AA has orthogonal eigenvectors:

v1=[050],v2=[100],v3=[0012]\vec{v}_1 = \begin{bmatrix} 0 \\ 5 \\ 0 \end{bmatrix}, \quad \vec{v}_2 = \begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix}, \quad \vec{v}_3 = \begin{bmatrix} 0 \\ 0 \\ 12 \end{bmatrix}

Which of the following is an orthonormal set of eigenvectors for AA?

Select the correct answer:

+ 2 more questions

Orthogonal diagonalisation of symmetric matrices

Explanation

A key property of symmetric matrices is that they can always be written in a special form using their eigenvectors and eigenvalues. Specifically, if AA is a symmetric matrix, then there exists an orthogonal matrix QQ and a diagonal matrix DD such that:

A=QDQT.A = Q D Q^T.

Here:

  • QQ is an orthogonal matrix, meaning its columns are orthonormal eigenvectors of AA (each has length 11, and they are perpendicular to each other).
  • DD is a diagonal matrix, which just means all the non‑zero entries are on the main diagonal (from top-left to bottom-right). Those diagonal entries are the eigenvalues of AA.

This way of writing AA is called orthogonal diagonalisation. It tells us that every symmetric matrix can be “simplified” into a diagonal form by choosing the right orthonormal eigenvectors.

Ordering QQ and DD

Gotcha

In the decomposition A=QDQTA = Q D Q^T:

  • The first column of QQ is an eigenvector, and its matching eigenvalue must go in the first diagonal entry of DD.
  • The second column of QQ is the next eigenvector, and its eigenvalue must go in the second diagonal entry of DD.
  • And so on.

If we change the order of eigenvalues in DD, we must reorder the columns of QQ in the same way otherwise the eigenvalue–eigenvector pairs no longer match, and QDQTAQ D Q^T \neq A.

Why this matters

Details

Example

Consider the symmetric matrix:

A=[2112].A = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}.

This matrix has eigenvalues λ1=3\lambda_1 = 3, λ2=1\lambda_2 = 1, with corresponding orthonormal eigenvectors:

v^1=12[11],v^2=12[11].\hat{\vec{v}}_1 = \frac{1}{\sqrt{2}}\begin{bmatrix}1 \\ 1\end{bmatrix}, \quad \hat{\vec{v}}_2 = \frac{1}{\sqrt{2}}\begin{bmatrix}1 \\ -1\end{bmatrix}.

Verify that AA can be written in the form:

A=QDQT.A = Q D Q^T.

where QQ is an orthogonal matrix and DD is a diagonal matrix.

Solution

We form QQ by putting the eigenvectors as the columns:

Q=[12121212],Q = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\[6pt] \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix},

and DD by putting the corresponding eigenvalues on the diagonal:

D=[3001].D = \begin{bmatrix} 3 & 0 \\ 0 & 1 \end{bmatrix}.

We need to check that A=QDQTA = Q D Q^T holds:

QDQT=[12121212][3001][12121212]=[32123212][12121212]=[2112]=A.\begin{align*} Q D Q^T &= \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\[6pt] \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix} \begin{bmatrix} 3 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\[6pt] \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix} \\ &= \begin{bmatrix} \frac{3}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\[6pt] \frac{3}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix} \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\[6pt] \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix} \\ &= \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} \\ &= A. \end{align*}

And so we have verified that AA can indeed be written in the form A=QDQTA = Q D Q^T. This is an example of orthogonal diagonalisation.

Practice questions

3 questions

In the orthogonal decomposition, A=QDQTA = Q D Q^T, of a symmetric matrix AA, what do the columns of QQ represent?

Select the correct answer:

+ 2 more questions