%
% Program IrrepMain
%
% 
% Revised, extended and implemented in Matlab by Per-Olof Janssson
% 
%
fid = fopen('Irrep_res.m','a');
%
%                      ******************************
%                      *                            *
%                      *                            *
%                      *           IRREP            *
%                      *                            *
%                      *        Version 1.0         *
%                      *                            *
%                      *        2006-06-25          *
%                      *                            *
%                      *  Author: Per-Olof Jansson  *
%                      *                            *
%                      *  This MATLAB code is an    *
%                      *  extension of the IRREP,   *
%                      *  FORTRAN program, written  *
%                      *  by Esko Blokker and       *
%                      *  Stig Flodmark. QCPE 163   *
%                      *                            *
%                      ******************************
%
%
% Input group order G
%
load order_C4v_C4v.dat;
G = order_C4v_C4v;
if G > 0
  load steer.dat
%
% The values of steer(I) will steer different functions of the program.
%
% Read an identification of the program.
%
%load xname
%
% Read the multiplication table of the group.
%
  load C4v_C4v.dat;
  multab = C4v_C4v;
%
  M = G*(G + 1)/2;
%
% A short check is made, that the input multiplication table is correct.
%
  A = sum(multab.');
  for I = 1:G
    if A(I) ~= M
      fprintf(fid, 'I = %4d\n', I);
      fprintf(fid, 'A(I) = %4d\n', A(I));
      fprintf(fid, 'M = %4d\n', M);
      steer(11) = 0;
    end
  end
%
  if steer(11)
% 
% If steer(11) = true, the multiplication table will be printed, row by row.
%
    fprintf(fid, '\n');  
    fprintf(fid, 'The group multiplication table:\n\n');
    for I = 1:G
      fprintf(fid, '%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n', multab(I,1:G));
      fprintf(fid, '\n');
    end
    fprintf(fid, '\n');  
%
% The inverse group elements are calculated and stored in inel(J).
%
    fclose(fid);
    inel = inverse(G,multab,steer);
    if steer(11)
      fid = fopen('Irrep_res.m','a'); 
      if steer(3)
        fprintf(fid, 'The inverse element of each group element is:\n\n');
        fprintf(fid, '%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n', inel);
        fprintf(fid, '\n\n');
      end
%
% A set of prime numbers are calculated.
%
      prime = primen;
      fprintf(fid, 'One hundred prime numbers are calculated:\n\n');
      fprintf(fid, '%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n', prime);
      fprintf(fid, '\n');
%
% A set of group element generators are calculated.
%
      [map,nmberg,ngen] = genera(G,multab);
      if steer(4)
        fprintf(fid, '\n');
        fprintf(fid, 'There are %3d generators of the group\n', nmberg);
        fprintf(fid, '\n');
        fprintf(fid, 'The generators are:\n');
        fprintf(fid, '%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n', ngen);
        fprintf(fid, '\n');
        fprintf(fid, 'map(:,1):\n');
        fprintf(fid, '%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n', map(:,1)');
        fprintf(fid, '\n');
        fprintf(fid, 'map(:,2):\n');
        fprintf(fid, '%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n', map(:,2)');
        fprintf(fid, '\n');
      end
%
% The group classes are calculated.
%
      [nfirst,h,classl,ncl] = classes(G,multab,inel);
      if steer(5)
        fprintf(fid, '\n');
        fprintf(fid, 'There are %3d classes of the group\n', ncl);
        fprintf(fid, '\n');
        for I = 1:ncl
          L = nfirst(I);
          M = L + h(I) - 1;
          fprintf(fid, 'Class %3d has %3d elements: %3d %3d %3d %3d %3d %3d %3d %3d', I, h(I), classl(L:M));
          fprintf(fid, '\n');
        end
        fprintf(fid, '\n');
      end
%
% The irreducible group characters are calculated.
%
      status = fclose(fid);
      [ch,cind,onert,sirt,lj,norder] = charac(multab,G,ncl,...
                                              h,nfirst,classl,inel,steer,prime);
%
% The irreducible representations of the group elements are calculated.
%
      if steer(11)
        if steer(2)
          repres(multab,G,steer,inel,lj,ncl,nfirst,classl,sirt,...
                 onert,cind,ch,nmberg,ngen,map,norder);
        end
      end
    end  
  else
    fclose(fid);  
    fprintf('The group multiplication table is not consistent\n');
  end
else
 fclose(fid);   
 fprintf('Group order not consistent\n');
end
fprintf('Irrep_main has finished executing');