[Numpy-discussion] "Extended" Outer Product
Tue Aug 21 15:32:49 CDT 2007
On 21/08/07, Timothy Hochberg <firstname.lastname@example.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, 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.
More information about the Numpy-discussion