up vote 0 down vote favorite
share [g+] share [fb]

I have some problem with matrix multiplication:

I want to multiplicate for example a and b:

a=array([1,3])                     # a is random and is array!!! (I have no impact on that)
                                     # there is a just for example what I want to do...

b=[[[1], [2]],                     #b is also random but always size(b)=  even 
   [[3], [2]], 
   [[4], [6]],
   [[2], [3]]]

So what I want is to multiplicate in this way

[1,3]*[1;2]=7
[1,3]*[3;2]=9
[1,3]*[4;6]=22
[1,3]*[2;3]=11

So result what I need will look:

x1=[7,9]
x2=[22,8]

I know is very complicated but I try 2 hours to implement this but without success :(

link|improve this question

2  
that doesn't look much like matrix multiplication to me – David Heffernan May 10 '11 at 20:15
I'm not very good at english, sorry for wrong USE, but I think it's clear what I want to do.... Many thanks – thaking May 10 '11 at 20:22
There is an error in your example - shouldn't the last entry be [1,3]*[2,3] = 11? – talonmies May 10 '11 at 20:26
Is this related to your eigenvalue question? – David Heffernan May 10 '11 at 20:27
Are you sure b is supposed to have 3 dimensions? – Jim Brissom May 10 '11 at 20:29
feedback

3 Answers

up vote 2 down vote accepted

How about this:

In [16]: a
Out[16]: array([1, 3])

In [17]: b
Out[17]: 
array([[1, 2],
       [3, 2],
       [4, 6],
       [2, 3]])

In [18]: np.array([np.dot(a,row) for row in b]).reshape(-1,2)
Out[18]: 
array([[ 7,  9],
       [22, 11]])
link|improve this answer
3  
Unnecessary complicated solution, why not just dot(b, a)?. Thanks – eat May 11 '11 at 9:38
feedback
result = \
[[sum(reduce(lambda x,y:x[0]*y[0]+x[1]*y[1],(a,[b1 for b1 in row]))) \
  for row in b][i:i+2] \
    for i in range(0, len(b),2)]
link|improve this answer
feedback

Your b seem to have an unnecessary dimension.

With proper b you can just use dot(.), like:

In []: a
Out[]: array([1, 3])
In []: b
Out[]:
array([[1, 2],
       [3, 2],
       [4, 6],
       [2, 3]])
In []: dot(b, a).reshape((2, -1))
Out[]:
array([[ 7,  9],
       [22, 11]])
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.