Numerically stable simplification of sinc function

953 Views Asked by At

I would like to know if there is an alternate, explicit (non-iterative) form of the sinc function which behaves in a numerically stable way for all real numbers. The definition I am aware of is:

$$\texttt{sinc}(a) = \dfrac{\sin(a)}{a}$$

However, although this function is well-defined everywhere (even at 0), this function presents problems when implemented in code and evaluated at 0. I would like to know if there is an alternate form involving simple functions which would avoid possible division by 0 when we try to implement this is code.

Some programming packages (like numpy in python) provide a sinc function, presumably to sidestep this issue. But I would like to know generally if there is a better way of implementing it in an arbitrary language without resorting to some if statement. For instance, numpy simply checks if a=0. If it is, then it replaces a with some small value $\neq 0$. Specifically:

def sinc(x):
    y = pi * where(x == 0, 1.0e-20, x)
    return sin(y)/y

EDIT

As Sangchul Lee points out, you could consider the series form, but that representation will break down for large $a$. So the series form is essentially pushing the problem somewhere else (from a numerical point of view). Furthermore, you might consider switching between the two forms depending on whether $a$ is small, but that is introducing its own if statement, and I would like to avoid a piecewise solution.

Note: I have no idea what tags to use here. So please update the tags to what you view as appropriate.

2

There are 2 best solutions below

2
On BEST ANSWER

If it is a must, you may consider (with or without $\pi$) $$ \text{sinc}(x)\approx\frac{\sin\left(\pi\left|x\right|+\epsilon\right)}{\pi\left|x\right|+\epsilon}, $$ where $\epsilon>0$ is a small parameter, preferably your machine epsilon.

3
On

For small $x$ the following definition is very well-behaved numerically:

$$\mathrm{sinc}(x) = \frac{1}{\Gamma(1+\frac{x}{\pi})\Gamma(1-\frac{x}{\pi})}$$