LU分解

import numpy as np

a = [[2, 2, 3], [4, 7, 7], [-2, 4, 5]]
b = [[3], [1], [-7]]

U = [[] for i in range(3)]
L = [[] for i in range(3)]

for i in range(3) :
    for j in range(3) :
        U[i].append(a[i][j])
        L[i].append(a[i][j])

#转上三角
for i in range(3 - 1) :
    for j in range(i + 1, 3) :
        t = U[j][i] / U[i][i]
        for k in range(3) :
            U[j][k] -= t * U[i][k]

U = np.mat(U)
L = np.mat(a) * U.I

y = L.I * np.mat(b)
x = U.I * y

print(x)

 

你可能感兴趣的:(数值分析)