On 8-Dec-09, at 8:59 PM, Dan Drake wrote:

> On Tue, 08 Dec 2009 at 10:44PM -0300, Pablo De Napoli wrote:
>> I'm trying to do some computations with Bessel functions using Sage.
>> Unfortunately, they don't seem to behave like other functions. For  
>> example:
>> to get the plot of the sine function over the interval (0,100)
>>
>> plot(sin(x),(x,0,100))
>>
>> works. However,
>>
>> plot(bessel_J(0,x),(x,0,100))
>>
>> does not.
>
> Part of the reason is that (I think) the Bessel functions are not
> implemented as fully symbolic functions; they are more numerical in
> nature under our current implementation.

Implementing a particular symbolic function is not outlandishly  
difficult, thanks to the tireless work of Burcin Erocal and Mike  
Hansen.  (Apologies to any contributers I have forgotten.)

You need to subclass sage.symbolic.function.SFunction.  I don't see  
many examples, so here is a minimal one:

from sage.symbolic.function import SFunction
from sage.rings.all import RealField

class bessel_J_class(SFunction):
     def __init__(self, *args, **kwds):
         kwds['nargs'] = 2
         kwds['evalf_func'] = self._evalf_func_
         SFunction.__init__(self, "bessel_J", *args, **kwds)

     def _evalf_func_(self, *args, **kwds):
         prec = kwds['prec']
         vals = [ arg.n(prec) for arg in args ]
         v = bessel_J(*vals)
         return RealField(prec)(v)

symbolic_bessel_J = bessel_J_class()

Then the following works for me:

sage: var('x')
sage: plot(symbolic_bessel_J(0, x), (x, 0, 100))

Nick

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

Reply via email to