# Calc Eigen Value


```python
import numpy as np
```


```python
a=np.array([[16,-1,1,2],
                        [2,12,1,-1],
                        [1,3,-24,2],
                        [4,-2,1,20]],dtype='f')
#initial value s.t. np.linang.norm(x) is 1
x=np.array([0.5,0.5,0.5,0.5]).T
x
```




    array([ 0.5,  0.5,  0.5,  0.5])




```python
epsilon=0.01
maxiteration=100
```


```python
x_new=a @ x
eigen=x_new.dot(x)
x_new/=np.linalg.norm(x_new)
```


```python
for iteration in range(maxiteration):
    x_new =a@x
    eigen=x_new.dot(x)
    if(np.linalg.norm(x_new-eigen*x)<epsilon):
        break
    #normalize
    x_new/=np.linalg.norm(x_new)
    #update x
    x=x_new
else:
    print("fail to calc eigen value")
    exit(1)
    
print("num of iteration is %d"%iteration)
print("one of eigen value of a is %f"%eigen)
print("its eigen vector is \n{}")

```

    num of iteration is 0
    one of eigen value of a is -24.148327
    its eigen vector is 
    [-0.0244012  -0.0268873   0.99911089 -0.02142602]


# final code is...


```python
import numpy as np

a=np.array([[16,-1,1,2],
                        [2,12,1,-1],
                        [1,3,-24,2],
                        [4,-2,1,20]],dtype='f')
#initial value s.t. np.linang.norm(x) is 1
x=np.array([0.5,0.5,0.5,0.5]).T
epsilon=0.01
maxiteration=100
for iteration in range(maxiteration):
    x_new =a@x
    eigen=x_new.dot(x)
    if(np.linalg.norm(x_new-eigen*x)<epsilon):
        break
    #normalize
    x_new/=np.linalg.norm(x_new)
    #update x
    x=x_new
else:
    print("fail to calc eigen value")
    exit(1)
    
print("num of iteration is %d"%iteration)
print("one of eigen value of a is %f"%eigen)
print("its eigen vector is \n{}".format(x))

```

    num of iteration is 78
    one of eigen value of a is -24.148327
    its eigen vector is 
    [-0.0244012  -0.0268873   0.99911089 -0.02142602]



```python
import sympy as sy
sy.init_printing()
sy_a=sy.Matrix(a)
sy_a
```


```python
sy_a.eigenvals()

```


```python
sy_a.eigenvects()
```


```python
from scipy import linalg as sla
```


```python
sla.eigvals(a)
```




    array([-24.14852524+0.j,  21.57934570+0.j,  12.23095798+0.j,
            14.33821487+0.j], dtype=complex64)




```python
sla.eigvalsh(a)
```




    array([-24.29197502,  10.03090763,  15.71542454,  22.54564667], dtype=float32)




```python
sla.eig(a)
```




    (array([-24.14852524+0.j,  21.57934570+0.j,  12.23095798+0.j,
             14.33821487+0.j], dtype=complex64),
     array([[ 0.02447723, -0.34770194, -0.14668337,  0.67053527],
            [ 0.02688304,  0.02018997, -0.97126281,  0.70048612],
            [-0.99910468, -0.04737921, -0.09344059,  0.05994677],
            [ 0.02163069, -0.93618959, -0.16248505, -0.23687127]], dtype=float32))




```python

```

15010c1425aa7ebafea8582557609f9c38684f98
ipynb
Jupyter Notebook
eigenValue/powerMethod.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
eigenValue/powerMethod.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
eigenValue/powerMethod.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
Qwen/Qwen-72B
1. YES
2. YES