# [SciPy-user] Problem with signal.correlate and sparse matrices

Andreas Mueller andrmuel@ee.ethz...
Thu Mar 19 08:05:07 CDT 2009

```Hello!

I'm trying to use signal.correlate with sparse matrices, but I can't
understand the results I get. As soon as I use sparse matrices, the
results are weird, e.g. (my actual arrays are much larger of course):

In [1]: from scipy import signal,sparse,array,matrix
In [2]: a = array([[1,2,3],[4,5,6]])
In [3]: b = array([[1,1],[1,1]])
In [4]: print signal.correlate(a,b,mode='valid')
[[12 16]]

-> OK, thats what I would expect to get

In [5]: ma = matrix(a)
In [6]: mb = matrix(b)
In [7]: print signal.correlate(ma,mb,mode='valid')
[[12 16]]

-> OK, same result

In [8]: sa = sparse.lil_matrix(ma)
In [9]: sb = sparse.lil_matrix(mb)
In [10]: print signal.correlate(sa,sb,mode='valid')
[..]
ValueError: dimension mismatch

-> why? After some trying around, I noticed that it works with the first
matrix transposed:

In [11]: print signal.correlate(sa.T,sb,mode='valid')
(0, 1)        5
(0, 0)        5
(1, 1)        7
(1, 0)        7
(2, 1)        9
(2, 0)        9

But the result is weird. Obviously, 5+7=12 and 7+9=16, which is the result I
expect, but what am I doing (or thinking) wrong?

Is signal.correlate supported with sparse matrices?

I'm pretty much a SciPy newbie and haven't used matrices before, but there
seems to be no such thing as sparse arrays, so I guess I have no choice ..

Regards, Andreas

PS I tried this both on SciPy 0.6.0 (Ubuntu 8.10) and SciPy 0.7.0
(Gentoo), with the same results (expect I get a shape mismatch error
instead of a dimension mismatch with 0.6.0).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20090319/6ac3418d/attachment.bin
```