<br><br><div class="gmail_quote">On Nov 26, 2007 2:30 PM, Hans-Andreas Engel <<a href="mailto:engel@physics.harvard.edu">engel@physics.harvard.edu</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Dear all:<br><br>After using numpy for several weeks, I am very happy about it and<br>deeply impressed about the performance improvements it brings in my<br>python code.  Now I have stumbled upon a problem, where I cannot use
<br>numpy to eliminate all my loops in python.<br><br>Currently the return value of inner(a, b) is defined as<br>   inner(a, b)[I, J] = sum_k  a[I, k] * b[J, k],<br>for some super indices I and J.  Somewhat more general is the
<br>tensordot() function that allows to specify over which axes K is<br>summed over.<br><br>However, if I understand numpy correctly, the following more general<br>version is currently missing:<br>  inner(a, b, keep_axis=0)[H, I, J] = sum_k a[H, I, k] * b[H, J, k].
<br>Here H would be an additional super index (specified via the keep_axis<br>keyword), on which no outer product is taken, i.e., the same index is<br>used for a[] and b[].<br><br>This more general definition would allow elimination of an extra level
<br>of loops.  For example, I wish to calculate the following<br>   a = rand(200, 5, 2)<br>   b = rand(200, 4, 2)<br>   r = empty(a.shape[:-1] + b.shape[1:-1])<br>   for h in range(a.shape[0]):<br>       r[h] = inner(a[h], b[h])
<br>How could I eliminate the loop?  It would be great if there would be<br>the mentioned generalized version of the inner() [or tensordot()]<br>function, since it would eliminate this loop and make my code much<br>faster.
<br><br>What are your opinions?  Would such a feature be desirable (or is it<br>already implemented)?</blockquote><div><br>Essentially, you want to operate on a stack of two dimensional arrays, correct? I'd be mildly supportive of something like this for tensordot; I'd prefer more descriptive name for keep_axis, but I don't know what it would be off the top of my head. In any event it should be XXX_axes and optionally take a sequence of axes so that more than one can be ignored. You could trivially build more specific functions on top of tensordot, so I don't see that inner needs to be changed as it's basically a convenience function anyway.
<br><br></div></div>-- <br>.  __<br>.   |-\<br>.<br>.  <a href="mailto:tim.hochberg@ieee.org">tim.hochberg@ieee.org</a>