QR分解
修正施密特正交
算法:
代码如下:
function [q,r] = mgs(a)
[m,n] = size(a);
v = a;
r = zeros(n);
q = zeros(m,n);
for i = 1:n
r(i,i) = norm(v(:,i),2);
q(:,i) = v(:,i)/r(i,i);
for j = (i+1) : n
r(i,j) = q(:,i)'*v(:,j);
v(:,j) = v(:,j) - r(i,j)*q(:,i);
end
end
end
基于Householder的QR分解
推理过程:
算法:
题目:
代码如下:
function [W,R] = house(A)
[m,n] = size(A);
W = zeros(m,n);
R = A;
for i = 1 : n
x = R(i:m,i);
s = sign(x(1));
if(s == 0)
s = 1;
end
v = x;
v(1) = s*norm(x) + v(1);
v = v/norm(v);
W(i:m,i) = v;
R(i:m,i:n) = R(i:m,i:n) - 2*v*(v'*R(i:m,i:n));
end
R = R(1:n,1:n);
function Q = formQ(W)
[m,n] = size(W);
Q = eye(m);
for i = 1 : m
for j = n : -1: 1
Q(j:m,i) = Q(j:m,i) - 2*W(j:m,j)*(W(j:m,j)'*Q(j:m,i));
end
end