[Matrix-SIG] Question for the NumPy gurus

Janne Sinkkonen janne@avocado.pc.helsinki.fi
05 Aug 1999 13:44:14 +0300


Travis Oliphant <Oliphant.Travis@mayo.edu> writes:

> I have two arrays with in1.shape = (N,M,P) and in2.shape = (N,P)
> 
> Is there a fast way (no Python for loop) to perform
> an innerproduct element-wise along the first dimension to 
> return an array of shape (N,M)?  I was hoping for the equivalent of

In general, you could compute the outer product and then sum and take
a diagonal. No loops, just some extra computing and astronomical
memory requirements. :)

How about something like:

   reshape(dot(reshape(in1,(-1,P)),in2),(N,M))


dot() should really be extended for general tensor products, i.e. to
handle N-dimensional arrays with user-specified dimension pairs over
which inner products are computed:

   dot(A,B,((dimA1,dimB1),(dimA2,dimB2),...))

Then you could write

   dot(in1,in2,((-1,-1)))

-- 
Janne