[SciPy-User] Sparse matrix multiply

208 views
Skip to first unread message

Jaakko Luttinen

unread,
Mar 22, 2012, 5:34:52 AM3/22/12
to SciPy Users List
Hi!

Why do I get two different results for the code below?

import numpy as np
import scipy.sparse as sp
A = sp.rand(20,20,density=0.1)
B = sp.rand(20,20,density=0.1)
np.multiply(A,B).sum()
# out: 21.058793740984925
A.multiply(B).sum()
# out: 0.76482546226069481

Am I missing something?
I think numpy.multiply should either return the correct answer or an
error that it can't compute the correct answer.

Regards,
Jaakko
_______________________________________________
SciPy-User mailing list
SciPy...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

Christopher Mutel

unread,
Mar 22, 2012, 6:03:29 AM3/22/12
to SciPy Users List
On Thu, Mar 22, 2012 at 10:34 AM, Jaakko Luttinen
<jaakko....@aalto.fi> wrote:
> Hi!
>
> Why do I get two different results for the code below?
>
> import numpy as np
> import scipy.sparse as sp
> A = sp.rand(20,20,density=0.1)
> B = sp.rand(20,20,density=0.1)
> np.multiply(A,B).sum()
> # out: 21.058793740984925
> A.multiply(B).sum()
> # out: 0.76482546226069481
>
> Am I missing something?
> I think numpy.multiply should either return the correct answer or an
> error that it can't compute the correct answer.

np.multiply performs element-wise multiplication, while A.multiply is
matrix multiplication. They are both "correct", but answer different
questions.

See:
http://en.wikipedia.org/wiki/Matrix_multiplication
http://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html

> Regards,
> Jaakko
> _______________________________________________
> SciPy-User mailing list
> SciPy...@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

--
############################
Chris Mutel
Ökologisches Systemdesign - Ecological Systems Design
Institut f.Umweltingenieurwissenschaften - Institute for Environmental
Engineering
ETH Zürich - HIF C 44 - Schafmattstr. 6
8093 Zürich

Telefon: +41 44 633 71 45 - Fax: +41 44 633 10 61
############################

Jaakko Luttinen

unread,
Mar 22, 2012, 6:07:23 AM3/22/12
to SciPy Users List
On 03/22/2012 12:03 PM, Christopher Mutel wrote:
> On Thu, Mar 22, 2012 at 10:34 AM, Jaakko Luttinen
> <jaakko....@aalto.fi> wrote:
>> Hi!
>>
>> Why do I get two different results for the code below?
>>
>> import numpy as np
>> import scipy.sparse as sp
>> A = sp.rand(20,20,density=0.1)
>> B = sp.rand(20,20,density=0.1)
>> np.multiply(A,B).sum()
>> # out: 21.058793740984925
>> A.multiply(B).sum()
>> # out: 0.76482546226069481
>>
>> Am I missing something?
>> I think numpy.multiply should either return the correct answer or an
>> error that it can't compute the correct answer.
>
> np.multiply performs element-wise multiplication, while A.multiply is
> matrix multiplication. They are both "correct", but answer different
> questions.

Is it so..?

http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csc_matrix.multiply.html

I don't know what "point-wise multiplication" means..

Anyway, I thought that dot computes matrix multiplication and multiply
computes matrix multiplication.

-Jaakko

Jaakko Luttinen

unread,
Mar 22, 2012, 6:08:47 AM3/22/12
to SciPy Users List

TYPOFIX: I thought that multiply computes element-wise multiplication.

Pauli Virtanen

unread,
Mar 22, 2012, 6:18:40 AM3/22/12
to scipy...@scipy.org
22.03.2012 10:34, Jaakko Luttinen kirjoitti:
> import numpy as np
> import scipy.sparse as sp
> A = sp.rand(20,20,density=0.1)
> B = sp.rand(20,20,density=0.1)
> np.multiply(A,B).sum()
> # out: 0.76482546226069481
> Am I missing something?
> I think numpy.multiply should either return the correct answer or an
> error that it can't compute the correct answer.

The answer is the same as to your previous questions --- the Numpy
ufuncs do not deal with sparse matrices in a reasonable way. This lack
of integration between dense and sparse is a bug.

Why it does not raise an error instead, is probably that as a
consequence of the operation overloading rules defined, there is a
(nonsensical) operation that matches the call.

--
Pauli Virtanen

Jaakko Luttinen

unread,
Mar 22, 2012, 6:18:56 AM3/22/12
to SciPy Users List
On 03/22/2012 12:03 PM, Christopher Mutel wrote:
> On Thu, Mar 22, 2012 at 10:34 AM, Jaakko Luttinen
> <jaakko....@aalto.fi> wrote:
>> Hi!
>>
>> Why do I get two different results for the code below?
>>
>> import numpy as np
>> import scipy.sparse as sp
>> A = sp.rand(20,20,density=0.1)
>> B = sp.rand(20,20,density=0.1)
>> np.multiply(A,B).sum()
>> # out: 21.058793740984925
>> A.multiply(B).sum()
>> # out: 0.76482546226069481
>>
>> Am I missing something?
>> I think numpy.multiply should either return the correct answer or an
>> error that it can't compute the correct answer.
>
> np.multiply performs element-wise multiplication, while A.multiply is
> matrix multiplication. They are both "correct", but answer different
> questions.

Actually it seems that np.multiply computes matrix multiplication in
this case! Below, only A.multiply(B) computes element-wise multiplication.

import numpy as np
import scipy.sparse as sp
A = sp.rand(20,20,density=0.1)
B = sp.rand(20,20,density=0.1)
np.multiply(A,B).sum()

# out: 25.240683127057885
A.multiply(B).sum()
# out: 2.6382118196920503
A.dot(B).sum()
# out: 25.240683127057885
np.dot(A,B).sum()
# out: 25.240683127057885

-Jaakko

Christopher Mutel

unread,
Mar 22, 2012, 6:24:04 AM3/22/12
to SciPy Users List
On Thu, Mar 22, 2012 at 11:18 AM, Jaakko Luttinen

Indeed. Sorry for the confusion.

Reply all
Reply to author
Forward
0 new messages