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
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
> --
> 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
>
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
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
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
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
- 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
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
Cheers,
Mike
--
Robert Schwarz <ma...@rschwarz.net>
Get my public key at http://rschwarz.net/key.asc
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
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
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