Bachelor of Ecole Polytechnique Computational Mathematics, year 2, semester 1
Lecturer: Lucas Gerin (send mail) (mailto:lucas.gerin@polytechnique.edu
Symbolic computation 3: Linear recurrences
Table of contents
A periodic sequence
A number theory theorem with SymPy A linear recurrence (almost) by hand Solving linear recurrences with Sympy
The function rsolve Application 1
Application 2
Exercise 1. A periodic sequence
# execute this part to modify the css style from IPython.core.display import HTML def css_styling():
styles = open("./style/custom2.css").read() return HTML(styles)
css_styling()
## loading python libraries
# necessary to display plots inline:
%matplotlib inline
# load the libraries
import matplotlib.pyplot as plt # 2D plotting library
import numpy as np # package for scientific computing from pylab import *
from math import * # package for mathematics (pi, arctan, sqrt, factorial ...) import sympy as sympy # package for symbolic computation
from sympy import *
Do it yourself. Let be a real number, we de�ne the sequence as follows: and for
Using SymPy , prove that for every the sequence is periodic.
a ∉ {1, −1} ( u
n)
n≥0= a
u
0n ≥ 1
= .
u
n1 + u
n−11 − u
n−1a ( ) u
nAnswers. According to the above script we have
for every . Since is de�ned by a simple recurrence of order one this su�ces to prove that
for every , i.e. is periodic with period at most . Indeed we can prove eq. ( ) by a simple induction:
For we trust SymPy.
Assume eq. ( ) is true for some . We write
where . By the induction hypothesis
and the proof is done.
= u
4u
0a u
n=
u
n+4u
n(⋆)
n ( ) u
n4 ⋆
n = 0
⋆ n
= = f ( ) u
n+1+4u
n+5u
n+4f ( x ) = (1 + x )/(1 − x )
f ( u
n+4) = f ( ) = u
nu
n+1Exercise 2. A number theory theorem with Sympy
u_0 = a
u_1 = -(a + 1)/(a - 1) u_2 = -1/a
u_3 = (a - 1)/(a + 1) u_4 = a
u_5 = -(a + 1)/(a - 1) u_6 = -1/a
u_7 = (a - 1)/(a + 1) u_8 = a
u_9 = -(a + 1)/(a - 1) u_10 = -1/a
u_11 = (a - 1)/(a + 1)
# # We compute the first few terms:
def InductionFormula(x):
return (1+x)/(1-x) var('a')
Sequence=[a]
print('u_0 = a') for i in range(1,12):
Sequence.append(simplify(InductionFormula(Sequence[-1]))) print('u_'+str(i)+' = '+str(Sequence[-1]))
Do it yourself.
With Sympy simplify the following expression:
1.
Prove the following:
Theorem. Every integer can be written as the sum of three cubes of rational numbers.
To save you time in the proof you can use the following notations:
2.
+ +
(x6+ 45 − 81 + 27x4 x2 ) 3x( + 3x2 )2
3
(− + 30 − 9x4 x2 ) 3x( + 3)x2
3
(−12 + 36xx3 ) ( + 3x2 )2
3
n
p q r s t u
= x
6+ 45 − 81 + 27, x
4x
2= 3 x ( + 3 , x
2)
2= − + 30 − 9, x
4x
2= 3 x ( + 3), x
2= −12 + 36 x
3x ,
= ( + 3 x
2)
2Answers. Let be an integer. The above script shows that
where are integers. Thus
Therefore can be written as a sum of cubes of rationals.
x 8 x = ( p / q )
3+ ( r / s )
3+ ( t / u )
3, p , q , r , s , t , u
x = ( p /2 q )
3+ ( r /2 s )
3+ ( t /2 u )
3. x
Exercise 3. Solving a recurrence (almost) by hand
8*x var('x')
A=(x**6+45*x**4-81*x**2+27)/(3*x*(x**2+3)**2) B=(-x**4+30*x**2-9)/(3*x*(x**2+3))
C=(-12*x**3+36*x)/((x**2+3)**2) simplify(A**3+B**3+C**3)
Do it yourself. Let be the sequence de�ned by
The goal of the exercise is to �nd a formula for .
For this purpose we introduce the sequence de�ned by
for some parameters . Use SymPy to �nd .
To solve a system of equations with SymPy with unknowns , the syntax is solve([x-y-2,3*y+x],[x,y])
1.
Prove with SymPy that this sequence satis�es the recurrence ( ): for every we have
2.
( ) u
n{ u
0u
n= 1, = 2 u
n−1+ 3 n
2(∀ n ≥ 1) (⋆) u
n( ) v
n{ v
0= , = , = , = u
0v
1u
1v
2u
2v
3u
3= α + a + bn + c (∀ n ≥ 0) v
n2
nn
2α , a , b , c α , a , b , c
x , y
( ) v
n⋆ n
= 2 + 3 . v
nv
n−1n
2Answers. According to the above script we get
With this choice of parameters,
α = 19, a = −3,
andb = −12,
coincide for the �rst values.c = −18.
( ) u
n( ) v
n{c: -18, alpha: 19, b: -12, a: -3}
# --- Question 1 --- var('n',integer=True)
var('alpha a b c') def v(n,alpha,a,b,c):
return alpha*2**n +a*n**2 +b*n +c def u(n):
if n==0:
return 1 else:
return u(n-1)*2+3*n*n Solutions=solve(
# Quantities which are to be zero:
[v(0,alpha,a,b,c)-u(0), # initial condition v(1,alpha,a,b,c)-u(1),
v(2,alpha,a,b,c)-u(2), v(3,alpha,a,b,c)-u(3), ],
# Unknowns:
[alpha,a,b,c]
)
print(Solutions)
Answers. Question 2. According to the above script we proved that the formula is true: for every we have that
Finally we have proved that the solution of ( ) is given by
n v
n− 2 v
n−1= 3 . n
2= 19 × − 3 − 12 ⋆ n − 18.
v
n2
nn
2Solving recurrences with SymPy: rsolve
With these coefficients the simplification of v(n)-2v(n-1) gives : 3*n*
*2
#--- Question 2 ---
# We fix parameters according to Question 1:
c_s=-18 alpha_s=19 b_s=-12 a_s=-3 var('n')
print('With these coefficients the simplification of v(n)-2v(n-1) gives : ' +str(simplify(v(n,alpha_s,a_s,b_s,c_s)-2*v(n-1,alpha_s,a_s,b_s,c_s))))
# We can check that u and v coincide for the first values:
#print([u(n) for n in range(10)])
#print([v(n,alpha_s,a_s,b_s,c_s) for n in range(10)])
The function rsolve
We will now see how to use Sympy to obtain explicit formulas for some sequences de�ned by linear recurrences. More precisely, we will see how to obtain an explicit formula for in two cases:
Linear recurrence of order one: this is a sequence is de�ned by
where are some given constants and is an arbitrary function.
1.
Linear recurrence of order two: this is a sequence is de�ned by
where are some given constants and is an arbitrary function.
2.
Some known examples �t in this settings:
Geometric sequences: .
1.
Arithmetic sequences: .
2.
The Fibonacci sequence: .
3.
The following script shows how to solve the linear recurrence of a geometric sequence, using function rsolve :
u
n( u
n)
n≥0{ u
0u
n= a ,
= α u
n−1+ f ( n ), ( n ≥ 1),
a , α f
( u
n)
n≥0
u
0u
1u
n= a ,
= b ,
= α u
n−1+ β u
n−2+ f ( n ), ( n ≥ 2),
a , , b , α , β f
= a , = r u
0u
nu
n−1= a , = + r u
0u
nu
n−1= 1, = 1, = +
F
1F
2F
nF
n−1F
n−2Exercise 4. Application: Fibonacci with rsolve
Do it yourself.
Use SymPy to solve the recurrence of the Fibonacci sequence and �nd an explicit formula for .
1.
(Theory) Use the formula obtained at Question 1 to prove that
(Hint: Set .)
2.
F
n= φ = .
n→+∞
lim F
nF
n−11 + 5 √ ⎯⎯
θ = (1 − √ 5 ⎯⎯ )/2 2
---
Example 1 (order one): u(n)=3*u(n-1), u(0)=5 The formula for u(n) is 5*3**n
---
Example 2 (order one): 2v(n)=v(n-1)+2**n, v(0)=1 The formula for v(n) is 2*2**n/3 + 2**(-n)/3 ---
Example 3 (order two): w(n)=2*w(n-1)+w(n-2)-1, w(0)=1,w(1)=1
The formula for w(n) is (1 + sqrt(2))**n/4 + (-sqrt(2) + 1)**n/4 + 1/2
# First example: a geometric sequence print('---')
print('Example 1 (order one): u(n)=3*u(n-1), u(0)=5')
u = Function('u') # declares the name of the sequence n = symbols('n',integer=True) # declares the variable
f = u(n)-3*u(n-1) # the expression which is to be zero
ExplicitFormula=rsolve(f,u(n),{u(0):5}) # solves f=0 with a given initial condition print('The formula for u(n) is '+str(ExplicitFormula)+'')
# Second example with a non-constant term print('---')
print('Example 2 (order one): 2v(n)=v(n-1)+2**n, v(0)=1')
v = Function('v') # declares the name of the sequence n = symbols('n',integer=True) # declares the variable
f = 2*v(n)-v(n-1)-2**n # the expression which is to be zero ExplicitFormula2=rsolve(f,v(n),{v(0):1})
print('The formula for v(n) is '+str(ExplicitFormula2)+'')
# Third example: order two
print('---')
print('Example 3 (order two): w(n)=2*w(n-1)+w(n-2)-1, w(0)=1,w(1)=1') w = Function('w') # declares the name of the sequence n = symbols('n',integer=True) # declares the variable
f =w(n)-2*w(n-1)-w(n-2)+1 # the expression which is to be zero ExplicitFormula3=rsolve(f,w(n),{w(0):1,w(1):1})
print('The formula for w(n) is '+str(ExplicitFormula3)+'')
Answers.
We put the above answer in : 1.
The factor cancels out so we obtain:
Since the numerator tends to , the denominator to one.
2.
𝙻𝚊𝚝𝚎𝚇
= ( − ) = ( − ) .
F
n2
−n5 √ 5 ⎯⎯ ( 1 + √ 5 ⎯⎯ )
n( − √ 5 ⎯⎯ + 1 )
n√ 5 ⎯⎯
5 φ
nθ
n√5
F
n 5F
n−1= φ
n− θ
n,
− φ
n−1θ
n−1= φ − θ ( θ / φ )
n−1, (we divide everything by .)
1 − θ ( θ / φ )
n−1φ
n−1| θ / φ | < 1 φ
Remark. The output of rsolve is an expression which depends on the symbolic variable n . If we want to evaluate this expression (for instance for ) we must write:
n = 10
Exercise 5. Application: Another example of order two
Do it yourself. Let be de�ned by
Use rsolve to �nd an explicit formula for .
( ) J
n .
J
0J
1J
n= 1 = 2
= 2 J
n−2+ 5 for every n ≥ 2 J
nThe formula for u(n) is \frac{2^{- n}}{5} \sqrt{5} \left(\left(1 + \sqr t{5}\right)^{n} - \left(- \sqrt{5} + 1\right)^{n}\right)
10th Fibonacci number = sqrt(5)*(-(-sqrt(5) + 1)**10 + (1 + sqrt(5))**1 0)/5120
After simplification : 55
u = Function('u') # declares the name of the sequence n = symbols('n',integer=True) # declares the variable
f = u(n)-u(n-1)-u(n-2) # the expression which is to be zero ExplicitFormula=simplify(rsolve(f,u(n),{u(1):1,u(2):1}))
Formula2=latex(simplify(ExplicitFormula))
print('The formula for u(n) is '+str(Formula2)+'')
Value=ExplicitFormula.subs(n,10)
print('10th Fibonacci number = '+str(Value))
print('After simplification : '+str(simplify(Value)))
Answers. The explicit formula for
J
n is( 7 + 12 ) + ( −7 + 12 ) − 5 2
n2−2√ 2 ⎯⎯ ( − √ 2 ⎯⎯ )
n4 √ 2 ⎯⎯
Do it yourself.(Theory) According to the formula found by SymPy , what happens when for the sequence ?
n → +∞
√J2nnAnswers. We rewrite the above formula as
Therefore oscillates between and .
= ( 2 + 5 + (−1 (−2 + 5) ) − 1.
J
n√ 2 ⎯⎯
n14√ 2 ⎯⎯ )
n√ 2 ⎯⎯
Jn
√2n
10/4 = 5/2 4 √ 2 ⎯⎯ /4 = √ 2 ⎯⎯
Sympy says the formula for J(n) is 2**(n/2 - 2)*(7*sqrt(2) + 12) + (-sq rt(2))**n*(-7*sqrt(2) + 12)/4 - 5
2^{\frac{n}{2} - 2} \left(7 \sqrt{2} + 12\right) + \frac{\left(- \sqrt{
2}\right)^{n}}{4} \left(- 7 \sqrt{2} + 12\right) - 5
J = Function('J') # declares the name of the sequence n = symbols('n',integer=True) # declares the variable
f = J(n)-2*J(n-2)-5 # the expression which is to be zero ExplicitFormula=rsolve(f,J(n),{J(0):1,J(1):2})
Formula=simplify(ExplicitFormula)
print('Sympy says the formula for J(n) is '+str(Formula)+'') print(latex(simplify(Formula)))