<a href="https://colab.research.google.com/github/tofighi/Linear-Algebra/blob/main/9B_Matrix_Diagonalization_and_Large_Power.ipynb" target="_parent"></a>


```python
import sympy as sp
```

# Orignal Matrix A


```python
A = sp.Matrix([[1,6], [4,3]])
A
```




$\displaystyle \left[\begin{matrix}1 & 6\\4 & 3\end{matrix}\right]$




```python
A.eigenvals()
```




    {-3: 1, 7: 1}




```python
A.eigenvects()
```




    [(-3, 1, [Matrix([
       [-3/2],
       [   1]])]), (7, 1, [Matrix([
       [1],
       [1]])])]



# Matrices P, D, and P-inverse


```python
P = sp.Matrix([[1,-3], [1,2]])
P
```




$\displaystyle \left[\begin{matrix}1 & -3\\1 & 2\end{matrix}\right]$




```python

```


```python
Pinv = P.inv()
Pinv
```




$\displaystyle \left[\begin{matrix}\frac{2}{5} & \frac{3}{5}\\- \frac{1}{5} & \frac{1}{5}\end{matrix}\right]$




```python
D = sp.Matrix([[7,0], [0,-3]])
D
```




$\displaystyle \left[\begin{matrix}7 & 0\\0 & -3\end{matrix}\right]$




```python
# Need to define k as a symbolic variable
k = sp.symbols('k')
D_k = D**k
D_k
```




$\displaystyle \left[\begin{matrix}7^{k} & 0\\0 & \left(-3\right)^{k}\end{matrix}\right]$




```python
# Formula for A to the power of k
A_k = P*D_k*Pinv
A_k
```




$\displaystyle \left[\begin{matrix}\frac{3 \left(-3\right)^{k}}{5} + \frac{2 \cdot 7^{k}}{5} & - \frac{3 \left(-3\right)^{k}}{5} + \frac{3 \cdot 7^{k}}{5}\\- \frac{2 \left(-3\right)^{k}}{5} + \frac{2 \cdot 7^{k}}{5} & \frac{2 \left(-3\right)^{k}}{5} + \frac{3 \cdot 7^{k}}{5}\end{matrix}\right]$




```python
# Diagonalize A directly:  
P*(D**k)*(P.inv())
```




$\displaystyle \left[\begin{matrix}\frac{3 \left(-3\right)^{k}}{5} + \frac{2 \cdot 7^{k}}{5} & - \frac{3 \left(-3\right)^{k}}{5} + \frac{3 \cdot 7^{k}}{5}\\- \frac{2 \left(-3\right)^{k}}{5} + \frac{2 \cdot 7^{k}}{5} & \frac{2 \left(-3\right)^{k}}{5} + \frac{3 \cdot 7^{k}}{5}\end{matrix}\right]$



# 3x3 Diagonalization Example


```python
B = sp.Matrix([[319/118,-70/59,-49/118], [-105/59,-243/59,-147/59], [35/118,70/59,403/118]])
B
```




$\displaystyle \left[\begin{matrix}2.70338983050847 & -1.1864406779661 & -0.415254237288136\\-1.77966101694915 & -4.11864406779661 & -2.49152542372881\\0.296610169491525 & 1.1864406779661 & 3.41525423728814\end{matrix}\right]$




```python
#EigenValue
print(B.eigenvals())

```

    {-4.00000000000000: 1, 3.00000000000000: 1, 3.00000000000000: 1}



```python
Q = sp.Matrix([[1,-4,1], [-2,1,6], [5,0,-1]])
Q
```




$\displaystyle \left[\begin{matrix}1 & -4 & 1\\-2 & 1 & 6\\5 & 0 & -1\end{matrix}\right]$




```python
E = sp.Matrix([[3,0,0], [0,3,0], [0,0,-4]])
E
```




$\displaystyle \left[\begin{matrix}3 & 0 & 0\\0 & 3 & 0\\0 & 0 & -4\end{matrix}\right]$




```python
# Exact fractions - many digits: 
B_k = Q*(E**k)*(Q.inv())
B_k
```




$\displaystyle \left[\begin{matrix}\frac{5 \left(-4\right)^{k}}{118} + \frac{113 \cdot 3^{k}}{118} & \frac{10 \left(-4\right)^{k}}{59} - \frac{10 \cdot 3^{k}}{59} & \frac{7 \left(-4\right)^{k}}{118} - \frac{7 \cdot 3^{k}}{118}\\\frac{15 \left(-4\right)^{k}}{59} - \frac{15 \cdot 3^{k}}{59} & \frac{60 \left(-4\right)^{k}}{59} - \frac{3^{k}}{59} & \frac{21 \left(-4\right)^{k}}{59} - \frac{21 \cdot 3^{k}}{59}\\- \frac{5 \left(-4\right)^{k}}{118} + \frac{5 \cdot 3^{k}}{118} & - \frac{10 \left(-4\right)^{k}}{59} + \frac{10 \cdot 3^{k}}{59} & - \frac{7 \left(-4\right)^{k}}{118} + \frac{125 \cdot 3^{k}}{118}\end{matrix}\right]$




```python
# Easy decimal approcimations: 
1.0*B_k
```




$\displaystyle \left[\begin{matrix}0.0423728813559322 \left(-4\right)^{k} + 0.957627118644068 \cdot 3^{k} & 0.169491525423729 \left(-4\right)^{k} - 0.169491525423729 \cdot 3^{k} & 0.0593220338983051 \left(-4\right)^{k} - 0.0593220338983051 \cdot 3^{k}\\0.254237288135593 \left(-4\right)^{k} - 0.254237288135593 \cdot 3^{k} & 1.01694915254237 \left(-4\right)^{k} - 0.0169491525423729 \cdot 3^{k} & 0.355932203389831 \left(-4\right)^{k} - 0.355932203389831 \cdot 3^{k}\\- 0.0423728813559322 \left(-4\right)^{k} + 0.0423728813559322 \cdot 3^{k} & - 0.169491525423729 \left(-4\right)^{k} + 0.169491525423729 \cdot 3^{k} & - 0.0593220338983051 \left(-4\right)^{k} + 1.05932203389831 \cdot 3^{k}\end{matrix}\right]$



d30af555e5590977ca0babdb9a3f4470c388daf7
ipynb
Jupyter Notebook
9B_Matrix_Diagonalization_and_Large_Power.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
9B_Matrix_Diagonalization_and_Large_Power.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
9B_Matrix_Diagonalization_and_Large_Power.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
Qwen/Qwen-72B
1. YES
2. YES