Re: [Numpy-discussion] method to calculate the magnitude squared

2015-10-11 Thread Nils Becker
Hey, I use complex numbers a lot and obviously need the modulus a lot. However, I am not sure if we need a special function for _performance_ reasons. At 10:01 AM 9/20/2015, you wrote: It is, but since that involves taking sqrt, it is *much* slower. Even now, ``` In [32]: r =

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-10-11 Thread Marten van Kerkwijk
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

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-10-10 Thread Phillip Feldman
The ufunc approach makes sense. Something like abs2 is essential for anyone who does signal processing simulations using NumPy. Phillip On Sat, Oct 10, 2015 at 11:29 AM, Nathaniel Smith wrote: > On Oct 10, 2015 10:50 AM, "Charles R Harris" > wrote:

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-10-10 Thread Charles R Harris
On Fri, Oct 9, 2015 at 10:32 PM, Phillip Feldman < phillip.m.feld...@gmail.com> wrote: > Hello Nathaniel, > > It is hard to say what is normative practice with NumPy, because there are > at least three paradigms: > > (1) Some operations are implemented as methods of the `ndarray` class. > `sum`

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-10-10 Thread Nathaniel Smith
On Oct 10, 2015 10:50 AM, "Charles R Harris" wrote: > > On Sat, Oct 10, 2015 at 11:14 AM, Marten van Kerkwijk < m.h.vankerkw...@gmail.com> wrote: >> >> > We tend to avoid adding methods. 2) would be a very easy enhancement, just a slight modification of sqr. >> >> Did

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-10-10 Thread Charles R Harris
On Sat, Oct 10, 2015 at 11:14 AM, Marten van Kerkwijk < m.h.vankerkw...@gmail.com> wrote: > > We tend to avoid adding methods. 2) would be a very easy enhancement, > just a slight modification of sqr. > > Did you mean `np.square`? Sadly, that doesn't do the right thing: > `np.square(1+1j)` yields

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-10-09 Thread Phillip Feldman
Hello Nathaniel, It is hard to say what is normative practice with NumPy, because there are at least three paradigms: (1) Some operations are implemented as methods of the `ndarray` class. `sum` and `mean` are examples. (2) Some operations are implemented via functions that invoke a private

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-10-08 Thread Nathaniel Smith
Hi Phillip, My advice would be to stick with the function call. It's consistent with most other array operations (esp. when you consider that the vast majority of operations on arrays are functions defined in third party libraries like yours), and the more things we add to the core array object,

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-10-05 Thread Phillip Feldman
My apologies for the slow response; I was experiencing some technical problems with e-mail. In answer to Antoine's question, my main desire is for a numpy ndarray method, for the convenience, with a secondary goal being improved performance. I have added the function `magsq` to my library, but

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-09-20 Thread R Schumacher
At 10:01 AM 9/20/2015, you wrote: 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(1)*(1+1j) In [33]: %timeit np.abs(r)**2 1000 loops, best of 3: 213 µs per loop In [34]: %timeit r.real**2 +

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-09-20 Thread Antoine Pitrou
On Fri, 18 Sep 2015 21:16:42 -0700 Phillip Feldman 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

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-09-20 Thread Marten van Kerkwijk
> > 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(1)*(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 1 loops, best of

[Numpy-discussion] method to calculate the magnitude squared

2015-09-18 Thread Phillip Feldman
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 A high

Re: [Numpy-discussion] method to calculate the magnitude squared

2015-09-18 Thread R Schumacher
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.  Â