<<Combinatorica`

Get[NotebookDirectory[]<>"mathmzv2.m"];

aToN[a_List] := Join[{0}, #, {1}]&@Flatten[Join[{1}, Table[0, {#-1}]]&/@a]

nToA[a_List] := If[#[[1]] ≠ 2, {{#[[1]]-2}, Rest[#]}, Rest[#]]&@Differences[Flatten[Position[Join[{1}, a], 1]]]

zAtoiN[a_List] := (-1)^Length[a] ii[aToN[a]];

d[ii[a_List],n_Integer]/;n≥3:=Sum[il[(a[[Range[i,i+n+1]]])]*im[(a[[Join[Range[i],Range[i+n+1,Length[a]]]]])],{i,1,Length[a]-n-1}];

d[sum_Plus,n_Integer]/;n≥3:=Expand[d[#1,n]&/@sum];

d[c[i_Integer]*ii[a_List],n_Integer]/;n≥3:=c[i] d[ii[a],n];
d[l_*c[i_Integer]*ii[a_List],n_Integer]/;NumberQ[l]&&n≥3:=l c[i] d[ii[a],n];
d[l_*ii[a_List],n_Integer]/;NumberQ[l]&&n≥3:=l d[ii[a],n];

r1x[a_List, func_] := Block[{}, 
(
If[Length[a] == 2, Return[func[{0,1}]]];
If[a[[1]] == a[[-1]], Return[0]];
If[Length[Union[a[[2;;-2]]]] == 1, Return[0]];
Return[func[a]]
)];

r2x[a_List, func_] := Block[{pos,k, nk,comp,sum},(
If[Length[a] == 2, Return[func@@{0,1}]];

If[a[[1]] ≠ 0 || a[[-1]] ≠ 1, Return[func[a]]];

(* returns (k, n1, ... nr) *)
pos = Differences[Join[{2},Flatten[Position[a,1]]]];
k = pos[[1]];
nk = Rest[pos];

comp = Compositions[k, Length[nk]];

sum = (-1)^k *Sum[(Times@@(Binomial[nk[[#]] + comp[[i,#]]-1, comp[[i,#]]]&/@Range[Length[nk]]))*(func[(aToN[(nk+comp[[i]])])]), {i,1,Length[comp]}];
Return[(sum)]
)];

r3x[a_List, func_] := Block[{}, (
If[Length[a] == 2, Return[func@@{0,1}]];

If[a[[1]] > a[[-1]], Return[(-1)^Length[a]*(func[Reverse[a]])]];
Return[func[a]]
)];

r4x[a_List, func_] := Block[{}, (
If[Length[a] == 2, Return[func[{0,1}]]];

If[a[[1]] == 0 && a[[-1]] == 1 && a[[2]] == 1 && a[[-2]] == 1, Return[(-1)^Length[a] *(func[Reverse[1-a]])]];
Return[func[a]];
)];

candualx[a_List, func_] := Block[{}, (
If[Length[a] == 2, Return[func[{0,1}]]];

If[a[[1]] == 0 && a[[-1]] == 1 && a[[2]] == 1 && a[[-2]] == 0
&& FromDigits[a, 2] > FromDigits[Reverse[1-a], 2], Return[(-1)^(Length[a])func[Reverse[1-a]]]];

Return[func[a]];
)];

tozx[a_List, func_] := Block[{}, (
(-1)^(Count[a, 1]-1)*func[nToA[a]]
)];

r1[expr_] := (expr)/.{il[a_List]:>r1x[a, il],im[a_List]:>r1x[a, im]}//Expand
r2[expr_] := (expr)/.{il[a_List]:>r2x[a, il],im[a_List]:>r2x[a, im]}//Expand
r3[expr_] := (expr)/.{il[a_List]:>r3x[a, il],im[a_List]:>r3x[a, im]}//Expand
r4[expr_] := (expr)/.{il[a_List]:>r4x[a, il],im[a_List]:>r4x[a, im]}//Expand

candual[expr_] := (expr)/.{il[a_List]:>candualx[a, il],im[a_List]:>candualx[a, im]}//Expand

toz[expr_] := (expr)/.{il[a_List]:>tozx[a, zl],im[a_List]:>tozx[a, zm]}//Expand

(zA[{1,3,1,2,3,2,2}] + zA[{1,3,2,1,2,2,3}] + zA[{1,2,3,2,2,1,3}] + zA[{2,1,2,2,3,1,3}] + zA[{2,2,1,3,1,3,2}])/(Pi^14/15!)/.zA->z

out3 = (d[#, 3]//r1//r3//r2//r4//r2//candual//toz)&/@(zA[{1,3,1,2,3,2,2}] + zA[{1,3,2,1,2,2,3}] + zA[{1,2,3,2,2,1,3}] + zA[{2,1,2,2,3,1,3}] + zA[{2,2,1,3,1,3,2}]/.zA->zAtoiN)

out3 - (zA[{1,2,2,3,3}] + zA[{3,1,3,2,2}] - zA[{1,3,2,2,1,2}]-  zA[{2,2,1,2,1,3}]/.zA->zAtoiN/.ii->im//r1//r3//r2//r4//r2//candual//toz)*-3zl[{3}] +
(zA[{1,2,3,2,3}] + zA[{2,3,1,3,2}] - zA[{1,3,2,1,2,2}]-  zA[{2,1,2,2,1,3}]/.zA->zAtoiN/.ii->im//r1//r3//r2//r4//r2//candual//toz)*3zl[{3}]//Expand

(* ------- *)

zA[{1,2,2,3,3}] + zA[{3,1,3,2,2}] - zA[{1,3,2,2,1,2}]-  zA[{2,2,1,2,1,3}]/.zA->z

outb7 = (d[#,7]//r1//r3//r2//r4//r2//candual//toz)&/@(zA[{1,2,2,3,3}] + zA[{3,1,3,2,2}] - zA[{1,3,2,2,1,2}]-  zA[{2,2,1,2,1,3}]/.zA->zAtoiN)

outb7L = (outb7/.zm[{1,3}]->(zm[{2,2}]/3))/zm[{2,2}]//Expand

basis = {zl[{2}]*zl[{2,3}], zl[{2}]*zl[{3,2}], zl[{3}]*zl[{2,2}]};

Join[{outb7L} ,basis]/.zl->z//FindIntegerNullVector

outb7L - (-zl[{2}]*zl[{2,3}] - 2zl[{2}]zl[{3,2}] + 2 zl[{3}]*zl[{2,2}])/.zl->z