%% Hessenbergův tvar matice pomocí Householderovy transformace

format short
Ap=randi([-5 5],5)

%

A=Ap;
x=A(2:end,1);
Hp=HhM(x);
H1=eye(5);
H1(2:end,2:end)=Hp;
A2=H1*A*H1;
A2(3:end,1)=0

%

x=A2(3:end,2);
Hp=HhM(x);
H2=eye(5);
H2(3:end,3:end)=Hp;
A3=H2*A2*H2;
A3(4:end,2)=0

%

x=A3(4:end,3);
Hp=HhM(x);
H3=eye(5);
H3(4:end,4:end)=Hp;
T=H3*A3*H3;
T(5:end,3)=0
Q=H1*H2*H3
eig(A)
eig(T)

%
% Výsledek Matlabu

[Q1,T1]=hess(A)

%
% Hessenbergův tvar pro symetrickou matici

A=Ap'*Ap;
[Q2,T2]=hess(A)

%

function H=HhM(x)
% function H=HhM(x)
% Householder matrix such that Hx=ce_1
c=norm(x);
if c==0
    H=NaN;
    return
end
u=x;
if x(1)>0
    u(1)=u(1)+c;
else
    u(1)=u(1)-c;
end
H=eye(length(x))-2/(u'*u)*u*u';
end