SetDirectory["C:/drorbn/AcademicPensieve/Projects/Arrow_Diagrams_and_gl(N)"];
<< "Arrow_Diagrams_and_gl(N).m"
base=10;
arbit={Diagrams[4 ar][[1]]};
For[i=1,i<17,i++,
arbit=Append[arbit,Diagrams[4 ar][[i*100]]]];
ds=Join[Take[BasisAArrow[4],All],arbit];
Load[diag_, type_String] := Module[
{fstring},
fstring=ToString[FromDigits[Cases[diag, _Integer, Infinity], base]];
Print["Loading ", fstring, " ", type," information ..."];
Last@Get["data/U"<>type<>"For"<>fstring<>".m"]
];
U[deg_, diag_] :=Expand[16 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];
Print[i];
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]