Created
October 20, 2019 10:38
-
-
Save wrist/c85f00ff93f829bae5644a1ab61cc7ee to your computer and use it in GitHub Desktop.
power_method.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""べき乗則を用いて複素行列の最大固有値を求める""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
seed=1 | |
ndim = 10 | |
np.random.seed=seed | |
A = np.random.randn(ndim,ndim)+1.j*np.random.randn(ndim,ndim) | |
x = np.random.randn(ndim) + 1.j*np.random.randn(ndim) | |
niter = 300 | |
eig_ary =[] | |
for i in range(niter): | |
x_old = x[:] | |
x = np.dot(A, x) | |
x_norm = np.sqrt(np.sum(x**2)) | |
numer = np.dot(x_old, x) | |
denom = np.dot(x_old, x_old) | |
eigval = numer / denom | |
print(eigval) | |
eig_ary.append(eigval) | |
x /= x_norm | |
eigvals = np.linalg.eigvals(A) | |
print(np.abs(eigvals)) | |
fig = plt.figure(1) | |
ax = fig.add_subplot(111) | |
ax.plot(np.abs(eig_ary)) | |
ax.axhline(np.sort(np.abs(eigvals))[-1], color='red') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment