This may not be perfect yet, but seems to work. This uses NumPy for numeric evaluation and SymPy for symbolic evaluation (e.g. of sinc(0)
).
import numpy as np
import sympy
def sinc_evalf(self, x, parent=None, algorithm=None):
if parent is None:
parent = RR
return parent(np.sinc(x / np.pi))
def sinc_eval(self, x):
return sympy.functions.sinc(x._sympy_())
def sinc_deriv(self, x, diff_param=None):
_x = SR.var("_x")
return (sin(_x) / _x).diff(_x).subs({_x: x})
sinc = function("sinc", nargs=1, evalf_func=sinc_evalf,
eval_func=sinc_eval, derivative_func=sinc_deriv)
# dirty workaround to make conversion from sympy to sage work
sympy.functions.sinc._sage_ = lambda self: sinc(*(arg._sage_() for arg in self.args), hold=True)
Examples:
sage: sinc(0)
1
sage: sinc(-1)
sinc(1)
sage: sinc(1).n()
0.841470984807897
sage: sinc(0.01)
0.999983333416666
sage: sinc(pi)
0
sage: sinc(x)
sinc(x)
sage: sinc(x).diff(x)
cos(x)/x - sin(x)/x^2
sage: x, y = SR.var("x,y")
sage: sinc(x).diff(y)
0
sage: sinc(x).integral(x)
sin_integral(x)
sage: bool(sinc(2*x^2).diff(x) == (sin(2*x^2) / (2*x^2)).diff(x))
True