Python Matrix multiplication; numpy array
I have some problem with matrix multiplication:
I want to multiplicate for example a and b:
开发者_如何学Ca=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 :(
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]])
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]])
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)]
精彩评论