Solve recurrences using Sage ?

1,562 views
Skip to first unread message

Nathann Cohen

unread,
Feb 10, 2010, 3:54:57 AM2/10/10
to Sage Support
Hello everybody !!!!

I just learnt about the "rsolve" function from Maple, which seems to
give the formula of sequences defined by recurrence.. Is there a
similar function in Sage ?

For example, how could I have Sage give me the general formula of
fibonacci's sequence ? :-)

Thank you very much !!!

Nathann

Mike Hansen

unread,
Feb 10, 2010, 3:58:02 AM2/10/10
to sage-s...@googlegroups.com
On Wed, Feb 10, 2010 at 12:54 AM, Nathann Cohen <nathan...@gmail.com> wrote:
> Hello everybody !!!!
>
> I just learnt about the "rsolve" function from Maple, which seems to
> give the formula of sequences defined by recurrence.. Is there a
> similar function in Sage ?

I don't believe there is anything in the Sage library itself, but
Maxima does provide some of this functionality:

http://maxima.sourceforge.net/docs/tutorial/en/gaertner-tutorial-revision/Pages/DiffEq0001.htm

--Mike

Nathann Cohen

unread,
Feb 10, 2010, 4:05:39 AM2/10/10
to sage-s...@googlegroups.com
Thank you ! :-)

Nathann

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>

Harald Schilly

unread,
Feb 10, 2010, 4:55:22 AM2/10/10
to sage-support
On Feb 10, 9:54 am, Nathann Cohen <nathann.co...@gmail.com> wrote:
> I just learnt about the "rsolve" function from Maple, which seems to
> give the formula of sequences defined by recurrence.. Is there a
> similar function in Sage ?

sympy has rsolve

>
> For example, how could I have Sage give me the general formula of
> fibonacci's sequence ? :-)

I wasn't able to do that one (bug or i don't understand what i'm
doing), but let me quote the doctest:

from sympy import *
>>> y = Function('y')
>>> n = Symbol('n', integer=True)
>>> f = (n-1)*y(n+2) - (n**2+3*n-2)*y(n+1) + 2*n*(n+1)*y(n)
>>> rsolve(f, y(n))
C0*n! + C1*2**n
>>> rsolve(f, y(n), { y(0):0, y(1):3 })
-3*n! + 3*2**n

Nathann Cohen

unread,
Feb 10, 2010, 5:13:21 AM2/10/10
to sage-s...@googlegroups.com
sage: f = y(n+2) - y(n+1) - y(n)
sage: rsolve(f, y(n), { y(0):1, y(1):1 })
(1/2 + 5**(1/2)/2)**n/2 + (1/2 - 5**(1/2)/2)**n/2 - 5**(1/2)*(1/2 -
5**(1/2)/2)**n/10 + 5**(1/2)*(1/2 + 5**(1/2)/2)**n/10

That's perfect !! Thank you very much !! :-)

Their interface is a bit clumsy though, I admit... It could be good to
be able to do it directly in Sage :-)

Nathann

Simon King

unread,
Feb 10, 2010, 5:16:02 AM2/10/10
to sage-support

On Feb 10, 9:55 am, Harald Schilly <harald.schi...@gmail.com> wrote:
> > For example, how could I have Sage give me the general formula of
> > fibonacci's sequence ? :-)
>
> I wasn't able to do that one

Perhaps like that:

sage: from sympy import *
sage: y = Function('y')
sage: n = Symbol('n',integer=True)


sage: f = y(n+2) - y(n+1) - y(n)

sage: rsolve(f,y(n))
C0*(1/2 + 5**(1/2)/2)**n + C1*(1/2 - 5**(1/2)/2)**n

Isn't that the general Fibonacci formula?

If I understand the documentation ("rsolve?") correctly, f must be a
relation that is linear in finitely many terms of y with
hypergeometric coefficients. In other words, if you insert y(n) into f
then the result is zero for all n.

Cheers,
Simon

Simon King

unread,
Feb 10, 2010, 5:20:41 AM2/10/10
to sage-support
Hi Nathann!

Your were faster than I ... :-)

On Feb 10, 10:13 am, Nathann Cohen <nathann.co...@gmail.com> wrote:
> Their interface is a bit clumsy though, I admit... It could be good to
> be able to do it directly in Sage :-)

But how would a better interface look like?

I mean, you define a recurrence relation f, involving a function y
depending on an integer valued symbol n. And then you call rsolve. OK,
it is a function rather than a method of f, so, it isn't pythonic.
But apart from that, what's wrong with it?

Cheers,
Simon

Nathann Cohen

unread,
Feb 10, 2010, 5:25:19 AM2/10/10
to sage-s...@googlegroups.com
Well, for example :

- You need to import simpy to use it
- For this reason, it does not appear in Sage's documentation
- It could be good to be able to define functions and variables as
they usually are in Sage and not through Simpy types
- Only define one function... I'd have enjoyed the ability to type :
rsolve( f(n) == f(n-1) + f(n-2) )

These are just remarks about the interface, though, this function is
definitely useful and I think I will make great use of it :-)

Nathann

Message has been deleted

Harald Schilly

unread,
Feb 10, 2010, 7:42:26 AM2/10/10
to sage-support
On Feb 10, 11:16 am, Simon King <simon.k...@nuigalway.ie> wrote:
> sage: f = y(n+2) - y(n+1) - y(n)

ahh ... ok. now i get it ^^
When I look into sympy/solvers/recurr.py right the first thing rsolve
does is to compute lhs - rhs. So, f = y(n) == y(n-1) - y(n-2) *should*
work. But it doesn't, because that equation doesn't get transformed
into a sympy Equality (just a "bool").

This works:

sage: f = Equality(y(n), y(n-1) + y(n-2))
sage: rsolve(f, y(n))


C0*(1/2 + 5**(1/2)/2)**n + C1*(1/2 - 5**(1/2)/2)**n

I'm +1 for introducing rsolve in sage (and yes, more namespace
pollution, but we have to think about all the newcomers who expect
some easy functions to work just out of the box, integrate, diff,
etc...)

H

Nathann Cohen

unread,
Feb 10, 2010, 8:48:14 AM2/10/10
to sage-s...@googlegroups.com
I mentionned having to import simpy.*, which included classes like
Symbol or Function, and having to use those types. I wouldn't
personally mind if I had to import rsolve from some Sage class... Why
should it necessarily imported by default (namespace kept clean)?

Nathann

Walking Randomly

unread,
Feb 10, 2010, 8:50:39 AM2/10/10
to sage-support
Regarding namespace pollution. Mathematica has thousands of function
names in it's global namespace but it never causes programmers a
problem because they have a convention. All mathematica functions
start with a capital: Integrate, Plot, ListPlot etc. So, stick to
lower case variables and you don't need to worry about a thing. Could
SAGE not follow a similar convention?

Cheers,
Mike

Robert Schwarz

unread,
Feb 10, 2010, 8:49:21 AM2/10/10
to sage-s...@googlegroups.com
I think you always mean
from sympy import *
Don't get it confused with simpy, which is another software package for
simulation (versus symbolic computation).

--
Robert Schwarz <ma...@rschwarz.net>

Get my public key at http://rschwarz.net/key.asc

Harald Schilly

unread,
Feb 10, 2010, 9:53:03 AM2/10/10
to sage-support
On Feb 10, 2:48 pm, Nathann Cohen <nathann.co...@gmail.com> wrote:
> I mentionned having to import simpy.*...

nonono, rsolve should be a new sage function, inside of it it only
imports the really necessary sympy classes. how is
integrate(algorithm='sympy') done? i think that's a good starting
point. see calculus/calculus.py:873

H

David Joyner

unread,
Feb 10, 2010, 10:33:05 AM2/10/10
to sage-s...@googlegroups.com
On Wed, Feb 10, 2010 at 8:50 AM, Walking Randomly
<michael.p...@googlemail.com> wrote:
> Regarding namespace pollution.  Mathematica has thousands of function
> names in it's global namespace but it never causes programmers a
> problem because they have a convention.  All mathematica functions
> start with a capital: Integrate, Plot, ListPlot etc.  So, stick to
> lower case variables and you don't need to worry about a thing.  Could
> SAGE not follow a similar convention?


Python has naming conventions, which Sage follows:
http://www.python.org/doc/essays/styleguide.html


>
> Cheers,
> Mike
>
> On Feb 10, 12:42 pm, Harald Schilly <harald.schi...@gmail.com> wrote:
>> On Feb 10, 11:16 am, Simon King <simon.k...@nuigalway.ie> wrote:
>>
>> > sage: f = y(n+2) - y(n+1) - y(n)
>>
>> ahh ... ok. now i get it ^^
>> When I look into sympy/solvers/recurr.py right the first thing rsolve
>> does is to compute lhs - rhs. So, f = y(n) == y(n-1) - y(n-2) *should*
>> work. But it doesn't, because that equation doesn't get transformed
>> into a sympy Equality (just a "bool").
>>
>> This works:
>>
>> sage: f = Equality(y(n), y(n-1) + y(n-2))
>> sage: rsolve(f, y(n))
>> C0*(1/2 + 5**(1/2)/2)**n + C1*(1/2 - 5**(1/2)/2)**n
>>
>> I'm +1 for introducing rsolve in sage (and yes, more namespace
>> pollution, but we have to think about all the newcomers who expect
>> some easy functions to work just out of the box, integrate, diff,
>> etc...)
>>
>> H
>

Nathann Cohen

unread,
Feb 15, 2010, 6:06:02 AM2/15/10
to sage-support
Should we create a ticket for this ? I'd have done it if not for my
doubt on the section I should pick for this... :-)

Nathann

On Feb 10, 1:42 pm, Harald Schilly <harald.schi...@gmail.com> wrote:
> On Feb 10, 11:16 am, Simon King <simon.k...@nuigalway.ie> wrote:
>
> > sage: f = y(n+2) - y(n+1) - y(n)
>
> ahh ... ok. now i get it ^^
> When I look into sympy/solvers/recurr.py right the first thingrsolve
> does is to compute lhs - rhs. So, f = y(n) == y(n-1) - y(n-2) *should*
> work. But it doesn't, because that equation doesn't get transformed
> into a sympy Equality (just a "bool").
>
> This works:
>
> sage: f = Equality(y(n), y(n-1) + y(n-2))
> sage:rsolve(f, y(n))
> C0*(1/2 + 5**(1/2)/2)**n + C1*(1/2 - 5**(1/2)/2)**n
>

> I'm +1 for introducingrsolvein sage (and yes, more namespace

Reply all
Reply to author
Forward
0 new messages