#restart; read `G:/My Drive/Aek/DD3Avoidance/DD3Avoid.txt`; with(combinat): # First version July 26, 2026 # Wilf class and double deficiency print(`First Written: July 2026 `): print(): print(`This is DD3Avoid.txt, A Maple package`): print(`accompanying Tipaluck Krityakierne, Thotsaporn "Aek" Thanatipanonda, and Doron Zeilberger's for an article`): print(`Generating Function of Double Deficiencies for length3-avoidance permutation. `): print(): print(`The most current version is available on WWW at:`): print(` http://sites.math.rutgers.edu/~zeilberg/tokhniot/DD3Avoid.txt .`): print(`Please report all bugs to: DoronZeil at gmail dot com .`): print(): print(`For general help, and a list of the MAIN functions,`): print(` type "ezra();". For specific help type "ezra(procedure_name);" `): print(``): print(`-------------------`): print(`For a list of the supporting functions type: ezra1();`): print(): print(`-------------------`): ezra1:=proc() if args=NULL then print(`The SUPPORTING procedures are`): print(`AvoidP, IsTherePattern, LR, RevDD, InvPi`): print(`GenDec, GenDecUp, GenDecDown, MT, MS, MyT, MyS, EN, AmyFu`): print(`GenF0, GenF1, S321, GenFill, Fill, SFill,`): print(`MA132, MA231, RevFill, RL, LRMin, Map123, MA123, MS123, A123,`): print(`Pos, Print321, Print132, Rec132A, Rec132B, Rec132C, Rec123231,`): print(``): else ezra(args): fi: end: ezra:=proc() if args=NULL then print(` DD3Avoid.txt: A Maple package for `): print(`The MAIN procedures are`): print(`Wilf, Cat, Mot, DD, GenDD, GenEmpty, GA321, A321, RecA321,`): print(`DDShift, GenShift, A132, A213, A231, Gen321A, Gen321B, Gen321C,`): print(`Gen132A, Gen132B, Gen132C, Gen123231,`): elif nargs=1 and args[1]=Wilf then print(`Wilf(n,PP): the set of permutations of length n that do not contain patterns in the set PP. Try:`): print(`[seq(nops(Wilf(n,{})),n=1..5)];`): print(`[seq(Wilf(n,{[1,2,3]}),n=1..5)];`): print(`[seq(Wilf(n,{[3,2,1],[3,1,2]}),n=1..5)];`): elif nargs=1 and args[1]= Cat then print(`Cat(n): nth Catalan numbers. Try:`): print(`[seq(Cat(n),n=0..8)]; `): print(`[seq(add(binomial(n,k)*Mot(k),k=0..n),n=0..10)];`): print(`[seq(Cat(n+1),n=0..10)];`): elif nargs=1 and args[1]= Mot then print(`Mot(n): nth Motzkin numbers. Try:`): print(`[seq(Mot(n),n=0..8)];`): elif nargs=1 and args[1]= LR then print(`List of indices of left-to-right maxima and list of elements of left-to-right maxima. Try:`): print(`LR([3,1,2,4]);`): print(`LR([4,2,1,5,3,7,6]);`): elif nargs=1 and args[1]= AvoidP then print(`AvoidP(pi,PP): A brute force function to check if a permutation contains`); print(`pattern avoidances of length 3.`); print(`Output: true if permutation pi avoids pattern in the set PP and false if pi contains pattern in PP. Try:`): print(`AvoidP([1,2,3,4],{[3,2,1]});`): elif nargs=1 and args[1]= IsTherePattern then print(`IsTherePattern(W,P): true if a list W of length 3 contains a pattern P, and false otherwise. Try:`): print(`IsTherePattern([5,2,3],[3,2,1]);`): print(`IsTherePattern([5,3,2],[3,2,1]);`): elif nargs=1 and args[1]= DD then print(`DD(pi): The set of double-deficiencies (element k such that pi(k)k>pi^(-1)(k)) of permutation pi. Try:`): print(`RevDD([1,3,2,4]);`): elif nargs=1 and args[1]= InvPi then print(`InvPi(pi): The inverse of the permutation pi. Try:`): print(`InvPi([2,1,3]);`): print(`InvPi([4,2,1,3]);`): elif nargs=1 and args[1]= GenDD then print(`GenDD(n,x,PP): Generating function of DD of permutation of length n with pattern avoidance of set PP. Try:`): print(`[seq(GenDD(n,x,{[3,2,1],[1,2,3]}),n=1..9)];`): print(`[seq(GenDD(n,x,{[3,2,1],[2,3,1]}),n=1..9)];`): elif nargs=1 and args[1]= GenDec then print(`GenDec(n,x): Generating function of DD of permutation of length n`): print(`(with no other restriction). Try:`): print(`GenDec(3,x);`): print(`seq(GenDec(n,x),n=1..8);`): print(`seq(GenDD(n,x,{}),n=1..8);`): elif nargs=1 and args[1]= GenDecUp then print(`GenDecUp(l,n,x): Generating function of DD when for permutation of length n`): print(`starts with l and follows by bigger number. Try:`): print(`n:=5: seq(GenDecUp(l,n,x),l=1..n);`): elif nargs=1 and args[1]= GenDecDown then print(`GenDecDown(l,n,x): Generating function of DD when for permutation of length n`): print(`starts with l and follows by smaller number. Try:`): print(`n:=5: seq(GenDecDown(l,n,x),l=1..n);`): elif nargs=1 and args[1]= MT then print(`MT(l,n,x): Empirical generating function of DD when starting with at least i`): print(`and the second entry must be bigger. Try:`): print(`seq(MT(i,2,x),i=1..2);`): print(`MT(3,7,x);`): print(`add(GenDecUp(i,7,x),i=3..7);`): elif nargs=1 and args[1]= MS then print(`MS(l,n,x): Empirical generating function of DD when starting with at least i`): print(`and the second entry must be smaller. Try:`): print(`seq(MS(i,2,x),i=1..2);`): print(`MS(3,7,x);`): print(`add(GenDecDown(i,7,x),i=3..7);`): elif nargs=1 and args[1]= MyT then print(`MyT(l,n,x): Faster calculation of MT with short recurrence`): print(`MyT(l,n,x)=add(U(i,n,x),i=l..n). Try:`): print(`MyT(3,15,x);`): print(`MT(3,15,x);`): elif nargs=1 and args[1]= MyS then print(`MyS(l,n,x): Faster calculation of MS with short recurrence`): print(`MyS(l,n,x)=add(D(i,n,x),i=l..n). Try:`): print(`MyS(3,15,x);`): print(`MS(3,15,x);`): elif nargs=1 and args[1]= GenEmpty then print(`GenEmpty(n,x): Fast generating function of DD for permutation of length n`): print(`(with no other restriction). Try:`): print(`GenEmpty(20,x);`): print(`GenDec(20,x);`): elif nargs=1 and args[1]= EN then print(`EN(N,y): Elizalde and Noy's Exponential `): print(`generating function for double descents. Try:`): print(`EN(9,x);`): print(`[seq(GenEmpty(n,x),n=1..9)];`): elif nargs=1 and args[1]= AmyFu then print(`AmyFu(N): Equation 1.5 of Amy Fu's paper: List of numbers of k-permutation`): print(`with no descent from 0 up to N. Try:`): print(`AmyFu(10);`): elif nargs=1 and args[1]= GenF0 then print(`GenF0(z,x): Generating function of Motzkin 2 colors. Keeping track of one of the color with z.`): print(`Solve by setting up relation. Try:`): print(`A:=GenF0(z,1);`): print(`simplify(taylor(A,z=0,17));`): print(`seq(Cat(n+1),n=0..14);`): print(`B:=GenF0(z,0);`): print(`simplify(taylor(B,z=0,17));`): print(`seq(Mot(n),n=0..14);`): elif nargs=1 and args[1]= GenF1 then print(`GenF1(z,x): Generating function of Motzkin 2 colors but horizontal move on x-axis has no color.`): print(`Keeping track of one of the color with z. Solve by setting up relation. Try:`): print(`A := GenF1(z,1);`): print(`taylor(A,z=0,17);`): print(`seq(Cat(n),n=0..16);`): print(`B := GenF1(z,0);`): print(`taylor(B,z=0,17);`): print(`seq(Mot(n),n=0..16);`): elif nargs=1 and args[1]= GA321 then print(`GA321(n,x): Generating function of Motzkin 2 colors with no color for horizontal move on x-axis.`): print(`Extract from taylor series of GenF1. Try:`): print(`seq(GA321(n,x),n=1..8);`): print(`seq(GenDD(n,x,{[3,2,1]}),n=1..8);`): elif nargs=1 and args[1]= A321 then print(`A321(n,x): Generating funciton of DD avoiding 321. Translate from relation for GenF1.`): print(`Same results as GA321 but compute with recurrence directly. Try:`): print(`A:=[seq(A321(n,x),n=1..20)];`): print(`B:=[seq(GA321(n,x),n=1..20)];`): print(`A-B;`): elif nargs=1 and args[1]= S321 then print(`S321(n,x): Helper function for A321. Compute with recurrence.`): print(`Translate from relation for GenF0. Try:`): print(`S321(5,x);`): print(`seq( S321(n,x),n=1..6);`): elif nargs=1 and args[1]= RecA321 then print(`RecA321(n,x): Same result as A321. But computed from recurrences`): print(`from holonomic guessing. Try:`): print(`RecA321(4,x); `): print(`A:=[seq(A321(n,x),n=0..80)]:`): print(`B:=[seq(RecA321(n,x),n=0..80)]:`): print(`A-B;`): elif nargs=1 and args[1]= GenFill then print(`GenFill(n,x): Empirical generating function of DD of Fill(n). Turns out to be the same as S321 (with shift in n).`): print(`G(n)=(x+1)*G(n-1)+add(G(i)*G(n-i),i=2..n-2). Try:`): print(`[seq(GenFill(n,x),n=3..8)];`): print(`[seq( S321(n,x),n=1..6)];`): elif nargs=1 and args[1]= Fill then print(`Fill(n): Prefix restriction of n-permutation with 321-avoidance. Try:`): print(`Fill(2);`): print(`Fill(4);`): print(`Fill(5);`): print(`[seq(nops(Fill(n)),n=1..7)];`): elif nargs=1 and args[1]= SFill then print(`SFill(P,L): SubFill of function Fill: Start with P and try to fill and element in L. Try:`): print(`SFill([2],{1,3,4});`): print(`SFill([2,4,3],{1}); `): elif nargs=1 and args[1]= DDShift then print(`DDShift(pi,c): Set of double deficiency with shift in position by c.`): print(`That is pi[k]k. c could be plus or minus. Try:`): print(`DDShift([4,1,5,2,3,6,7],1);`): elif nargs=1 and args[1]= GenShift then print(`GenShift(n,x,PP,c): Empirical generating function of DD with shift c.`): print(`Note that GenShift with c=0 is GenDD. Try:`): print(`GenShift(4,x,{[1,3,2]},-1);`): print(`[seq(GenShift(n,x,{[1,3,2]},-1),n=1..8)];`): elif nargs=1 and args[1]= MA132 then print(`MA132(n,c,x): Empirical generating function of DD (with shift c) from n-permutation that avoid pattern 132.`): print(`It turns out that GenDD avoid [1,3,2] is the same as GenDD avoid [2,1,3]. Try:`): print(`[seq(GenDD(n,x,{[1,3,2]}),n=1..8)];`): print(`[seq(GenDD(n,x,{[2,1,3]}),n=1..8)];`): print(`[seq(MA132(n,0,x),n=1..8)];`): elif nargs=1 and args[1]= A132 then print(`A132(n,c,x): Fast generating function of DD from n-permutation avoid pattern 132.`): print(`Compute by recurrence. Try:`): print(`[seq(MA132(n,0,x),n=1..8)];`): print(`[seq(A132(n,0,x),n=1..8)];`): elif nargs=1 and args[1]= A213 then print(`A213(n,c,x): Fast generating function of DD from n-permutation avoid pattern 213.`): print(`Compute by recurrence. Try:`): print(`[seq(A132(n,0,x),n=1..11)];`): print(`[seq(A213(n,0,x),n=1..11)];`): elif nargs=1 and args[1]= MA231 then print(`MA231(n,c,x): Empirical generating function of DD (with shift c)`): print(`from n-permutation that avoid pattern 231. Try:`): print(`MA231(5,0,x);`): print(`[seq(MA231(n,2,x),n=1..6)];`): elif nargs=1 and args[1]= A231 then print(`A231(n,c,x): Fast generating function of DD from n-permutation avoid pattern 231.`): print(`Compute by recurrence. Try:`): print(`A231(3,0,x);`): print(`A:=[seq(MA231(n,2,x),n=1..8)];`): print(`B:=[seq(A231(n,2,x),n=1..8)];`): elif nargs=1 and args[1]= RevFill then print(`RevFill(n): Prefix restriction of n-permutation with 123-avoidance`): print(`i.e. a complete block of the highest possible numbers. Try:`): print(`RevFill(3);`): elif nargs=1 and args[1]= RL then print(`RL(pi): Set of indices of right-to-left maxima`): print(`and set of elements of right-to-left maxima. Try:`): print(`RL([3,1,2,4]);`): elif nargs=1 and args[1]= LRMin then print(`LRMin(pi): Set of indices of left-to-right minima`): print(`and list of elements of left-to-right minima. Try:`): print(`LRMin([3,1,2,4]);`): elif nargs=1 and args[1]= Map123 then print(`Map123(pi,c): Still an experiment on 123: Print various data of pi [supposed from RevFill(n)]`): print(`with shift c and elements in A(n-1) in two ways: one by LRMin and another by RL. Try:`): print(` seq( Map123(pi,2), pi in RevFill(5)); `): elif nargs=1 and args[1]= MA123 then print(`MA123(n,c,x): Empirical generating function of DD (with shift c)`): print(`from n-permutation that avoid pattern 123. Try:`): print(`[seq(MA123(n,0,x),n=1..8)];`): elif nargs=1 and args[1]= MS123 then print(`MS123(n,c,x): Empirical generating function of DD (with shift c)`): print(`from an undecomposble block of size n that avoid pattern 123.`): print(`Undecomposable block is a block of {1,2,..n} without complete reverse fill before n. Try:`): print(`MS123(5,0,x);`): elif nargs=1 and args[1]= A123 then print(`Not done! Can't compute fast MS123.`): print(`A123(n,c,x): Fast generating function of DD from n-permutation`): print(`avoid pattern 123. Compute by recurrence. Try:`): print(`[seq(A123(n,0,x),n=0..8)];`): print(`[seq(MA123(n,0,x),n=0..8)];`): elif nargs=1 and args[1]= Pos then print(`Pos(k,pi): indexes of pi that has value k, i.e. pi^(-1)(k). Try:`): print(`Pos(5,[1,5,3,2,4]);`): elif nargs=1 and args[1]= Print321 then print(`Print321(n,PP): Short cut for n-permutation avoidance of {[321]} union PP `): print(`print set of permutations where number 1 is at index i, 1<=i<=n. Try:`): print(`Print321(5,{[2,3,1]});`): print(`Print321(5,{[1,3,2]});`): elif nargs=1 and args[1]= Gen321A then print(`Gen321A(n,x): Generating funciton of DD of PP={[3,2,1],[2,3,1]}.`): print(`Computed by recursive method. Try:`): print(`[seq(Gen321A(n,x),n=1..8)];`): print(`[seq(GenDD(n,x,{[3,2,1],[2,3,1]}),n=1..8)];`): print(``): print(`A:=[seq(Gen321A(n,x),n=1..28)]:`): print(`seq( simplify(A[i+2]-(x+1)*A[i+1]+(x-1)*A[i]),i=1..26);`): elif nargs=1 and args[1]= Gen321B then print(`Gen321B(n,x): Generating funciton of DD of PP={[3,2,1],[1,3,2]}.`): print(`Computed by recursive method. Try:`): print(`[seq(Gen321B(n,x),n=1..8)];`): print(`[seq(GenDD(n,x,{[3,2,1],[1,3,2]}),n=1..8)];`): print(``): print(`A:=[seq(Gen321B(n,x),n=1..28)]:`): print(`seq( simplify(A[i+5]-(x+2)*A[i+4]+2*x*A[i+3]+2*A[i+2]-(2*x+1)*A[i+1]+x*A[i]),i=1..23);`): elif nargs=1 and args[1]= Gen321C then print(`Gen321C(n,x): Generating funciton of DD of PP={[3,2,1],[2,1,3]}.`): print(`Computed by recursive method. This turns out to be the same as case PP={[3,2,1],[1,3,2]}. Try:`): print(`[seq(Gen321C(n,x),n=1..8)];`): print(`[seq(GenDD(n,x,{[3,2,1],[2,1,3]}),n=1..8)];`): elif nargs=1 and args[1]= Print132 then print(`Print132(n,PP): Short cut for n-permutation avoidance of {[132]} union PP`): print(`print set of permutations where number n is at index i, 1<=i<=n. Try:`): print(`Print132(5,{[2,3,1]});`): print(`Print132(5,{[1,2,3]});`): print(`Print132(5,{[2,1,3]});`): elif nargs=1 and args[1]= Gen132A then print(`Gen132A(n,c,x): Generating funciton of DD of PP={[1,3,2],[2,3,1]}.`): print(`Compute by recursive method. Try:`): print(`A:=[seq(Gen132A(n,2,x),n=1..9)];`): print(`B:=[seq(GenShift(n,x,{[1,3,2],[2,3,1]},2),n=1..9)];`): print(`A-B;`): print(``): print(`factor( N^3-(x+1)*N^2+(x-2)*N+2);`): print(`A:=[seq(Gen132A(n,0,x),n=1..25)]:`): print(`seq( simplify(A[i+3]-(x+1)*A[i+2]+(x-2)*A[i+1]+2*A[i]),i=1..22);`): elif nargs=1 and args[1]= Rec132A then print(`Rec132A(n,x): C-finite relation of generating funciton `): print(`of DD of PP={[1,3,2],[2,3,1]}. Try:`): print(`A := [seq(Gen132A(n,0,x),n=1..19)];`): print(`B := [seq(Rec132A(n,x),n=1..19)];`): print(`A-B;`): elif nargs=1 and args[1]= Gen132B then print(`Gen132B(n,c,x): Generating funciton of DD of PP={[1,3,2],[1,2,3]}`): print(`with shift. Compute by recursive method. Try:`): print(`[seq(Gen132B(n,2,x),n=1..8)];`): print(`[seq(GenShift(n,x,{[1,3,2],[1,2,3]},2),n=1..8)];`): print(``): elif nargs=1 and args[1]= Rec132B then print(`Rec132B(n,x): C-finite relation of generating funciton`): print(`of DD of PP={[1,3,2],[1,2,3]}. N^3=N^2+4N-4. Try:`): print(`A:=[seq(Rec132B(n,x),n=1..19)];`): print(`B:=[seq(Gen132B(n,0,x),n=1..19)];`): print(`A-B;`): elif nargs=1 and args[1]= Gen132C then print(`Gen132C(n,c,x): Generating funciton of DD of PP={[1,3,2],[2,1,3]}`): print(`with shift. Compute by recursive method. Try:`): print(`[seq(GenShift(n,x,{[1,3,2],[2,1,3]},2),n=1..8)];`): print(`[seq(Gen132C(n,2,x),n=1..8)];`): elif nargs=1 and args[1]= Rec132C then print(`Rec132C(n,x): C-finite relation of generating funciton of DD of PP={[1,3,2],[2,1,3]}.`): print(`The recurrence is not transparent: N^4-xN^3= (N^3-xN^2)+4(N^2-xN)-4(N-x). Try:`): print(`A:=[seq(Gen132C(n,0,x),n=1..28)];`): print(`B:=[seq(Rec132C(n,x),n=1..28)];`): print(`A-B;`): elif nargs=1 and args[1]= Gen123231 then print(`Gen123231(n,c,x): Generating funciton of DD of PP={[1,2,3],[2,3,1]}`): print(`with shift. Compute by recursive method. Try:`): print(`Gen123231(3,0,x);`): print(``): print(`A:=[seq(GenShift(n,x,{[2,3,1],[1,2,3]},2),n=1..8)];`): print(`B:=[seq(Gen123231(n,2,x),n=1..8)];`): print(`A-B;`): print(``): print(`A:=Gen123231(170,0,x):`): print(`B:=[seq( coeff(A,x,i),i=0..70)]:`): print(`plot([seq(i,i=0..70)],B/(binomial(170,2)+1));`): elif nargs=1 and args[1]= Rec123231 then print(`Rec123231(n,x): C-finite relation of generating funciton `): print(`of DD of PP={[1,2,3],[2,3,1]}. Try:`): print(`A:=[seq(Gen123231(n,0,x),n=1..18)];`): print(`B:=[seq(Rec123231(n,x),n=1..18)];`): print(`A-B;`): elif nargs=1 and args[1]= AA then print(`:`): print(` Try:`): print(``): print(``): print(``): fi: end: ############################ # Section 1: Pattern Avoidance # of length 3 by brute force # # Functions: # Wilf(n,PP), AvoidP(pi,PP), # IsTherePattern(W,P), # # Cat(n), Mot(n), # LR(pi), # ############################ # Output: the set of permutations # of length n that do not # contain patterns in the set PP # Try: # [seq(nops(Wilf(n,{})),n=1..5)]; # # [seq(Cat(n),n=1..8)]; # [seq(nops(Wilf(n,{[1,2,3]})),n=1..8)]; # # Wilf(5,{[3,2,1],[3,1,2]}); # [seq(nops(Wilf(n,{[3,2,1],[3,1,2]})),n=1..8)]; # [seq(2^(n-1),n=1..8)]; Wilf := proc(n,PP) option remember; local pi,T; T := {}; for pi in permute(n) do if AvoidP(pi,PP) then T := T union {pi}; fi: od: T; end: # A brute force function # to check if a permutation contains # pattern avoidances of length 3 # Output: true if permutation pi avoids pattern # in the set PP and false if pi contains pattern # in PP. # Try: # AvoidP([1,2,3,4],{[3,2,1]}); AvoidP := proc(pi,PP) option remember; local i,j,k,n,P; n := nops(pi); for i from 1 to n do for j from i+1 to n do for k from j+1 to n do if member(true, {seq(IsTherePattern([pi[i],pi[j],pi[k]],P),P in PP)}) then return(false); fi: od: od: od: return(true); end: # Output: true if a list W of length 3 # contains a pattern P, and false otherwise. # Try: # IsTherePattern([5,2,3],[3,2,1]); # IsTherePattern([5,3,2],[3,2,1]); IsTherePattern := proc(W,P) option remember; if P = [3,2,1] then if W[1]>W[2] and W[2]>W[3] then return(true); fi: elif P = [3,1,2] then if W[1]>W[3] and W[3]>W[2] then return(true); fi: elif P = [2,3,1] then if W[2]>W[1] and W[1]>W[3] then return(true); fi: elif P = [2,1,3] then if W[3]>W[1] and W[1]>W[2] then return(true); fi: elif P = [1,3,2] then if W[2]>W[3] and W[3]>W[1] then return(true); fi: elif P = [1,2,3] then if W[3]>W[2] and W[2]>W[1] then return(true); fi: else ERROR("NoCase"); fi: return(false) end: ################################################# # Output: nth Catalan numbers # Try: # [seq(Cat(n),n=0..8)]; # # [seq(add(binomial(n,k)*Mot(k),k=0..n),n=0..10)]; # [seq(Cat(n+1),n=0..10)]; Cat := proc(n) option remember; local k; if n=0 then return(1); fi: add( Cat(k-1)*Cat(n-k),k=1..n); end: # Output: nth Motzkin numbers # Try: # [seq(Mot(n),n=0..8)]; Mot := proc(n) option remember; local k; if n=0 or n=-1 then return(1); fi: Mot(n-1)+add( Mot(k-2)*Mot(n-k) ,k=2..n); end: # List of indices of left-to-right maxima # and list of elements of left-to-right maxima. # Try: # LR([3,1,2,4]); LR := proc(pi) option remember; local i,max1,Ind,V; Ind:={}; V:= {}; max1 := 0; for i from 1 to nops(pi) do if pi[i] > max1 then Ind := Ind union {i}; V := V union {pi[i]}; max1 := pi[i]; fi: od: [Ind,V]; end: ############################ # Section 2: About DD # double-deficiencies # # Functions: # DD(pi), RevDD(pi), # InvPi(pi), GenDD(n,x,PP), # ############################# # DD(pi): The set of double-deficiencies # (element k such that pi(k)k>pi^(-1)(k)) # of permutation pi. # Try: # RevDD([1,3,2,4]); RevDD:=proc(pi) local n,gu,pi1,k: n:=nops(pi): pi1:=InvPi(pi): gu:={}: for k from 1 to n do if pi[k]>k and k>pi1[k] then gu:=gu union {k}: fi: od: gu: end: # InvPi(pi): The inverse of the permutation pi. # Try: # InvPi([2,1,3]); InvPi:=proc(pi) local n,i,T: n:=nops(pi): for i from 1 to n do T[pi[i]]:=i: od: [seq(T[i],i=1..n)]: end: # Generating function of DD # from pattern avoidance SS # of length n. # Try: # [seq(GenDD(n,x,{[3,2,1],[1,2,3]}),n=1..9)]; # Trivial. GenDD becomes 0 for n>=5. # # [seq(GenDD(n,x,{[3,2,1],[2,3,1]}),n=1..8)]; # This one total is 2^(n-1) and NoDD is Fibonacci # # [seq(GenDD(n,x,{[3,2,1],[1,3,2]}),n=1..8)]; # [seq(GenDD(n,x,{[3,2,1],[2,1,3]}),n=1..8)]; # These two are the same again. # Total=[1,2,4,7,11,16,22,29] # # [seq(GenDD(n,x,{[2,3,1],[1,3,2]}),n=1..9)]; # [seq(GenDD(n,x,{[2,3,1],[2,1,3]}),n=1..8)]; # Same. This one total is 2^(n-1) and NoDD might be simple. # # [seq(GenDD(n,x,{[1,3,2],[1,2,3]}),n=1..9)]; # [seq(GenDD(n,x,{[2,1,3],[1,2,3]}),n=1..9)]; # Total 2^(n-1). Same NoDD as [132]+[213]. # But generating function is a polynomial degree 1. # # [seq(GenDD(n,1,{[1,3,2],[2,1,3]}),n=1..8)]; # Total 2^(n-1). NoDD=Number of achiral color patterns # (set partitions) in a row or cycle of length n # with 4 or fewer colors (subsets). # # [seq(GenDD(n,x,{[2,3,1],[1,2,3]}),n=1..9)]; # Total=[1,2,4,7,11,16,22,29] # Nothing interesting # C1: This one looks simple # [seq(GenDD(n,x,{[3,2,1],[2,3,1],[1,3,2]}),n=1..9)]; # [seq(GenDD(n,x,{[3,2,1],[2,3,1],[2,1,3]}),n=1..9)]; # Total=n. GenDD=x^(n-2)+x^(n-3)+...+x+2. # C2 # [seq(GenDD(n,x,{[3,2,1],[1,3,2],[2,1,3]}),n=1..9)]; # [seq(GenDD(n,x,{[2,3,1],[1,3,2],[2,1,3]}),n=1..9)]; # Total=n. Looks simple too. # C3 # [seq(GenDD(n,x,{[2,3,1],[1,3,2],[1,2,3]}),n=1..9)]; # [seq(GenDD(n,x,{[2,3,1],[2,1,3],[1,2,3]}),n=1..9)]; # Total=n. Looks simple too. # C4 # [seq(GenDD(n,x,{[1,3,2],[2,1,3],[1,2,3]}),n=1..9)]; # Total=Fibonacci. But GenDD is pol. degree 1. # Nothing interesting # D1 # [seq(GenDD(n,x,{[3,2,1],[2,3,1],[1,3,2],[2,1,3]}),n=1..9)]; # n>=3. Total=2. Perm=[123..n] and [n123...(n-1)]. # Then GenDD= x^(n-2)+1. # D2 # seq(GenDD(n,x,{[1,2,3],[2,3,1],[1,3,2],[2,1,3]}),n=1..9)]; # n>=4. Perm=[n,n-1,n-2,...,3,2,1] and [n,n-1,n-2,...,3,1,2]. # GenDD=2 for n>=4. GenDD := proc(n,x,PP) option remember; local w; sort(add( x^nops(DD(w)),w in Wilf(n,PP))); end: ################################ # Section 3: Recurrences for # generating functions # # Section 3.1 with no restriction # of pattern avoidance # # Functions: # GenDec(n,x), GenDecUp(l,n,x), # GenDeckDown(l,n,x), # # MT(l,n,x), MS(l,n,x), # MyT(l,n,x), MyS(l,n,x), # GenEmpty(n,x), # # EN(N,y), AmyFu(N), # ################################ # Generating function of DD # of permutation of length n # (no other restriction) # Try: # GenDec(3,x); # # seq(GenDec(n,x),n=1..8); # seq(GenDD(n,x,{}),n=1..8); # # [seq(GenDD(n,0,{}),n=1..7)]; # [seq(GenDec(n,0),n=1..7)]; # This is number of permutations of n # elements not containing the consecutive pattern 123. GenDec := proc(n,x) option remember; if n=0 then return(0); fi: GenDecUp(1,n+1,x); end: # Generating function of DD when # for permutation of length n # starts with l and follows by bigger number. # Try: # n:=5: seq(GenDecUp(l,n,x),l=1..n); GenDecUp := proc(l,n,x) option remember; local i,A; if n L minus {a}) and AvoidP(NP,{[3,2,1]}) then T := T union SFill( NP, L minus {a}); fi: od: return(T); end: ################################ # Section 3.3 for 132-avoidance # (or 213-avoidance) # # Functions: # DDShift(pi,c), GenShift(n,x,PP,c), # # MA132(n,c,x), A132(n,c,x), # A213(n,c,x), # ################################ # Set of double deficiency with # shift in position by c. That is # pi[k]k. # c could be plus or minus # Try: # DDShift([4,1,5,2,3,6,7],1); DDShift := proc(pi,c) option remember; local n,gu,k: n:=nops(pi): gu:={}: for k from 1 to n do if pi[k] max1 then Ind := Ind union {i}; V := V union {pi[i]}; A := [i+pi[i],op(A)]; max1 := pi[i]; fi: od: [Ind,V,A]; end: # Set of indices of left-to-right minima # and list of elements of left-to-right minima. # Try: # LRMin([3,1,2,4]); LRMin := proc(pi) option remember; local i,min1,Ind,V,n; n := nops(pi); Ind:={}; V:= []; min1 := nops(pi)+1; for i from 1 to nops(pi) do if pi[i] < min1 then Ind := Ind union {i}; V := [op(V),pi[i]]; min1 := pi[i]; fi: od: [Ind,V]; end: ############################################################## # Still an experiment on 123: Print various data of pi # [supposed from RevFill(n)] with shift c and elements # in A(n-1) in two ways; one by LRMin and another by RL. # Try: # seq( Map123(pi,2), pi in RevFill(5)); # seq( print(w,DD(w),LRMin(w),RL(w)), w in Wilf(4,{[1,2,3]})); # # seq({seq( Map123(pi,0), pi in RevFill(n))},n=2..10); # seq({seq( Map123(pi,1), pi in RevFill(n))},n=2..10); # seq({seq( Map123(pi,2), pi in RevFill(n))},n=2..10); Map123 := proc(pi,c) option remember; local i,a,n,S,T,Ind,V,A1,A2; n := nops(pi); A1 := [0$(n-1)]; S := LRMin(pi)[1]; T := LRMin(pi)[2]; for i from 1 to nops(S) do A1[S[i]] := T[i]; od: Ind := {seq(i,i=1..n-1)} minus S; V := {seq(i,i=1..n-1)} minus {op(T)}; for i from 1 to nops(Ind) do A1[Ind[i]] := V[nops(Ind)+1-i]; od: A2 := [0$(n-1)]; S := {seq(a-1, a in RL(pi)[1])}; T := sort([seq(a-1, a in RL(pi)[2])],`>`); for i from 1 to nops(S) do A2[S[i]] := T[i]; od: Ind := {seq(i,i=1..n-1)} minus S; V := {seq(i,i=1..n-1)} minus {op(T)}; for i from 1 to nops(Ind) do A2[Ind[i]] := V[nops(Ind)+1-i]; od: #return(evalb( DDShift(pi,c)= #DDShift(A1,c) union {seq(a+1, a in DDShift(A2,c))})); print( pi,DDShift(pi,c),A1,A2, DDShift(A1,c),{seq(a+1, a in DDShift(A2,c))}); #return(); end: ############################################ # Empirical generating function of DD (with shift c) # from n-permutation that avoid pattern 123. # Try: # [seq(MA123(n,0,x),n=1..8)]; MA123 := proc(n,c,x) option remember; GenShift(n,x,{[1,2,3]},c); end: # Empirical generating function of DD (with shift c) # from an undecomposble block of size n that avoid pattern 123. # Undecomposable block is a block of {1,2,..n} # without complete reverse fill before n. # Try: # MS123(5,0,x); MS123 := proc(n,c,x) option remember; local pi; sort(add(x^nops(DDShift(pi,c)),pi in RevFill(n))); end: # Not done! Can't compute fast MS123. # Fast generating function of DD from n-permutation # avoid pattern 123. Compute by recurrence. # Try: # [seq(A123(n,0,x),n=0..8)]; # [seq(MA123(n,0,x),n=0..8)]; A123 := proc(n,c,x) option remember; local i,A; if n=0 then return(1); fi: A := add(MS123(i,c-(n-i),x)*MA123(n-i,i+c,x),i=1..n); sort(expand(A)); end: ################################# # Section 4: Recurrences for # generating functions with 2 # patterns avoiding # # Section 4.1 321&231-avoidance, # 321&132-avoidance and 321&213-avoidance. # # Function: # Pos(n,pi), Print321(n,PP), # Gen321A(n,x), Gen321B(n,x), # Gen321C(n,x), # ################################## # Output: indexes of pi that has value k, i.e. pi^(-1)(k). # Try: Pos(5,[1,5,3,2,4]); Pos := proc(k,pi) option remember; local i; for i from 1 to nops(pi) do if pi[i]=k then return(i); fi: od: ERROR("NotSupposeToHappen",pi); end: # Short cut for n-permutation avoidance of {[321]} union PP # print set of permutations where number 1 is at index i, 1<=i<=n. # Try: # Print321(5,{[2,3,1]}); # Print321(5,{[1,3,2]}); Print321 := proc(n,PP) local i,w,A; A := [{}$n]; for w in Wilf(n,{[3,2,1]} union PP) do A[Pos(1,w)] := A[Pos(1,w)] union {w}; od: for i from 1 to n do print(A[i]); od: return(); end: # Generating funciton of DD of PP={[3,2,1],[2,3,1]}. # Computed by recursive method. # Try: # [seq(Gen321A(n,x),n=1..8)]; # [seq(GenDD(n,x,{[3,2,1],[2,3,1]}),n=1..8)]; # # A:=[seq(Gen321A(n,x),n=1..28)]: # seq( simplify(A[i+2]-(x+1)*A[i+1]+(x-1)*A[i]),i=1..26); Gen321A := proc(n,x) option remember; local i,A; if n=0 then return(1); fi: A := Gen321A(n-1,x)+add(x^(n-i-1)*Gen321A(i-1,x),i=1..n-1); sort(expand(A)); end: # Generating funciton of DD of PP={[3,2,1],[1,3,2]}. # Computed by recursive method. # Try: # [seq(Gen321B(n,x),n=1..8)]; # [seq(GenDD(n,x,{[3,2,1],[1,3,2]}),n=1..8)]; # # A:=[seq(Gen321B(n,x),n=1..28)]: # seq( simplify(A[i+5]-(x+2)*A[i+4]+2*x*A[i+3] # +2*A[i+2]-(2*x+1)*A[i+1]+x*A[i]),i=1..23); Gen321B := proc(n,x) option remember; local i,A; if n=0 then return(1); fi: A := Gen321B(n-1,x)+ add(x^max(0,n-2*i),i=1..n-1); #A := Gen321B(n-1,x)+ add(x^(n-2*i),i=1..floor(n/2)) #+floor((n-1)/2); sort(expand(A)); end: # Generating funciton of DD of PP={[3,2,1],[2,1,3]}. # Computed by recursive method. # This turns out to be the same as case PP={[3,2,1],[1,3,2]}. # Try: # [seq(Gen321C(n,x),n=1..8)]; # [seq(GenDD(n,x,{[3,2,1],[2,1,3]}),n=1..8)]; Gen321C := proc(n,x) option remember; local i,A; if n=0 then return(1); fi: A := Gen321C(n-1,x)+ add(x^max(0,n-2*(i-1)),i=2..n); sort(expand(A)); end: ################################ # Section 4.2 for 132&231-avoidance # and 132&123-avoidance and 132&213-avoidance. # # Function: # Print132(n,PP), # Gen132A(n,c,x), Rec132A(n,x), # Gen132B(n,c,x), Rec132B(n,x), # Gen132C(n,c,x), Rec132C(n,x), # ################################ # Short cut for n-permutation avoidance of {[132]} union PP; # print set of permutations where number n is at index i, 1<=i<=n. # Try: # Print132(5,{[2,3,1]}); # Print132(5,{[1,2,3]}); # Print132(5,{[2,1,3]}); Print132 := proc(n,PP) local i,w,A; A := [{}$n]; for w in Wilf(n,{[1,3,2]} union PP) do A[Pos(n,w)] := A[Pos(n,w)] union {w}; od: for i from 1 to n do print(A[i]); od: return(); end: # Generating funciton of DD of PP={[1,3,2],[2,3,1]}. # Compute by recursive method. # The biggest element has to be # on far left or far right. # Try: # A:=[seq(Gen132A(n,2,x),n=1..9)]; # B:=[seq(GenShift(n,x,{[1,3,2],[2,3,1]},2),n=1..9)]; # A-B; # # factor( N^3-(x+1)*N^2+(x-2)*N+2); # A:=[seq(Gen132A(n,0,x),n=1..25)]: # seq( simplify(A[i+3]-(x+1)*A[i+2] # +(x-2)*A[i+1]+2*A[i]),i=1..22); Gen132A := proc(n,c,x) option remember; local t,A; if n=0 or n=1 then return(1); fi: if member(n,{seq(c+i,i=1..n-1)}) then t:=1; else t:=0; fi: A:= Gen132A(n-1,c+1,x)+x^t*Gen132A(n-1,c,x); sort(expand(A)); end: # C-finite relation of generating funciton # of DD of PP={[1,3,2],[2,3,1]}, # Give the combinatorial explanation to # the C-finite recurrence # First half: [4,1,2,3]=x*[3,1,2] or [4,2,1,3]=x*[3,2,1]. # Second half: [312]->[54 12 3] and [54 21 3] # (+1 for position and +1 for values # and 12 or 21 always next to each other.) # [4312] -> [65 4 12 3] and [65 4 21 3]. # Try: # A := [seq(Gen132A(n,0,x),n=1..19)]; # B := [seq(Rec132A(n,x),n=1..19)]; # A-B; Rec132A := proc(n,x) option remember; local A; if n=1 or n=2 then return(n); elif n=3 then return(x+3); fi: A := Rec132A(n-1,x)+x*(Rec132A(n-1,x)-Rec132A(n-2,x)) +2*(Rec132A(n-2,x)-Rec132A(n-3,x)); sort(expand(A)); end: ###################################################### # Generating funciton of DD of PP={[1,3,2],[1,2,3]} # with shift. Compute by recursive method. # LHS of n has to be decreasing and more than RHS. # It looks like [n] or [n-1,n] or [n-1,n-2,n], ... # # Try: # [seq(Gen132B(n,2,x),n=1..8)]; # [seq(GenShift(n,x,{[1,3,2],[1,2,3]},2),n=1..8)]; Gen132B := proc(n,c,x) option remember; local i,j,t,A; if n=0 or n=1 then return(1); elif n<=c then return(2^(n-1)); fi: A := 0; for i from 1 to n do if member(n,{seq(c+j,j=1..i-1)}) then t:=1; else t:=0; fi: A := A+x^t*Gen132B(n-i,c+i,x); od: sort(expand(A)); end: # C-finite relation of generating funciton # of DD of PP={[1,3,2],[1,2,3]} # N^3=N^2+4N-4 # Try: # A:=[seq(Rec132B(n,x),n=1..19)]; # B:=[seq(Gen132B(n,0,x),n=1..19)]; # A-B; Rec132B := proc(n,x) option remember; local i,A; if n=0 or n=1 then return(1); elif n=2 then return(2); elif n=3 then return(x+3); fi: A := Rec132B(n-1,x)+4*Rec132B(n-2,x)-4*Rec132B(n-3,x); #A := 4*Rec132B(n-2,x)+x-1; sort(expand(A)); end: ######################################################## # Generating funciton of DD of PP={[1,3,2],[2,1,3]} # with shift. Compute by recursive method. # Always starts with [1,2,..n] # or [2,3,..,n] or [3,4,...,n], ... # Try: # [seq(GenShift(n,x,{[1,3,2],[2,1,3]},2),n=1..8)]; # [seq(Gen132C(n,2,x),n=1..8)]; Gen132C := proc(n,c,x) option remember; local i,t,A; if n=0 then return(1); elif n<=c then return(2^(n-1)); fi: A := 0; for i from 1 to n do if n-c < i then t := n-c; else t := 0; fi: A := A+x^t*Gen132C(n-i,c+i,x); od: #A := add( x^nops(DDShift( #[seq(n-i+j,j=1..i),seq(n-j,j=i..n-1)],c)) #*Gen132C(n-i,c+i,x),i=1..n); sort(expand(A)); end: # C-finite relation of generating funciton # of DD of PP={[1,3,2],[2,1,3]}, # The recurrence is not transparent # N^4-xN^3= (N^3-xN^2)+4(N^2-xN)-4(N-x). # Try: # B:=[seq(Rec132C(n,x),n=1..28)]; # A:=[seq(Gen132C(n,0,x),n=1..28)]; # A-B; Rec132C := proc(n,x) option remember; local i,A,C; if n=0 then return(1); elif n=1 or n=2 then return(n); elif n=3 then return(x+3); elif n=4 then return(x^2+7); fi: A := (x+1)*Rec132C(n-1,x)-(x-4)*Rec132C(n-2,x) -4*(x+1)*Rec132C(n-3,x)+4*x*Rec132C(n-4,x); sort(expand(A)); #A := add(4^(i-1)*x^(n-2*i),i=1..floor((n-1)/2)); #C := 2^(n-1)-subs(x=1,A); #sort(expand(A+C)); end: ################################ # Section 4.3 for 123&231-avoidance # # Function: # Gen123231(n,c,x), Rec123231(n,x), # ################################ # Generating funciton of DD of PP={[1,2,3],[2,3,1]} # with shift. Compute by recursive method. # Try: # Gen123231(3,0,x); # # A:=[seq(GenShift(n,x,{[2,3,1],[1,2,3]},2),n=1..8)]; # B:=[seq(Gen123231(n,2,x),n=1..8)]; # A-B; # # A:=Gen123231(170,0,x): # B:=[seq( coeff(A,x,i),i=0..70)]: # plot([seq(i,i=0..70)],B/(binomial(170,2)+1)); Gen123231 := proc(n,c,x) option remember; local i,A; if n=0 then return(1); elif n<=c then return(n*(n-1)/2+1); fi: A := add(x^min(c,i,n-c,n-i),i=1..n-1)+Gen123231(n-1,c+1,x); sort(expand(A)); end: # C-finite relation of generating funciton # of DD of PP={[1,2,3],[2,3,1]} # Try: # A:=[seq(Gen123231(n,0,x),n=1..18)]; # B:=[seq(Rec123231(n,x),n=1..18)]; # A-B; Rec123231 := proc(n,x) option remember; local T,A; T := [1,2,x+3,2*x+5,5*x+6,x^2+6*x+9, 2*x^2+10*x+10,5*x^2+10*x+14]; if n=0 then return(1); elif n<=8 then return(T[n]); fi: A := Rec123231(n-1,x)+2*Rec123231(n-2,x) +(x-2)*Rec123231(n-3,x)-(x+1)*Rec123231(n-4,x) -(2*x-1)*Rec123231(n-5,x)+2*x*Rec123231(n-6,x) +x*Rec123231(n-7,x)-x*Rec123231(n-8,x); sort(expand(A)); end: ############################################# # Fail attempt for 4321 # Functions: # Wilf4321(n), Avoid4(pi), Gen4(n,x), # Avoid 4321 # Try: # Wilf4321(4); Wilf4321 := proc(n) option remember; local pi,T; T := {}; for pi in permute(n) do if Avoid4(pi) then T := T union {pi}; fi: od: T; end: Avoid4 := proc(pi) option remember; local i,j,k,l,n,P; n := nops(pi); for i from 1 to n do for j from i+1 to n do for k from j+1 to n do for l from k+1 to n do if pi[i]>pi[j] and pi[j]>pi[k] and pi[k]>pi[l] then return(false); fi: od: od: od: od: return(true); end: # Try: # Gen4(5,x); Gen4 := proc(n,x) option remember; local w; sort(add( x^nops(DD(w)),w in Wilf4321(n))); end: