Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

How to do matrix multiplication under numpy in higher dimensions?

$
0
0

Under numpy I want to perform a "usual" matrix multiplication like this:

C=A*B

where

A is a "2D-kind" Matrix, but each matrix element has shape (1,5)

and

B is a "1D-kind" Vector, but each vector element element has shape (20,5)

The result

C shall be a "1D-kind" Vector, but each vector element element again has shape (20,5)

enter image description here

I tried to produce the elements C1 and C2 of C manually:

>>> A.shape(2, 2, 1, 5)>>> B.shape(2, 20, 5)>>> C0 = A[0,0]*B[0]+A[0,1]*B[1]>>> C0.shape(20, 5)>>> C1 = A[1,0]*B[0]+A[1,1]*B[1]>>> C1.shape(20, 5)>>>

Broadcasting (1,5) from A with (20,5) of B works as expected.

However, I was not able to find out, how this can be written like a matrix multiplication:

C = np.matmul(A, B)

Of course this doesn't work because numpy can't know what indices I want to sum over. But I guess that some simple "numpythonic" solution must exist...


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>