Chapter 3 Catalan and generating functions
Definition 3.1
Let
denote the generating function of the Catalan numbers.
We can first deduce the recursive formula of the Catalan numbers
We use the ordinary generating function for the Catalan numbers, we have Since we know , we have Therefore, we got
Use the recursive formula () to prove that is a solution of the following equation of degree two:
From the equation we have Then we try to prove the LHS equal to RHS.
For LHS we have
For RHS, we apply we got
Therefore we have Then we prove that is a solution for the equation of
Deduce a formula for and the radius of convergence.
Here we have the computation code in python
= Symbol('x')
x = Symbol('c')
c = solve(c - 1 - x * c**2, c)
solution
# display the solution in latex
print("--------- Question 4.2 ----------")
print("The solution of the equation in is ")
0]), display(solution[1])
display(solution[print("The solution of the equation in Latex is ", (latex(solution[0])),
1])) latex(solution[
We use sympy
to solve the equation and we get
From the two possibilities, the must be chosen because only the choice of the first gives
Therefore we have
Then we compute the radius of convergence of . For the definition of radius of convergence from Wiki page. We use the expression of in power series that Base on [D’Alembert’s test], for radius of convergence we compute for Since , then we have the radius of convergence
Compare the efficiency of different approaches.
::include_graphics('static/pic3.png') knitr
::include_graphics('static/pic4.png') knitr
From the two graph we can see that the formula of has the highest computation efficiency. However, the non-recursive method has more execution time than the method above, compare with recursive method the complexity is still much better since the execution time is in the level of .
We can prove the expression of with
For power series we have We denote that we got Therefore we got We can deduce that
3.1 Manipulate Generating Functions with SymPy
Definition 3.2
We work out with the example of
We first introduce variable and function as follows:
=var('x')
x=(1/(1-2*x))
f
print('f = '+str(f))
print('series expansion of f at 0 and of order 10 is: '+str(f.series(x,0,10)))
0,10)) display(f.series(x,
One can extract coefficient as follows:
- has to be truncated at order (for some ) with f.series(x,0,k)
- the -th coefficient is then extracted by f.coeff(x**n)
= f.series(x,0,8)
f_truncated print('Truncation of f is '+str(f_truncated))
=6
n=f_truncated.coeff(x**n)
nthcoefficientprint(str(n)+'th coefficient is: '+str(nthcoefficient))
display(f_truncated)
Use SymPy to write another programm which computes the Catalan numbers using , and compare the execution times with the functions of the first part of the Project.
# We compute C(x) with Taylor Expansion
= var('x')
x = (1 - sqrt(1 - 4 * x)) / (2 * x)
f
def CatalanTaylorExpansion(n):
return f.series(x, 0, n).coeff(x**(n - 1))
= [CatalanTaylorExpansion(i) for i in range(11)]
lis 1] = 1
lis[print("The first ten Catalan numbers with Taylor Expansion are ", lis[1:])
with Taylor Expansion are [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862] The first ten Catalan numbers
::include_graphics('static/pic5.png') knitr
::include_graphics('static/pic6.png') knitr
From the two graph we can see that the Taylor Expansion method is much faster than the recursive method but much slower than the method non-recursive and Binomial Formula.
3.2 Motzkin Numbers
Definition 3.3
The Motzkin numbers are similar to Catalan numbers and defined by and for every
- Find the generating function .
- Compute the radius of convergence of and . Compare with numerical experiments: which sequence is growing fastest between and ?
We first try to simplify the motzkin numbers recurrence relation. We have Since we have , therefore we have
Therefore, we know that is a root for .
= symbols('x')
x = symbols('M')
M = solve(x**2 * M**2 + x * M - M + 1, M)
SeriesM print(SeriesM)
0])
display(SeriesM[1]) display(SeriesM[
We use sympy
to solve the equation we have
Since we know , therefore the only possible solution is
# Implementation of the formula of Motzkin Numbers
def MotzkinFormula(n):
= symbols('x')
x = (1-x-sqrt(1-2*x-3*x**2))/(2*x**2)
m= m.series(x,0,n+1)
m_truncated return m_truncated.coeff(x**n)
print("--------- Question 4.1.1.1 ----------")
= [MotzkinFormula(i) for i in range(11)]
lis 0] = 1
lis[print("The first ten Motzkin numbers with Taylor Expansion are ", lis[1:])
--------- Question 4.1.1.1 ----------
with Taylor Expansion are [1, 2, 4, 9, 21, 51, 127, 323, 835, 2188] The first ten Motzkin numbers
An integral representation of Motzkin numbers is given by They have the asymptotic behaviour Therefore, they have the same radius of convergence. We apply the similar method as Catalan number. Base on [D’Alembert’s test], for radius of convergence we compute for Since , then we have the radius of convergence .
Or we can illustrate in this way, from the definition of radius of convergence, we need to find an such that when the series converges. Then we observe that radius shows when . Then we got or . Since when , the function diverges, we can also get
Then we can say the radius of convergence of is smaller than . Therefore the sequence of is growing faster than . We can also show this in the graph.
# plot the Catalan numbers and Motzkin numbers in the same graph
= [i for i in range(1, 16)]
N =(10, 7))
plt.figure(figsize= [CatalanNonRecursive(i) for i in range(1, 16)]
lis_catalan = [MotzkinFormula(i) for i in range(1, 16)]
lis_motzkin 'o-', label='Catalan Numbers')
plt.plot(N, lis_catalan, 'o-', label='Motzkin Numbers')
plt.plot(N, lis_motzkin,
plt.legend()"Catalan Numbers and Motzkin Numbers")
plt.title("N")
plt.xlabel("Numbers")
plt.ylabel( plt.show()
::include_graphics('static/pic7.png') knitr