Skip to content

Commit 39de3dc

Browse files
ENH: dpnp.convolve fallback; tests (#798)
1 parent 35c12e2 commit 39de3dc

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

dpnp/dpnp_iface_mathematical.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"ceil",
5757
"conj",
5858
"conjugate",
59+
"convolve",
5960
"copysign",
6061
"cross",
6162
"cumprod",
@@ -317,6 +318,23 @@ def conjugate(x1, **kwargs):
317318
conj = conjugate
318319

319320

321+
def convolve(a, v, mode='full'):
322+
"""
323+
Returns the discrete, linear convolution of two one-dimensional sequences.
324+
325+
For full documentation refer to :obj:`numpy.convolve`.
326+
327+
Examples
328+
--------
329+
>>> ca = dpnp.convolve([1, 2, 3], [0, 1, 0.5])
330+
>>> print(ca)
331+
[0. , 1. , 2.5, 4. , 1.5]
332+
333+
"""
334+
335+
return call_origin(numpy.convolve, a=a, v=v, mode=mode)
336+
337+
320338
def copysign(x1, x2, dtype=None, out=None, where=True, **kwargs):
321339
"""
322340
Change the sign of x1 to that of x2, element-wise.

tests/test_mathematical.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@
55
import numpy
66

77

8+
class TestConvolve:
9+
def test_object(self):
10+
d = [1.] * 100
11+
k = [1.] * 3
12+
numpy.testing.assert_array_almost_equal(dpnp.convolve(d, k)[2:-2], dpnp.full(98, 3))
13+
14+
def test_no_overwrite(self):
15+
d = dpnp.ones(100)
16+
k = dpnp.ones(3)
17+
dpnp.convolve(d, k)
18+
numpy.testing.assert_array_equal(d, dpnp.ones(100))
19+
numpy.testing.assert_array_equal(k, dpnp.ones(3))
20+
21+
def test_mode(self):
22+
d = dpnp.ones(100)
23+
k = dpnp.ones(3)
24+
default_mode = dpnp.convolve(d, k, mode='full')
25+
full_mode = dpnp.convolve(d, k, mode='f')
26+
numpy.testing.assert_array_equal(full_mode, default_mode)
27+
# integer mode
28+
with numpy.testing.assert_raises(ValueError):
29+
dpnp.convolve(d, k, mode=-1)
30+
numpy.testing.assert_array_equal(dpnp.convolve(d, k, mode=2), full_mode)
31+
# illegal arguments
32+
with numpy.testing.assert_raises(TypeError):
33+
dpnp.convolve(d, k, mode=None)
34+
35+
836
@pytest.mark.parametrize("array",
937
[[[0, 0], [0, 0]],
1038
[[1, 2], [1, 2]],

0 commit comments

Comments
 (0)