[Numpy-discussion] method to calculate the magnitude squared

Marten van Kerkwijk m.h.vankerkwijk at gmail.com
Sun Oct 11 14:56:08 EDT 2015


Hi Nils,

I think performance will actually be better than I indicated, especially
for larger arrays, since `r.real**2 + r.imag**2` makes a quite unnecessary
intermediate arrays. With a `ufunc`, this can be done much faster. Indeed,
it should be no slower than `np.square` (which does more operations):

%timeit b = np.square(a)
100000 loops, best of 3: 16.6 µs per loop

[This is on same laptop as the timings above.]

Chuck: agreed that a future np.abs2 could just reuse the internal ufunc
loops for np.square except for the complex case.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20151011/8d4c71ec/attachment.html>


More information about the NumPy-Discussion mailing list