% CLS   Construction of a lower triangular semiseparable matrix
%
%       [G,d]=CLS(A)
%       Transforms a matrix A into a lower triangular semiseparable one
%       the upper left square block is stored in the Givens vector
%       representation. The transformations are Givens and
%       Householder transformations
%
%       [G,d]=CLS(S,'g')
%       performs the computations with only Givens transformations.
%
%
%       Software of the MaSe - Group
%       mase-at-cs.kuleuven.ac.be
%       Revision Date: 04/05/2004
% 


function [G,d]=CLS(A,opt)
 
  if not(exist('opt'))
    opt='s';
  end
      
  
  % some initializations of variables
  [n,m]=size(A);
  l=min(n,m);
  
  % the initial Givens diagonal representation
  G=[];
  d=[];
  
  % the main loop
  if opt=='s'
    for i=1:l-1
      A=LeftHouse(A,1,1);
      A=RightHouse(A,1,2);
      %A=LeftHouse(A,1,1);
      %A
      %pause
      [A,G,d]=updaterep(A,G,d);  
    end 
  elseif opt=='g'
    for i=1:l-1 
      A=LeftGiv(A,1,1);
      A=RightGiv(A,1,2);
      %A 
      %pause
      [A,G,d]=updaterep(A,G,d);  
    end 
  else
    disp('UNKNOWN OPTION')
  end  

  
  
  % to make the last rows independent
  % or the columns
  if opt=='s'
    if m<n  % m smaller than n
      A
      pause
      [v,b,sigma]=household(A);
      d(1)=sigma;
    elseif n<m    % n larger than m
      A,
      [v,b,sigma]=household(A(1,2:m-n+1)');
      % this is a trick to update the representation such
      % that also the last columns information will
      % be stored in the Givens diagonal representation
      [A,G,d]=updaterep([A(1,1),sigma;0,0],G,d);
      G=G(:,2:n);
      d=d(2:n+1);
    elseif n==1
      d=A(1,1);
    end;
  elseif opt=='g'
    if m<n  % m larger than n
      A=RightGiv(A',1,1);
      d(1)=A(1,1);
    elseif n<m    % n larger than m
      A=LeftGiv(A',2,1);
      %[v,b,sigma]=household(A(2:n-m+1,1));
      % this is a trick to update the representation such
      % that also the last columns information will
      % be stored in the Givens diagonal representation
      [A,G,d]=updaterep([A(1,1),A(2,1);0,0],G,d);
      G=G(:,2:n);
      d=d(2:n+1);
    elseif n==1
      d=A(1,1);
    end;
  end
  
    
  
  %-------------------------------------------------
function  A=RightGiv(A,l,k);
  
  [n,m]=size(A);
  
  for i=m:-1:k+1
    A=ColGiv(A,i,i-1,l);
  end
  
function  A=LeftGiv(A,l,k);
  
  [n,m]=size(A);
  
  for i=n:-1:l+1
    A=RowGiv(A,i,i-1,k);
  end
    
        
  
  
% ----------------------------------------------------
% function Right House, will perform a Right Householder
% transformation, and annihilate row l from column k
% to the complete right.
  
function A=RightHouse(A,l,k)
  
  [n,m]=size(A);
  
  % calculating the householder characterizations
  % namely v, beta and sigma
  [v,beta,sigma]=household(A(l,k:m)');
  
  % Performing the multiplications
  % in the most efficient way, which means only
  % multiplying the involved pieces of the matrix
  % therefor we remove column k
  if n~=1
    Atemp=A([1:l-1,l+1:n],k:m);
    w=beta*Atemp*v;
    Atemp=Atemp-w*v';
    A([1:l-1,l+1:n],k:m)=Atemp;
  end
  
  % if m=1 only these last rows have to be performed
  A(l,k+1:m)=zeros(1,m-k);
  A(l,k)=sigma;
  
  
 % --------------------------------------------------
% function Left House, will perform a left Householder
% transformation, and annihilate column k, from
% element l down to the bottom

function A=LeftHouse(A,l,k)
  
  [n,m]=size(A);
  
  % calculating the householder characterizations
  % namely v, beta and sigma
  [v,beta,sigma]=household(A(l:n,k));
  
  % Performing the multiplications
  % in the most efficient way, which means only
  % multiplying the involved pieces of the matrix
  % therefor we remove column k
  if m~=1
    Atemp=A(l:n,[1:k-1,k+1:m]);
    w=beta*Atemp'*v;
    Atemp=Atemp-v*w';
    A(l:n,[1:k-1,k+1:m])=Atemp;
  end
  
  % if m=1 only these last rows have to be performed
  A(l+1:n,k)=zeros(n-l,1);
  A(l,k)=sigma;
  
  
%----------------------------------------------------      
 
% [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, performs a Givens rotation
% on element, by multipying on the lefthandside, 
% in fact applying row operations

% The element that we want to annihilate, 
% is the element in the matrix M
% on position Row Column

% the col that will be used for the givens is 
% the col col2 

function [M,s,c]=ColGiv(M,Col,Col2,Row)

G=givens(M(Row,Col2),M(Row,Col));

Mtransf=[M(:,Col2) , M(:,Col) ];

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

M(:,Col2)=Mtransf(:,1);
M(:,Col)=Mtransf(:,2);

s=G(1,2);
c=G(2,2);


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

% This function, performs a Givens rotation
% on element, by multipying on the lefthandside, 
% in fact applying row operations

% 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,s,c]=RowGiv(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,:);

s=G(1,2);
c=G(2,2);


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

% updating the representation of G,d
% for the upper triangularization

function [A,G,d]=updaterep(A,G,d)
  
  % initializations of all the needed variables
  [n,m]=size(A);
  l=size(G,2);
  
  % B represents the block we will work with
  % and A is just the remaining matrix part
  B=A(1:2,1:2);
  A=A(2:n,2:m);

  % one has to be careful because if we are a little further in the loop
  % B needs to be updated a little bit, especially the first row has
  % to be multiplied with a factor coming from the Givens transformation
  % dtemp1 is stored because otherwise we have to divide later on
  % by G(1,1)
  
  if l>0
    dtemp1=B(1,1);
    B(1,1)=B(1,1)*G(1,1);
  end
  

  % applying the first Givens transformation,
  % before the update
  dnew(1)=B(2,2);
  %B
  [B,s,c]=ColGiv(B(1,:),2,1,1);
  %B
  %pause
  Gnew(:,1)=[c;s];
 
  % we will now start with the sequence of
  % updating the upper triangular semiseparable part

  for i=1:l
    % we will calculate the new block B used to calculate
    % all the givens and the updates
    B(1,2)=0;
    B(2,2)=B(1,1);
    % in fact B(1,1) and B(1,2) need multiplication by a factor
    % this is not done because in this way 1 operation is saved
    % and this is more stable
    B(1,1)=d(i+1);
    B(2,1)=dtemp1*G(2,i); 
  
    % now the Col givens will be executed
    % than we can already store the new diagonal element
    % for the row givens we still have to multiply with
    % the factor we omitted just above
    %B
    B=RowGiv(B,2,1,1);
    %B
    %pause
    
    dnew(i+1)=B(2,2);
    dtemp1=B(1,1);
    if i<l
      B(1,1)=B(1,1)*G(1,i+1);
    end
  
    %B
    [B,s,c]=ColGiv(B(1,:),2,1,1);
    %B
    %pause
    
    Gnew(:,i+1)=[c;s];
  
  end
  
  % The last element still needs adaptation
  % l+2 because the size of G is l,
  % therefore the size of d used to be l+1, but has 
  % grown in dimension and therefore equals now l+2
  dnew(l+2)=B(1,1);
  
  % preparing d, and G to be returned by the routine
  d=dnew;
  G=Gnew;
