NLA-3.3

2013 days 전, jhlee2chn 작성

Algorithm 3.3.1  QR decomposition (use Householder transformations)

from numpy import * A = array([[4.0, -2.0, 4.0], [-2.0, 10.0, -2.0], [4.0, -2.0, 8.0], [2.0, -7.0, 4.0]]) print "A=" print A m0,n0 = A.shape # 행렬의 크기 d=zeros(n0) # beta 기억 # householder transformation 함수 정의 def householder(x): n=len(x) # 벡터 x의 길이 v=zeros(n) eta=linalg.norm(x, inf) x=x/eta sig=linalg.norm(x[1:n]) v[0]=1;v[1:n]=x[1:n] if sig!=0: alp=sqrt(square(x[0])+sig) if x[0]<=0: v[0]=x[0]-alp else: v[0]=-sig/(x[0]+alp) beta=2*square(v[0])/(sig+square(v[0])) v=v/v[0] else: beta=0 return v, beta # main program # QR decomposition for j in range(0, n0): v, beta=householder(A[j:m0, j]) A[j:m0, j:n0]=A[j:m0, j:n0]-beta*dot(array([v]).transpose(), array([dot(v, A[j:m0, j:n0])])) d[j]=beta if j < m0: A[j+1:m0, j]=v[1:m0-j+1] print A print print "d=", d 
       
A=
[[  4.  -2.   4.]
 [ -2.  10.  -2.]
 [  4.  -2.   8.]
 [  2.  -7.   4.]]
[[  6.70439962  -8.38049953  10.05659943]
 [  1.01717424  11.54442152  -2.75229932]
 [ -2.03434848  -1.3685473    5.25993787]
 [ -1.01717424   0.06355554   2.22714633]]

d= [ 0.32956004  0.69566663  0.18510126]
A=
[[  4.  -2.   4.]
 [ -2.  10.  -2.]
 [  4.  -2.   8.]
 [  2.  -7.   4.]]
[[  6.70439962  -8.38049953  10.05659943]
 [  1.01717424  11.54442152  -2.75229932]
 [ -2.03434848  -1.3685473    5.25993787]
 [ -1.01717424   0.06355554   2.22714633]]

d= [ 0.32956004  0.69566663  0.18510126]