[Numpy-discussion] efficient usage of tensordot

Gideon Simpson simpson@math.toronto....
Sat Jan 17 23:30:21 CST 2009


This is related to a question I posted earlier.

Suppose I have array A with dimensions n x m x l and array x with  
dimensions m x l.  Interpret this as an array of l nxm matrices and  
and array of l m dimensional vectors.  I wish to compute the matrix- 
vector product A[:,:,k] x[:,k] for each k = 0,... l -1.  I discovered  
that I could accomplish this with the command

np.diagonal(np.tensordot(A, k, axes=(1,0)), axis1= 1, axis2 = 2)

The tensordot command gives me

A_{ijk}x_{jl} = C_{ikl}

And the diagonal command grabs the entries in array C where k=l.

Is this the "optimal" way to make this calculation in numpy?  It  
certainly makes for nice, clean code, but is it the fastest I can get?


-gideon



More information about the Numpy-discussion mailing list