% CSS   Construction of a semiseparable matrix
%
%       [G,d]=CSS(S)
%       produces the representation of a semiseparable 
%       matrix from the symmetric matrix S. With the
%       fastest implementation.
%       G represents a sequence of givens
%       d represents a diagonal
%
%       [G,d]=CSS(S,'h')
%       performs the computation with a more stable, but
%       slower householder implementation.
%
%       [G,d]=CSS(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: 16/12/2003


function [G,d]=CSS(S,opt)

if not(exist('opt'))
    opt='s';
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

% 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);
    case 's'
        A=SymHouse(A,j-1);
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=flipud(v);
  p=beta*(A(1:l,1:l)'*v);
  w=p-((beta/2)*(p'*v))*v;

  
  A(l+1,l)=sigma;
  A(l,l+1)=A(l+1,l);
  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=size(A,1);
  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);
  VAL=A(n1,n1-1);

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

  for i=j:n-1
    a=d(i-1); 
    % The matrix G calculated below
    % is the matrix for which
    % [b,c]*G=[0,*];
    [ct,st,rt]=Givens1(d(i),VAL);
    d(i)=(ct*G(1,i)*(rt+st*VAL)+st^2*a);
    G(:,i-1)=[ct;st];
    d(i-1)=(ct*a-st*VAL*G(1,i));
    VAL=rt*G(2,i);
  end

  a=d(n-1);
  % The matrix G calculated below
  % is the matrix for which
  % [b,c]*G=[0,*];
  [ct,st,rt]=Givens1(d(n),VAL);
  d(n)=(ct*(rt+st*VAL)+st^2*a);
  G(:,n-1)=[ct;st];
  d(n-1)=(ct*a-st*VAL);
 
  A(n1-1,n1-1)=d(j-1);
  A=A(1:n1-1,1:n1-1);


%------------------------------------------------------
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=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'; 

  
  % 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)
  % 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:n-2
   [A]=SymGiv(A,i,i+1,n);
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,Row2,Column)

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

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

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

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


Column=Row2;
Column2=Row;


clear Mtransf

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

Mtransf=Mtransf*G';

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

M;
