2

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 :(

5
  • 2
    that doesn't look much like matrix multiplication to me Commented May 10, 2011 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 Commented May 10, 2011 at 20:22
  • There is an error in your example - shouldn't the last entry be [1,3]*[2,3] = 11? Commented May 10, 2011 at 20:26
  • Is this related to your eigenvalue question? Commented May 10, 2011 at 20:27
  • Are you sure b is supposed to have 3 dimensions? Commented May 10, 2011 at 20:29

3 Answers 3

7

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]])
Sign up to request clarification or add additional context in comments.

Comments

3

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]])

1 Comment

Unnecessary complicated solution, why not just dot(b, a)?. Thanks
-1
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)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.