In [26]:
# 多行结果输出支持
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
In [3]:
import numpy as np
In [4]:
def LU(A):
U = np.copy(A)
m, n = A.shape
L = np.eye(n)
for k in range(n-1):
for j in range(k+1,n):
L[j,k] = U[j,k]/U[k,k]
U[j,k:n] -= L[j,k] * U[k,k:n]
return L, U
In [15]:
A = np.array([[2,1,1,0],[4,3,3,1],[8,7,9,3],[6,7,9,8]]).astype(np.float)
In [16]:
L, U = LU(A)
In [17]:
L
Out[17]:
In [18]:
U
Out[18]:
In [19]:
A
Out[19]:
In [20]:
L @ U
Out[20]:
In [12]:
np.allclose(A, L @ U)
Out[12]:
The LU factorization is useful!
Solving Ax = b becomes LUx = b:
In [27]:
v=np.array([1,2,3])
v
v.shape
Out[27]:
Out[27]:
In [32]:
v1=np.expand_dims(v, -1)
v1
v1.shape
Out[32]:
Out[32]:
In [39]:
v2 = v[np.newaxis]
v2
v2.shape
Out[39]:
Out[39]:
In [40]:
v3 = v[:, np.newaxis]
v3
v3.shape
Out[40]:
Out[40]:
In [47]:
import sklearn
In [ ]: