Sympy
## 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 *
# ------------- Question 1 --------------
var('n',integer=True)
var('alpha a b c')
def w(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 2*u(n-1)+3*n**2
FourEquations=[w(n,alpha,a,b,c)-u(n) for n in range(4)]
Solutions=solve(
# Quantities which are to be zero:
FourEquations,
# Unknowns:
[alpha,a,b,c]
)
print('--------------')
print('Question 1:', Solutions)
--------------
Question 1: {alpha: 19, a: -3, b: -12, c: -18}
#-------------- Question 2 ----------------
print('--------------')
c_s=Solutions[c]
alpha_s=Solutions[alpha]
b_s=Solutions[b]
a_s=Solutions[a]
Expr=w(n,alpha_s,a_s,b_s,c_s)-2*w(n-1,alpha_s,a_s,b_s,c_s)-3*n**2
print('Question 2:')
print('With these coefficients the simplificaion of w(n)-2w(n-1)-3n**2 gives for every n : '+str(simplify(Expr)))
#--------------- We check the solutions
print('--------------')
print('Safety check:')
print('First values of w_n: '+str([w(i,alpha_s,a_s,b_s,c_s) for i in range(10)]))
# In order to check our results:
def Recurrence(n):
if n==0:
return 1
else:
return 2*Recurrence(n-1)+3*n**2
print('First values (with recurrence formula) : '+str([Recurrence(n) for n in range(10)]))
-------------- Question 2: With these coefficients the simplificaion of w(n)-2w(n-1)-3n**2 gives for every n : 0 -------------- Safety check: First values of w_n: [1, 5, 22, 71, 190, 455, 1018, 2183, 4558, 9359] First values (with recurrence formula) : [1, 5, 22, 71, 190, 455, 1018, 2183, 4558, 9359]
rsolve¶rsolve¶We will use Sympy to obtain explicit formulas for some sequences defined by linear recurrences. More precisely, we will see how to obtain an explicit formula for $u_n$ in two cases:
Some known examples fit in this settings:
The following script shows how to solve the two recurrences
\begin{align*}
u_0=5,\qquad u_{n}&=3u_{n-1},\\
v_0=1,\qquad v_{n}&=2v_{n-1}+n,\\
\end{align*}
using function rsolve.
# First example: a geometric sequence
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
ExplicitFormula1=rsolve(f,u(n),
{u(0):5}) # initial condition
print('The formula for u(n) is '+str(ExplicitFormula1)+'')
# Second example with a non-linear term
print('-------')
v = Function('v') # declares the name of the sequence
n = symbols('n',integer=True) # declares the variable
f = v(n)-2*v(n-1)-n # the expression which is to be zero
ExplicitFormula2=rsolve(f,v(n),
{v(0):1}) # initial condition
print('The formula for v(n) is '+str(ExplicitFormula2)+'')
# Third example
print('-------')
w = Function('w') # declares the name of the sequence
n = symbols('n',integer=True) # declares the variable
g = w(n)-w(n-2)-8*n-2 # the expression which is to be zero
ExplicitFormula3=simplify(rsolve(g,w(n),{w(0):2,w(1):10}))
print('The formula for w(n) is '+str(ExplicitFormula3)+'')
The formula for u(n) is 5*3**n ------- The formula for v(n) is 2**n/2 + n/2 + 1/2 ------- The formula for w(n) is -(-1)**n/2 + 2*n**2 + 5*n + 5/2
rsolve¶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)+'')
The formula for u(n) is \frac{2^{- n} \sqrt{5} \left(- \left(1 - \sqrt{5}\right)^{n} + \left(1 + \sqrt{5}\right)^{n}\right)}{5}
Value=ExplicitFormula.subs(n,10)
print('10th Fibonacci number = '+str(Value))
print('After simplification : '+str(simplify(Value)))
10th Fibonacci number = sqrt(5)*(-(1 - sqrt(5))**10 + (1 + sqrt(5))**10)/5120 After simplification : 55
#Question 3
A=Matrix([[1,2],[1,1]])
var('n', integer=True)
Power=A**(n-1)
#print(Power)
an=latex(simplify(Power[0,0]+Power[0,1]))
print(an)
bn=latex(simplify(Power[1,0]+Power[1,1]))
print(bn)
\frac{\left(1 - \sqrt{2}\right)^{n}}{2} + \frac{\left(1 + \sqrt{2}\right)^{n}}{2}
\frac{\sqrt{2} \left(- \left(1 - \sqrt{2}\right)^{n} + \left(1 + \sqrt{2}\right)^{n}\right)}{4}
# Tests
u1=Rational(1,2)
u2=Rational(1,1+Rational(1,3))
u3=Rational(1,1+Rational(1,2+Rational(1,4)))
def u_n(n):
u=n+1
for k in range(1,n):
u=n-k+Rational(1,u)
return Rational(1,u)
for n in range(1,21):
print(n,'---',u_n(n))
1 --- 1/2 2 --- 3/4 3 --- 9/13 4 --- 37/53 5 --- 187/268 6 --- 1129/1618 7 --- 7933/11369 8 --- 63621/91177 9 --- 573561/821986 10 --- 5742571/8229836 11 --- 63224941/90609397 12 --- 759216193/1088053549 13 --- 9875036179/14152185188 14 --- 138308505777/198213712978 15 --- 2075328803577/2974210627873 16 --- 33214434676489/47600517297953 17 --- 564774524186833/809393860526194 18 --- 10167887629480051/14571878633638372 19 --- 193221133200680401/276910505412260141 20 --- 3864956170297235421/5538974690732597941
var('x y z')
Expression=(x+2*y+z)**12
display(expand(Expression))