# Diagonalize Matrix Calculator

Created by Maciej Kowalski, PhD candidate
Reviewed by Bogna Szyk and Jack Bowater
Last updated: Feb 02, 2023

Welcome to the diagonalize matrix calculator, where we'll take you on a mathematical journey to the land of matrix diagonalization. We'll go through the topic of how to diagonalize a matrix using its eigenvalues and eigenvectors together. This process is extremely useful in advanced array calculations since it's so much easier to deal with a diagonal matrix rather than a full one.

But is it a simple algorithm? Is every array a diagonalizable matrix? Sit back, grab your morning/afternoon coffee, and let's find out, shall we?

## What is a matrix?

Do you remember the good old days of primary school mathematics? You counted how many oranges Mr. Smith had if he bought eight and ate two, and they told you that these were called integer numbers, and math seemed simple enough.

Then Mr. Smith opened up a little shop and sold his own fruit - $1.20 per pound for apples and$0.90 per pound for bananas, including taxes.They told you that these new values were called rational numbers, and you spent a few months getting the hang of them, multiplying and adding them together. Still, it wasn't too bad.

But they had to go further, didn't they? As long as geometry consisted of shapes such as squares or rectangles, the calculations were pretty simple and straightforward. But once they introduced triangles, especially right triangles, and the Pythagorean theorem, some weird values appeared which were called roots, and, apparently, they can't be described in the form of a good old fraction. And don't even get us started on the $\pi$ that spoiled all the circle calculations.

All these numbers were called real numbers because they appear naturally in the real world. Well, fair enough, but it's so much easier to look at an approximation of the cubic root of three, $1.44$, than the flimsy $\sqrt[3]{3}$ sign.

And you know what? The big-headed mathematicians didn't stop there and decided that it's quite a shame that -1 doesn't have a square root, so why not imagine that it does and call it i. Funnily enough, they actually named it the imaginary number and called this whole new extension complex numbers. But, to give credit where credit is due, complex numbers are extremely useful in all kinds of sciences, including and .

So, now that we have all these fancy numbers, what good can we do with them? That's right - take a boatload of them, write them in a table and treat them as one thing.

A matrix is an array of elements (usually numbers) that has a set number of rows and columns. An example of a matrix would be:

$A=\begin{pmatrix} 3 & -1 \\ 0 & 2\\ 1 & -1 \end{pmatrix}$

Moreover, we say that a matrix has cells, or boxes, into which we write the elements of our array. For example, matrix A above has the value $2$ in the cell that is in the second row and the second column. The starting point here are 1-cell matrices, which are, for all intents and purposes, the same thing as real numbers.

As you can see, matrices came to be when a scientist decided that he needs to write a few numbers concisely and operate with the whole lot as a single object. As such, they naturally appear when dealing with:

• Systems of equations, especially when calculating Cramer's rule and the (reduced) row echelon form;
• Vectors and vector spaces);
• 3-dimensional geometry (e.g., the dot product and the cross product);
• linear transformations (translation and rotation); and
• graph theory and discrete mathematics.

We can look at matrices as an extension of the numbers as we know them (real or complex). As such, it would make sense to define some basic operations on them, like, for example, addition and subtraction. And indeed, it can be easily done, but these new objects have a much more complex structure, and, to each array, we can find several important values, for example we can calculate the matrix's rank.

However, we've gathered here today to look at something quite different: matrix diagonalization. We will need some tricks to define it, but how about we start with what exactly this diagonal matrix is and why they're easier to deal with.

## Diagonal matrix: definition and properties

We call a square array of numbers a diagonal matrix if it is of the form:

$A=\begin{pmatrix} x_1 & 0 & \ldots & 0\\ 0&x_2&\ldots&0\\ \vdots&\vdots&\ddots&\vdots\\ 0&0&\ldots&x_n \end{pmatrix}$

where $x_1$, $x_2$, ..., $x_n$ are some numbers. In other words, a diagonal matrix is an array whose non-zero entries only appear on the main diagonal.

Now let's list a few useful properties of diagonal matrices to convince you that they are fairly easy objects.

1. The sum and product of diagonal matrices is again a diagonal matrix. In other words, if $A$ and $B$ are diagonal matrices, then $A + B$, $A\cdot B$, $A\circ B$ are also diagonal. What's the last symbol? The Hadamard product: learn how to calculate it with our Hadamard product calculator!

2. Diagonal matrices are transpose-invariant. This means that if $A$ is a diagonal matrix, then the calculated transposed matrix is the same object: $A^\intercal = A$.

3. The k-th power of a diagonal matrix is a diagonal matrix with the same entries individually raised to the k-th power. This one might be easier to understand symbolically. Recall the array $A$ above with the $x_i$'s on the diagonal. Then:

$A^k=\begin{pmatrix} x_1^k & 0 & \ldots & 0\\ 0&x_2^k&\ldots&0\\ \vdots&\vdots&\ddots&\vdots\\ 0&0&\ldots&x_n^k \end{pmatrix}$

Hopefully, you can see some advantages in learning how to diagonalize a matrix. Especially the third property above shows that if we need to take the matrix to some high power, then matrix diagonalization would make the task quite a lot easier. And why spend ages doing gruesome calculations if a simple trick can make the task so much simpler?

We now need to define eigenvalues and eigenvectors, which are the most important tools when studying matrix diagonalization, and that's exactly what the next section is all about.

## Eigenvalues and eigenvectors

Suppose that we have a square matrix $A$ of size $n \times n$. If there exists some number $\lambda$ and a non-zero vector $\boldsymbol{v}$ of length $n$ for which:

$A\cdot \boldsymbol{v} = \lambda \boldsymbol{v}$

then we call $\lambda$ an eigenvalue of $A$ and $\boldsymbol{v}$ its corresponding eigenvector.

The above is a formal definition, so let's now try to translate it into everyday language. You know, the kind of language that you use to talk about eigenvalues and eigenvectors on a daily basis.

Suppose that you have a square matrix that has some ugly entries and is generally difficult to look at. But maybe its beauty is on the inside? Perhaps we can use some not-so-terrible smaller objects to describe it throughly?

Eigenvalues and eigenvectors are exactly that. Once we know them, we know the initial matrix quite well and are able to simplify any further calculations (as an example, recall taking a power of a matrix described in the section above).

So, how do we get these eigen-thingies? Well, let's take a second look at the equation that describes them above. If we move the right side to the left:

$A\cdot \boldsymbol{v} - \lambda\boldsymbol{v} = 0$

and write it using the identity matrix, $I$ (the diagonal matrix with $1$-s on the diagonal), then we'll get an equivalent matrix equation:

$\left(A-\lambda I\right)\cdot \boldsymbol{v} = 0$

Here we calculated the multiplication of a matrix $A - \lambda I$ by a non-zero vector (a one-column matrix) that should give $0$, or rather the zero matrix. In order for this to be true, we need to have:

$\text{det}\left(A - \lambda I\right)=0$

Where $\text{det}$ denotes the determinant of a matrix (it's also denoted $\left|A - \lambda I\right|$ — not to be confused with the absolute value of a number).

Since we know the entries of $A$, this equality gives us an equation with $\lambda$ as an unknown variable called the characteristic polynomial of $A$. Its degree is equal to the size of the array, i.e., it's a quadratic equation for $2\times2$ matrices and a cubic equation for $3\times3$ matrices.

As the last thing in this section, let's point out a few important things concerning eigenvalues and eigenvectors.

1. Eigenvalues don't always exist. As we've mentioned above, we have quadratic and cubic equations for $2\times2$ and $3\times3$ matrices, respectively. Such things don't always have a real solution (e.g., no real number satisfies the equation $x^2 + 1 = 0$). However, if we instead consider the complex numbers, then every equation will have at least one solution.

2. Eigenvalues have multiplicities. It can happen that, say, a quadratic equation has only one solution. For example, the equation $x^2 + 2x + 1 = 0$ is satisfied only for $x = -1$ because $x^2 + 2x + 1 = (x + 1)^2$. This small $2$ in the exponent is called the multiplicity of $x = -1$. What is more, in the field of complex numbers, the sum of multiplicities of an equation's solutions is always equal to the degree of the polynomial. For instance, a cubic equation (an equation of degree $3$) with complex numbers can have three solutions of multiplicity $1$, one solution of multiplicity $2$, and one solution of multiplicity $1$, or one solution of multiplicity $3$.

3. A matrix with too few eigenvalues (counted with multiplicities) is not a diagonalizable matrix. As points 1. and 2. suggest, this can only happen if we don't consider complex numbers. In particular, a matrix with no real eigenvalues is not a diagonalizable matrix (in the field of real numbers).

4. One eigenvalue can have multiple eigenvectors. For example, the identity matrix (the diagonal matrix with $1$'s in the diagonal) has only one eigenvalue, $\lambda = 1$, and it corresponds to as many (linearly independent) eigenvectors as the size of the matrix (which is equal to the multiplicity of $\lambda = 1$).

5. A matrix with too few eigenvectors is not a diagonalizable matrix. One example of when that happens is point 3. above. But there's more! As opposed to eigenvalues, a matrix's eigenvectors don't have multiplicities. It may, however, happen that, say, an eigenvalue of multiplicity $2$ has only one eigenvector, even if we take complex numbers. Such matrices are called non-diagonalizable. They are rather rare, but be sure to keep an eye out for them!

Phew, that was quite a lot of theory, wouldn't you say? We keep defining some things, their properties, and a minute after minute passes without a clear set of instructions on what we're here for: how to diagonalize a matrix. It's high time we move to that, isn't it?

## How to diagonalize a matrix?

Say that you're given a square array, $A$, of size $n\times n$, and you know that it's a diagonalizable matrix. We've seen in the section Diagonal matrix: definition and properties what a diagonal matrix is, so, at first glance, it may seem a bit too much like magic to transform one thing into the other. Well, we might need some help with that.

The matrix diagonalization of $A$ is given by:

$A=S\cdot D\cdot S^{-1}$

Where

• $D$ is diagonal; and
• $S$ is our little helper (on the right, we have the inverse of $S$, $S^{-1}$).

Finding $S$ and $D$ may seem like no easy task. In fact, as mentioned in the above section, once we have the eigenvalues and eigenvectors of $A$, we have everything.

Below we list the main points of the procedure whenever you want to find matrix diagonalization. We should warn you, however, that, in full generality, some of them are no walk in the park and can take some time to finish. Oh, how lucky we are that the diagonalize matrix calculator exists!

1. Take your square matrix $A$ of size $n \times n$ and alculate the determinant, $\text{det}\left(A - \lambda I\right)$, i.e., the determinant of $A$ with $\lambda$'s subtracted from the diagonal entries.

2. Find the solutions of $\text{det}\left(A - \lambda I\right)=0$ and figure out their multiplicities. If the sum of the multiplicities is not equal to $n$, then $A$ is not a diagonalizable matrix. Otherwise, proceed.

3. The solutions of $\text{det}\left(A - \lambda I\right)=0$ are the eigenvalues of $A$. Take each eigenvalue $λ$ separately and write the matrix $A - \lambda I$.

4. To find the eigenvectors, define a vector $v = (x_1, x_2,\ldots, x_n)$ of length $n$ and solve the matrix equation $\left(A - \lambda I\right) \cdot \boldsymbol{v} = 0$. (It will have infinitely many solutions that depend on finitely many parameters.)

5. There are as many eigenvectors corresponding to $λ$ as there are parameters in the solution of $\left(A - \lambda I\right) \cdot \boldsymbol{v} = 0$. Each of them is given by substituting in the solution $1$ for one of the parameters, and $0$ for all the other ones.

6. If an eigenvalue has fewer eigenvectors than its multiplicity, then $A$ is not a diagonalizable matrix.

In the end, if $A$ is diagonalizable, we should obtain eigenvalues $λ_1$, $λ_2$, ..., $λ_n$ (some may be repeated according to their multiplicities) and eigenvectors $v_1$, $v_2$, ..., $v_n$. Then:

$A=S\cdot D\cdot S^{-1}$

With:

$D = \begin{pmatrix} \lambda_1&0&\ldots&0\\ 0&\lambda_2&\ldots&0\\ \vdots&\vdots&\ddots&\vdots\\ 0&0&\ldots&\lambda_n \end{pmatrix}$

And $S$ is the matrix whose first column contains the entries of $v_1$, the second has entries of $v_2$, and so on.

Phew, that was a lot of symbols, fancy scientific language, and generally a lot to get our heads around. But if you've made it this far, you deserve a cold one and a nice numerical example to see the diagonalize matrix calculator in practice.

Let's not waste another second, and get to it!

## Example: using the diagonalize matrix calculator

It's the last algebra class of the year. All the students already feel the laziness creeping in, and even the teacher seems like she's about to doze off. Unfortunately, she still has enough energy to give the class one last test, but tells you that whoever finishes it can leave and celebrate the beginning of summer.

With a little smirk on her face, the teacher writes on the blackboard a $3\times3$ matrix and tells you to find its 20th power. You hear a quiet murmur go up all around you and think about how long it will take to make so many matrix multiplications. Well, let's at least appreciate that it's not of size $4\times4$.

You suddenly remember the trick with matrix diagonalization that should help speed up the calculations. Excited in spite of yourself, you grab a piece of paper and begin your last exercise of the year.

The matrix is:

$A=\begin{pmatrix}1&0&0\\2&1&-1\\0&-1&1\end{pmatrix}$

Let's first see how easy it is to find the answer with our diagonalize matrix calculator.

We have a $3\times3$ matrix, so the first thing we need to do is tell the calculator that by choosing the correct option under "Matrix size". This will show us a symbolic example of such a matrix that tells us what notation we use for its entries. For example, the first row has elements $a_1$, $a_2$, and $a_3$, so we look back at our array and input

$a_1 = 1$, $a_2 = 0$, and $a_3 = 0$.

Similarly, the other two rows give

$b_1 = 2$, $b_2= 1$, $b_3 = -1$