[SciPy-user] how to make a sinc(x) function - "divide by zero" !

Arnd Baecker arnd.baecker at web.de
Tue Dec 13 00:45:43 CST 2005


On Mon, 12 Dec 2005 lanceboyle at qwest.net wrote:

> On Dec 12, 2005, at 10:39 AM, Arnd Baecker wrote:

[...]

> > Yuo could try to use `vectorize` (warning: untested code)
> >
> > def sinc(x):
> >     if x==0.0:                # presumably better to check for small x
> >         return 0.0            # here ...
> >     else:
> >        return sin(x)/x
> >
>
> sinc(0.0) is 1.0, not 0.0.

Presumably that's one thing which I anticipated with
"untested code". Clearly a nice way of showing my competence ;-).
((note to self: should stop trying to give advice when
already rushing out of the door.))

> > sinc_vectorized=scipy.vectorize(sinc)

I just saw the scipy.special also implements `sinc(x)`:

In [3]:scipy.special.sinc??
Type:           function
Base Class:     <type 'function'>
String Form:    <function sinc at 0x412170d4>
Namespace:      Interactive
File:           /usr/lib/python2.3/site-packages/scipy/special/basic.py
Definition:     scipy.special.sinc(x)
Source:
def sinc(x):
    """Returns sin(pi*x)/(pi*x) at all points of array x.
    """
    w = asarray(asarray(x)*pi)
    return where(x==0, 1.0, sin(w)/w)

Best, Arnd



More information about the SciPy-user mailing list