[SciPy-user] Mathematica Element-wise Multiplication
Tom Johnson
tjhnson@gmail....
Fri Dec 14 23:26:59 CST 2007
While playing with Mathematica, I must say that I was surprised to
find that it handled element-wise multiplication differently from
scipy.
In scipy,
>>> A = array([[1,2],[3,4]])
>>> B = array([2,3])
>>> A * B
array([[2,6],[6,12]])
which essentially multiplies each COLUMN of A with each COLUMN of B.
In mathematica,
>>> A = {{1,2},{3,4}}
>>> B = {2,3}
>>> A * B
{{2,4},{9,12}}
which essentially multiples each ROW of A with each COLUMN of B. I
actually find this method of multiplication more intuitive (as it is
like matrix multiplication).
Now, scipy can easily obtain the Mathematica result by the following:
>>> A = array([[1,2],[3,4]])
>>> B = array([[2],[3]])
>>> A * B
array([[2,4],[9,12]])
but it isn't as easy to get Mathematica to return the scipy
result...that is, the following natural attempt does not work:
>>> A = {{1,2},{3,4}}
>>> B = {{2},{3}}
>>> A * B
Thread::tdlen: Objects of unequal length in {1,2}{2} cannot be combined.
Thread::tdlen: Objects of unequal length in {3,4}{3} cannot be combined.
and it is easy to see why Mathematica is unable to complete this
operation...but of course there are other ways to achieve this result.
Anyway, I just thought the difference was interesting....
More information about the SciPy-user
mailing list