Created
March 2, 2017 08:46
-
-
Save sergeant-wizard/a71e114192066666139c290826dfd733 to your computer and use it in GitHub Desktop.
linear equation performance
This file contains hidden or 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 scipy.linalg | |
import numpy as np | |
import sys | |
num_samples = 500000 | |
n = 7 | |
def inv(Q, x, num_eqs): | |
for _ in range(num_samples): | |
Q_inv = np.linalg.inv(Q) | |
for eq in range(num_eqs): | |
Q_inv.dot(x) | |
def solve(Q, x, num_eqs): | |
for _ in range(num_samples): | |
for eq in range(num_eqs): | |
np.linalg.solve(Q, x) | |
def chol(Q, x, num_eqs): | |
for _ in range(num_samples): | |
ch = scipy.linalg.cholesky(Q) | |
for eq in range(num_eqs): | |
scipy.linalg.solve_triangular(ch, x) | |
if __name__ == '__main__': | |
np.random.seed(0) | |
Q = np.random.rand(n, n) | |
Q = Q + Q.T + n * np.identity(n) | |
x = np.zeros(n) | |
num_eqs = int(sys.argv[2]) | |
if sys.argv[1] == '0': | |
inv(Q, x, num_eqs) | |
elif sys.argv[1] == '1': | |
chol(Q, x, num_eqs) | |
elif sys.argv[1] == '2': | |
solve(Q, x, num_eqs) | |
else: | |
raise Exception |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment