from __future__ import division
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
from time import time
# vector-vector
nlist1 = np.linspace(1e3,1e5,1e3).astype(np.int32)
ntests = 5
time1 = []
for n in nlist1:
u = np.random.rand(n)
v = np.random.rand(n)
t_start = time()
for test in range(ntests):
u.dot(v)
time1.append((time() - t_start)/ntests)
# matrix-vector
nlist2 = np.linspace(1e2, 6e3, 20).astype(np.int32)
ntests = 5
time2 = []
for n in nlist2:
print n,
A = np.random.rand(n,n)
u = np.random.rand(n)
t_start = time()
for test in range(ntests):
np.dot(A, u)
time2.append((time() - t_start)/ntests)
# matrix-matrix
nlist3 = np.linspace(1e2, 2e3, 20).astype(np.int32)
time3 = []
for n in nlist3:
print n,
A = np.random.rand(n,n)
B = np.random.rand(n,n)
t_start = time()
np.dot(A, B)
time3.append(time() - t_start)
# solve
nlist4 = np.linspace(1e2, 2e3, 20).astype(np.int32)
time4 = []
for n in nlist4:
print n,
A = np.random.rand(n,n)
b = np.random.rand(n)
t_start = time()
la.solve(A,b)
time4.append(time() - t_start)
# inverse
nlist5 = np.linspace(1e2, 2e3, 12).astype(np.int32)
ntests = 1
time5 = []
for n in nlist5:
print n,
A = np.random.rand(n,n)
t_start = time()
la.inv(A)
time5.append(time() - t_start)
pt.figure(figsize=(9, 5))
pt.loglog(nlist1, np.array(time1), linewidth=3, label='dot product')
pt.loglog(nlist2, np.array(time2), linewidth=3, label='matrix-vector product')
pt.loglog(nlist3, np.array(time3), linewidth=3, label='matrix-matrix product')
pt.loglog(nlist4, np.array(time4), linewidth=3, label='solve')
pt.loglog(nlist5, np.array(time5), linewidth=3, label='inverse')
pt.legend()
pt.grid()