load "Subgroups/functions.txt";

function SomeModulesSelfDual(n,Irr2)

Irr:=[];
for i in [j:j in [1..#Irr2]|Dimension(Irr2[j]) le n] do
  nn:=IdentifyModule(Dual(Irr2[i]),Irr2);
  if(nn eq i) then Append(~Irr,Irr2[i]);
    elif(nn gt i and Dimension(Irr2[i]) le n div 2) then Append(~Irr,DirectSum(Irr2[i],Irr2[nn]));
  end if;
end for;
return OrderByDimension(Irr);
end function;

/* This function takes a set of irreducible modules and computes all possible sets of composition factors
  from them with consistent traces, with CC the conjugacy classes of G. P is a special set of integers
  you want to test first, but just the traces, not the eigenvalues. This is the same if the integer is a
  prime, of course. Ans is a previously known set of dimensions for composition factors. Leave as [] if
  you want to test all of them. */


function ComputeConspicuousFactors(nn,Irr,CC,Tr,P,Ans)

G:=Group(Irr[1]);
p:=Characteristic(Field(Irr[1]));

Br:=[BrauerCharacter(i):i in Irr];
// Now compute all of the sets of composition factors of dimension nn.

"Computed Brauer characters";
if(Ans eq []) then Ans:=RestrictedPartitions(nn,{Dimension(i):i in Irr}); end if;

dims:=[Dimension(i):i in Irr];
Ans2:=[];
for CurrentDegreeSeq in Ans do
  "On case",Position(Ans,CurrentDegreeSeq),"of",#Ans;
  NewAns:=[];
  for j in SequenceToSet(CurrentDegreeSeq) do
    m:=[]; for k in [1..#dims] do if(dims[k] eq j) then Append(~m,k); end if; end for;
    n:=Multiplicity(SequenceToMultiset(CurrentDegreeSeq),j);
    ThingsToAppend:=DecreasingSequences(n,m);
    if(NewAns eq []) then NewAns:=ThingsToAppend; delete ThingsToAppend;
    else
      NewAns2:=[];
      for alpha in NewAns do
        for beta in ThingsToAppend do Append(~NewAns2,alpha cat beta);
        end for;
      end for;
      delete NewAns; NewAns:=NewAns2; delete NewAns2; delete ThingsToAppend;
    end if;
  end for;
  "Have so far",#NewAns,"sets of factors";
  for r in P do
    al:=Position([i[1]:i in CC],r);
    TrCheck:={i[1]:i in Tr[r]};
    Ans3:=[];
    for i in [1..#NewAns] do
      if (i mod 10000) eq 0 then i,#NewAns,#Ans3; end if;
      test:=&+[Br[k,al]:k in NewAns[i]];
      if(test in TrCheck) then Append(~Ans3,NewAns[i]); end if;
    end for;
    delete NewAns;
    NewAns:=Ans3;
    delete Ans3;
    delete TrCheck;
    "Down to",#NewAns,"sets of factors";
  end for;
  Ans2 cat:=NewAns;
  delete NewAns;
end for;
"Found",#Ans2,"possible sets of composition factors";

lim:=#[i:i in [j[1]:j in CC]|i le #Tr];
phi:=PowerMap(G);
CPM:=[[phi(j,i):i in Prune(Divisors(CC[j,1]))]:j in [1..lim]];
"Computed the conjugation power map for semisimple elements";

Ans3:=[];
for i in Ans2 do
  test:=[&+[Br[k,j]:k in i]:j in [1..lim]];
  for j in [2..#CPM] do if(CC[j,1] le #Tr and GCD(CC[j,1],p) eq 1) then
    ord:=CC[j,1]; divs:=Prune(Divisors(ord));
    tt:=[test[k]:k in CPM[j]];
    if(not(tt in Tr[ord])) then continue i; end if;
    end if;
  end for;
  Append(~Ans3,i);
end for;

"Reduced via traces to",#Ans3,"conspicuous sets of factors";
Ans3:=Sort([Sort(i):i in Ans3]);
Mods:=[DirectSum([Irr[i]:i in j]):j in Ans3];

return Ans3,Mods;

end function;

"At the end of this file are commands that load traces depending on which algebraic group you are interested in.";
/*
load "Traces/E7/TracesE7.txt";
load "Traces/E7/Tr14E7";
load "Traces/E7/Tr15E7";
Trac:=<{[[56],[133]]},Tr2E7,Tr3E7,Tr4E7,Tr5E7,Tr6E7,Tr7E7,Tr8E7,Tr9E7,Tr10E7,Tr11E7,Tr12E7,Tr13E7,Tr14E7,Tr15E7>;
Tr:=<{i[1]:i in j}:j in Trac>;


load "Traces/E6/TracesE6.txt";
load "Traces/E6/Tr15E6Eigs";
Tr15E6:={[[&+[i^j:i in k]:j in [1,3,5]]:k in l]:l in Tr15E6Eigs};
load "Traces/E6/Tr16E6Eigs";
Tr16E6:={[EigsToTracePowers(i,16):i in j]:j in Tr16E6Eigs};
Trac:=<{[[27],[78]]},Tr2E6,Tr3E6,Tr4E6,Tr5E6,Tr6E6,Tr7E6,Tr8E6,Tr9E6,Tr10E6,Tr11E6,Tr12E6,Tr13E6,{},Tr15E6,Tr16E6>;
Tr:=<{i[1]:i in j}:j in Trac>;

load "Traces/F4/TracesF4.txt";
Trac:=<{[[26],[52]]},Tr2F4,Tr3F4,Tr4F4,Tr5F4,Tr6F4,Tr7F4,Tr8F4,Tr9F4,Tr10F4,Tr11F4,Tr12F4,Tr13F4,Tr14F4,Tr15F4,Tr16F4,Tr17F4>;
Tr:=<{i[1]:i in j}:j in Trac>;
*/
                                                                                                            