###################################################################### ## Decoupage.txt Save this file as Decoupage.txt to use it, # # stay in the # ## same directory, get into Maple (by typing: maple ) # ## and then type: read `Decoupage.txt` # ## Then follow the instructions given there # ## # ## Written by Doron Zeilberger, Rutgers University , # # and Robert Dougherty-Bliss, Darmouth College # # and Natalya Ter-Saakov , Rutgers University , # ## DoronZeil at gmail dot com # ###################################################################### print(`First Written: Sept. 10, 2025: tested for Maple 2020 `): print(` This Version : April 14, 2026`): print(): print(`This is Decoupage.txt, A Maple package`): print(`accompanying Robert Dougherty-Bliss, Natalya Ter-Saakov and Doron Zeilberger's article: `): print(` Cutting 4 by n grids into two congruent pieces`): print(` http://sites.math.rutgers.edu/~zeilberg/tokhniot/mamarim/mamarimhtml/decoupage.html `): print(): print(`The most current version is available on WWW at:`): print(` http://sites.math.rutgers.edu/~zeilberg/tokhniot/Decoupage.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(`For a list of the supporting functions type: ezra1();`): print(): print(`-------------------`): ezraS:=proc() if args=NULL then print(`The Story procedures are: PM, PMa ` ): print(``): else ezra(args): fi: end: ezra1:=proc() if args=NULL then print(`The SUPPORTING procedures are: BV, CC, countM4n, Images, IsGM, countM4na, Good2ndRows, Mat4na, Natasha, , Story1, Story2, Tuples `): else ezra(args): fi: end: ezra:=proc() if args=NULL then print(` Decoupage.txt: A Maple package for studying ways to cut a a 4 by n rectangle into two congruent connected pieces`): print(`The MAIN procedures are: GF4, Mat4n, `): print(``): elif nargs=1 and args[1]=BV then print(`BV(n): all the 0-1 lists of length n. Try:`): print(`BV(6);`): elif nargs=1 and args[1]=CC then print(`CC(G,v0): the connected component of vertex v0 in the graph G=[V,E]. Try`): print(`CC([{1,2,3,4},{{1,2},{1,3},{3,4}}],1);`): elif nargs=1 and args[1]=GF4 then print(`GF4(x): The generating function for the number of cutting a 4 by n rectangle into two connected congruent pieces. Try:`): print(`GF4(x);`): elif nargs=1 and args[1]=countM4n then print(`countM4n(n): The number of ways to cut a 4 by n rectangle into two connected congruent pieces. Try:`): print(`countM4n(20)`): elif nargs=1 and args[1]=Good2ndRows then print(`Good2ndRows(n,a): the binary bectores of length n-1 such that if you append 1 you get a good matrix. Try:`): print(`Good2ndRows(10,0);`): elif nargs=1 and args[1]=Images then print(`Images(M): Given a 0-1 matrix, finds all its images under 0-1 switch, and reflection.`): print(`Try(Images([[0,1,0,1],[1,0,1,1]]):`): elif nargs=1 and args[1]=IsGM then print(`IsGM(M): Is the k by n 0-1 matrix a good one? i.e. are the 0's all connected and all the 1's all connected?. Try:`): print(`IsGM([[1,0,0],[0,0,1]]);`): elif nargs=1 and args[1]=Mat4n then print(`Mat4n(n): all 4 by n 0-1 matrices such that the 0-regions and 1-regions are both connected and they are congruent to each other.CONSIDERING SYMMETRY. Try:`): print(`Mat4n(6);`): elif nargs=1 and args[1]=Mat4nO then print(`Mat4nO(n): all 4 by n 0-1 matrices such that the 0-regions and 1-regions are both connected and they are congruent to each other. BEFORE CONSIDERING SYMMETRY. Try:`): print(`Mat4nO(10);`): elif nargs=1 and args[1]=Mat4na then print(`Mat4na(n,a): all 4 by n 0-1 matrices with a 0s at the top (and hence n-a at the bottom)`): print(`that can be cut into two congruent connected pieces. BEFORE CONSIDERING SYMMETRYCTry:`): print(`Mat4na(10,0);`): elif nargs=1 and args[1]=Mat4naA then print(`Mat4naA(n,a): Like Mat4na(n,a) but the matrices are cut in half, since the second half is implied. BEFORE CONSIDERING SYMMETRY. Try:`): print(`Mat4naA(10,0);`): elif nargs=1 and args[1]=Natasha then print(`Natasha(v): inputs a 0-1 vector and outputs its reflected complement. Try:`): print(`Natasha([0,1,1,0]);`): elif nargs=1 and args[1]=PM then print(`PM(K): prints all the Graham matrices for n<=K (except n=4). Try:`): print(`PM(10);`): elif nargs=1 and args[1]=PMa then print(`PMa(K): prints all the ABBREVIATED good matrices for n<=K. Try:`): print(`PMa(10);`): elif nargs=1 and args[1]=Story1 then print(`Story1(n,a): What possible letters can occur in the i-th location of a good matrix for i<=trunc(n/2). Try:`): print(`Story1(10,0);`): elif nargs=1 and args[1]=Story2 then print(`Story1(n,a): What possible edges can occur between the i-th location and (i+1)-location of a good matrix for i<=trunc(n/2)-1. Try:`): print(`Story2(10,0);`): elif nargs=1 and args[1]=Tuples then print(`Tuples(n,a,i,k): The set of {seq([op(i..i+k,x], x in Mat4naA(n,a))}: Try:`): print(`Tuples(10,0,1,0);`): else print(`There is no such thing as`, args): fi: end: ez:=proc(): print(` PM(K), Tuples(n,a,i,j), Story1(n,a), Story2(n,a) `): end: #start from Pegden #CC(G,v0): the connected component of vertex v0 in the graph G=[V,E] CC:=proc(G,v0) local V,E,N,v,e,v1,S,S1,S2: V:=G[1]: E:=G[2]: if not member(v0,V) then RETURN(FAIL): fi: for v in V do N[v]:={}: od: for e in E do N[e[1]]:=N[e[1]] union {e[2]}: N[e[2]]:=N[e[2]] union {e[1]}: od: S:={v0}: S1:=S union N[v0]: while S<>S1 do S2:=S1 union {seq(op(N[v1]),v1 in S1)}: S:=S1: S1:=S2: od: S1: end: #IsCon(G): is the graph G=[V,E] connected?. Try: #IsCon([{1,2,3},{{1,2},{1,3}}]); IsCon:=proc(G) local V: V:=G[1]: if V={} then RETURN(true): fi: evalb(CC(G,V[1])=V): end: #GG(k,n): the grid graph for a k by n square returns [Vertices, Edges]. Try: #GG(2,4); GG:=proc(k,n) local V,E,i,j: V:={seq(seq([i,j],j=1..n),i=1..k)}: E:={seq(seq({[i,j],[i+1,j]},i=1..k-1),j=1..n)} union {seq(seq({[i,j],[i,j+1]},i=1..k),j=1..n-1)}: [V,E]: end: #IndG(G,V1): Given a graph G=[V,E] and a subset V1 of V outputs the innduced graph. Try #IndG([{1,2,3,4},{{1,2},{1,3},{1,4},{2,3}}],{1,3}); IndG:=proc(G,V1) local V,E,e,E1: V:=G[1]: E:=G[2]: if V1 minus V<>{} then RETURN(FAIL): fi: E1:={}: for e in E do if e minus V1={} then E1:=E1 union {e}: fi: od: [V1,E1]: end: #IsGM(M): Is the k by n 0-1 matrix a good one? i.e. are the 0's all connected and all the 1's all connected? IsGM:=proc(M) local n,k,G,V,V0,V1,v: k:=nops(M): n:=nops(M[1]): G:=GG(k,n): V:=G[1]: V0:={}: V1:={}: for v in V do if M[v[1]][v[2]]=0 then V0:=V0 union {v}: elif M[v[1]][v[2]]=1 then V1:=V1 union {v}: fi: od: IsCon(IndG(G,V0)) and IsCon(IndG(G,V1)): end: #end from Pegden BV:=proc(n) local S,s: option remember: if n=0 then RETURN({[]}): fi: S:=BV(n-1): {seq([op(s),0],s in S), seq([op(s),1],s in S)}: end: #Natasha(v): inputs a 0-1 vector and outputs its reflected complement Natasha:=proc(v) local n,i: n:=nops(v): [seq(1-v[n+1-i],i=1..n)]: end: #Mat4na(n,a): all 4 by n 0-1 matrices with a 0s at the top (and hence n-a at the bottom) and all possible 2nd rows #and the implied third row. Try: #Mat4na(10,0); Mat4na:=proc(n,a) local S,gu, gu1,Ho,ho,i1: option remember: if a>n/2 then RETURN({}): fi: S[1]:=[0$a,1$(n-a)]: S[4]:=[0$(n-a),1$a]: gu:=BV(n-1): gu:=[seq([op(gu1),1],gu1 in gu)]: Ho:={}: for i1 from 1 to nops(gu) do S[2]:=gu[i1]: S[3]:=Natasha(S[2]): ho:=[S[1],S[2],S[3],S[4]]: if IsGM(ho) then Ho:=Ho union {ho}: fi: od: Ho: end: #Mat4nO(n): all 4 by n 0-1 matrices such that the 0-regions and 1-regions are both connected and they are congruent to each other. Try: #Mat4nO(10); Mat4nO:=proc(n) local a: option remember: {seq(op(Mat4na(n,a)),a=0..trunc(n/2))}: end: #PMold(K): prints all the good matrices for n<=K. Try: #PMold(10); PMold:=proc(K) local x,n,a,gu,gu1,lu,lu1: lu:=GF4(x): lu1:=taylor(lu,x=0, K+3): print(`All the 4 by n 0-1 matrices where the 0-part and 1-part are connected and congruent to each other (up to symmetry) `): print(`By symmetry we can assume that the top-rightmost enrty, i.e. the (1,n) entry is a 1, we will only list these `): for n from 2 to K do print(`There are `, coeff(lu1,x,n), `such 4-rowed 0-1 matrices with`, n, `columns `): print(``): print(`The first row has consecutive zeroes followed by consecutive ones`): print(`The number of zeroes at the top row is between 0 and`, trunc(n/2)): print(``): print(`We will now list them accordingly`): for a from 0 to trunc(n/2) do gu:=Mat4na(n,a): print(`The number of 4 by`, n, `such good 0-1 matrices starting with `, a, `zeroes at the top row followed by`, n-a, `ones is `, nops(gu)): print(`Here they are `): for gu1 in gu do print(``): print(`--------------------`): print(``): print(matrix(gu1)): od: print(``): print(`----------------------------`): print(``): od: od: end: #PMa(K): prints all the ABBREVIATED good matrices for n<=K. Try: #PMa(10); PMa:=proc(K) local n,a,gu,gu1: for n from 2 to K do for a from 0 to trunc(n/2) do print(`n=`, n, `a= `, a): gu:=Mat4naA(n,a): for gu1 in gu do print(``): print(`--------------------`): print(``): print(matrix(gu1)): od: od: od: end: #Tra(M): The transpose of the matrix M Tra:=proc(M) local i,j: [seq([seq(M[j][i],j=1..nops(M))],i=1..nops(M[1]))]: end: Mat4naA:=proc(n,a) local gu,gu1,lu,lu1: gu:=Mat4na(n,a): lu:={}: for gu1 in gu do lu1:=Tra(gu1): lu1:=[op(1..trunc(n/2),lu1)]: lu:=lu union {lu1}: od: lu: end: #Tuples(n,a,i,k): The set of {seq([op(i..i+k,x], x in Mat4naA(n,a))}: Try: #Tuples(10,0,1,0); Tuples:=proc(n,a,i,k) local gu,x: gu:=Mat4naA(n,a): {seq([op(i..i+k,x)], x in gu)}: end: #Story1(n,a): What possible letters can occur in the i-th location of a good matrix for i<=trunc(n/2). Try: #Story1(10,0); Story1:=proc(n,a) local i: print(`For the creatuers with n=`,n, `a=`, a): for i from 1 to trunc(n/2) do print(`The `, i, `-th letter may be `): lprint(Tuples(n,a,i,0)): od: end: #Story1(n,a): What possible edges can occur between the i-th location and the (i+1)-th of a good matrix for i<=trunc(n/2)-1. Try: #Story2(10,0); #Story2(n,a): Story2:=proc(n,a) local i: print(`For the creatuers with n=`,n, `a=`, a): for i from 1 to trunc(n/2)-1 do print(`----------------------`): print(``): print(`The `, [i,i+1], `-th letters may be `): lprint(Tuples(n,a,i,1)): od: end: #Good2ndRows(n,a): the binary bectores of length n-1 such that if you append 1 you get a good matrix. Try: #Good2ndRows(10,0); Good2ndRows:=proc(n,a) local S,gu, gu1,Ho,ho,i1: option remember: if a>n/2 then RETURN({}): fi: S[1]:=[0$a,1$(n-a)]: S[4]:=[0$(n-a),1$a]: gu:=BV(n-1): gu:=[seq([op(gu1),1],gu1 in gu)]: Ho:={}: for i1 from 1 to nops(gu) do S[2]:=gu[i1]: S[3]:=Natasha(S[2]): ho:=[S[1],S[2],S[3],S[4]]: if IsGM(ho) then Ho:=Ho union {[op(1..n-1,gu[i1])]}: fi: od: Ho: end: #GF4(x): The generating function for the number of cutting a 4 by n rectangle into two connected congruent pieces. Try: #GF4(x); GF4:=proc(x): TransferMatrixMethods[grahamGF](x): end: countM4n:=proc(n) TransferMatrixMethods[count_graham](n): end: L1 := [(x^6+x^5+x^3-3*x^2+1)/(x-1)/(x^4+3*x^2-1),-x^2*(x+1)^2/(x^4+3*x^2-1),x^4*(x^5-x^3-4*x^2+2*x+3)/(x-1)/(x^4+3*x^2-1),x^6*(3*x^5+x^4-3*x^3-9*x^2+5*x+7)/(x-1)/(x^4+3*x^2-1), x^8*(8*x^5+3*x^4-7*x^3-22*x^2+12*x+17)/(x-1)/(x^4+3*x^2-1), x^10*(20*x^5+8*x^4-17*x^3-53*x^2+29*x+41)/(x-1)/(x^4+3*x^2-1)]: ### start of transfer matrix methods # this is wrapped in a module because the transfer matrix states are annoying # maple symbols. the exported methods can be used directly, or via the # convenience functions defined above. TransferMatrixMethods := module() uses LinearAlgebra, gfun, GraphTheory; local first_states, first_edges, first_start, first_stop, first_even_stop, first_transfer_matrix, second_states, second_edges, second_start, second_stop, second_even_stop, second_transfer_matrix, count_walks: export first_graph, second_graph, adj1, adj, count, countA, countGF, countGFA, adj2, transfer_matrix, count_graham, grahamGF: first_states := [e,d,t,m,a,b,g,g_start,wa,wb]: first_edges := {[e, e], [e, d], [e, t], [e, m], [e, g], [e, b], [e, a], [e, wb], [d, d], [d, b], [d, g], [d, m], [t, t], [t, m], [t, b], [t, wb], [m, m], [m, d], [m, t], [m, b], [m, g], [a, a], [a, wb], [a, g], [a, b], [wb, wb], [wb, b], [wa, wa], [wa, a], [g, g], [g, b], [g, a], [b, b], [b, a], [b, g], [b, wa], [g_start, g_start], [g_start, a]}: first_start := {e, a, g_start}: first_stop := {wb, g, wa, g_start}: first_even_stop := {a, b}: first_graph := Digraph(first_states, first_edges): first_transfer_matrix := AdjacencyMatrix(first_graph): second_states := [m_start, e, d, m, t]: second_edges := {[e, e], [e, d], [e, m_start], [d, d], [d, m], [m_start, m_start], [m_start, d], [t, t], [t, m], [m, m], [m, d], [m, t]}: second_start := {e}: second_stop := {}: second_even_stop := {e, d, t}: second_graph := Digraph(second_states, second_edges): second_transfer_matrix := AdjacencyMatrix(second_graph): transfer_matrix := DiagonalMatrix([first_transfer_matrix, second_transfer_matrix]): count_walks := proc(G, starts, stops, n) local A, pow, total, start, i, final, j: A := AdjacencyMatrix(G): pow := MatrixPower(A, n): total := 0: for start in starts do i := ListTools[Search](start, Vertices(G)): for final in stops do j := ListTools[Search](final, Vertices(G)): total := total + pow[i, j]: od: od: total: end: count_graham := proc(n) local stop_set, total, new: # n = 4 is a square, which is a bit of an edge case. if n = 4 then return 6: fi: stop_set := first_stop: if n mod 2 = 0 then stop_set := stop_set union first_even_stop: fi: total := count_walks(first_graph, first_start, stop_set, ceil(n/2)-1): if n mod 2 = 0 then new := count_walks(second_graph, second_start, second_even_stop, ceil(n/2)-1): total := total + new: fi: return total: end: # return the generating function for the number of walks from starts to # stops of length n. countGF := proc(G, starts, stops, x) local A, A_gf, gf, s, i, t, j: A := AdjacencyMatrix(G): A_gf := (1 - A * x)^(-1): gf := 0: for s in starts do i := ListTools[Search](s, Vertices(G)): if i = 0 then fi: for t in stops do j := ListTools[Search](t, Vertices(G)): if j = 0 then fi: gf := gf + A_gf[i, j]: end: end: gf: end: # return the generating function for the number of 4xn Graham matrices. grahamGF := proc(x) local first_all_gf, first_even_gf, first_gf, second_even_gf, second_gf, gf, fourth_term: first_all_gf := countGF(first_graph, first_start, first_stop, x): first_even_gf := countGF(first_graph, first_start, first_even_stop, x): first_gf := x^2 * subs(x = x^2, first_all_gf + first_even_gf) + x * subs(x=x^2, first_all_gf): second_even_gf := countGF(second_graph, second_start, second_even_stop, x): second_gf := x^2 * subs(x = x^2, second_even_gf): gf := first_gf + second_gf: # n = 4 has additional symmetries not accounted for in our state machine. fourth_term := coeff(series(gf, x, 5), x, 4) * x^4: gf := gf - fourth_term + 6 * x^4: factor(gf): end: end: #RefH(M): reflection around the middle RefH:=proc(M) local i: [seq(M[5-i],i=1..4)]: end: #RefV1(v) RefV1:=proc(v) local i,k: k:=nops(v): [seq(v[k+1-i],i=1..k)]: end: #RefV(M) : vertical reflection of M RefV:=proc(M) local i: [seq(RefV1(M[i]),i=1..nops(M))]: end: Comp:=proc(M): subs({0=1,1=0},M): end: #Images(M): Given a 0-1 matrix, finds all its images under 0-1 switch, and reflection. #Try(Images([[0,1,0,1],[1,0,1,1]]): Images:=proc(M) local M1,gu1,gu2,gu3: gu1:={M}: gu2:=gu1 union {seq(RefV(M1), M1 in gu1), seq(RefH(M1), M1 in gu1), seq(Comp(M1),M1 in gu1)}: while gu2<>gu1 do gu3:=gu1 union {seq(RefV(M1), M1 in gu2), seq(RefH(M1), M1 in gu2), seq(Comp(M1),M1 in gu2)}: gu1:=gu2: gu2:=gu3: od: gu3: end: #Mat4n(N): all the good 4 by b Graham matrices (up to reflections and 0-1 switch) Mat4n:=proc(n) local mu,gu,a: mu:=Mat4nO(n): gu:={}: while mu<>{} do a:=mu[1]: gu:=gu union {a}: mu:=mu minus Images(a): od: gu: end: #PM(K): prints all the Graham matrices for n<=K. Try: #PM(10); PM:=proc(K) local x,n,lu,lu1,gu,i,t0: t0:=time(): lu:=GF4(x): lu1:=taylor(lu,x=0, K+3): print(`All the 4 by n 0-1 matrices where the 0-part and 1-part are connected and congruent to each other (up to symmetry) for all n up to`, K, ` except n=4 `): for n from 2 to K do print(``): print(`---------------------------------`): if n<>4 then gu:=Mat4n(n): if nops(gu)<>coeff(lu1,x,n) then print(`something bad happened`): RETURN(FAIL): fi: print(`There are`, nops(gu), `4 by `, n, `Graham matrices (up to symmetry). Here they are `): print(``): for i from 1 to nops(gu) do print(``): print(matrix(gu[i])): print(``): od: fi: od: print(`------------------------`): print(``): print(`This ends this article, that took`, time()-t0, `to generate. `): end: