% Philip Hall basis constructor.
%
% Written by Miguel Torres-Torriti (c) 7.MAR.2001.
%
%  k: Order or nilpontency, i.e. brackets of lenght k+1 are zero. 
%  m: Number of generators.
%

clear all;
k=5;
m=2;

%Declare generators
for g=1:m,
   x{g}=sprintf('%i',g);
end

%Generate all possible brackets
gt=m;   % Total population (before the breeding season starts).  
gold=0; % Old population: those that have giving a birht an offspring.
gacum=gt; % Accumulated population.
% b: Number of breeding seasons.
for b=1:k-1,
%offspring of all possible couples given the current population.
for g1=1:gt,
   % Set pointer gs to do the inter-generation and intra-generation
   % breedings.
   if g1>gold
      gs=g1+1;    % Breeding from first time parents.
   else
      gs=gold+1;  % Crossing between old generation and offspring.  
   end
   for g2=gs:gt,
      if (b>=2)&(g1<=gold)
         if orderpos(x{g2},x) <= g1 
            gacum=gacum+1;
            x{gacum}=sprintf('[%s.%s]',x{g1},x{g2});
         end
      else
      	gacum=gacum+1;
         x{gacum}=sprintf('[%s.%s]',x{g1},x{g2});
      end      
      % Eliminate all elements or order higher than k.
      if length(findstr(char(x{gacum}),'.'))>=k
         gacum=gacum-1;
         x=x(1:gacum);
      end
   end
end
gold=gt;
gt=gacum;
end

disp(sprintf('Philip Hall Basis elements (stored in ''x''):\n'));
disp(x);
