[Numpy-discussion] Vectorizing a function
Wed Jan 30 10:30:16 CST 2008
On 30/01/2008, Scott Ransom <email@example.com> wrote:
> That works fine with arrays, scalars, or array/scalar mixes in the
> calling. I do understand that more complicated functions might
> require vectorize(), however, I wonder if sometimes it is used
> when it doesn't need to be?
It certainly is! I would guess that it gets used for one of two reasons:
* People don't realize that it's just a for loop and assume it does
numpy magic to make things faster.
* People care more about convenience than speed.
I am usually (when I use it) in the second camp. I have some function
with a bunch of conditionals - patching up the phase of some inverse
trig function, say, or dealing with a few special cases, or cleanly
raising exceptions - and it matters more to get the thing written
quickly than to have it run quickly. So rather than stop and figure
out how to use where() or fancy indexing to handle it efficiently, I
just slap a vectorize() on a simple python function. I can always
accelerate it later. For the same reason, I often drop into list
comprehensions for a few lines, converting back to an array at the
IMHO, one of the primary values of numpy/scipy is that it lets you
express vector algorithms conveniently. When this makes it faster,
this is a bonus; when it makes it slower (as sometimes happens with
very small or very large arrays) it's a pain. But vectorize() lets me
use these vector expressions very readily with functions that are
written in a simple python style.
Nevertheless, I think (going by some of the questions we get here) a
lot of people start using numpy without appreciating that the *point*
of using it is that you can use vector expressions; they think, "well,
I'm using numbers in python, so I should use numpy", and then they
don't take advantage of numpy's speedups. So perhaps we should do some
more educating, in tutorials and docstrings, about the limitations of
Anyone want to start a wiki page "How to get rid of vectorize()"?
More information about the Numpy-discussion