[Numpy-discussion] method to calculate the magnitude squared
phillip.m.feldman at gmail.com
Sat Oct 10 00:32:40 EDT 2015
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 : x= array([1+1J])
In : x.__abs__()
Out: 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).
On Thu, Oct 8, 2015 at 3:05 PM, Nathaniel Smith <njs at pobox.com> wrote:
> 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, the more work
> it is for people implementing new array-style containers. I definitely
> would not recommend subclassing ndarray for this purpose -- there are all
> kinds of subtle problems that you'll run into that mean it's extremely
> difficult to do well, and may well be impossible to do perfectly.
> Good luck,
> On Oct 5, 2015 21:08, "Phillip Feldman" <phillip.m.feldman at gmail.com>
>> 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
>> I have added the function `magsq` to my library, but would like to access
>> it as a method rather than as a function. I understand that I could create
>> a class that inherits from NumPy and add a `magsq` method to that class,
>> but this has a number of disadvantages.
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion