```python
import numpy as np
import sympy as sy
sy.init_printing()  
```

# <font face="gotham" color="red"> Similarity </font>

If $A = PBP^{-1}$, we say $A$ is <font face="gotham" color="red">similar</font> to $B$, decomposing $A$ into $PBP^{-1}$ is also called a <font face="gotham" color="red">similarity transformation</font>.

<font face="gotham" color="red">If $n\times n$ matrices $A$ and $B$ are similar, they have the same eigenvalues</font>.

The <font face="gotham" color="red">diagnoalization</font>, which we will explain,  is a process of finding similar matrices.

# <font face="gotham" color="purple"> Diagonalizable Matrix</font>

Let $A$ be an $n\times n$ matrix. If there exists an $n\times n$ invertible matrix $P$ and a diagonal matrix $D$, such that

$$
A=PDP^{-1}
$$

then matrix $A$ is called a diagonalizable matrix.

And futher, <font face="gotham" color="red">the columns of $P$ are linearly independent eigenvectors of $A$, and its corresponding eigenvalues are on the diagonal of $D$. In other words, $A$ is diagonalizable if and only if the dimension of eigenspace basis is $n$</font>.

It's easy to show why this equation holds.

Let

$$
P = \left[\begin{array}{llll}
{v}_{1} & {v}_{2} & \cdots & {v}_{n}
\end{array}\right]\\
$$

$$
D = \left[\begin{array}{cccc}
\lambda_{1} & 0 & \cdots & 0 \\
0 & \lambda_{2} & \cdots & 0 \\
\vdots & \vdots & & \vdots \\
0 & 0 & \cdots & \lambda_{n}
\end{array}\right]
$$

where $v_i, i \in (1, 2, ...n)$ is an eigenvector of $A$, $\lambda_i, i \in (1, 2, ...n)$ is an eigenvalue of $A$.

$$
AP = A\left[\begin{array}{llll}
{v}_{1} & {v}_{2} & \cdots & {v}_{n}
\end{array}\right]=\left[\begin{array}{llll}
A {v}_{1} & A {v}_{2} & \cdots & A {v}_{n}
\end{array}\right]
$$

$$P D=P\left[\begin{array}{cccc}
\lambda_{1} & 0 & \cdots & 0 \\
0 & \lambda_{2} & \cdots & 0 \\
\vdots & \vdots & & \vdots \\
0 & 0 & \cdots & \lambda_{n}
\end{array}\right]=\left[\begin{array}{lllll}
\lambda_{1} {v}_{1} & \lambda_{2} {v}_{2} & \cdots & \lambda_{n} {v}_{n}
\end{array}\right]$$

We know that $A{v}_i = \lambda_i{v}_i$, i.e.

$$
AP = PD
$$

Since $P$ has all independent eigenvectors, then

$$
A = PDP^{-1}
$$

# <font face="gotham" color="purple"> Diagonalizing a Matrix</font>

Consider a matrix

$$A=\left[\begin{array}{rrr}
1 & 3 & 3 \\
-3 & -5 & -3 \\
3 & 3 & 1
\end{array}\right]$$

diagonalize the matrix $A$.

Following these steps:

1. Find the eigenvalues of $A$
2. Find the eigenvectors of $A$
3. Construct $P$.
4. Construct $D$ from the corresponding columns of $P$.


```python
A = sy.Matrix([[1,3,3], [-3, -5, -3], [3,3,1]])
eig = sy.matrices.MatrixEigen.eigenvects(A)
eig
```

Construct $P$


```python
P = sy.zeros(3, 3)
P[:, 0] = eig[0][2][0]
P[:, 1] = eig[0][2][1]
P[:, 2] = eig[1][2][0]
P
```

Construct $D$


```python
D = sy.diag(eig[0][0], eig[0][0], eig[1][0])
D
```

We can verify if $PDP^{-1}=A$ holds:


```python
P * D * P.inv() == A 
```

Of course we don't need to go through this process seperately. There is ```diagonalize``` method in SymPy.


```python
P, D = A.diagonalize()
```


```python
P
```


```python
D
```

Sometimes we just want to test if a matrix is diagonalizable, then use ```is_diagonalizable``` in SymPy.


```python
A.is_diagonalizable()
```




    True



cc15a65216d2ac2cc53bdb4c2da1437ec32af369
ipynb
Jupyter Notebook
Chapter 13 - Diagonalization.ipynb
lijungeometry/BasicPythonLinearAlgebra
0e6504469e898ace1898c33ec671617338d5e2bb
MIT
Chapter 13 - Diagonalization.ipynb
lijungeometry/BasicPythonLinearAlgebra
0e6504469e898ace1898c33ec671617338d5e2bb
MIT
Chapter 13 - Diagonalization.ipynb
lijungeometry/BasicPythonLinearAlgebra
0e6504469e898ace1898c33ec671617338d5e2bb
MIT
Qwen/Qwen-72B
1. YES
2. YES