
 function [r, C] =Lie_gen(varargin)   

%   
%   [r,W]=Lie_gen(H0, H1, H2, H3, H4, H5, H6, H7, H8, H9, H10)
%
%   generates a lie algebra spanned by the input NxN hamiltonians.
%   output
%     r- dimension of generated Lie algebra.
%     C- An r-length cell array, each component of which contains a generator in the 
%        Lie algebra.
%   The algorithm is based on Table 1 in:
%     S.G. Schirmer, H. Fu and A.I. Solomon, PRA, 63 063410 (2001)
%
%



M=nargin;

for m=1:M
   [N1,N2]=size(varargin{m});
   if m==1 N=N1; end;
   if N1~=N | N2~=N ;
        %disp(['H' num2str(m) 'must be an NxN hermetian Matrix']); return; 
   end;
end;



NN=N*N; 
%W=zeros(NN,NN);
W=varargin{1}(:);
C(1)={varargin{1}};
r=rank(W);
for m=[2:M]
  h=varargin{m};
  if rank([W,h(:)])>r;
    W=[W,h(:)];
    r=r+1;
    C(m)={h};
  end;
end;

r0=0; rn=r; %rn=rank(W);
while rn~=r0 & r<NN
   for l=[r0+1:rn];
      for j=[1:l-1];
          Hl=C{l}; % reshape(W(:,l),N,N);
          Hj=C{j};  %reshape(W(:,j),N,N);
          h=Hl*Hj-Hj*Hl;
          if rank([W,h(:)])>r 
	       W=[W,h(:)];
               r=r+1;
           C(r)={h};
          end;
      end;
   end;
   r0=rn;
   rn=r;  %rn=rank(W);
end;
