SetDirectory["C:/drorbn/AcademicPensieve/Projects/Arrow_Diagrams_and_gl(N)"];
<< "Arrow_Diagrams_and_gl(N).m"
base=10;

Uold[deg_, diag_] :=Expand[Plus[
UGLnBiAlg[diag] /. n^p_.*RW[l___] :> EW[n^p*FromDigits[{l}, base^2]] //. h[i_]^p_. EW[l_] :> EW[h[i]^p l],
Coproduct[diag, 1] /. T[d1_, d2_] :> Distribute[T[UGLnBiAlg[d1], UGLnBiAlg[d2]]] /.
T[c_?NumberQ * a1_, a2_] :> c*T[a1,a2] /. T[a1_, c_?NumberQ * a2_] :> c*T[a1,a2] /. T[a1_, a2_] :> EW[1, a1, a2],
Coproduct[diag, 2] /. T[d1_, d2_] :> Distribute[T[UGLnBiAlg[d1], UGLnBiAlg[d2]]] /.
T[c_?NumberQ * a1_, a2_] :> c*T[a1,a2] /. T[a1_, c_?NumberQ * a2_] :> c*T[a1,a2] /. T[a1_, a2_] :> EW[2, a1, a2]
]]

SOU[deg_, diag_] :=Expand[Plus[
USO[diag] /. n^p_.*RW[l___] :> EW[n^p*FromDigits[{l}, base^2]] //. h[i_]^p_. EW[l_] :> EW[h[i]^p l],
(*Coproduct[diag, 1] /. T[d1_, d2_] :> Distribute[T[UGLnBiAlg[d1], UGLnBiAlg[d2]]] /.
T[c_?NumberQ * a1_, a2_] :> c*T[a1,a2] /. T[a1_, c_?NumberQ * a2_] :> c*T[a1,a2] /. T[a1_, a2_] :> EW[1, a1, a2],
Coproduct[diag, 2] /. T[d1_, d2_] :> Distribute[T[UGLnBiAlg[d1], UGLnBiAlg[d2]]] /.
T[c_?NumberQ * a1_, a2_] :> c*T[a1,a2] /. T[a1_, c_?NumberQ * a2_] :> c*T[a1,a2] /. T[a1_, a2_] :> EW[2, a1, a2]*)
]]

EWs=Union[Cases[vs, _EW, Infinity]]

EWRule=Dispatch[Thread[EWs -> Array[v, {Length[EWs]}]]]

mat = SparseArray[
Join @@ Table[
t=List @@ (vs[[i]] /. EWRule);
Replace[#, c_.*v[j_] :> ({i,j} -> c)]& /@ t,
{i,Length[vs]}
],
{Length[vs], Length[EWs]}
]

ds=Take[BasisAArrow[4],All];
vs=Table[
Print[i," ",ds[[i]]];
U[4, ds[[i]]],
{i, Length[ds]}
];
Print["Finding EWs..."];
EWs=Union[Cases[vs, _EW, Infinity]];
EWRule=Dispatch[Thread[EWs -> Array[v, {Length[EWs]}]]];
Print["Computing mat..."];
Print[{Length[vs], Length[EWs]}];
mat = SparseArray[
Join @@ Table[
t=List @@ (vs[[i]] /. EWRule);
Replace[#, c_.*v[j_] :> ({i,j} -> c)]& /@ t,
{i,Length[vs]}
],
{Length[vs], Length[EWs]}
];
Print["Computing Rank..."];
MatrixRank[mat]

arbit={Diagrams[4 ar][[1]]};
For[i=1,i<17,i++,
arbit=Append[arbit,Diagrams[4 ar][[i*100]]]];

arbit

ds=Join[Take[BasisAArrow[4],All],arbit];

Length[Union[ds]]

SaveUSO[diag_Diag] := Module[
{fname},
fname= StringJoin[
"USOFor",
ToString[FromDigits[Cases[diag, _Integer, Infinity], sbase]],
".m"
];
Print["Saving ", fname, " ..."];
Put[{diag, USO[diag]},fname];
]

For[i=1,i≤Length[ds],i++,
Print[i," ",ds[[i]]];
SaveUSO[ds[[i]]]
];

SetDirectory["/scratch/leung"]

Directory[]

For[index=1,index≤Length[ds],i++,
Print[i," ",ds[[i]]];
SaveUSO[ds[[i]]]
];

For[index=45,index≤Length[ds],index++,
Print[index," ",ds[[index]]];
Timing[SaveUSO[ds[[index]]]]
];

Length[Diagrams[4 ar]]

For[index=1,index≤Length[ds],index++,
Print[index," ",ds[[index]]];
];

SetDirectory["/scratch/leung"]

Directory[]

For[index=132,index≤Length[ds],index++,
Print[index," ",ds[[index]]];
Print[Timing[SaveUSO[ds[[index]]]]]
];

Import["http://katlas.math.toronto.edu/drorbn/AcademicPensieve/Projects/Arrow_Diagrams_and_gl(N)/data/UGLFor17384562.m"]

Put[Import["http://katlas.math.toronto.edu/drorbn/AcademicPensieve/Projects/Arrow_Diagrams_and_gl(N)/data/UGLFor17384562.m"],"trial.m"]

Get["trial.m"]

For[index=1,index≤Length[ds],index++,
Print[index," ",ds[[index]]];
diag=ds[[index]];
fname= StringJoin[
"UGLFor",
ToString[FromDigits[Cases[diag, _Integer, Infinity], sbase]],
".m"
];
Put[Import["http://katlas.math.toronto.edu/drorbn/AcademicPensieve/Projects/Arrow_Diagrams_and_gl(N)/data/"<>fname],fname]
];

SaveUSO[diag_Diag] := Module[
{fname},
fname= StringJoin[
"USOFor",
ToString[FromDigits[Cases[diag, _Integer, Infinity], sbase]],
".m"
];
Print["Saving ", fname, " ..."];
Put[{diag, USO[diag]},fname];
]

base=10

Load[diag_, type_String] := Module[
{fstring},
fstring=ToString[FromDigits[Cases[diag, _Integer, Infinity], base]];
Print["Loading ", fstring, " ", type," information ..."];
Last@Get["U"<>type<>"For"<>fstring<>".m"]
]

U[deg_, diag_] :=Expand[Plus[
Load[diag, "GL"] /. n^p_.*RW[l___] :> EW[GL*n^p*FromDigits[{l}, base^2]] //. h[i_]^p_. EW[l_] :> EW[h[i]^p l],
Load[diag, "SO"] /. n^p_.*RSW[l___] :> EW[SO*n^p*FromDigits[{l}, sbase^3]] //. h[i_]^p_. EW[l_] :> EW[h[i]^p l],
Coproduct[diag, 1] /. T[d1_, d2_] :> Distribute[T[UGLnBiAlg[d1], UGLnBiAlg[d2]]] /.
T[c_?NumberQ * a1_, a2_] :> c*T[a1,a2] /. T[a1_, c_?NumberQ * a2_] :> c*T[a1,a2] /. T[a1_, a2_] :> EW[1, a1, a2],
Coproduct[diag, 2] /. T[d1_, d2_] :> Distribute[T[UGLnBiAlg[d1], UGLnBiAlg[d2]]] /.
T[c_?NumberQ * a1_, a2_] :> c*T[a1,a2] /. T[a1_, c_?NumberQ * a2_] :> c*T[a1,a2] /. T[a1_, a2_] :> EW[2, a1, a2]
]]

diags=Take[ds,All];
mat = {};
EWRules ={};
Do[
Print[{i,diags[[i]], MaxMemoryUsed[], TimeUsed[]}];
vect = U[4, diags[[i]]] /. EWRules;
Print["Length[vect] is ", Length[vect]];
EWs=Union[Cases[vect, _EW, Infinity]];
If[EWRules === {}, l=0, l=Length[EWRules[[1]]]];
Print["l is ", l];
If[EWRules === {}, 
EWRules=Dispatch[Thread[
EWs -> Table[v[j+l], {j, Length[EWs]}]
]],
EWRules=Dispatch[Join[EWRules[[1]], Thread[
EWs -> Table[v[j+l], {j, Length[EWs]}]
]]]
];
l=Length[EWRules[[1]]];
Print["l is ", l];
AppendTo[mat, 
Replace[#, c_.*v[j_] :> ({i,j} -> c)]& /@ (List @@ vect /. EWRules)
],
{i, Length[diags]}
];
mat = SparseArray[Flatten[mat], {Length[diags], l}];
Print["Computing Rank..."];
MatrixRank[mat]

U[4,ds[[1]]]

base=10

Load[ds[[1]],"GL"]

Load[ds[[1]], "GL"] /. n^p_.*RW[l___] :> EW[GL*n^p*FromDigits[{l}, base^2]] //. h[i_]^p_. EW[l_] :> EW[h[i]^p l]

(1/16) n ((h[1])^2) RW[]/. n^p_.*RW[l___] :> EW[GL*n^p*FromDigits[{l}, base^2]] //. h[i_]^p_. EW[l_] :> EW[h[i]^p l]

56 (n^2) RW[11]/. n^p_.*RW[l___] :> EW[GL*n^p*FromDigits[{l}, base^2]] //. h[i_]^p_. EW[l_] :> EW[h[i]^p l]

Last[Load[ds[[1]], "GL"] ]

24 (n^8) RW[18,27,36,45,54,63,72,81]/. n^p_.*RW[l___] :> EW[GL*n^p*FromDigits[{l}, base^2]] //. h[i_]^p_. EW[l_] :> EW[h[i]^p l]

diag=ds[[1]]

List@@Expand[(Times @@ diag) ]

diags=Take[ds,All];
mat = {};
EWRules ={};
Do[
Print[{i,diags[[i]], MaxMemoryUsed[], TimeUsed[]}];
vect = U[4, diags[[i]]] /. EWRules;
Print["Length[vect] is ", Length[vect]];
EWs=Union[Cases[vect, _EW, Infinity]];
If[EWRules === {}, l=0, l=Length[EWRules[[1]]]];
Print["l is ", l];
If[EWRules === {}, 
EWRules=Dispatch[Thread[
EWs -> Table[v[j+l], {j, Length[EWs]}]
]],
EWRules=Dispatch[Join[EWRules[[1]], Thread[
EWs -> Table[v[j+l], {j, Length[EWs]}]
]]]
];
l=Length[EWRules[[1]]];
Print["l is ", l];
AppendTo[mat, 
Replace[#, c_.*v[j_] :> ({i,j} -> c)]& /@ (List @@ vect /. EWRules)
],
{i, Length[diags]}
];
mat = SparseArray[Flatten[mat], {Length[diags], l}];
Print["Computing Rank..."];
MatrixRank[mat]
NullSpace[Transpose[mat]]

base=10