```python
import sympy as sp
import numpy as np
import numpy.linalg as nl
import fractions
np.set_printoptions(formatter={'all':lambda x: str(fractions.Fraction(x).limit_denominator())})
```


```python
sp.init_printing(use_latex='mathjax')
```

# Characteristic polynomial

for a 2x2 the formula is:

l**2 - ((a + d) * l) + (a * d - b * c)


```python
def reduce(matrix):
    matrix = np.array(matrix)
    a = matrix[0,0]
    b = matrix[0,1]
    c = matrix[1,0]
    d = matrix[1,1]
    l = sp.symbols('l')
    expr = l**2 - ((a + d) * l) + (a * d - b * c)
    expr = sp.simplify(expr)
    return expr
```


```python
def testEigenVector(matrix, lambda1, lambda2, *testVectors):
    result = []
    for vector in testVectors:
        vector = np.array(vector)
        dotP = np.dot(matrix, vector)
        for l in [lambda1, lambda2]:
            dotL = int(l) * vector
            if np.allclose(dotP, dotL):
               result.append(vector)
    
    return result
```

# New Matrix


```python
A = [[1,0], [0,2]]
```


```python
expr = reduce(A)
expr
```




$$l^{2} - 3 l + 2$$




```python
solutions = sp.solve(expr)
solutions
```




$$\left [ 1, \quad 2\right ]$$




```python
eigenvalues, eigenvectors = nl.eig(A)
eigenvectors
```




    array([[1, 0],
           [0, 1]])




```python
testEigenVector(A, solutions[0], solutions[1], [0, 2], [1, -1], [0, 3], [1,0])
```




    [array([0, 2]), array([0, 3]), array([1, 0])]



# New Matrix


```python
A = [[3,4], [0,5]]
```


```python
expr = reduce(A)
expr
```




$$l^{2} - 8 l + 15$$




```python
solutions = sp.solve(expr)
solutions
```




$$\left [ 3, \quad 5\right ]$$




```python
eigenvalues, eigenvectors = nl.eig(A)
eigenvectors
```




    array([[1, 832040/930249],
           [0, 416020/930249]])




```python
testEigenVector(A, solutions[0], solutions[1], [-1, -1/2], [0,0], [2,1], [3,0])
```




    [array([-1, -1/2]), array([0, 0]), array([0, 0]), array([2, 1]), array([3, 0])]



# New Matrix


```python
A = [[1,0],[-1,4]]
```


```python
expr = reduce(A)
expr
```




$$l^{2} - 5 l + 4$$




```python
solutions = sp.solve(expr)
solutions
```




$$\left [ 1, \quad 4\right ]$$




```python
eigenvalues, eigenvectors = nl.eig(A)
eigenvectors
```




    array([[0, 932975/983442],
           [1, 275423/870964]])




```python
testEigenVector(A, solutions[0], solutions[1], [0, 1], [3,2], [3,1], [3,-1])
```




    [array([0, 1]), array([3, 1])]



# New Matrix


```python
A = [[-3, 8], [2, 3]]
```


```python
expr = reduce(A)
expr
```




$$l^{2} - 25$$




```python
solutions = sp.solve(expr)
solutions
```




$$\left [ -5, \quad 5\right ]$$




```python
eigenvalues, eigenvectors = nl.eig(A)
eigenvectors
```




    array([[-283009/291719, -470832/665857],
           [213329/879578, -470832/665857]])




```python
testEigenVector(A, solutions[0], solutions[1], [0, 2], [-1, -1], [4,-1], [1,1])
```




    [array([-1, -1]), array([4, -1]), array([1, 1])]



# New Matrix


```python
A = [[5, 4], [-4, -3]]
```


```python
expr = reduce(A)
expr
```




$$l^{2} - 2 l + 1$$




```python
solutions = sp.solve(expr)
solutions
```




$$\left [ 1\right ]$$



# New Matrix


```python
A = [[-2, -3], [1,1]]
```


```python
expr = reduce(A)
expr
```




$$l^{2} + l + 1$$




```python
solutions = sp.solve(expr)
solutions
```




$$\left [ - \frac{1}{2} - \frac{\sqrt{3} i}{2}, \quad - \frac{1}{2} + \frac{\sqrt{3} i}{2}\right ]$$




```python

```

# New Matrix


```python
A = [[3/2, -1], [-1/2, 1/2]]
```


```python
expr = reduce(A)
expr
```




$$l^{2} - 2.0 l + 0.25$$



# New Matrix


```python
A = np.array([[3/2, -1], [-1/2, 1/2]])
```


```python
expr = reduce(A)
expr
```




$$l^{2} - 2.0 l + 0.25$$




```python
solutions = sp.solve(expr)
solutions
```




$$\left [ 0.133974596215561, \quad 1.86602540378444\right ]$$




```python
1 - np.sqrt(3) / 2
```




$$0.1339745962155614$$




```python
1 + np.sqrt(3) / 2
```




$$1.8660254037844386$$




```python
testEigenVector(
    A, 
    solutions[0], 
    solutions[1], 
    [-1-np.sqrt(3), 1], [-1+np.sqrt(3), 1], 
    [1-np.sqrt(5), 1], [1+np.sqrt(5), 1],
    [1-np.sqrt(3), 1], [1+np.sqrt(3), 1], 
    [-1-np.sqrt(5), 1], [-1+np.sqrt(5), 1])
```




$$\left [ \right ]$$




```python
A @ A
```




    array([[11/4, -2],
           [-1, 3/4]])




```python

```

f22dd3b719cc7416abc213d0f1cdae82034d62b5
ipynb
Jupyter Notebook
Certification 1/Week5.1 - characteristic polynomial.ipynb
The-Brains/MathForMachineLearning
5cbd9006f166059efaa2f312b741e64ce584aa1f
MIT
2018-04-16T02:53:59.000Z
2021-05-16T06:51:57.000Z
Certification 1/Week5.1 - characteristic polynomial.ipynb
The-Brains/MathForMachineLearning
5cbd9006f166059efaa2f312b741e64ce584aa1f
MIT
Certification 1/Week5.1 - characteristic polynomial.ipynb
The-Brains/MathForMachineLearning
5cbd9006f166059efaa2f312b741e64ce584aa1f
MIT
2019-05-20T02:06:55.000Z
2020-05-18T06:21:41.000Z
Qwen/Qwen-72B
1. YES
2. YES