allstr::usage="allstr[n] makes the first n-1 strings starting from A, as allstr[1] returns an empty list.";
EquivClasses::usage="EquivClasses[matrix, strings] finds the equivalence classes of strings using the matrix, returning a list of lists of strings. Two strings are related if the word they give, gives the same operator. For example, AB is the pattern A_B_, and the matrix gives an operation which takes two elements and makes another element. Hence a rank two tensor, and in this case retrieves the original matrix.";
opss::usage="opss are some interesting operators, of the form, {key, dimension}.";
StringValue::usage="StringValue[matrix,string] gives the operator value of the string, given the original binary matrix operator. It is only functional with strings of A's and B's.";
blackorder::usage="blackorder puts B's before A's, and for lists of strings, arranges them in terms of their smallest strings, again listing B's before A's. It assumes that the lists are disjoint, as is the case in Equivalence Classes.";
blacksort::usage="Sorts lists of lists of strings using blackorder.";
allstr[j_]:=Rest[Table[StringJoin[Rest[IntegerDigits[i,2]]/.{1"B",0"A"}],{i,j}]]
EquivClasses[m_,strings_]:=(Last/@#)&/@Split[Sort[#],First[#1]First[#2]&]&[{StringValue[m,#],#}&/@strings]
opss={{0,2},{8,2},{9,2},{81,3},{162,3},{2460,3},{19539,3},{2100224,4},{800768,4}}
{{0,2},{8,2},{9,2},{81,3},{162,3},{2460,3},{19539,3},{2100224,4},{800768,4}}
StringValue0[mm_,l_List]:=Module[{m=mm+1},Fold[m[[#1,#2]]&,First[l],Rest[l]]]
StringValue[mm_,str_String]:=Module[{m=mm+1,l,len=Length[mm]},Table[l=Characters[str]/.{"A"i,"B"j};Fold[m[[#1,#2]]&,First[l],Rest[l]],{i,len},{j,len}]]
blackorder[str1_String,str2_String]:=OrderedQ[Map[(Characters[#]/.{"B"0,"A"1})&,{str1,str2}]]
blackorder[a_List,b_List]:=OrderedQ[First/@Sort/@Map[(Characters[#]/.{"B"0,"A"1})&,{a,b}]]
blacksort[a_List]:=Sort[#,blackorder]&/@Sort[a,blackorder]
Clear[eqc,sv,allos,eos]
othermat2[o_]:=Module[{m=Partition[IntegerDigits[o[[1]],o[[2]],o[[2]]^2],o[[2]]]},Reverse[Reverse/@m]]
eqc[n_]:=Module[{m=othermat2[opss[[n]]]},blacksort[EquivClasses[m,allstr[60]]]]
sv[n_Integer,a_String]:=StringValue[othermat2[opss[[n]]],a]
eos[n_Integer]:=sv[n,#]&/@First/@eqc[n]
eqc2[n_,k_Integer]:=Module[{m=othermat2[opss[[n]]]},Print[MatrixForm[m]];blacksort[EquivClasses[m,allstr[k]]]]
eos2[n_Integer,k_Integer]:=sv[n,#]&/@First/@eqc2[n,k]
allos[n_Integer]:=Partition[Partition[Flatten[Nest[Flatten/@Flatten[Outer[List,#1,List/@Range[n],1],1]&,Range[n],n^2-1]],n],n]
EG4[data_]:=Module[{mx=3,allbut,noc=7},If[Length[data]>3,mx=2];allbut=Plus@@Length/@Take[data,Length[data]-1];If[allbut<7,mx=9-allbut];If[Count[data,x_/;Length[x]>2,1]>3,noc=5];GraphicsRow[If[Length[#]>noc,Append[Take[#,noc],Graphics[Text["…",{0,0}]]],#]&[Surround[GraphicsRow[If[Length[#]>mx,Append[Take[#,mx],Graphics[Text["…",{0,0}]]],#]]&[GridGraphics[{Characters[#]/.{"A".15,"B"1}}]&/@#,-.02]]&/@blacksort[data]],0]]