Created
September 22, 2018 23:28
-
-
Save cshjin/9b09abd85bced1ed0d7c5b237412fe5d to your computer and use it in GitHub Desktop.
Power method
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 sys | |
# reference: https://en.wikipedia.org/wiki/Power_iteration | |
def power_method(M): | |
b = np.random.rand(M.shape[1]) | |
diff = np.linalg.norm(b) | |
while diff > 10 ** (-6): | |
b_new = np.dot(M, b) | |
b_norm = np.linalg.norm(b_new) | |
b_new = b_new / b_norm | |
diff = abs(np.linalg.norm(b) - np.linalg.norm(b_new)) | |
b = b_new | |
print(b) | |
return b | |
def main(): | |
M = np.random.random_integers(-100, 100, size=(20, 20)) | |
M = (M + M.T)/2 | |
b = power_method(M) | |
print(b) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment