dot product of subarrays without for loop

462 views Asked by At

when we have:

array 1: A, shape (49998,3,3) 
array 2: B, shape (3, 49998)

and i want to multiply their subarrays to get

array 3: C, shape(3,49998)

for which im using generator:

def genC(A,B):
    for a,b in itertools.izip(A,B.T):
        c=np.dot(a,b)
        yield c.T[0]

C=np.array([c for c in genC()]).T

so how could i do array multiplication insides of A,B without for loop to get array C?

i was trying to use np.tensordot, but i cant get it

NOTE:

this is just basic example, for some test cos in orginal data i had

4*3*37 arrays A(500 000,3,3) B(3,500 000)

to do, and for loop sems for me not pythonic way xD

1

There are 1 answers

1
Jaime On BEST ANSWER

If I am getting your code right, you want to perform 49998 dot products of a 3x3 matrix with a 3 vector, right? That is very easy to do with np.einsum:

np.einsum('ijk,ki->ij', A, B)