Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9972e60

Browse files
authored
add support for numpy.correlate and unit test (#1990)
1 parent 8eb3e31 commit 9972e60

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pint/facets/numpy/numpy_func.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,15 @@ def _trapz(y, x=None, dx=1.0, **kwargs):
779779
return y.units._REGISTRY.Quantity(ret, units)
780780

781781

782+
@implements("correlate", "function")
783+
def _correlate(a, v, mode="valid", **kwargs):
784+
a = _base_unit_if_needed(a)
785+
v = _base_unit_if_needed(v)
786+
units = a.units * v.units
787+
ret = np.correlate(a._magnitude, v._magnitude, mode=mode, **kwargs)
788+
return a.units._REGISTRY.Quantity(ret, units)
789+
790+
782791
def implement_mul_func(func):
783792
# If NumPy is not available, do not attempt implement that which does not exist
784793
if np is None:

pint/testsuite/test_numpy_func.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ def test_trapz_no_autoconvert(self):
216216
with pytest.raises(OffsetUnitCalculusError):
217217
np.trapz(t, x=z)
218218

219+
def test_correlate(self):
220+
a = self.Q_(np.array([1, 2, 3]), "m")
221+
v = self.Q_(np.array([0, 1, 0.5]), "s")
222+
res = np.correlate(a, v, "full")
223+
ref = np.array([0.5, 2.0, 3.5, 3.0, 0.0])
224+
assert np.array_equal(res.magnitude, ref)
225+
assert res.units == "meter * second"
226+
219227
def test_dot(self):
220228
with ExitStack() as stack:
221229
stack.callback(

0 commit comments

Comments
 (0)