Reputation: 121
Does Python has a Package to solve sequence which are defined using recurrence relation?
For example "Maxima" can solve a recurrence relation directly using the package "solve_rec".
Is something similar available for Python?
Maxima Code:
kill(all);
load("solve_rec")$
a[n]-a[n-1]-a[n-2]=0;
solve_rec(%, a[n],a[1]=2,a[2]=3);
Output:
a[n]-a[n-1]-a[n-2]=0
a[n]=((sqrt(5)+1)^n*(3*sqrt(5)+5)*2^(-n-1))/5-((sqrt(5)-1)^n*(3*sqrt(5)-5)*2^(-n-1)*(-1)^n)/5
Upvotes: 2
Views: 215
Reputation: 603
I have made a package called resolvepy which solves homogenous recurrence relations. After downloading, you can solve a reccurrence relation like this:
from sympy import *
from resolvepy import *
n = Symbol('n')
# create the sequence
f = Recurrence('f')
f.index = n
# input the starting items
f[0] = 1
f[1] = 2
# provide a recursive formula
f[n] = f[n-1] + f[n-2]
explicit = f.resolve()
Upvotes: 0
Reputation: 1574
This type of recurrence relation is linear and homogeneous. Linear homogeneous recurrence relations are a special class of recurrence relations that are relatively easy to solve (at least by hand). However, not all recurrence relations have closed forms, and among those that do, not all belong to a special class that computers can solve routinely. Python does not have a built-in recurrence relation solver, and no module can be written in Python that can solve an arbitrary recurrence relation.
Upvotes: 1