Re: [Numpy-discussion] method to calculate the magnitude squared
Sept. 19, 2015
4:41 a.m.
At 09:16 PM 9/18/2015, you wrote:
In communications and signal processing, it is frequently necessary to calculate the power of a signal. This can be done with a function like the following:
def magsq(z): Â Â """ Â Â Return the magnitude squared of the real- or complex-valued input. Â Â """ Â Â return z.real**2 + z.imag**2
Is that not the same as np.abs(z)**2 ? - Ray
September 2015
5:01 p.m.
New subject: method to calculate the magnitude squared
Is that not the same as np.abs(z)**2 ?
It is, but since that involves taking sqrt, it is *much* slower. Even now, ``` In [32]: r = np.arange(10000)*(1+1j) In [33]: %timeit np.abs(r)**2 1000 loops, best of 3: 213 µs per loop In [34]: %timeit r.real**2 + r.imag**2 10000 loops, best of 3: 47.5 µs per loop ``` -- Marten
3825
Age (days ago)
3826
Last active (days ago)
1 comments
2 participants
participants (2)
-
Marten van Kerkwijk -
R Schumacher