Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
3

Sinc function

asked 10 years ago

ajd gravatar image

updated 10 years ago

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?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 10 years ago

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.

Preview: (hide)
link
2

answered 3 years ago

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
Preview: (hide)
link

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: 10 years ago

Seen: 1,542 times

Last updated: Feb 10 '22