Skip to content

Commit bd00f3a

Browse files
ENH: add sinc (#20)
* ENH: add `sinc` * TST: sinc: add tests * DOC: sinc: tweak * DOC: sinc: more tweaks * TST: sinc: add 3D test * improve `where` call Co-authored-by: jakirkham <[email protected]> * Apply suggestions from code review * DOC: sinc: add `xp` param * Update _funcs.py Co-authored-by: jakirkham <[email protected]> * appease formatter --------- Co-authored-by: jakirkham <[email protected]>
1 parent 3dad7f7 commit bd00f3a

File tree

4 files changed

+109
-4
lines changed

4 files changed

+109
-4
lines changed

Diff for: docs/api-reference.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
cov
1111
expand_dims
1212
kron
13+
sinc
1314
```

Diff for: src/array_api_extra/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

3-
from ._funcs import atleast_nd, cov, expand_dims, kron
3+
from ._funcs import atleast_nd, cov, expand_dims, kron, sinc
44

55
__version__ = "0.1.2.dev0"
66

7-
__all__ = ["__version__", "atleast_nd", "cov", "expand_dims", "kron"]
7+
__all__ = ["__version__", "atleast_nd", "cov", "expand_dims", "kron", "sinc"]

Diff for: src/array_api_extra/_funcs.py

+86-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if TYPE_CHECKING:
77
from ._typing import Array, ModuleType
88

9-
__all__ = ["atleast_nd", "cov", "expand_dims", "kron"]
9+
__all__ = ["atleast_nd", "cov", "expand_dims", "kron", "sinc"]
1010

1111

1212
def atleast_nd(x: Array, /, *, ndim: int, xp: ModuleType) -> Array:
@@ -348,3 +348,88 @@ def kron(a: Array, b: Array, /, *, xp: ModuleType) -> Array:
348348
a_shape = xp.asarray(a_shape)
349349
b_shape = xp.asarray(b_shape)
350350
return xp.reshape(result, tuple(xp.multiply(a_shape, b_shape)))
351+
352+
353+
def sinc(x: Array, /, *, xp: ModuleType) -> Array:
354+
r"""
355+
Return the normalized sinc function.
356+
357+
The sinc function is equal to :math:`\sin(\pi x)/(\pi x)` for any argument
358+
:math:`x\ne 0`. ``sinc(0)`` takes the limit value 1, making ``sinc`` not
359+
only everywhere continuous but also infinitely differentiable.
360+
361+
.. note::
362+
363+
Note the normalization factor of ``pi`` used in the definition.
364+
This is the most commonly used definition in signal processing.
365+
Use ``sinc(x / xp.pi)`` to obtain the unnormalized sinc function
366+
:math:`\sin(x)/x` that is more common in mathematics.
367+
368+
Parameters
369+
----------
370+
x : array
371+
Array (possibly multi-dimensional) of values for which to calculate
372+
``sinc(x)``. Must have a real floating point dtype.
373+
xp : array_namespace
374+
The standard-compatible namespace for `x`.
375+
376+
Returns
377+
-------
378+
res : array
379+
``sinc(x)`` calculated elementwise, which has the same shape as the input.
380+
381+
Notes
382+
-----
383+
The name sinc is short for "sine cardinal" or "sinus cardinalis".
384+
385+
The sinc function is used in various signal processing applications,
386+
including in anti-aliasing, in the construction of a Lanczos resampling
387+
filter, and in interpolation.
388+
389+
For bandlimited interpolation of discrete-time signals, the ideal
390+
interpolation kernel is proportional to the sinc function.
391+
392+
References
393+
----------
394+
.. [1] Weisstein, Eric W. "Sinc Function." From MathWorld--A Wolfram Web
395+
Resource. https://mathworld.wolfram.com/SincFunction.html
396+
.. [2] Wikipedia, "Sinc function",
397+
https://en.wikipedia.org/wiki/Sinc_function
398+
399+
Examples
400+
--------
401+
>>> import array_api_strict as xp
402+
>>> import array_api_extra as xpx
403+
>>> x = xp.linspace(-4, 4, 41)
404+
>>> xpx.sinc(x, xp=xp)
405+
Array([-3.89817183e-17, -4.92362781e-02,
406+
-8.40918587e-02, -8.90384387e-02,
407+
-5.84680802e-02, 3.89817183e-17,
408+
6.68206631e-02, 1.16434881e-01,
409+
1.26137788e-01, 8.50444803e-02,
410+
-3.89817183e-17, -1.03943254e-01,
411+
-1.89206682e-01, -2.16236208e-01,
412+
-1.55914881e-01, 3.89817183e-17,
413+
2.33872321e-01, 5.04551152e-01,
414+
7.56826729e-01, 9.35489284e-01,
415+
1.00000000e+00, 9.35489284e-01,
416+
7.56826729e-01, 5.04551152e-01,
417+
2.33872321e-01, 3.89817183e-17,
418+
-1.55914881e-01, -2.16236208e-01,
419+
-1.89206682e-01, -1.03943254e-01,
420+
-3.89817183e-17, 8.50444803e-02,
421+
1.26137788e-01, 1.16434881e-01,
422+
6.68206631e-02, 3.89817183e-17,
423+
-5.84680802e-02, -8.90384387e-02,
424+
-8.40918587e-02, -4.92362781e-02,
425+
-3.89817183e-17], dtype=array_api_strict.float64)
426+
427+
"""
428+
if not xp.isdtype(x.dtype, "real floating"):
429+
err_msg = "`x` must have a real floating data type."
430+
raise ValueError(err_msg)
431+
# no scalars in `where` - array-api#807
432+
y = xp.pi * xp.where(
433+
x, x, xp.asarray(xp.finfo(x.dtype).smallest_normal, dtype=x.dtype)
434+
)
435+
return xp.sin(y) / y

Diff for: tests/test_funcs.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010
from numpy.testing import assert_allclose, assert_array_equal, assert_equal
1111

12-
from array_api_extra import atleast_nd, cov, expand_dims, kron
12+
from array_api_extra import atleast_nd, cov, expand_dims, kron, sinc
1313

1414
if TYPE_CHECKING:
1515
Array = Any # To be changed to a Protocol later (see array-api#589)
@@ -224,3 +224,22 @@ def test_positive_negative_repeated(self):
224224
a = xp.empty((2, 3, 4, 5))
225225
with pytest.raises(ValueError, match="Duplicate dimensions"):
226226
expand_dims(a, axis=(3, -3), xp=xp)
227+
228+
229+
class TestSinc:
230+
def test_simple(self):
231+
assert_array_equal(sinc(xp.asarray(0.0), xp=xp), xp.asarray(1.0))
232+
w = sinc(xp.linspace(-1, 1, 100), xp=xp)
233+
# check symmetry
234+
assert_allclose(w, xp.flip(w, axis=0))
235+
236+
@pytest.mark.parametrize("x", [0, 1 + 3j])
237+
def test_dtype(self, x):
238+
with pytest.raises(ValueError, match="real floating data type"):
239+
sinc(xp.asarray(x), xp=xp)
240+
241+
def test_3d(self):
242+
x = xp.reshape(xp.arange(18, dtype=xp.float64), (3, 3, 2))
243+
expected = xp.zeros((3, 3, 2))
244+
expected[0, 0, 0] = 1.0
245+
assert_allclose(sinc(x, xp=xp), expected, atol=1e-15)

0 commit comments

Comments
 (0)