diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/code_generators/numpy_api_order.txt | 2 | ||||
-rw-r--r-- | numpy/core/numeric.py | 69 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 18 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 18 |
4 files changed, 48 insertions, 59 deletions
diff --git a/numpy/core/code_generators/numpy_api_order.txt b/numpy/core/code_generators/numpy_api_order.txt index 3d1e40add..2b71fc547 100644 --- a/numpy/core/code_generators/numpy_api_order.txt +++ b/numpy/core/code_generators/numpy_api_order.txt @@ -173,5 +173,5 @@ PyArray_CompareString PyArray_MultiIterFromObjects PyArray_GetEndianness PyArray_GetNDArrayCFeatureVersion -PyArray_Acorrelate +PyArray_Correlate2 PyArray_NeighborhoodIterNew diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index b497e4898..31b021b71 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -6,7 +6,7 @@ __all__ = ['newaxis', 'ndarray', 'flatiter', 'ufunc', 'set_numeric_ops', 'can_cast', 'asarray', 'asanyarray', 'ascontiguousarray', 'asfortranarray', 'isfortran', 'empty_like', 'zeros_like', - 'correlate', 'acorrelate', 'convolve', 'inner', 'dot', 'outer', 'vdot', + 'correlate', 'convolve', 'inner', 'dot', 'outer', 'vdot', 'alterdot', 'restoredot', 'roll', 'rollaxis', 'cross', 'tensordot', 'array2string', 'get_printoptions', 'set_printoptions', 'array_repr', 'array_str', 'set_string_function', @@ -22,6 +22,7 @@ __all__ = ['newaxis', 'ndarray', 'flatiter', 'ufunc', 'CLIP', 'RAISE', 'WRAP', 'MAXDIMS', 'BUFSIZE', 'ALLOW_THREADS'] import sys +import warnings import multiarray import umath from umath import * @@ -585,7 +586,7 @@ def _mode_from_name(mode): return _mode_from_name_dict[mode.lower()[0]] return mode -def correlate(a,v,mode='valid'): +def correlate(a,v,mode='valid',old_behavior=True): """ Discrete, linear correlation of two 1-dimensional sequences. @@ -602,14 +603,25 @@ def correlate(a,v,mode='valid'): mode : {'valid', 'same', 'full'}, optional Refer to the `convolve` docstring. Note that the default is `valid`, unlike `convolve`, which uses `full`. + old_behavior : bool + If True, uses the old, numeric behavior (correlate(a,v) == correlate(v, + a), and the conjugate is not taken for complex arrays). If False, uses + the conventional signal processing definition (see note). See Also -------- convolve : Discrete, linear convolution of two one-dimensional sequences. - acorrelate: Discrete correlation following the usual signal processing - definition for complex arrays, and without assuming that - correlate(a, b) == correlate(b, a) + + Note + ---- + If old_behavior is False, this function computes the correlation as + generally defined in signal processing texts:: + + z[k] = sum_n a[n] * conj(v[n+k]) + + with a and v sequences being zero-padded where necessary and conj being the + conjugate. Examples -------- @@ -622,42 +634,17 @@ def correlate(a,v,mode='valid'): """ mode = _mode_from_name(mode) - return multiarray.correlate(a,v,mode) - -def acorrelate(a, v, mode='valid'): - """ - Discrete, linear correlation of two 1-dimensional sequences. - - This function computes the correlation as generally defined in signal - processing texts:: - - z[k] = sum_n a[n] * conj(v[n+k]) - - with a and v sequences being zero-padded where necessary and conj being the - conjugate. - - Parameters - ---------- - a, v : array_like - Input sequences. - mode : {'valid', 'same', 'full'}, optional - Refer to the `convolve` docstring. Note that the default - is `valid`, unlike `convolve`, which uses `full`. - - See Also - -------- - convolve : Discrete, linear convolution of two - one-dimensional sequences. - correlate: Deprecated function to compute correlation - - Notes - ----- - This is the function which corresponds to matlab xcorr. - - """ - mode = _mode_from_name(mode) - return multiarray.acorrelate(a, v, mode) - + if old_behavior: + warnings.warn(""" +The current behavior of correlate is deprecated for 1.4.0, and will be removed +for NumPy 1.5.0. + +The new behavior fits the conventional definition of correlation: inputs are +never swapped, and the second argument is conjugated for complex arrays.""", + DeprecationWarning) + return multiarray.correlate(a,v,mode) + else: + return multiarray.correlate2(a,v,mode) def convolve(a,v,mode='full'): """ diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 8f71a4d14..c45952910 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -906,7 +906,7 @@ PyArray_CopyAndTranspose(PyObject *op) } /* - * Implementation which is common between PyArray_Correlate and PyArray_Acorrelate + * Implementation which is common between PyArray_Correlate and PyArray_Correlate2 * * inverted is set to 1 if computed correlate(ap2, ap1), 0 otherwise */ @@ -1065,13 +1065,13 @@ _pyarray_revert(PyArrayObject *ret) } /*NUMPY_API - * acorrelate(a1,a2,mode) + * correlate(a1,a2,mode) * - * This function computes the usual correlation (acorrelate(a1, a2) != - * accorrelate(a2, a1), and conjugate the second argument for complex inputs + * This function computes the usual correlation (correlate(a1, a2) != + * correlate(a2, a1), and conjugate the second argument for complex inputs */ NPY_NO_EXPORT PyObject * -PyArray_Acorrelate(PyObject *op1, PyObject *op2, int mode) +PyArray_Correlate2(PyObject *op1, PyObject *op2, int mode) { PyArrayObject *ap1, *ap2, *ret = NULL; int typenum; @@ -1796,7 +1796,7 @@ static PyObject *array_correlate(PyObject *NPY_UNUSED(dummy), PyObject *args, Py } static PyObject* -array_acorrelate(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) +array_correlate2(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) { PyObject *shape, *a0; int mode = 0; @@ -1806,7 +1806,7 @@ array_acorrelate(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) &a0, &shape, &mode)) { return NULL; } - return PyArray_Acorrelate(a0, shape, mode); + return PyArray_Correlate2(a0, shape, mode); } static PyObject * @@ -2416,8 +2416,8 @@ static struct PyMethodDef array_module_methods[] = { {"correlate", (PyCFunction)array_correlate, METH_VARARGS | METH_KEYWORDS, NULL}, - {"acorrelate", - (PyCFunction)array_acorrelate, + {"correlate2", + (PyCFunction)array_correlate2, METH_VARARGS | METH_KEYWORDS, NULL}, {"frombuffer", (PyCFunction)array_frombuffer, diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 18f879cb4..9353bc960 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -896,19 +896,20 @@ class _TestCorrelate(TestCase): def test_float(self): self._setup(np.float) - z = np.correlate(self.x, self.y, 'full') - assert_array_almost_equal(z, self.z1) - z = np.correlate(self.y, self.x, 'full') + z = np.correlate(self.x, self.y, 'full', old_behavior=self.old_behavior) assert_array_almost_equal(z, self.z1) + z = np.correlate(self.y, self.x, 'full', old_behavior=self.old_behavior) + assert_array_almost_equal(z, self.z2) def test_object(self): self._setup(Decimal) - z = np.correlate(self.x, self.y, 'full') - assert_array_almost_equal(z, self.z1) - z = np.correlate(self.y, self.x, 'full') + z = np.correlate(self.x, self.y, 'full', old_behavior=self.old_behavior) assert_array_almost_equal(z, self.z1) + z = np.correlate(self.y, self.x, 'full', old_behavior=self.old_behavior) + assert_array_almost_equal(z, self.z2) class TestCorrelate(_TestCorrelate): + old_behavior = True def _setup(self, dt): # correlate uses an unconventional definition so that correlate(a, b) # == correlate(b, a), so force the corresponding outputs to be the same @@ -923,7 +924,8 @@ class TestCorrelate(_TestCorrelate): z = np.correlate(x, y, 'full') assert_array_almost_equal(z, r_z) -class TestAcorrelate(_TestCorrelate): +class TestCorrelateNew(_TestCorrelate): + old_behavior = False def test_complex(self): x = np.array([1, 2, 3, 4+1j], dtype=np.complex) y = np.array([-1, -2j, 3+1j], dtype=np.complex) @@ -932,7 +934,7 @@ class TestAcorrelate(_TestCorrelate): #assert_array_almost_equal(z, r_z) r_z = r_z[::-1].conjugate() - z = np.acorrelate(y, x, 'full') + z = np.correlate(y, x, 'full', old_behavior=self.old_behavior) assert_array_almost_equal(z, r_z) class TestArgwhere: |