NLA-1.3

1107 days 전, jhlee2chn 작성

Algorithm 1.3.1  Cholesky Factorization

from numpy import * A = array([[4.0, -2.0, 4.0, 2.0], [-2.0, 10.0, -2.0, -7.0], [4.0, -2.0, 8.0, 4.0], [2.0, -7.0, 4.0, 7.0]]) n = A.shape[0] # 행렬의 행의 크기 print "A = " print A print for i in range(n): A[i,i] = sqrt(A[i,i]) A[i+1:n,i] = A[i+1:n,i]/A[i,i] for j in range(i+1, n): A[j:n,j] = A[j:n,j] - A[j:n,i]*A[j,i] print A 
       
A = 
[[  4.  -2.   4.   2.]
 [ -2.  10.  -2.  -7.]
 [  4.  -2.   8.   4.]
 [  2.  -7.   4.   7.]]

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

[[ 2. -2.  4.  2.]
 [-1.  3. -2. -7.]
 [ 2.  0.  2.  4.]
 [ 1. -2.  1.  1.]]
L = array([[2.0, 0.0, 0.0, 0.0], [-1.0, 3.0, 0.0, 0.0], [2.0, 0.0, 2.0, 0.0], [1.0, -2.0, 1.0, 1.0]]) print L print print dot(L, L.transpose()) 
       
[[ 2.  0.  0.  0.]
 [-1.  3.  0.  0.]
 [ 2.  0.  2.  0.]
 [ 1. -2.  1.  1.]]

[[  4.  -2.   4.   2.]
 [ -2.  10.  -2.  -7.]
 [  4.  -2.   8.   4.]
 [  2.  -7.   4.   7.]]
[[ 2.  0.  0.  0.]
 [-1.  3.  0.  0.]
 [ 2.  0.  2.  0.]
 [ 1. -2.  1.  1.]]

[[  4.  -2.   4.   2.]
 [ -2.  10.  -2.  -7.]
 [  4.  -2.   8.   4.]
 [  2.  -7.   4.   7.]]

Algorithm 1.3.2  LDLT Factorization

from numpy import * A = array([[4.0, -2.0, 4.0, 2.0], [-2.0, 10.0, -2.0, -7.0], [4.0, -2.0, 8.0, 4.0], [2.0, -7.0, 4.0, 7.0]]) n = A.shape[0] # 행렬의 행의 크기 print "A = " print A print for j in range(n): if j == 0: A[j+1:n,j]=A[j+1:n,j]/A[j,j] else: v = zeros(j) for i in range(0,j): v[i]=A[j,i]*A[i,i] A[j,j]=A[j,j]-sum(A[j,0:j]*v[0:j]) A[j+1:n,j]=(A[j+1:n,j]-dot(A[j+1:n,0:j],v[0:j]))/A[j,j] print A 
       
A = 
[[  4.  -2.   4.   2.]
 [ -2.  10.  -2.  -7.]
 [  4.  -2.   8.   4.]
 [  2.  -7.   4.   7.]]

[[ 4.         -2.          4.          2.        ]
 [-0.5         9.         -2.         -7.        ]
 [ 1.          0.          4.          4.        ]
 [ 0.5        -0.66666667  0.5         1.        ]]
A = 
[[  4.  -2.   4.   2.]
 [ -2.  10.  -2.  -7.]
 [  4.  -2.   8.   4.]
 [  2.  -7.   4.   7.]]

[[ 4.         -2.          4.          2.        ]
 [-0.5         9.         -2.         -7.        ]
 [ 1.          0.          4.          4.        ]
 [ 0.5        -0.66666667  0.5         1.        ]]