function [v,beta]=house(x)
% HOUSE Given an n-dimensional vector x. This function computes
% n-dimensional vector, v, and a scalar, beta, such that the Householder
% matrix P=I_n-beta v v' is orthogonal and Px=norm(x,'fro')*e_1.
% SYNTAX
% [v,beta]=house(x)
% ON INPUT
% x - An n-dimensional vector
% ON OUTPUT
% v - Householder vector.
% beta - scalar used to form the Householder matrix P
n=length(x);
sigma=x(2:n)'*x(2:n);
v=[1;x(2:n)];
if sigma==0
    beta=0;
else
    mu=sqrt(x(1)^2+sigma);
    if x(1) <= 0
        v(1)=x(1)-mu;
    else
        v(1)=-sigma/(x(1)+mu);
    end
    beta=2*v(1)^2/(sigma+v(1)^2);
    v=v/v(1);
end
