Ask Your Question
3

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
1

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
2

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)

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
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

Stats

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

Seen: 650 times

Last updated: Feb 10