# Sinc function

I am trying to perform some calculus involving the function $f(x) = \sin(x)/x$ in Sage. This function has a removable sigularity at the origin. Is there a way that I can "modify" the function in Sage to set $f(0) = 1$ while preserving the ability to do things like symbolically differentiate it?

edit retag close merge delete

Sort by » oldest newest most voted

The general answer is to write your own symbolic sinc function. See the symbolic functions wiki page on trac for how to do this and lots of example tickets.

more

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

more