[Numpy-discussion] "Extended" Outer Product

Travis E. Oliphant oliphant@enthought....
Wed Sep 19 20:10:12 CDT 2007


Anne Archibald wrote:
> On 21/08/07, Timothy Hochberg <tim.hochberg@ieee.org> wrote:
>
>   
>> This is just a general comment on recent threads of this type and not
>> directed specifically at Chuck or anyone else.
>>
>> IMO, the emphasis on avoiding FOR loops at all costs is misplaced. It is
>> often more memory friendly and thus faster to vectorize only the inner loop
>> and leave outer loops alone. Everything varies with the specific case of
>> course, but trying to avoid FOR loops on principle is not a good strategy.
>>     
>
> Yes and no. From a performance point of view, you are certainly right;
> vectorizing is definitely not always a speedup. But for me, the main
> advantage of vectorized operations is generally clarity: C = A*B is
> clearer and simpler than C = [a*b for (a,b) in zip(A,B)]. When it's
> not clearer and simpler, I feel no compunction about falling back to
> list comprehensions and for loops.
>
> That said, it would often be nice to have something like
> map(f,arange(10)) for arrays; the best I've found is
> vectorize(f)(arange(10)).
>
> vectorize, of course, is a good example of my point above: it really
> just loops, in python IIRC, but conceptually it's extremely handy for
> doing exactly what the OP wanted. Unfortunately vectorize() does not
> yield a sufficiently ufunc-like object to support .outer(), as that
> would be extremely tidy.
>   
I'm not sure what you mean by sufficiently ufunc-like.  In fact, 
vectorize is a ufunc (it's just an object-based one).  Thus, it should 
produce what you want (as long as you use newaxis so that the 
broadcasting is done).   If you just want it to support the .outer 
method that could be easily done (as under the covers is a real ufunc). 

I just over-looked adding these methods to the result of vectorize.   
The purpose of vectorize is to create a ufunc out of a scalar-based 
function, so I don't see any problem in giving them the methods of 
ufuncs as well (as long as the signature is right --- 2 inputs and 1 
output).

-Travis



More information about the Numpy-discussion mailing list