using maxima output from solve_rec

110 views
Skip to first unread message

Ken Levasseur

unread,
Apr 2, 2013, 5:58:43 PM4/2/13
to sage...@googlegroups.com
Hello,

I've been trying to solve recurrence relations and managed to find the solve_rec in maxima.  For example, when I evaluate 

sol=maxima('solve_rec(a[n+2]+a[n+1]-2*a[n]=0,a[n],a[0]=1,a[1]=3)')

the value of sol is

a[n]=(-2)^(n+1)/3+5/3


Now what I'd like to do is define a Sage function that matches this output.  I haven't been about to do so.  Anyone know how?

Ken Levasseur
UMass Lowell





Ken Levasseur

unread,
Apr 4, 2013, 8:35:28 AM4/4/13
to sage...@googlegroups.com
Kent:
Thanks, I wasn't aware of the rhs function in maxima.
Ken


On Wednesday, April 3, 2013 3:42:12 PM UTC-4, Kent Morrison wrote:
You can define a Sage function by grabbing the right hand side of sol

a(n) = maxima('rhs(sol)')

 

kcrisman

unread,
Apr 4, 2013, 9:36:58 AM4/4/13
to sage...@googlegroups.com


On Thursday, April 4, 2013 8:35:28 AM UTC-4, Ken Levasseur wrote:
Kent:
Thanks, I wasn't aware of the rhs function in maxima.
Ken


There is also a .rhs() method for most symbolic things in Sage, just in case that helps some other place.

In this case, this leads to a problem.

First, implicit in Ken's post is that we need to load that package.

sage: maxima('load(solve_rec)')
"/Users/.../sage-5.7.rc0/local/share/maxima/5.29.1/share/solve_rec/solve_rec.mac"

That's fine.  But next,

sage: sol=maxima('solve_rec(a[n+2]+a[n+1]-2*a[n]=0,a[n],a[0]=1,a[1]=3)')
sage: sol
a[n]=(-2)^(n+1)/3+5/3
sage: type(sol)
sage.interfaces.maxima.MaximaElement
sage: a(n) = maxima('rhs(sol)')
sage: a
n |--> 0

This is because Maxima doesn't know what sol is, only Sage does.  In general Kent's solution would work, but here that little quirk prevents it from working.  Try instead

sage: sol.rhs()  # a Maxima method, so still a Maxima element
(-2)^(n+1)/3+5/3
sage: sol.rhs().sage()  # convert it to Sage
1/3*(-2)^(n + 1) + 5/3
sage: a(n) = sol.rhs().sage()
sage: a  # Hooray!
n |--> 1/3*(-2)^(n + 1) + 5/3
sage: a(55)  # is this right?
24019198012642647

This has the advantage of staying object-oriented, if one cares about such things (which I probably don't...)

- kcrisman
Reply all
Reply to author
Forward
0 new messages