## Eigenvalues and Eigenvectors

### Learning to obtain eigenvalues and eigenvectors using python.

#### [4,3,2],[1,4,1],[3,10,4]


```
import numpy as np
I=np.array([[4,3,2],[1,4,1],[3,10,4]])
print("#### input matrix")
print(I)
print()
x=np.linalg.eigvals(I)
y=np.linalg.eig(I)
print("#### Eigen values")
print(x)
print()
print("#### Eigen vectors")
print(y)
```

    #### input matrix
    [[ 4  3  2]
     [ 1  4  1]
     [ 3 10  4]]
    
    #### Eigen values
    [8.98205672 2.12891771 0.88902557]
    
    #### Eigen vectors
    (array([8.98205672, 2.12891771, 0.88902557]), array([[-0.49247712, -0.82039552, -0.42973429],
           [-0.26523242,  0.14250681, -0.14817858],
           [-0.82892584,  0.55375355,  0.89071407]]))


#### [1,-3,3],[3,-5,3],[6,-6,4]


```
import numpy as np
I=np.array([[1,-3,3],[3,-5,3],[6,-6,4]])
print("#### input matrix")
print(I)
print()
x=np.linalg.eigvals(I)
y=np.linalg.eig(I)
print("#### Eigen values")
print(x)
print()
print("#### Eigen vectors")
print(y)
```

    #### input matrix
    [[ 1 -3  3]
     [ 3 -5  3]
     [ 6 -6  4]]
    
    #### Eigen values
    [ 4.+0.00000000e+00j -2.+1.10465796e-15j -2.-1.10465796e-15j]
    
    #### Eigen vectors
    (array([ 4.+0.00000000e+00j, -2.+1.10465796e-15j, -2.-1.10465796e-15j]), array([[-0.40824829+0.j        ,  0.24400118-0.40702229j,
             0.24400118+0.40702229j],
           [-0.40824829+0.j        , -0.41621909-0.40702229j,
            -0.41621909+0.40702229j],
           [-0.81649658+0.j        , -0.66022027+0.j        ,
            -0.66022027-0.j        ]]))


### Properties of eigenvalues and eigenvectors:
1. For a nxn matrix, the number of eigen values is n.
2. The sum of eigen values is equal to the sum of the diagonal elements of matrix.
3. The product of eigenvalues is equal to the determinant of the matrix.
4. The eigen value for an identity matrix is 1.
5. The eigenvalue of a triangular matrix is same as the diagonal elements of a matrix.
6. For a skew symmetric matrix, the eigenvalues are imaginary.
7. For orthogonal matrix eigenvalues is 1.(Orthogonal matrix---> A.(A)ˆt=I).
8. For indempotent matrix the eigenvalues are 0 and 1(Aˆ2=identity matrix).


```
import math
from math import *
import numpy as np
from numpy import *
```

#### property 2


```
A=array([[1,2,3],[2,3,5],[3,1,1]])
print("#### Input matrix")
print(A)
print()
X=A[0][0]+A[1][1]+A[2][2]
print("#### X")
print(X)
print()
Y=sum(linalg.eigvals(A))
Z=round(Y)
print("#### Z")
print(Z)
print()
print("The sum of eigen values is equal to the sum of the diagonal elements of matrix")
equal(X,Z)
```

    #### Input matrix
    [[1 2 3]
     [2 3 5]
     [3 1 1]]
    
    #### X
    5
    
    #### Z
    5
    
    The sum of eigen values is equal to the sum of the diagonal elements of matrix





    True



Property 2 is verified.

#### property 3


```
B=array([[1,2,3],[1,3,5],[4,1,2]])
print("#### B")
print(B)
print()
M=linalg.det(B)
print("#### M")
print(M)
print()
Q=prod(linalg.eigvals(B))
P=np.round(Q)
print("#### P")
print(P)
print()
print("The product of eigenvalues is equal to the determinant of the matrix.")
equal(P,M)
```

    #### B
    [[1 2 3]
     [1 3 5]
     [4 1 2]]
    
    #### M
    4.000000000000002
    
    #### P
    (4+0j)
    
    The product of eigenvalues is equal to the determinant of the matrix.





    False



Property 3 is verified.

#### Property 4


```
I=array([[1,0,0],[0,1,0],[0,0,1]])
print("#### Input matrix")
print(I)
print()
print("The eigen value for an identity matrix is 1")
linalg.eigvals(I)
```

    #### Input matrix
    [[1 0 0]
     [0 1 0]
     [0 0 1]]
    
    The eigen value for an identity matrix is 1





    array([1., 1., 1.])



Property 4 is verified.

#### Property 5


```
T=array([[4,0,0],[2,3,0],[1,2,3]])
print("#### Input matrix")
print(T)
print()
print("The eigenvalue of a triangular matrix is same as the diagonal elements of the matrix.")
linalg.eigvals(T)
```

    #### Input matrix
    [[4 0 0]
     [2 3 0]
     [1 2 3]]
    
    The eigenvalue of a triangular matrix is same as the diagonal elements of the matrix.





    array([3., 3., 4.])



Property 5 is verified

#### Property 6


```
E=(B-B.transpose())/2
print("#### skew matrix")
print(E)
print()
print("For a skew symmetric matrix, the eigenvalues are imaginary.")
linalg.eigvals(E)
```

    #### skew matrix
    [[ 0.   0.5 -0.5]
     [-0.5  0.   2. ]
     [ 0.5 -2.   0. ]]
    
    For a skew symmetric matrix, the eigenvalues are imaginary.





    array([0.+0.j        , 0.+2.12132034j, 0.-2.12132034j])



Property 6 is verified.

#### Property 7


```
F=array([[1,0],[0,-1]])
print("#### orthogonal matrix")
print(F)
print()
print("For orthogonal matrix eigenvalues is 1.(Orthogonal matrix---> A.(A)ˆt=I).")
linalg.eigvals(F)
```

    #### orthogonal matrix
    [[ 1  0]
     [ 0 -1]]
    
    For orthogonal matrix eigenvalues is 1.(Orthogonal matrix---> A.(A)ˆt=I).





    array([ 1., -1.])



Property 7 verified.

### Diagonalization of sqaure matrix


```
import numpy as np
from math import *
A= np.mat([[2,-2,3],[1,1,1],[1,3,-1]])
X,P=np.linalg.eig(A)
I=np.linalg.inv(P)
Z=np.around(I*A*P)
for i in range(len(Z)):
    for j in range(len(Z)):
        if Z[i,j]==-0:
            Z[i,j]=0
print("The final diagonalized matrix is")
print(Z)
print()

print("Eigen vectors")
print(P)
print()

print("Eigen values")
print(X)
print()
```

    The final diagonalized matrix is
    [[ 3.  0.  0.]
     [ 0.  1.  0.]
     [ 0.  0. -2.]]
    
    Eigen vectors
    [[ 0.57735027  0.57735027 -0.61684937]
     [ 0.57735027 -0.57735027 -0.05607722]
     [ 0.57735027 -0.57735027  0.78508102]]
    
    Eigen values
    [ 3.  1. -2.]
    


### Cayley-Hamilton Theorem


```
A=np.mat([[2, 3],[4, 5]])
from math import *
X=np.poly(A)
print("The co-efficients of the characteristic equation are",X)

trace_A = np.trace(A)
det_A = np.linalg.det(A)
I = np.eye(len(A))
A*A - trace_A * A + det_A * I

from sympy import *
from math import *
from numpy import *
print("Enter elements of the matrix: ")
A=mat(input())
s=0
print()
print("The matrix is: ")
print(A)
print()
I=eye(len(A),len(A))
print("The identity matrix is: ")
print(I)
print()
ce=poly(A)
ce=ce.round()
print("The coefficients of the characteristic equation=",ce)
for i in range (len(ce)):
    eq=ce[i]*I*(A**(len(ce)-i))
    s=s+eq
print()
s
```

    The co-efficients of the characteristic equation are [ 1. -7. -2.]
    Enter elements of the matrix: 
    1 2 3 4; 3 7 9 2; 1 4 7 8; 1 5 9 2
    
    The matrix is: 
    [[1 2 3 4]
     [3 7 9 2]
     [1 4 7 8]
     [1 5 9 2]]
    
    The identity matrix is: 
    [[1. 0. 0. 0.]
     [0. 1. 0. 0.]
     [0. 0. 1. 0.]
     [0. 0. 0. 1.]]
    
    The coefficients of the characteristic equation= [  1. -17. -38. 116. -32.]
    





    matrix([[0., 0., 0., 0.],
            [0., 0., 0., 0.],
            [0., 0., 0., 0.],
            [0., 0., 0., 0.]])



### Conclusion: 
The extraction of eigenvalues and eigenvectors of matrices were obtained. The extraction rows and columns in matrix was learnt. We also learnt the propertities of eigen values and eigen vectors, the process of diagonalization of matrix, solving system of equation using Cramer’s rule. The Cayley-Hamilton
Theorem was verified in Python in this lab. The extraction of coefficients of the characteristic equation of matrix was learned.


```

```

b2a9ddda4d9a7f627f761162c3c058fc4f961af5
ipynb
Jupyter Notebook
Eigen_value_and_eigen_vectors.ipynb
noufalsalim/eigenvalues
8b1acedc6dbc13d24042120d48817750c520e325
MIT
Eigen_value_and_eigen_vectors.ipynb
noufalsalim/eigenvalues
8b1acedc6dbc13d24042120d48817750c520e325
MIT
Eigen_value_and_eigen_vectors.ipynb
noufalsalim/eigenvalues
8b1acedc6dbc13d24042120d48817750c520e325
MIT
Qwen/Qwen-72B
1. YES
2. YES