How do I get the dot product but without the summation

Refresh

November 2018

Views

126 time

4

consider array's a and b

a = np.array([
        [-1, 1, 5],
        [-2, 3, 0]
    ])
b = np.array([
        [1, 1, 0],
        [0, 2, 3],
    ])

Looking at

d = a.T.dot(b)
d

array([[-1, -5, -6],
       [ 1,  7,  9],
       [ 5,  5,  0]])

d[0, 0] is -1. and is the sum of a[:, 0] * b[:, 0]. I'd like a 2x2 array of vectors where the [0, 0] position would be a[:, 0] * b[:, 0].

with the above a and b, I'd expect

d = np.array([[a[:, i] * b[:, j] for j in range(a.shape[1])] for i in range(b.shape[1])])

d

array([[[-1,  0],
        [-1, -4],
        [ 0, -6]],

       [[ 1,  0],
        [ 1,  6],
        [ 0,  9]],

       [[ 5,  0],
        [ 5,  0],
        [ 0,  0]]])

The sum of d along axis==2 should be the dot product a.T.dot(b)

d.sum(2)

array([[-1, -5, -6],
       [ 1,  7,  9],
       [ 5,  5,  0]])

Question

What is the most efficient way of getting d?

3 answers

0
for r in a.T:
    print(np.multiply(r,b.T))

[[-1  0]
 [-1 -4]
 [ 0 -6]]
[[1 0]
 [1 6]
 [0 9]]
[[5 0]
 [5 0]
 [0 0]]
6

Вот один из способов:

In [219]: a
Out[219]: 
array([[-1,  1,  5],
       [-2,  3,  0]])

In [220]: b
Out[220]: 
array([[1, 1, 0],
       [0, 2, 3]])

In [221]: a.T[:,None,:] * b.T[None,:,:]
Out[221]: 
array([[[-1,  0],
        [-1, -4],
        [ 0, -6]],

       [[ 1,  0],
        [ 1,  6],
        [ 0,  9]],

       [[ 5,  0],
        [ 5,  0],
        [ 0,  0]]])

Или же...

In [231]: (a[:,None,:] * b[:,:,None]).T
Out[231]: 
array([[[-1,  0],
        [-1, -4],
        [ 0, -6]],

       [[ 1,  0],
        [ 1,  6],
        [ 0,  9]],

       [[ 5,  0],
        [ 5,  0],
        [ 0,  0]]])
2

Наиболее эффективным из них будет с , broadcastingкак показано , @Warren Weckesser's postкак мы в основном имеем дело с поэлементного умножения без какого - либо sum-reduction.

Альтернативой один с np.einsumбудет вот так -

np.einsum('ij,ik->jki',a,b)