This notebook contains worked examples from Chapter 3 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.
In [1]:
# MAKE SURE TO RUN THIS CELL!
var('w x y z r s i j')
Out[1]:
(w, x, y, z, r, s, i, j)
Asymptotic Expansions in Sage¶
In [2]:
# Sage has good built-in support for (univariate) asymptotics through the AsymptoticRing structure
SCR = SR.subring(no_variables=True) # Create symbolic ring with no variables
A.<n> = AsymptoticRing(
growth_group='QQ^n * n^QQ * log(n)^QQ',
coefficient_ring=SCR,
default_prec=4
)
A
Out[2]:
Asymptotic Ring <QQ^n * n^QQ * log(n)^QQ * Signs^n> over Symbolic Constants Subring
In [3]:
# When n generates an AsymptoticRing, Sage automatically computes expansions
show(SR("sqrt(1+n)"), " = \t", sqrt(1+n))
show(SR("(1+1/n)^n"), " = \t", (1+1/n)^n)
show(SR("log(1+n)"), " = \t", log(1+n))
\(\displaystyle \sqrt{n + 1} \verb| |\verb|=|\verb| |\verb| | n^{\frac{1}{2}} + \frac{1}{2} n^{-\frac{1}{2}} - \frac{1}{8} n^{-\frac{3}{2}} + \frac{1}{16} n^{-\frac{5}{2}} + O\!\left(n^{-\frac{7}{2}}\right)\)
\(\displaystyle {\left(\frac{1}{n} + 1\right)}^{n} \verb| |\verb|=|\verb| |\verb| | e - \frac{1}{2} \, e n^{-1} + \frac{11}{24} \, e n^{-2} - \frac{7}{16} \, e n^{-3} + O\!\left(n^{-4}\right)\)
\(\displaystyle \log\left(n + 1\right) \verb| |\verb|=|\verb| |\verb| | \log\left(n\right) + n^{-1} - \frac{1}{2} n^{-2} + \frac{1}{3} n^{-3} - \frac{1}{4} n^{-4} + O\!\left(n^{-5}\right)\)
In [4]:
# The package can compute asymptotic expansions of meromorphic functions from its singularities
# using the results discussed in this chapter
def GF(z):
return 1/((1-2*z)^2*(1+z))
asm = A.coefficients_of_generating_function(
function=GF,
singularities=(1/2,),
precision=3
)
show("[z^n]", SR("1/((1-2*z)^2*(1+z))"), " = \t", asm)
\(\displaystyle \verb|[z^n]| \frac{1}{{\left(2 \, z - 1\right)}^{2} {\left(z + 1\right)}} \verb| |\verb|=|\verb| |\verb| | \frac{2}{3} 2^{n} n + \frac{8}{9} 2^{n} + O\!\left(2^{n} n^{-2}\right)\)
In [5]:
# It can also compute expansions near logarithmic and algebraic singularities using transfer theorems
def harmonic(z):
return - log(1 - z) / (1 - z)
asm = A.coefficients_of_generating_function(
function=harmonic,
singularities=(1,),
precision=10
)
show("[z^n]", SR("- log(1 - z) / (1 - z)"), " = \t", asm)
def catalan(z):
return (1-sqrt(1-4*z))/(2*z)
asm = A.coefficients_of_generating_function(
function=catalan,
singularities=(1/4,),
precision=3
)
show("[z^n]", SR("(1-sqrt(1-4*z))/(2*z)"), " = \t", asm)
\(\displaystyle \verb|[z^n]| \frac{\log\left(-z + 1\right)}{z - 1} \verb| |\verb|=|\verb| |\verb| | \log\left(n\right) + \gamma_E + \frac{1}{2} n^{-1} - \frac{1}{12} n^{-2} + \frac{1}{120} n^{-4} + O\!\left(n^{-5} \log\left(n\right)\right)\)
\(\displaystyle \verb|[z^n]| -\frac{\sqrt{-4 \, z + 1} - 1}{2 \, z} \verb| |\verb|=|\verb| |\verb| | \frac{1}{\sqrt{\pi}} 4^{n} n^{-\frac{3}{2}} - \frac{9}{8 \, \sqrt{\pi}} 4^{n} n^{-\frac{5}{2}} + \frac{145}{128 \, \sqrt{\pi}} 4^{n} n^{-\frac{7}{2}} + O\!\left(4^{n} n^{-4}\right)\)
Partial Fractions and Residues¶
In [6]:
# Sage can compute partial fraction decompositions for rational functions
F = (3*z+4)/(-6*z^5 + 13*z^4 - 4*z^3 - 6*z^2 + 2*z + 1)
show(F, " = \t", F.partial_fraction())
\(\displaystyle -\frac{3 \, z + 4}{6 \, z^{5} - 13 \, z^{4} + 4 \, z^{3} + 6 \, z^{2} - 2 \, z - 1} \verb| |\verb|=|\verb| |\verb| | \frac{243}{64 \, {\left(3 \, z + 1\right)}} - \frac{40}{27 \, {\left(2 \, z + 1\right)}} - \frac{907}{1728 \, {\left(z - 1\right)}} + \frac{83}{144 \, {\left(z - 1\right)}^{2}} - \frac{7}{12 \, {\left(z - 1\right)}^{3}}\)
In [7]:
# It can also compute residues
F = tan(z)
show("The residue of \t", F, " at z = pi/2 is", F.residue(z==pi/2))
\(\displaystyle \verb|The|\verb| |\verb|residue|\verb| |\verb|of|\verb| |\verb| | \tan\left(z\right) \verb| |\verb|at|\verb| |\verb|z|\verb| |\verb|=|\verb| |\verb|pi/2|\verb| |\verb|is| -1\)
Example 3.8 - Surjections¶
In [8]:
# Note that Sage does not know there is an exponentially smaller error
def surjection(z):
return 1/(2-exp(z))
asm = A.coefficients_of_generating_function(
function=surjection,
singularities=(log(2),),
precision=10
)
show("[z^n]", SR("1/(2-exp(z))"), " = \t", asm)
\(\displaystyle \verb|[z^n]| -\frac{1}{e^{z} - 2} \verb| |\verb|=|\verb| |\verb| | \frac{1}{2 \, \log\left(2\right)} \left(\frac{1}{\log\left(2\right)}\right)^{n} + O\!\left(\left(\frac{1}{\log\left(2\right)}\right)^{n} n^{-9}\right)\)
Lemma 3.10 - Basic Scale¶
In [9]:
# These asymptotic expansions can be computed automatically in Sage
def algsing(z):
return (1-z)^(-3/2)
asm = A.coefficients_of_generating_function(
function=algsing,
singularities=(1,),
precision=10
)
show("[z^n]", SR("(1-z)^(-3/2)"), " = \t", asm)
\(\displaystyle \verb|[z^n]| \frac{1}{{\left(-z + 1\right)}^{\frac{3}{2}}} \verb| |\verb|=|\verb| |\verb| | \frac{2}{\sqrt{\pi}} n^{\frac{1}{2}} + \frac{3}{4 \, \sqrt{\pi}} n^{-\frac{1}{2}} - \frac{7}{64 \, \sqrt{\pi}} n^{-\frac{3}{2}} + \frac{9}{512 \, \sqrt{\pi}} n^{-\frac{5}{2}} + \frac{59}{16384 \, \sqrt{\pi}} n^{-\frac{7}{2}} - \frac{483}{131072 \, \sqrt{\pi}} n^{-\frac{9}{2}} - \frac{2323}{2097152 \, \sqrt{\pi}} n^{-\frac{11}{2}} + \frac{42801}{16777216 \, \sqrt{\pi}} n^{-\frac{13}{2}} + \frac{923923}{1073741824 \, \sqrt{\pi}} n^{-\frac{15}{2}} - \frac{30055311}{8589934592 \, \sqrt{\pi}} n^{-\frac{17}{2}} + O\!\left(n^{-\frac{19}{2}}\right)\)
Example 3.11 - Darboux Example¶
In [10]:
# These asymptotic expansions can be computed automatically in Sage
def EvenCycles(z):
return exp(-z/2)/sqrt(1-z)
asm = A.coefficients_of_generating_function(
function=EvenCycles,
singularities=(1,),
precision=3
)
show("[z^n]", SR("exp(-z/2)/sqrt(1-z)"), " = \t", asm)
\(\displaystyle \verb|[z^n]| \frac{e^{\left(-\frac{1}{2} \, z\right)}}{\sqrt{-z + 1}} \verb| |\verb|=|\verb| |\verb| | \left(\frac{e^{\left(-\frac{1}{2}\right)}}{\sqrt{\pi}}\right) n^{-\frac{1}{2}} + \left(-\frac{3 \, e^{\left(-\frac{1}{2}\right)}}{8 \, \sqrt{\pi}}\right) n^{-\frac{3}{2}} + \left(\frac{e^{\left(-\frac{1}{2}\right)}}{128 \, \sqrt{\pi}}\right) n^{-\frac{5}{2}} + O\!\left(n^{-\frac{7}{2}}\right)\)
Example 3.13 - 2-Regular Graphs¶
In [11]:
# These asymptotic expansions can be computed automatically in Sage
def TwoRegular(z):
return exp(-z/2-z^2/4)/sqrt(1-z)
asm = A.coefficients_of_generating_function(
function=TwoRegular,
singularities=(1,),
precision=3
)
show("[z^n]", SR("exp(-z/2)/sqrt(1-z)"), " = \t", asm)
\(\displaystyle \verb|[z^n]| \frac{e^{\left(-\frac{1}{2} \, z\right)}}{\sqrt{-z + 1}} \verb| |\verb|=|\verb| |\verb| | \left(\frac{e^{\left(-\frac{3}{4}\right)}}{\sqrt{\pi}}\right) n^{-\frac{1}{2}} + \left(-\frac{5 \, e^{\left(-\frac{3}{4}\right)}}{8 \, \sqrt{\pi}}\right) n^{-\frac{3}{2}} + \left(\frac{e^{\left(-\frac{3}{4}\right)}}{128 \, \sqrt{\pi}}\right) n^{-\frac{5}{2}} + O\!\left(n^{-\frac{7}{2}}\right)\)
Example 3.16 - Catalan Numbers¶
In [12]:
# Copied from above
def catalan(z):
return (1-sqrt(1-4*z))/(2*z)
asm = A.coefficients_of_generating_function(
function=catalan,
singularities=(1/4,),
precision=3
)
show("[z^n]", SR("(1-sqrt(1-4*z))/(2*z)"), " = \t", asm)
\(\displaystyle \verb|[z^n]| -\frac{\sqrt{-4 \, z + 1} - 1}{2 \, z} \verb| |\verb|=|\verb| |\verb| | \frac{1}{\sqrt{\pi}} 4^{n} n^{-\frac{3}{2}} - \frac{9}{8 \, \sqrt{\pi}} 4^{n} n^{-\frac{5}{2}} + \frac{145}{128 \, \sqrt{\pi}} 4^{n} n^{-\frac{7}{2}} + O\!\left(4^{n} n^{-4}\right)\)
Example 3.18 - Ordered Set Partitions¶
In [13]:
var('n')
F = exp(z/(1-z))
coeffs = list(F.series(z,100).truncate().polynomial(QQ))
asm = n^(-3/4)*exp(2*sqrt(n))/sqrt(4*pi*exp(1))
print("We have shown in the book that")
show("[z^n]", SR("exp(z/(1-z))"), " ~ \t", asm)
We have shown in the book that
\(\displaystyle \verb|[z^n]| e^{\left(-\frac{z}{z - 1}\right)} \verb| |\verb|~|\verb| |\verb| | \frac{e^{\left(2 \, \sqrt{n} - \frac{1}{2}\right)}}{2 \, \sqrt{\pi} n^{\frac{3}{4}}}\)
In [14]:
# Here we plot the ratio of the EGF coefficients to the predicted asymtptotic behaviour
line([[k,coeffs[k]/asm.subs(n=k)] for k in range(1,100)])
Out[14]:
Example 3.19 - involutions¶
In [15]:
var('n')
F = exp(z + z^2/2)
coeffs = list(F.series(z,100).truncate().polynomial(QQ))
asm = n^(n/2)*exp(sqrt(n)-n/2)/sqrt(2*exp(1/2))
print("We have shown in the book that")
show("[z^n]", SR("exp(z + z^2/2)"), " ~ \t", asm)
We have shown in the book that
\(\displaystyle \verb|[z^n]| e^{\left(\frac{1}{2} \, z^{2} + z\right)} \verb| |\verb|~|\verb| |\verb| | \frac{1}{2} \, \sqrt{2} n^{\frac{1}{2} \, n} e^{\left(-\frac{1}{2} \, n + \sqrt{n} - \frac{1}{4}\right)}\)
In [16]:
# Here we plot the ratio of the EGF coefficients to the predicted asymtptotic behaviour
line([[k,factorial(k)*coeffs[k]/asm.subs(n=k)] for k in range(1,100)])
Out[16]:
In [ ]: