[Numpy-discussion] 2d*3d matrix dot product

Friedrich Romstedt friedrichromstedt at gmail.com
Wed Feb 9 14:10:49 EST 2011


2011/2/9 Davide Lasagna <davide.lasagna at polito.it>:
> Hi,
>
> I want to compute the following dot product:
>
> P = np.array( [[ p11, p12 ], [p21, p22]] )
> C = np.array( [c1, c2] )
>
> a1 = p11*c1 + p12*c2
> a2 = p21*c1 + p22*c2
>
> P.shape = (n, n)
> C.shape = (n, m, l)
>
> and with a result as:
>
> A.shape = (n, m, l)

Interesting problem.

So:

A[i, j, k] = \sum_q P[i, q] * C[q, j, k]

Your problem description is an interesting way to look at tensordot.
Normally the sum character is emphasised, but in your description the
indiced not summed over play the role of forming complete matrices
which are multiplied by scalars.

Alternatively, you can look at your tensordot as a (m, l) matrix of
(n,) vectors which originate from dot-multiplying earch (n,) vector in
a (m, l) matrix, which comprises C, with the P matrix.  But this is
just an interpretation, I like the equation above best of them all.

Ben's suggestion is identical to:

A = numpy.tensordot(P, C, axes=(1, 0))

Depends on what you want to emphasis, the concrete indices or how many
of the last/first indices to take in.

http://docs.scipy.org/doc/numpy/reference/generated/numpy.tensordot.html

Friedrich



More information about the NumPy-Discussion mailing list