Added Householder algorithm and introduced Householder QR (got R, gotta find Q)

This commit is contained in:
Sergiusz Warga 2021-03-14 15:34:25 +01:00
parent 121ada24f9
commit 7e55d72cfe
3 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,21 @@
function [Q R] = Alg11(A)
% Algorithm 11: QR factorization via Housholder algorithm.
[m, n] = size(A);
if ~ (m>=n)
error("m has to be greater or equal n!")
end
for j = 1:n
[v, beta] = householder(A(j:m, j))
A(j:m, j:n) = (eye(m-j+1)-beta*(v*v.'))*A(j:m, j:n)
if j < m
A(j+1:m, j) = v(2:m-j+1)
end
end
R = triu(A)
end

View File

@ -0,0 +1,6 @@
function [Q R] = Alg13(A)
end

View File

@ -0,0 +1,27 @@
function [v,beta] = householder(x)
% householder takes vertical vector x
if ~iscolumn(x)
x = x.';
end
n = length(x);
sig = x(2:n).' * x(2:n);
v = [1;
x(2:n)];
if isempty(sig) || (sig == 0)
beta = 0;
else
mu = sqrt(x(1)^2+sig);
if x(1) <= 0
v(1) = x(1) - mu;
else
v(1) = -sig/(x(1) + mu);
end
beta = 2*v(1)^2/(sig + v(1)^2);
v = v/v(1);
end
end