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

lanceboyle@qwest.net lanceboyle at qwest.net
Mon Dec 12 23:35:45 CST 2005

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

> On Mon, 12 Dec 2005, Sebastian Haase wrote:
>
>
>
>> Hi,
>> I was trying to implement a "sinc" [sin(x)/x] in numarray. But the
>> "where"-semantics makes it choke on the x=0: 0/0 case.
>> (This should of course work for x being an array - so "if" is no
>> option)
>> The best I could come up with was:
>>
>> def sinc(r):
>>     na.Error.pushMode(all="ignore")
>>     a = na.where(r, na.divide(na.sin(r),r), 1)
>>     na.Error.popMode()
>>     return a
>>
>> but I still seem to get  a warning...
>>
>>
>>
>>>>> F.sinc(0)
>>>>>
>>>>>
>>
>> Warning: Encountered invalid numeric result(s)  in divide
>> 1.0
>>
>> What is a better way of doing this ?
>>
>>
>
> 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.

>
> sinc_vectorized=scipy.vectorize(sinc)
>
> HTH,
>
> Arnd
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
>
>