ea1331d4创建于 2022年6月25日历史提交
import numpy as np
import scipy.linalg as la
import time
import platform
from timeit import timeit


print("Multiplication of two matrices")
dims = [1000, 2000, 3000, 4000, 5000]
for dim in dims:
    total_ms = 0.0
    repeat = 5
    for _ in range(repeat):
        a = np.random.randn(dim, dim)
        b = np.random.randn(dim, dim)
        total_ms += timeit(lambda: np.dot(a, b), number=1) * 1000
    avg_ms = total_ms / repeat
    print("dim = %d: %.3f ms" % (dim, avg_ms))

print("Solution of a linear system")
dims = [1000, 2000, 3000, 4000, 5000]
for dim in dims:
    total_ms = 0.0
    repeat = 5
    for _ in range(repeat):
        a = np.random.randn(dim, dim)
        b = np.random.randn(dim)
        total_ms += timeit(lambda: np.linalg.solve(a, b), number=1) * 1000
    avg_ms = total_ms / repeat
    print("dim = %d: %.3f ms" % (dim, avg_ms))

# a = np.random.randn(dim, dim)
# t0 = time.clock()
# la.inv(a)
# t1 = time.clock()
# print("dim = %d" % dim)
# print("inv...costs = %.3f ms" % ( (t1-t0)*1000))


# la.lu(a)



# np.linalg.solve(a,b)
# t2 = time.clock()

# print("dim = %d" % dim)
# print("lu factorization...costs = %.3f ms" % ( (t1-t0)*1000))
# print("linear solve...costs = %.3f ms" % ( (t2-t1)*1000))
# print("total costs = %.3f ms" % ((t2-t0)*1000))

# print("Test cholesky")
# for dim in dims:
#     total_ms = 0.0
#     repeat = 1
#     for _ in range(repeat):
#         a = np.random.randn(dim, dim)
#         at = a.transpose()
#         b = a + at
#         neyen = np.eye(dim,dim) * dim
#         c = b + neyen
#         b = np.random.randn(dim)
#         total_ms += timeit(lambda: np.linalg.cholesky(c), number=1) * 1000
#     avg_ms = total_ms / repeat
#     print("dim = %d: %.3f ms" % (dim, avg_ms))