This notebook contains worked examples from Chapter 2 of the text Analytic Combinatorics in Several Variables (2nd edition) by Pemantle, Wilson and Melczer.¶
Further details can be found on the book website at https://acsvproject.com/acsvbook/ .
Example numbers match the published version.
Note: Commands involving accessing the OEIS require Sage to have access to OpenSSL, which is often not done during typical installs. Calling the OEIS is thus left as an optional parameter.
In [1]:
# We begin by defining some helper functions for below
# MAKE SURE TO RUN THIS CELL!
var('w x y z r s i j')
def GFinfo(F, z=z, exponential=False, OEIS=False):
if exponential==False:
show("This generating function has the expansion")
show(F, "=\t", F.series(z,10))
else:
pol = F.series(z,10).truncate().polynomial(QQ)
terms = [[pol[k],"\\frac{" + latex(pol[k]*factorial(k)) + "}{" + k + "!} \cdot" + latex(z^k)] for k in range(10)]
nz_terms = [st for (p,st) in terms if p != 0]
st = ''
for k in nz_terms[0:-1]: st += k + " + "
show("This exponential generating function has an expansion beginning")
show(F, "=\t", st + nz_terms[-1])
if OEIS==True:
print("The OEIS has the following entry for this starting sequence")
oeis(list(F.series(z,10).truncate().polynomial(QQ)))
def GFinfo2D(F, var1=x, var2=y, semi=False):
if semi==False:
show("The initial array of coefficients for this generating function is")
ser = QQ[var1,var2](F.taylor((var1,0), (var2,0), 17))
show(matrix([[ser[i,j] for i in range(8)] for j in range(8)]))
else:
pol = QQ[var1,var2](F.taylor((var1,0), (var2,0), 21))
terms = [[pol[k,n-k],"\\frac{" + latex(pol[k,n-k]*factorial(n-k)) + "}{" + (n-k) + "!} \cdot" + latex(var1^k*var2^(n-k))] for n in range(10) for k in range(n+1)]
nz_terms = [st for (p,st) in terms if p != 0]
st = ''
for k in nz_terms[0:-1]: st += k + " + "
show("This exponential generating function has an expansion beginning")
show(F, "=\t", st + nz_terms[-1])
Example 2.1 - Binary Sequences Avoiding 11¶
In [2]:
GFinfo(1/(1-z-z^2))
\(\displaystyle \verb|This|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|the|\verb| |\verb|expansion|\)
\(\displaystyle -\frac{1}{z^{2} + z - 1} \verb|= | 1 + 1 z + 2 z^{2} + 3 z^{3} + 5 z^{4} + 8 z^{5} + 13 z^{6} + 21 z^{7} + 34 z^{8} + 55 z^{9} + \mathcal{O}\left(z^{10}\right)\)
Example 2.2 - Binomial Coefficients¶
In [3]:
GFinfo2D(1/(1-x-y))
show("This equals the expected coefficients")
show(matrix([[binomial(i+j,i) for i in range(8)] for j in range(8)]))
\(\displaystyle \verb|The|\verb| |\verb|initial|\verb| |\verb|array|\verb| |\verb|of|\verb| |\verb|coefficients|\verb| |\verb|for|\verb| |\verb|this|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|is|\)
\(\displaystyle \left(\begin{array}{rrrrrrrr}
1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
1 & 3 & 6 & 10 & 15 & 21 & 28 & 36 \\
1 & 4 & 10 & 20 & 35 & 56 & 84 & 120 \\
1 & 5 & 15 & 35 & 70 & 126 & 210 & 330 \\
1 & 6 & 21 & 56 & 126 & 252 & 462 & 792 \\
1 & 7 & 28 & 84 & 210 & 462 & 924 & 1716 \\
1 & 8 & 36 & 120 & 330 & 792 & 1716 & 3432
\end{array}\right)\)
\(\displaystyle \verb|This|\verb| |\verb|equals|\verb| |\verb|the|\verb| |\verb|expected|\verb| |\verb|coefficients|\)
\(\displaystyle \left(\begin{array}{rrrrrrrr}
1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
1 & 3 & 6 & 10 & 15 & 21 & 28 & 36 \\
1 & 4 & 10 & 20 & 35 & 56 & 84 & 120 \\
1 & 5 & 15 & 35 & 70 & 126 & 210 & 330 \\
1 & 6 & 21 & 56 & 126 & 252 & 462 & 792 \\
1 & 7 & 28 & 84 & 210 & 462 & 924 & 1716 \\
1 & 8 & 36 & 120 & 330 & 792 & 1716 & 3432
\end{array}\right)\)
Example 2.4 - Strings by 0s and 1s¶
In [4]:
GFinfo2D(1/(1-x-x*y))
\(\displaystyle \verb|The|\verb| |\verb|initial|\verb| |\verb|array|\verb| |\verb|of|\verb| |\verb|coefficients|\verb| |\verb|for|\verb| |\verb|this|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|is|\)
\(\displaystyle \left(\begin{array}{rrrrrrrr}
1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
0 & 0 & 1 & 3 & 6 & 10 & 15 & 21 \\
0 & 0 & 0 & 1 & 4 & 10 & 20 & 35 \\
0 & 0 & 0 & 0 & 1 & 5 & 15 & 35 \\
0 & 0 & 0 & 0 & 0 & 1 & 6 & 21 \\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 7 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1
\end{array}\right)\)
Example 2.7 - Delannoy Numbers¶
In [5]:
GFinfo2D(1/(1-x-y-x*y))
\(\displaystyle \verb|The|\verb| |\verb|initial|\verb| |\verb|array|\verb| |\verb|of|\verb| |\verb|coefficients|\verb| |\verb|for|\verb| |\verb|this|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|is|\)
\(\displaystyle \left(\begin{array}{rrrrrrrr}
1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
1 & 3 & 5 & 7 & 9 & 11 & 13 & 15 \\
1 & 5 & 13 & 25 & 41 & 61 & 85 & 113 \\
1 & 7 & 25 & 63 & 129 & 231 & 377 & 575 \\
1 & 9 & 41 & 129 & 321 & 681 & 1289 & 2241 \\
1 & 11 & 61 & 231 & 681 & 1683 & 3653 & 7183 \\
1 & 13 & 85 & 377 & 1289 & 3653 & 8989 & 19825 \\
1 & 15 & 113 & 575 & 2241 & 7183 & 19825 & 48639
\end{array}\right)\)
Example 2.8 - no gaps of size 2¶
In [6]:
# Build the generating function in the book
var('x y z w')
G1 = x*y*z*w
G2 = x^2*y*z*w
G3 = x*y*z*w*(x*y+x^2*y*z)/(1-x^2*y*z)
G4 = x*y*z*w*(x*z+x^2*y*z)/(1-x^2*y*z)
G = G1+G2+G3+G4
F = (1/(1-G) - 1)/(x*y*z*w)
# Find initial terms in the expansion
show("The series expansion of the generating function")
show(F.numerator().expand()/F.denominator().expand())
show("begins \t", F.taylor(x,0,2).expand())
\(\displaystyle \verb|The|\verb| |\verb|series|\verb| |\verb|expansion|\verb| |\verb|of|\verb| |\verb|the|\verb| |\verb|generating|\verb| |\verb|function|\)
\(\displaystyle -\frac{x^{3} y z - x^{2} y z - x y - x z - x - 1}{w x^{4} y^{2} z^{2} - w x^{3} y^{2} z^{2} - w x^{2} y^{2} z - w x^{2} y z^{2} - w x^{2} y z - w x y z - x^{2} y z + 1}\)
\(\displaystyle \verb|begins|\verb| |\verb| | w^{2} x^{2} y^{2} z^{2} + 2 \, w x^{2} y^{2} z + 2 \, w x^{2} y z^{2} + 2 \, w x^{2} y z + w x y z + 2 \, x^{2} y z + x y + x z + x + 1\)
Example 2.9 - Binary Strings via a Transfer Matrix¶
In [7]:
A = Matrix([[1,1],[1,0]])
print("The matrix containing the generating functions enumerating binary strings without consecutive 1s by starting and ending symbol is")
show((matrix.identity(2) - z*A).inverse().simplify_full())
The matrix containing the generating functions enumerating binary strings without consecutive 1s by starting and ending symbol is
\(\displaystyle \left(\begin{array}{rr}
-\frac{1}{z^{2} + z - 1} & -\frac{z}{z^{2} + z - 1} \\
-\frac{z}{z^{2} + z - 1} & \frac{z - 1}{z^{2} + z - 1}
\end{array}\right)\)
Example 2.11 - Smirnov Words¶
In [8]:
dim = 4
Z = var('Z', n=dim)
F = 1/(1-add([Z[k]/(1+Z[k]) for k in range(dim)]))
print("The generating function for Smirnov words in dimension {} is".format(dim))
show(F)
print("and its series expansion at the origin begins")
R = QQ[[Z]]
show(R(F.numerator())/(R(F.denominator()) + O(R(Z[0]^5))))
The generating function for Smirnov words in dimension 4 is
\(\displaystyle -\frac{1}{\frac{Z_{0}}{Z_{0} + 1} + \frac{Z_{1}}{Z_{1} + 1} + \frac{Z_{2}}{Z_{2} + 1} + \frac{Z_{3}}{Z_{3} + 1} - 1}\)
and its series expansion at the origin begins
\(\displaystyle 1 + Z_{0} + Z_{1} + Z_{2} + Z_{3} + 2 Z_{0} Z_{1} + 2 Z_{0} Z_{2} + 2 Z_{0} Z_{3} + 2 Z_{1} Z_{2} + 2 Z_{1} Z_{3} + 2 Z_{2} Z_{3} + Z_{0}^{2} Z_{1} + Z_{0}^{2} Z_{2} + Z_{0}^{2} Z_{3} + Z_{0} Z_{1}^{2} + 6 Z_{0} Z_{1} Z_{2} + 6 Z_{0} Z_{1} Z_{3} + Z_{0} Z_{2}^{2} + 6 Z_{0} Z_{2} Z_{3} + Z_{0} Z_{3}^{2} + Z_{1}^{2} Z_{2} + Z_{1}^{2} Z_{3} + Z_{1} Z_{2}^{2} + 6 Z_{1} Z_{2} Z_{3} + Z_{1} Z_{3}^{2} + Z_{2}^{2} Z_{3} + Z_{2} Z_{3}^{2} + 2 Z_{0}^{2} Z_{1}^{2} + 6 Z_{0}^{2} Z_{1} Z_{2} + 6 Z_{0}^{2} Z_{1} Z_{3} + 2 Z_{0}^{2} Z_{2}^{2} + 6 Z_{0}^{2} Z_{2} Z_{3} + 2 Z_{0}^{2} Z_{3}^{2} + 6 Z_{0} Z_{1}^{2} Z_{2} + 6 Z_{0} Z_{1}^{2} Z_{3} + 6 Z_{0} Z_{1} Z_{2}^{2} + 24 Z_{0} Z_{1} Z_{2} Z_{3} + 6 Z_{0} Z_{1} Z_{3}^{2} + 6 Z_{0} Z_{2}^{2} Z_{3} + 6 Z_{0} Z_{2} Z_{3}^{2} + 2 Z_{1}^{2} Z_{2}^{2} + 6 Z_{1}^{2} Z_{2} Z_{3} + 2 Z_{1}^{2} Z_{3}^{2} + 6 Z_{1} Z_{2}^{2} Z_{3} + 6 Z_{1} Z_{2} Z_{3}^{2} + 2 Z_{2}^{2} Z_{3}^{2} + O(Z_{0}, Z_{1}, Z_{2}, Z_{3})^{5}\)
Example 2.14 - Catalan Numbers¶
In [9]:
C = (1 - sqrt(1-4*z))/(2*z)
print("The Catalan generating function is")
show(C)
GFinfo(C)
The Catalan generating function is
\(\displaystyle -\frac{\sqrt{-4 \, z + 1} - 1}{2 \, z}\)
\(\displaystyle \verb|This|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|the|\verb| |\verb|expansion|\)
\(\displaystyle -\frac{\sqrt{-4 \, z + 1} - 1}{2 \, z} \verb|= | 1 + 1 z + 2 z^{2} + 5 z^{3} + 14 z^{4} + 42 z^{5} + 132 z^{6} + 429 z^{7} + 1430 z^{8} + 4862 z^{9} + \mathcal{O}\left(z^{10}\right)\)
Example 2.15 - A Random Walk Game¶
In [10]:
F = 2/(1+sqrt(1-x)-y)
print("The generating function for this sequence is")
show(F)
GFinfo2D(F)
# Compute starting terms in sequence using recursion
@cached_function
def rec_a(r,s):
if (r<0 or s<0):
return 0
elif (r==0 and s==0):
return 1
else:
return (rec_a(r,s-1) + rec_a(r-1,s+1))/2
print("This matches the terms computed from the recurrence")
show(matrix([[rec_a(i,j) for i in range(8)] for j in range(8)]))
The generating function for this sequence is
\(\displaystyle -\frac{2}{y - \sqrt{-x + 1} - 1}\)
\(\displaystyle \verb|The|\verb| |\verb|initial|\verb| |\verb|array|\verb| |\verb|of|\verb| |\verb|coefficients|\verb| |\verb|for|\verb| |\verb|this|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|is|\)
\(\displaystyle \left(\begin{array}{rrrrrrrr}
1 & \frac{1}{4} & \frac{1}{8} & \frac{5}{64} & \frac{7}{128} & \frac{21}{512} & \frac{33}{1024} & \frac{429}{16384} \\
\frac{1}{2} & \frac{1}{4} & \frac{5}{32} & \frac{7}{64} & \frac{21}{256} & \frac{33}{512} & \frac{429}{8192} & \frac{715}{16384} \\
\frac{1}{4} & \frac{3}{16} & \frac{9}{64} & \frac{7}{64} & \frac{45}{512} & \frac{297}{4096} & \frac{1001}{16384} & \frac{429}{8192} \\
\frac{1}{8} & \frac{1}{8} & \frac{7}{64} & \frac{3}{32} & \frac{165}{2048} & \frac{143}{2048} & \frac{1001}{16384} & \frac{221}{4096} \\
\frac{1}{16} & \frac{5}{64} & \frac{5}{64} & \frac{75}{1024} & \frac{275}{4096} & \frac{1001}{16384} & \frac{455}{8192} & \frac{3315}{65536} \\
\frac{1}{32} & \frac{3}{64} & \frac{27}{512} & \frac{55}{1024} & \frac{429}{8192} & \frac{819}{16384} & \frac{1547}{32768} & \frac{2907}{65536} \\
\frac{1}{64} & \frac{7}{256} & \frac{35}{1024} & \frac{77}{2048} & \frac{637}{16384} & \frac{637}{16384} & \frac{2499}{65536} & \frac{4845}{131072} \\
\frac{1}{128} & \frac{1}{64} & \frac{11}{512} & \frac{13}{512} & \frac{455}{16384} & \frac{119}{4096} & \frac{969}{32768} & \frac{969}{32768}
\end{array}\right)\)
This matches the terms computed from the recurrence
\(\displaystyle \left(\begin{array}{rrrrrrrr}
1 & \frac{1}{4} & \frac{1}{8} & \frac{5}{64} & \frac{7}{128} & \frac{21}{512} & \frac{33}{1024} & \frac{429}{16384} \\
\frac{1}{2} & \frac{1}{4} & \frac{5}{32} & \frac{7}{64} & \frac{21}{256} & \frac{33}{512} & \frac{429}{8192} & \frac{715}{16384} \\
\frac{1}{4} & \frac{3}{16} & \frac{9}{64} & \frac{7}{64} & \frac{45}{512} & \frac{297}{4096} & \frac{1001}{16384} & \frac{429}{8192} \\
\frac{1}{8} & \frac{1}{8} & \frac{7}{64} & \frac{3}{32} & \frac{165}{2048} & \frac{143}{2048} & \frac{1001}{16384} & \frac{221}{4096} \\
\frac{1}{16} & \frac{5}{64} & \frac{5}{64} & \frac{75}{1024} & \frac{275}{4096} & \frac{1001}{16384} & \frac{455}{8192} & \frac{3315}{65536} \\
\frac{1}{32} & \frac{3}{64} & \frac{27}{512} & \frac{55}{1024} & \frac{429}{8192} & \frac{819}{16384} & \frac{1547}{32768} & \frac{2907}{65536} \\
\frac{1}{64} & \frac{7}{256} & \frac{35}{1024} & \frac{77}{2048} & \frac{637}{16384} & \frac{637}{16384} & \frac{2499}{65536} & \frac{4845}{131072} \\
\frac{1}{128} & \frac{1}{64} & \frac{11}{512} & \frac{13}{512} & \frac{455}{16384} & \frac{119}{4096} & \frac{969}{32768} & \frac{969}{32768}
\end{array}\right)\)
Example 2.29 - Trivariate Binomial Diagonal¶
In [11]:
F = 1/(1-x-y-z)
print("The trivariate function here is")
show(F)
print("whose series expansion begins")
R = QQ[['x,y,z']]
show(1/(R(1-x-y-z) + O(R(x^4))))
The trivariate function here is
\(\displaystyle -\frac{1}{x + y + z - 1}\)
whose series expansion begins
\(\displaystyle 1 + x + y + z + x^{2} + 2 x y + 2 x z + y^{2} + 2 y z + z^{2} + x^{3} + 3 x^{2} y + 3 x^{2} z + 3 x y^{2} + 6 x y z + 3 x z^{2} + y^{3} + 3 y^{2} z + 3 y z^{2} + z^{3} + O(x, y, z)^{4}\)
Example 2.33 - Delannoy Diagonal¶
In [12]:
F = 1/(1-x-y-x*y)
print("The sequence of interest is the main diagonal of")
show(F)
GFinfo2D(F)
print("so the diagonal sequence begins")
ser = F.taylor((x,0), (y,0),21).polynomial(QQ)
print([ser[k,k] for k in range(10)])
print("This matches the univariate generating function.")
f = 1/sqrt(1-6*y+y^2)
GFinfo(f, z=y)
The sequence of interest is the main diagonal of
\(\displaystyle -\frac{1}{x y + x + y - 1}\)
\(\displaystyle \verb|The|\verb| |\verb|initial|\verb| |\verb|array|\verb| |\verb|of|\verb| |\verb|coefficients|\verb| |\verb|for|\verb| |\verb|this|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|is|\)
\(\displaystyle \left(\begin{array}{rrrrrrrr}
1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
1 & 3 & 5 & 7 & 9 & 11 & 13 & 15 \\
1 & 5 & 13 & 25 & 41 & 61 & 85 & 113 \\
1 & 7 & 25 & 63 & 129 & 231 & 377 & 575 \\
1 & 9 & 41 & 129 & 321 & 681 & 1289 & 2241 \\
1 & 11 & 61 & 231 & 681 & 1683 & 3653 & 7183 \\
1 & 13 & 85 & 377 & 1289 & 3653 & 8989 & 19825 \\
1 & 15 & 113 & 575 & 2241 & 7183 & 19825 & 48639
\end{array}\right)\)
so the diagonal sequence begins [1, 3, 13, 63, 321, 1683, 8989, 48639, 265729, 1462563] This matches the univariate generating function.
\(\displaystyle \verb|This|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|the|\verb| |\verb|expansion|\)
\(\displaystyle \frac{1}{\sqrt{y^{2} - 6 \, y + 1}} \verb|= | 1 + 3 y + 13 y^{2} + 63 y^{3} + 321 y^{4} + 1683 y^{5} + 8989 y^{6} + 48639 y^{7} + 265729 y^{8} + 1462563 y^{9} + \mathcal{O}\left(y^{10}\right)\)
Example 2.35 - Shifted Catalan as a Diagonal¶
In [13]:
C = (1 - sqrt(1-4*z))/2
print("The Catalan generating function is")
show(C)
GFinfo(C)
print("and is a root of the polynomial")
P = y^2 - y + x
show("P(x,y) = \t", P)
F = (y^2*diff(P,y)/P).subs(x=x*y)
print("This sequence is the main diagonal of the bivariate generating function")
show(F)
GFinfo2D(F)
The Catalan generating function is
\(\displaystyle -\frac{1}{2} \, \sqrt{-4 \, z + 1} + \frac{1}{2}\)
\(\displaystyle \verb|This|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|the|\verb| |\verb|expansion|\)
\(\displaystyle -\frac{1}{2} \, \sqrt{-4 \, z + 1} + \frac{1}{2} \verb|= | 1 z + 1 z^{2} + 2 z^{3} + 5 z^{4} + 14 z^{5} + 42 z^{6} + 132 z^{7} + 429 z^{8} + 1430 z^{9} + \mathcal{O}\left(z^{10}\right)\)
and is a root of the polynomial
\(\displaystyle \verb|P(x,y)|\verb| |\verb|=|\verb| |\verb| | y^{2} + x - y\)
This sequence is the main diagonal of the bivariate generating function
\(\displaystyle \frac{{\left(2 \, y - 1\right)} y^{2}}{x y + y^{2} - y}\)
\(\displaystyle \verb|The|\verb| |\verb|initial|\verb| |\verb|array|\verb| |\verb|of|\verb| |\verb|coefficients|\verb| |\verb|for|\verb| |\verb|this|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|is|\)
\(\displaystyle \left(\begin{array}{rrrrrrrr}
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
-1 & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\
-1 & -1 & 0 & 2 & 5 & 9 & 14 & 20 \\
-1 & -2 & -2 & 0 & 5 & 14 & 28 & 48 \\
-1 & -3 & -5 & -5 & 0 & 14 & 42 & 90 \\
-1 & -4 & -9 & -14 & -14 & 0 & 42 & 132 \\
-1 & -5 & -14 & -28 & -42 & -42 & 0 & 132
\end{array}\right)\)
Example 2.37 - Narayana as a Diagonal¶
In [14]:
N = (1 + x*(y-1) - sqrt(1 - 2*x*(y+1) + x^2*(y-1)^2) )/2
print("The Narayana generating function is")
show(N)
GFinfo2D(N)
print("and the GF is a root of the polynomial")
P = w^2 - (1+x*(y-1))*w +x*y
show("P(w,x,y) = \t", P)
F = (w^2*diff(P,w)/P).subs(x=x*w)
print("This sequence is a diagonal of the trivariate generating function")
show(F)
ser = F.taylor((x,0), (y,0), (w, 0), 26).polynomial(QQ)
print("We see that the coefficients where x and y have the same power match the initial GF")
show(matrix([[ser[i,i,j] for i in range(8)] for j in range(8)]))
The Narayana generating function is
\(\displaystyle \frac{1}{2} \, x {\left(y - 1\right)} - \frac{1}{2} \, \sqrt{x^{2} {\left(y - 1\right)}^{2} - 2 \, x {\left(y + 1\right)} + 1} + \frac{1}{2}\)
\(\displaystyle \verb|The|\verb| |\verb|initial|\verb| |\verb|array|\verb| |\verb|of|\verb| |\verb|coefficients|\verb| |\verb|for|\verb| |\verb|this|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|is|\)
\(\displaystyle \left(\begin{array}{rrrrrrrr}
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
0 & 0 & 0 & 1 & 3 & 6 & 10 & 15 \\
0 & 0 & 0 & 0 & 1 & 6 & 20 & 50 \\
0 & 0 & 0 & 0 & 0 & 1 & 10 & 50 \\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 15 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{array}\right)\)
and the GF is a root of the polynomial
\(\displaystyle \verb|P(w,x,y)|\verb| |\verb|=|\verb| |\verb| | -{\left(x {\left(y - 1\right)} + 1\right)} w + w^{2} + x y\)
This sequence is a diagonal of the trivariate generating function
\(\displaystyle -\frac{{\left(w x {\left(y - 1\right)} - 2 \, w + 1\right)} w^{2}}{w x y - {\left(w x {\left(y - 1\right)} + 1\right)} w + w^{2}}\)
We see that the coefficients where x and y have the same power match the initial GF
\(\displaystyle \left(\begin{array}{rrrrrrrr}
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
0 & 0 & 0 & 1 & 3 & 6 & 10 & 15 \\
0 & 0 & 0 & 0 & 1 & 6 & 20 & 50 \\
0 & 0 & 0 & 0 & 0 & 1 & 10 & 50 \\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 15 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{array}\right)\)
Example 2.42 - Safonov Generalized Diagonal¶
In [15]:
N = 10
f = x*sqrt(1-x-y)
F = w*x+w*x*(2*w^2+2*w)/(2+w+x+x*y)
print("Start with the generating function")
show(f)
GFinfo2D(f)
print("This can be embedded into the expansion of the trivariate function")
show(F)
ser = F.taylor((x,0), (y,0), (w, 0), 40).polynomial(QQ)
print("We see that the coefficients where x and y have the same power match the initial GF")
show(matrix([[ser[i+j,i+j,j] for i in range(8)] for j in range(8)]))
Start with the generating function
\(\displaystyle x \sqrt{-x - y + 1}\)
\(\displaystyle \verb|The|\verb| |\verb|initial|\verb| |\verb|array|\verb| |\verb|of|\verb| |\verb|coefficients|\verb| |\verb|for|\verb| |\verb|this|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|is|\)
\(\displaystyle \left(\begin{array}{rrrrrrrr}
0 & 1 & -\frac{1}{2} & -\frac{1}{8} & -\frac{1}{16} & -\frac{5}{128} & -\frac{7}{256} & -\frac{21}{1024} \\
0 & -\frac{1}{2} & -\frac{1}{4} & -\frac{3}{16} & -\frac{5}{32} & -\frac{35}{256} & -\frac{63}{512} & -\frac{231}{2048} \\
0 & -\frac{1}{8} & -\frac{3}{16} & -\frac{15}{64} & -\frac{35}{128} & -\frac{315}{1024} & -\frac{693}{2048} & -\frac{3003}{8192} \\
0 & -\frac{1}{16} & -\frac{5}{32} & -\frac{35}{128} & -\frac{105}{256} & -\frac{1155}{2048} & -\frac{3003}{4096} & -\frac{15015}{16384} \\
0 & -\frac{5}{128} & -\frac{35}{256} & -\frac{315}{1024} & -\frac{1155}{2048} & -\frac{15015}{16384} & -\frac{45045}{32768} & -\frac{255255}{131072} \\
0 & -\frac{7}{256} & -\frac{63}{512} & -\frac{693}{2048} & -\frac{3003}{4096} & -\frac{45045}{32768} & -\frac{153153}{65536} & -\frac{969969}{262144} \\
0 & -\frac{21}{1024} & -\frac{231}{2048} & -\frac{3003}{8192} & -\frac{15015}{16384} & -\frac{255255}{131072} & -\frac{969969}{262144} & -\frac{6789783}{1048576} \\
0 & -\frac{33}{2048} & -\frac{429}{4096} & -\frac{6435}{16384} & -\frac{36465}{32768} & -\frac{692835}{262144} & -\frac{2909907}{524288} & -\frac{22309287}{2097152}
\end{array}\right)\)
This can be embedded into the expansion of the trivariate function
\(\displaystyle \frac{2 \, {\left(w^{2} + w\right)} w x}{x y + w + x + 2} + w x\)
We see that the coefficients where x and y have the same power match the initial GF
\(\displaystyle \left(\begin{array}{rrrrrrrr}
0 & 1 & -\frac{1}{2} & -\frac{1}{8} & -\frac{1}{16} & -\frac{5}{128} & -\frac{7}{256} & -\frac{21}{1024} \\
0 & -\frac{1}{2} & -\frac{1}{4} & -\frac{3}{16} & -\frac{5}{32} & -\frac{35}{256} & -\frac{63}{512} & -\frac{231}{2048} \\
0 & -\frac{1}{8} & -\frac{3}{16} & -\frac{15}{64} & -\frac{35}{128} & -\frac{315}{1024} & -\frac{693}{2048} & -\frac{3003}{8192} \\
0 & -\frac{1}{16} & -\frac{5}{32} & -\frac{35}{128} & -\frac{105}{256} & -\frac{1155}{2048} & -\frac{3003}{4096} & -\frac{15015}{16384} \\
0 & -\frac{5}{128} & -\frac{35}{256} & -\frac{315}{1024} & -\frac{1155}{2048} & -\frac{15015}{16384} & -\frac{45045}{32768} & -\frac{255255}{131072} \\
0 & -\frac{7}{256} & -\frac{63}{512} & -\frac{693}{2048} & -\frac{3003}{4096} & -\frac{45045}{32768} & -\frac{153153}{65536} & -\frac{969969}{262144} \\
0 & -\frac{21}{1024} & -\frac{231}{2048} & -\frac{3003}{8192} & -\frac{15015}{16384} & -\frac{255255}{131072} & -\frac{969969}{262144} & -\frac{6789783}{1048576} \\
0 & -\frac{33}{2048} & -\frac{429}{4096} & -\frac{6435}{16384} & -\frac{36465}{32768} & -\frac{692835}{262144} & -\frac{2909907}{524288} & -\frac{22309287}{2097152}
\end{array}\right)\)
Example 2.46 - Permutations EGF¶
In [16]:
GFinfo(1/(1-z), exponential=True)
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle -\frac{1}{z - 1} \verb|= | \frac{ 1 }{ 0 !} \cdot 1 + \frac{ 1 }{ 1 !} \cdot z + \frac{ 2 }{ 2 !} \cdot z^{2} + \frac{ 6 }{ 3 !} \cdot z^{3} + \frac{ 24 }{ 4 !} \cdot z^{4} + \frac{ 120 }{ 5 !} \cdot z^{5} + \frac{ 720 }{ 6 !} \cdot z^{6} + \frac{ 5040 }{ 7 !} \cdot z^{7} + \frac{ 40320 }{ 8 !} \cdot z^{8} + \frac{ 362880 }{ 9 !} \cdot z^{9}\)
Example 2.48 - Permutations by Number of Cycles (Stirling numbers of second kind)¶
In [17]:
# First examine EGF for permutations by number of cycles
GFinfo(-log(1-z), exponential=True)
# Then do bivariate semi-exponential GF for permutations by length and number of cycles
GFinfo2D((1-z)^(-y),var1=y, var2=z,semi=True)
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle -\log\left(-z + 1\right) \verb|= | \frac{ 1 }{ 1 !} \cdot z + \frac{ 1 }{ 2 !} \cdot z^{2} + \frac{ 2 }{ 3 !} \cdot z^{3} + \frac{ 6 }{ 4 !} \cdot z^{4} + \frac{ 24 }{ 5 !} \cdot z^{5} + \frac{ 120 }{ 6 !} \cdot z^{6} + \frac{ 720 }{ 7 !} \cdot z^{7} + \frac{ 5040 }{ 8 !} \cdot z^{8} + \frac{ 40320 }{ 9 !} \cdot z^{9}\)
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle \frac{1}{{\left(-z + 1\right)}^{y}} \verb|= | \frac{ 1 }{ 0 !} \cdot 1 + \frac{ 1 }{ 1 !} \cdot y z + \frac{ 1 }{ 2 !} \cdot y z^{2} + \frac{ 2 }{ 3 !} \cdot y z^{3} + \frac{ 1 }{ 2 !} \cdot y^{2} z^{2} + \frac{ 6 }{ 4 !} \cdot y z^{4} + \frac{ 3 }{ 3 !} \cdot y^{2} z^{3} + \frac{ 24 }{ 5 !} \cdot y z^{5} + \frac{ 11 }{ 4 !} \cdot y^{2} z^{4} + \frac{ 1 }{ 3 !} \cdot y^{3} z^{3} + \frac{ 120 }{ 6 !} \cdot y z^{6} + \frac{ 50 }{ 5 !} \cdot y^{2} z^{5} + \frac{ 6 }{ 4 !} \cdot y^{3} z^{4} + \frac{ 720 }{ 7 !} \cdot y z^{7} + \frac{ 274 }{ 6 !} \cdot y^{2} z^{6} + \frac{ 35 }{ 5 !} \cdot y^{3} z^{5} + \frac{ 1 }{ 4 !} \cdot y^{4} z^{4} + \frac{ 5040 }{ 8 !} \cdot y z^{8} + \frac{ 1764 }{ 7 !} \cdot y^{2} z^{7} + \frac{ 225 }{ 6 !} \cdot y^{3} z^{6} + \frac{ 10 }{ 5 !} \cdot y^{4} z^{5}\)
Example 2.49 - Involutions¶
In [18]:
GFinfo(exp(z+z^2/2),exponential=True)
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle e^{\left(\frac{1}{2} \, z^{2} + z\right)} \verb|= | \frac{ 1 }{ 0 !} \cdot 1 + \frac{ 1 }{ 1 !} \cdot z + \frac{ 2 }{ 2 !} \cdot z^{2} + \frac{ 4 }{ 3 !} \cdot z^{3} + \frac{ 10 }{ 4 !} \cdot z^{4} + \frac{ 26 }{ 5 !} \cdot z^{5} + \frac{ 76 }{ 6 !} \cdot z^{6} + \frac{ 232 }{ 7 !} \cdot z^{7} + \frac{ 764 }{ 8 !} \cdot z^{8} + \frac{ 2620 }{ 9 !} \cdot z^{9}\)
Example 2.50 - Set Partitions¶
In [19]:
F = exp(y*(exp(z) - 1))
GFinfo2D(F,var1=y, var2=z,semi=True)
print("Substituting y = 1 gives the univariate EGF for set partitions")
G = F.subs(y=1)
GFinfo(G,exponential=True)
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle e^{\left(y {\left(e^{z} - 1\right)}\right)} \verb|= | \frac{ 1 }{ 0 !} \cdot 1 + \frac{ 1 }{ 1 !} \cdot y z + \frac{ 1 }{ 2 !} \cdot y z^{2} + \frac{ 1 }{ 3 !} \cdot y z^{3} + \frac{ 1 }{ 2 !} \cdot y^{2} z^{2} + \frac{ 1 }{ 4 !} \cdot y z^{4} + \frac{ 3 }{ 3 !} \cdot y^{2} z^{3} + \frac{ 1 }{ 5 !} \cdot y z^{5} + \frac{ 7 }{ 4 !} \cdot y^{2} z^{4} + \frac{ 1 }{ 3 !} \cdot y^{3} z^{3} + \frac{ 1 }{ 6 !} \cdot y z^{6} + \frac{ 15 }{ 5 !} \cdot y^{2} z^{5} + \frac{ 6 }{ 4 !} \cdot y^{3} z^{4} + \frac{ 1 }{ 7 !} \cdot y z^{7} + \frac{ 31 }{ 6 !} \cdot y^{2} z^{6} + \frac{ 25 }{ 5 !} \cdot y^{3} z^{5} + \frac{ 1 }{ 4 !} \cdot y^{4} z^{4} + \frac{ 1 }{ 8 !} \cdot y z^{8} + \frac{ 63 }{ 7 !} \cdot y^{2} z^{7} + \frac{ 90 }{ 6 !} \cdot y^{3} z^{6} + \frac{ 10 }{ 5 !} \cdot y^{4} z^{5}\)
Substituting y = 1 gives the univariate EGF for set partitions
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle e^{\left(e^{z} - 1\right)} \verb|= | \frac{ 1 }{ 0 !} \cdot 1 + \frac{ 1 }{ 1 !} \cdot z + \frac{ 2 }{ 2 !} \cdot z^{2} + \frac{ 5 }{ 3 !} \cdot z^{3} + \frac{ 15 }{ 4 !} \cdot z^{4} + \frac{ 52 }{ 5 !} \cdot z^{5} + \frac{ 203 }{ 6 !} \cdot z^{6} + \frac{ 877 }{ 7 !} \cdot z^{7} + \frac{ 4140 }{ 8 !} \cdot z^{8} + \frac{ 21147 }{ 9 !} \cdot z^{9}\)
Example 2.51 - Partitions into Ordered Sets¶
In [20]:
F = exp(y*z/(1-z))
GFinfo2D(F,var1=y, var2=z,semi=True)
print("Substituting y = 1 gives the univariate EGF")
G = F.subs(y=1)
GFinfo(G,exponential=True)
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle e^{\left(-\frac{y z}{z - 1}\right)} \verb|= | \frac{ 1 }{ 0 !} \cdot 1 + \frac{ 1 }{ 1 !} \cdot y z + \frac{ 2 }{ 2 !} \cdot y z^{2} + \frac{ 6 }{ 3 !} \cdot y z^{3} + \frac{ 1 }{ 2 !} \cdot y^{2} z^{2} + \frac{ 24 }{ 4 !} \cdot y z^{4} + \frac{ 6 }{ 3 !} \cdot y^{2} z^{3} + \frac{ 120 }{ 5 !} \cdot y z^{5} + \frac{ 36 }{ 4 !} \cdot y^{2} z^{4} + \frac{ 1 }{ 3 !} \cdot y^{3} z^{3} + \frac{ 720 }{ 6 !} \cdot y z^{6} + \frac{ 240 }{ 5 !} \cdot y^{2} z^{5} + \frac{ 12 }{ 4 !} \cdot y^{3} z^{4} + \frac{ 5040 }{ 7 !} \cdot y z^{7} + \frac{ 1800 }{ 6 !} \cdot y^{2} z^{6} + \frac{ 120 }{ 5 !} \cdot y^{3} z^{5} + \frac{ 1 }{ 4 !} \cdot y^{4} z^{4} + \frac{ 40320 }{ 8 !} \cdot y z^{8} + \frac{ 15120 }{ 7 !} \cdot y^{2} z^{7} + \frac{ 1200 }{ 6 !} \cdot y^{3} z^{6} + \frac{ 20 }{ 5 !} \cdot y^{4} z^{5}\)
Substituting y = 1 gives the univariate EGF
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle e^{\left(-\frac{z}{z - 1}\right)} \verb|= | \frac{ 1 }{ 0 !} \cdot 1 + \frac{ 1 }{ 1 !} \cdot z + \frac{ 3 }{ 2 !} \cdot z^{2} + \frac{ 13 }{ 3 !} \cdot z^{3} + \frac{ 73 }{ 4 !} \cdot z^{4} + \frac{ 501 }{ 5 !} \cdot z^{5} + \frac{ 4051 }{ 6 !} \cdot z^{6} + \frac{ 37633 }{ 7 !} \cdot z^{7} + \frac{ 394353 }{ 8 !} \cdot z^{8} + \frac{ 4596553 }{ 9 !} \cdot z^{9}\)
Example 2.52 - 2-Regular Graphs¶
In [21]:
GFinfo(exp(-z/2-z^2/4)/sqrt(1-z),exponential=True)
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle \frac{e^{\left(-\frac{1}{4} \, z^{2} - \frac{1}{2} \, z\right)}}{\sqrt{-z + 1}} \verb|= | \frac{ 1 }{ 0 !} \cdot 1 + \frac{ 1 }{ 3 !} \cdot z^{3} + \frac{ 3 }{ 4 !} \cdot z^{4} + \frac{ 12 }{ 5 !} \cdot z^{5} + \frac{ 70 }{ 6 !} \cdot z^{6} + \frac{ 465 }{ 7 !} \cdot z^{7} + \frac{ 3507 }{ 8 !} \cdot z^{8} + \frac{ 30016 }{ 9 !} \cdot z^{9}\)
Example 2.53 - Surjections¶
In [22]:
GFinfo(1/(2-exp(z)),exponential=True)
\(\displaystyle \verb|This|\verb| |\verb|exponential|\verb| |\verb|generating|\verb| |\verb|function|\verb| |\verb|has|\verb| |\verb|an|\verb| |\verb|expansion|\verb| |\verb|beginning|\)
\(\displaystyle -\frac{1}{e^{z} - 2} \verb|= | \frac{ 1 }{ 0 !} \cdot 1 + \frac{ 1 }{ 1 !} \cdot z + \frac{ 3 }{ 2 !} \cdot z^{2} + \frac{ 13 }{ 3 !} \cdot z^{3} + \frac{ 75 }{ 4 !} \cdot z^{4} + \frac{ 541 }{ 5 !} \cdot z^{5} + \frac{ 4683 }{ 6 !} \cdot z^{6} + \frac{ 47293 }{ 7 !} \cdot z^{7} + \frac{ 545835 }{ 8 !} \cdot z^{8} + \frac{ 7087261 }{ 9 !} \cdot z^{9}\)
In [ ]: