• Aucun résultat trouvé

Bachelor of Ecole Polytechnique

N/A
N/A
Protected

Academic year: 2022

Partager "Bachelor of Ecole Polytechnique"

Copied!
9
0
0

Texte intégral

(1)

Bachelor of Ecole Polytechnique Computational Mathematics, year 2, semester 1

Lecturer: Lucas Gerin (send mail) (mailto:[email protected]

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 *

(2)

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

0

n ≥ 1

= .

u

n

1 + u

n−1

1 − u

n−1

a ( ) u

n

Answers. 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

4

u

0

a u

n

=

u

n+4

u

n

(⋆)

n ( ) u

n

4 ⋆

n = 0

n

= = f ( ) u

n+1+4

u

n+5

u

n+4

f ( x ) = (1 + x )/(1 − x )

f ( u

n+4

) = f ( ) = u

n

u

n+1

Exercise 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]))

(3)

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

4

x

2

= 3 x ( + 3 , x

2

)

2

= − + 30 − 9, x

4

x

2

= 3 x ( + 3), x

2

= −12 + 36 x

3

x ,

= ( + 3 x

2

)

2

Answers. 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)

(4)

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

0

u

n

= 1, = 2 u

n−1

+ 3 n

2

(∀ n ≥ 1) (⋆) u

n

( ) v

n

{ v

0

= , = , = , = u

0

v

1

u

1

v

2

u

2

v

3

u

3

= α + a + bn + c (∀ n ≥ 0) v

n

2

n

n

2

α , a , b , c α , a , b , c

x , y

( ) v

n

n

= 2 + 3 . v

n

v

n−1

n

2

Answers. According to the above script we get

With this choice of parameters,

α = 19, a = −3,

and

b = −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)

(5)

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

n

2

n

n

2

Solving 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)])

(6)

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

0

u

n

= a ,

= α u

n−1

+ f ( n ), ( n ≥ 1),

a , α f

( u

n

)

n≥0

  u

0

u

1

u

n

= a ,

= b ,

= α u

n−1

+ β u

n−2

+ f ( n ), ( n ≥ 2),

a , , b , α , β f

= a , = r u

0

u

n

u

n−1

= a , = + r u

0

u

n

u

n−1

= 1, = 1, = +

F

1

F

2

F

n

F

n−1

F

n−2

(7)

Exercise 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

n

F

n−1

1 + 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)+'')

(8)

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

n

2

−n

5 √ 5 ⎯⎯ ( 1 + √ 5 ⎯⎯ )

n

( − √ 5 ⎯⎯ + 1 )

n

√ 5 ⎯⎯

5 φ

n

θ

n

√5

F

n 5

F

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

0

J

1

J

n

= 1 = 2

= 2 J

n−2

+ 5  for every  n ≥ 2 J

n

The 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)))

(9)

Answers. The explicit formula for

J

n is

( 7 + 12 ) + ( −7 + 12 ) − 5 2

n2−2

√ 2 ⎯⎯ ( 2 ⎯⎯ )

n

4 √ 2 ⎯⎯

Do it yourself.(Theory) According to the formula found by SymPy , what happens when for the sequence ?

n → +∞

J2nn

Answers. 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)))

Références

Documents relatifs

Ecrire une fonction SommePoly prenant comme argument deux listes associées aux polynômes P et Q et qui rend la liste (normalisée) associée au polynôme P + Q.. Calculer la

In section 6, we give our modification on Chudnovsky and Chudnovsky’s algorithm for computing one term in a recurrence with polynomial coefficients; a generalization to several terms

Because a native Python number does not know how to compare itself with a SymPy object Python will try the reflective operation, “x &gt; 1” and that is the form that gets

We improve the bound on the number of errors that we can correct, using techniques inspired by the decoding procedure of Interleaved Reed-Solomon Codes..

Concrete parameter sets are provided for generators with good equidistribution properties and whose speed is comparable to that of the fastest generators currently available...

Essentially, we replace the main results of [4] by those of [1], section 6. This is an inhomogeneous version of theorem 6.2 of [1].. As in the proof of [6], it is enough to prove

Schwarz preconditioning combined with GMRES, Newton basis and deflation DGMRES: preconditioning deflation AGMRES: augmented subspace. 11

Then we apply the result to the case of empirical means of inde- pendent and identically distributed random variables (Cramér’s theory) and obtain a general weak large