首页 > 编程知识 正文

矩阵qr分解c代码,求矩阵A的qr分解

时间:2023-05-03 10:00:05 阅读:171055 作者:4594

转载: https://www.cn blogs.com/qw12/p/6079244.html

1.Gram-Schmidt正交化http://www.Sina.com/http://www.Sina.com/b=bpb=batbatab=bpb=batbatata

2.http://www.Sina.com/http://www.Sina.com /

3358 www.Sina.com/http://www.Sina.com/http://www.Sina.com/http://www.Sina.com /

假设原来的矩阵为[a,b],a,b为线性无关的二维向量,下面我们通过Gram-Schmidt正交化使得矩阵A为标准正交矩阵:

假设正交化后的矩阵为Q=[A,B],我们可以令A=a,那么我们的目的根据AB=I来求B,B可以表示为b向量与b向量在a上的投影的误差向量:

1# coding 3360 utf 82 importnumpyasnp 34 def gram _ Schmidt (a ) :5('gram-schmidt正交化(' ' ' 6 Q=np.zeros_like(A ) a ) a CNT ) :11u-=NP.dot ) NP.dot ) Q[:i].T I )减去想要求出的向量在求出向量上的投影12e=u/NP.Linalg.norm(u ) #正规化13 a ) 16 return (Q r ) 1718defgivens_rotation(a ) :19“”“givens变换”“”20 ) r,c )=NP.shape(a ) 21718 q=NP.idef=0: # R[row,col]=0则c=1,s=0,r,q不变26r_=NP.hypot(r[col,col],R[row,col] ) D27c=r ) col col]=s32 G[col,row]=-S33 1n )…(g ) 12 ) a34q=NP.dot,G.T ) ) q=g(n,n ).t )…g(2n ).t *… r ) 3637 def household er _ reflection (a ) :38 '''Householder转换' ' 39 ' r, c )=NP.shape(a ) 40 Q=np.identity(r ) r ) 41 R=np.copy(A ) a ) 42forCNTinrange(r-1 ) 3:43 x=r [ CNT 3333333333333 CNT ) 44e=NP.Zeros_like(x ) 45 e[0]=np.linalg.norm(x ) 46 u=x - e47 v=u/np.linalg.norm(u ) u CNT:(=2.0*NP.outer(v,v ) 50r=NP.dot ) q_CNT,r ) r=h(n-1 )…*h ) h )1) a51q=Q_cnt r ) 5354NP.set_printoptions ) precision=4,surn 1,-14,[ 0,4,3 ],dtype=float(5657 ) q, r )=gram_schmidt(A ) a ) 58 print(Q ) q ) 59 print(R ) r ) 60printNP )=givens_rotation(a ) 63 print(Q )

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。