[Numpy-discussion] method to calculate the magnitude squared

Charles R Harris charlesr.harris at gmail.com
Sat Oct 10 10:44:38 EDT 2015


On Fri, Oct 9, 2015 at 10:32 PM, Phillip Feldman <
phillip.m.feldman at 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` and `mean` are examples.
>
> (2) Some operations are implemented via functions that invoke a private
> method of the class.  `abs` is an example of this:
>
> In [8]: x= array([1+1J])
> In [9]: x.__abs__()
> Out[9]: array([ 1.41421356])
>
> (3) Some operations are implemented as functions that operate directly on
> the array, e.g., RMS (root-mean-square).
>
> Because calculating the square of the magnitude is such a widely-used
> operation, and is often done in a grossly inefficient manner (e.g., by
> taking the absolute value, which involves a square-root, and then
> squaring), I believe that there is a strong argument for doing either (1)
> or (2).  I'd prefer (1).
>
>
We tend to avoid adding methods. 2) would be a very easy enhancement, just
a slight modification of sqr.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20151010/c19c1bf8/attachment.html>


More information about the NumPy-Discussion mailing list