Numpy slow at vector cross product?

Steve D'Aprano steve+python at pearwood.info
Tue Nov 22 11:48:56 EST 2016


On Tue, 22 Nov 2016 11:45 pm, BartC wrote:


> I will have a look. Don't forget however that all someone is trying to
> do is to multiply two vectors. They're not interested in axes
> transformation or making them broadcastable, whatever that means.

You don't know that.

Bart, you have a rather disagreeable tendency towards assuming that if you
don't want to do something, then nobody else could possibly want to do it.
You should try to keep an open mind to the possibility that perhaps there
are use-cases that you didn't think of. numpy is not *the* most heavily
used third-party library in the Python ecosystem because its slow.

numpy is a library for doing vectorised operations over massive arrays.
You're thinking of numpy users doing the cross product of two vectors, but
you should be thinking of numpy users doing the cross product of a million
pairs of vectors.

Could numpy optimize the single pair of vectors case a bit better? Perhaps
they could. It looks like there's a bunch of minor improvements which could
be made to the code, a few micro-optimizations that shave a microsecond or
two off the execution time. And maybe they could even detect the case where
the arguments are a single pair of vectors, and optimize that. Even
replacing it with a naive pure-Python cross product would be a big win.

But for the big array of vectors case, you absolutely have to support doing
fast vectorized cross-products over a huge number of vectors.

py> a = np.array([
...     [1, 2, 3],
...     [4, 5, 6],
...     [7, 8, 9],
...     ]*1000
...     )
py> b = np.array([
...     [9, 8, 7],
...     [6, 5, 4],
...     [3, 2, 1],
...     ]*1000
...     )
py> a.shape
(3000, 3)
py> result = np.cross(a, b)
py> result.shape
(3000, 3)


On my computer, numpy took only 10 times longer to cross-multiply 3000 pairs
of vectors than it took to cross-multiply a single pair of vectors. If I
did that in pure Python, it would take 3000 times longer, or more, so numpy
wins here by a factor of 300.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list