Hi all, I have been using numpy.correlate and was finding something weird. I now think that there might be a bug. Correlations should be order dependent eg. correlate(x,y) != correlate(y,x) in general (whereas convolutions are symmetric)
import numpy as N x = N.array([1,0,0]) y = N.array([0,0,1])
N.correlate(x,y,'full') array([1, 0, 0, 0, 0]) N.correlate(y,x,'full') array([0, 0, 0, 0, 1])
This works fine. However, if the arrays have different lengths, we get a problem.
y2=N.array([0,0,0,1]) N.correlate(x,y2,'full') array([0, 0, 0, 0, 0, 1]) N.correlate(y2,x,'full') array([0, 0, 0, 0, 0, 1])
I believe that somewhere in the code, the arrays are re-ordered by their length. Initially I thought that this was because correlate was deriving from convolution but looking at numpy.core, I can see that in fact convolution derives from correlate. After that, it becomes C code which I haven't managed to look at yet. Am I correct, is this a bug? regards Rob Steed
2009/5/18 rob steed
This works fine. However, if the arrays have different lengths, we get a problem.
y2=N.array([0,0,0,1]) N.correlate(x,y2,'full')
This looks like a bug to me. In [54]: N.correlate([1, 0, 0, 0], [0, 0, 0, 1],'full') Out[54]: array([1, 0, 0, 0, 0, 0, 0]) In [55]: N.correlate([1, 0, 0, 0, 0], [0, 0, 0, 1],'full') Out[55]: array([1, 0, 0, 0, 0, 0, 0, 0]) In [56]: N.correlate([1, 0, 0, 0, 0], [0, 0, 0, 0, 1],'full') Out[56]: array([1, 0, 0, 0, 0, 0, 0, 0, 0]) In [57]: N.correlate([1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1],'full') Out[57]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) Regards Stéfan
2009/5/18 Stéfan van der Walt
2009/5/18 rob steed
: This works fine. However, if the arrays have different lengths, we get a problem.
y2=N.array([0,0,0,1]) N.correlate(x,y2,'full')
This looks like a bug to me.
In [54]: N.correlate([1, 0, 0, 0], [0, 0, 0, 1],'full') Out[54]: array([1, 0, 0, 0, 0, 0, 0])
In [55]: N.correlate([1, 0, 0, 0, 0], [0, 0, 0, 1],'full') Out[55]: array([1, 0, 0, 0, 0, 0, 0, 0])
In [56]: N.correlate([1, 0, 0, 0, 0], [0, 0, 0, 0, 1],'full') Out[56]: array([1, 0, 0, 0, 0, 0, 0, 0, 0])
In [57]: N.correlate([1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1],'full') Out[57]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
comparing with scipy: signal.correlate behaves the same "flipping" way as np.correlate, ndimage.correlate keeps the orientation.
np.correlate([1, 2, 0, 0, 0], [0, 0, 1, 0, 0,0],'same') array([0, 0, 0, 2, 1, 0]) np.correlate([1, 2, 0, 0, 0], [0, 0, 1, 0, 0],'same') array([1, 2, 0, 0, 0]) np.correlate([1, 2, 0, 0, 0], [0, 0, 1, 0, 0, 0],'full') array([0, 0, 0, 0, 0, 2, 1, 0, 0, 0]) np.correlate([1, 2, 0, 0, 0], [0, 0, 1, 0, 0],'full') array([0, 0, 1, 2, 0, 0, 0, 0, 0])
signal.correlate([1, 2, 0, 0, 0], [0, 0, 1, 0, 0, 0]) array([0, 0, 0, 0, 0, 2, 1, 0, 0, 0]) signal.correlate([1, 2, 0, 0, 0], [0, 0, 1, 0, 0]) array([0, 0, 1, 2, 0, 0, 0, 0, 0]) ndimage.filters.correlate([1, 2, 0, 0, 0], [0, 0, 1, 0, 0, 0],mode='constant') array([0, 1, 2, 0, 0]) ndimage.filters.correlate([1, 2, 0, 0, 0], [0, 0, 1, 0, 0],mode='constant') array([1, 2, 0, 0, 0])
Josef
participants (3)
-
josef.pktd@gmail.com
-
rob steed
-
Stéfan van der Walt