Ask Your Question

Sinc function

asked 2014-08-13 19:57:09 +0200

ajd gravatar image

updated 2014-08-14 20:14:09 +0200

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-08-16 08:46:07 +0200

rws gravatar image

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.

edit flag offensive delete link more

answered 2022-02-10 22:57:49 +0200

mwageringel gravatar image

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)


sage: sinc(0)
sage: sinc(-1)
sage: sinc(1).n()
sage: sinc(0.01)
sage: sinc(pi)
sage: 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)
sage: sinc(x).integral(x)
sage: bool(sinc(2*x^2).diff(x) == (sin(2*x^2) / (2*x^2)).diff(x))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-08-13 19:57:09 +0200

Seen: 1,090 times

Last updated: Feb 10 '22