NLA-1.1

1121 days 전, jhlee2chn 작성

Algorithm 1.1.1    (Forward Substitution)

from numpy import * L = array([[3.0, 0.0, 0.0], [-2.0, 2.0, 0.0], [7.0, 2.0, -2.0]]) b = array([1.0, 2.0, -7.0]) n = L.shape[0] # 행렬의 행의 크기 print "L = " print L print "b = " print b for i in range(n-1): b[i] = b[i] / L[i,i] b[i+1:n] = b[i+1:n] - b[i] * L[i+1:n,i] b[n-1]=b[n-1]/L[n-1,n-1] print "y = " print b 
       
L = 
[[ 3.  0.  0.]
 [-2.  2.  0.]
 [ 7.  2. -2.]]
b = 
[ 1.  2. -7.]
y = 
[ 0.33333333  1.33333333  6.        ]
L = 
[[ 3.  0.  0.]
 [-2.  2.  0.]
 [ 7.  2. -2.]]
b = 
[ 1.  2. -7.]
y = 
[ 0.33333333  1.33333333  6.        ]

Algorithm 1.1.2   (Back Substitution)

from numpy import * U = array([[1.0, 4.0, 7.0], [0.0, -3.0, -6.0], [0.0, 0.0, 2.0]]) y = array([1.0, 2.0, -7.0]) n = U.shape[0] # 행렬의 행의 크기 print "U = " print U print "y = " print y for i in range(n-1, 0, -1): y[i] = y[i] / U[i,i] y[0:i] = y[0:i] - y[i] * U[0:i,i] y[0] = y[0] / U[0,0] print "x = " print y 
       
U = 
[[ 1.  4.  7.]
 [ 0. -3. -6.]
 [ 0.  0.  2.]]
y = 
[ 1.  2. -7.]
x = 
[ 0.16666667  6.33333333 -3.5       ]
U = 
[[ 1.  4.  7.]
 [ 0. -3. -6.]
 [ 0.  0.  2.]]
y = 
[ 1.  2. -7.]
x = 
[ 0.16666667  6.33333333 -3.5       ]

Algorithm 1.1.3   (LU decomposition)

from numpy import * A = array([[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 10.0]]) n = A.shape[0] # 행렬의 행의 크기 print "A = " print A print for i in range(n-1): A[i+1:n,i] = A[i+1:n,i] / A[i,i] A[i+1:n,i+1:n] = A[i+1:n,i+1:n] - dot(array([A[i+1:n,i]]).transpose(), array([A[i,i+1:n]])) print A # LU 분해 
       
A = 
[[  1.   4.   7.]
 [  2.   5.   8.]
 [  3.   6.  10.]]

[[ 1.  4.  7.]
 [ 2. -3. -6.]
 [ 3.  2.  1.]]
A = 
[[  1.   4.   7.]
 [  2.   5.   8.]
 [  3.   6.  10.]]

[[ 1.  4.  7.]
 [ 2. -3. -6.]
 [ 3.  2.  1.]]