Skip to main content

xsf/numpy/
function_base.rs

1//! <https://github.com/numpy/numpy/blob/4992eaf/numpy/lib/_function_base_impl.py>
2use core::f64::consts::PI;
3
4/// Normalized sinc function
5///
6/// The sinc function is equal to $\sin(\pi x) / (\pi x)$ for any argument $x \ne 0$.
7/// $\mathrm{sinc}(0)$ takes the limit value 1, making $\mathrm{sinc}$ not only everywhere
8/// continuous but also infinitely differentiable.
9///
10/// See [NumPy's documentation](https://numpy.org/doc/stable/reference/generated/numpy.sinc.html)
11/// for more details.
12#[must_use]
13#[inline]
14pub fn sinc(x: f64) -> f64 {
15    if x == 0.0 {
16        1.0
17    } else {
18        // Unlike in NumPy, sin(pi x) is computed with `sinpi`, as the rounding error of y = pi x in
19        // sin(y) / y is amplified for large x and near the nonzero integers, where sinc is 0.
20        crate::sinpi(x) / (PI * x)
21    }
22}