線性方程的解算器時這是爲4個功能求解線性系統方程我的Python代碼:在SciPy的
def inverse_solution(A, B):
inv_A = scipy.linalg.inv(A)
return [numpy.dot(inv_A, b) for b in B]
def scipy_standart_solution(A, B):
return [scipy.linalg.solve(A, b) for b in B]
def cholesky_solution(A, B):
K = scipy.linalg.cholesky(A, lower = True)
t_K = K.T
return [scipy.linalg.solve_triangular(t_K, scipy.linalg.solve_triangular(K, b, lower = True)) for b in B]
def scipy_cholesky_solution(A, B):
K = scipy.linalg.cho_factor(A)
return [scipy.linalg.cho_solve(K, b) for b in B]
我知道第一溶液是不effecient,第二個是良好的,如果數量的元件在b
較小,如果b
較大,則解決方案3和4較好。
但我的測試顯示相反
A = numpy.array([[1,0.000000001],[0.000000001,1]])
for length in range(5):
B = numpy.random.rand(length + 1,2)
r_1 = %timeit -o -q inverse_solution(A,B)
r_2 = %timeit -o -q scipy_standart_solution(A,B)
r_3 = %timeit -o -q cholesky_solution(A, B)
r_4 = %timeit -o -q scipy_cholesky_solution(A,B)
print[r_1.best, r_2.best, r_3.best, r_4.best, length + 1]
print("////////////////////////////")
A = scipy.linalg.hilbert(12)
for length in range(5):
B = numpy.random.rand(length + 1,12)
r_1 = %timeit -o -q inverse_solution(A,B)
r_2 = %timeit -o -q scipy_standart_solution(A,B)
r_3 = %timeit -o -q cholesky_solution(A, B)
r_4 = %timeit -o -q scipy_cholesky_solution(A,B)
print[r_1.best, r_2.best, r_3.best, r_4.best, length + 1]
輸出
[3.4187317043965046e-05, 4.0246286353324035e-05, 0.00010478259103165283, 5.702318102410473e-05, 1]
[5.8342068180991904e-05, 7.186096089739067e-05, 0.00015128208746822017, 8.083242991273707e-05, 2]
[3.369307648390851e-05, 9.87348262759614e-05, 0.00020628010956514232, 0.00012435874243853036, 3]
[3.79271575715201e-05, 0.00013554678863379266, 0.00027863672228316006, 0.0001421170191610699, 4]
[3.5385220680527143e-05, 0.00017145862333669016, 0.0003393085250830197, 0.00017440956920201814, 5]
////////////////////////////
[4.46813352e-05, 4.6984071999949605e-05, 9.794712904695047e-05, 5.9280641995266595e-05, 1]
[4.815794144123942e-05, 9.101890875345049e-05, 0.00017026901620170064, 9.290563584772826e-05, 2]
[4.604331714660219e-05, 0.00013565361678288213, 0.0002540085146054736, 0.00012585519183521684, 3]
[4.8241120284303915e-05, 0.0001758718917520369, 0.00031790739992344183, 0.00016162940724917405, 4]
[4.9397840771318616e-05, 0.00021475323253511647, 0.00037772389328304714, 0.00020302321951302815, 5]
爲什麼會出現這種情況?
爲什麼不只是'scipy.linalg.solve(A,B.T).T'?或者安排你的數據,所以它只是'scipy.linalg.solve(A,B)'? – user2357112