% CHL   Construction of a similar Hessenberg-Like matrix
%
%       [G,d,R]=CHL(S)
%       produces the representation of a semiseparable 
%       matrix from the symmetric matrix S. With Householder transformations
%       G represents a sequence of givens
%       d represents a diagonal
%       R is the strictly upper triangular part of the Hessenberg-like matrix
%
%
%       [G,d,R]=CHL(S,'g')
%       performs the computation with Givens transformations.
%       This is the most stable but slowest.
%
%       Software of the MaSe - Group
%       mase-at-cs.kuleuven.ac.be
%       Revision Date: 04/05/2004


function [G,d,R]=CHL(S,opt)

if not(exist('opt'))
    opt='h';
end
    
  
n=size(S,1);
A=S ;
G=zeros(2,n-1);
d=zeros(1,n);

if n==1
  d(1)=S(1,1);
else
  for j=n:-1:2
    [A,G,d]=red(A,G,d,j,opt);
  end
  
  
end
R=triu(A,1);R=R(1:n-1,2:n);

% final completion of the representation
% these values can easily be extracted
% from the remaining matrix A

%--------------------------------------------------------
% This is a reduction step of the matrix
% one row more will be made dependent.


function [A,G,d]=red(A,G,d,j,opt)

r=size(A,1);

% disp('Making zeros')
% dit moet nog door householders
% effectief vervangen worden.

switch opt
    case 'h'
        A=SymHouseh(A,j-1);
    case 'g'
        A=SymHouseg(A,j);
end

[A,G,d]=upd(A,G,d,j);


%---------------------------------------------------

% this function will perform
% a Symmetric householder operation
% on the matrix A the column which will be annihilated
% will be column k from element 1:l
% A=SymHouse(A,k,l)



function A=SymHouse(A,l)

% implemented like in Golub and vanloan
  

  %[v,beta,sigma]=household(flipud(A(1:l,l+1)));
  %[v, beta] = gallery('house',fliplr(A(l+1,1:l))');
  [v,beta,sigma]=household(fliplr(A(l+1,1:l))');

  %v=flipud(v);
  v=flipud(v);
  p=beta*(A(1:l,1:l)'*v);
  w=p-((beta/2)*(p'*v))*v;

  
  A(l,l+1)=sigma;
  A(l+1,l)=A(l,l+1);

  for i=1:l,
    for j=1:i,
       A(i,j)=A(i,j)-v(i)*w(j)-w(i)*v(j);
    end
  end
  for i=1:l-1,
    for j=i+1:l,
      A(i,j)=A(j,i);
    end
  end
 
%----------------------------------------------------------

% block the elements in these final rows and columns
% are zeros.
% Also a serie of givens and diagonal elements
% to calculate the remaining semiseparable part of this.
% matrix is given
% this function has the following form, and will
% update the representation.
% will reduce 1 in dimension and G and d will grow one.
% [A,G,d]=updrep(A,G,d);


function [A,G,d]=upd(A,G,d,j);

  % complete size of the starting matrix equals n
  n1=j;
  % is the size of the block wihihc is not yet in the proper form
  
  n=length(d);

  % for the first loop to be completed
  % in a perfect way: Somethings have to
  % be initialized

  d(j-1)=A(n1-1,n1-1);
             
  if j==n
   d(j)=A(n1,n1);
  end

  for i=j:n-1
    % The matrix G calculated below
    % is the matrix for which
    % [b,c]*G=[0,*];
    
    [ct,st,rt]=Givens1(d(i),A(i,i-1));
    
    % performing the transformations on the upper
    % part of the matrix A
    G(:,i-1)=[ct;st];
    Giv=[ct,st;-st,ct];
    A(1:i,i-1:i)=A(1:i,i-1:i)*Giv;
    
    %updating the givens vector represnetation
    d(i-1)=A(i-1,i-1);
    
    A(i,i)=A(i,i)*G(1,i);
    A(i-1:i,i:n)=Giv'*A(i-1:i,i:n);
        
    % updating this element, because it is coming from the
    %Givens vector rerpresentation
    A(i+1,i)=rt*G(2,i);
  end

  
  % The matrix G calculated below
  % is the matrix for which
  % [b,c]*G=[0,*];
  [ct,st,rt]=Givens1(d(n),A(n,n-1));
  
  G(:,n-1)=[ct;st];
  Giv=[ct,st;-st,ct];

  A(1:n,n-1:n)=A(1:n,n-1:n)*Giv;
  
  d(n-1)=A(n-1,n-1);
  
  A(n-1:n,n)=Giv'*A(n-1:n,n);
  
  d(n)=A(n,n);
  


%------------------------------------------------------
function [c,s,r] = Givens1(x,y);
%
%  computes c,s,r such that
%
%  [c  s] [x] = [r]
%  [-s c] [y]   [0]
%
%  with c*c + s*s = 1;
%
   if (y == 0),
      c = 1; s = 0; r = x;
   else 
      if (abs(x) >= abs(y))
         t = y/x; r = sqrt(1 + t*t);
         c = 1/r;
         s = t*c;
         r = x*r;
      else 
         t = x/y; r = sqrt(1 + t*t);
         s = 1/r;
         c = t*s;
         r = y*r;
      end
   end 

%----------------------------------------------------      
 
% [v,beta,sigma]=household(u)
% 
% this function will calculate the
% householder transformation defined
% as in Golub and Van Loan.
% H=EYE(N) - BETA*V*V'
% A Householder matrix H satisfies the
% relationship
% H*X = sigma*e1,
% where e1 is the first column of EYE(N).
% 


function [u,beta,sigma]=household(u)

  n=length(u);
  sigma=norm(u);
  
  if sigma==abs(u(1))
    beta=0;
    u=zeros(length(u),1);
  else
    sigma=-sign(u(1))*sigma;
    u(1)=u(1)-sigma; 		
    beta=2/(u'*u);
  end

%---------------------------------------------------

% this function will perform
% a Symmetric householder operation
% on the matrix A the column which will be annihilated
% will be column k from element 1:l
% A=SymHouse(A,k,l)



function A=SymHouseh(A,l)

  % implemented like in Golub and vanloan
   
  %[v, beta] = gallery('house',flipud(A(1:l,l+1)));
  [v, beta] = gallery('house',fliplr(A(l+1,1:l))');

  v=flipud(v);
  
  % multiplication left
  %w=beta*A(1:l,:)'*v;
  %A(1:l,:)=A(1:l,:)-v*w'; 

  % multiplication right
  w=beta*A(:,1:l)*v;
  A(:,1:l)=A(:,1:l)-w*v';
  
  %G,w,v
  %disp('house')
  %pause
 
  % multiplication left
  w=beta*A(1:l,:)'*v;
  A(1:l,:)=A(1:l,:)-v*w';
 % A
 % pause
  
  % this function will perform
% a Symmetric householder operation
% on the matrix A the column which will be annihilated
% will be column k from element 1:l
% A=SymHouse(A,k,l)

%------------------------------------------------

function A=SymHouseg(A,j)
  % Instead of applying a symmetric householder
  % a loop of Givens is performed, for
  % numerical unstable problems, this is the best
  % option

  % implemented like in Golub and vanloan
  %n=size(A,1);
  
  for i=1:j-2
   [A]=SymGiv(A,j,i+1,i);
end

%-------------------------------------------------

% This function, performs a Givens rotation
% on element, by multipying on the lefthandside, 
% in fact applying row operations
% the performed operation is a symmetric givens
%
% The element that we want to annihilate, 
% is the element in the matrix M
% on position Row Column
%
% the row that will be used for the givens is 
% the row Row2
%
% function [M]=SymGiv(M,Row,Row2,Column)

function [M]=SymGiv(M,Row,Column2,Column)

G=givens(M(Row,Column2),M(Row,Column));

Mtransf= [ M(:,Column2) M(:,Column)];

Mtransf=Mtransf*G';

M(:,Column2)=Mtransf(:,1);
M(:,Column)=Mtransf(:,2);

M;
%pause

Row2=Column2;
Row=Column;
M;
Mtransf=[M(Row2,:) ; M(Row,:) ];

Mtransf=G*Mtransf;
%Mtransf(2,1)=0;

M(Row2,:)=Mtransf(1,:);
M(Row,:)=Mtransf(2,:);
M;

