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).

Phillip

On Thu, Oct 8, 2015 at 3:05 PM, Nathaniel Smith <n...@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,
> -n
> On Oct 5, 2015 21:08, "Phillip Feldman" <phillip.m.feld...@gmail.com>
> wrote:
>
>> 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 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.
>>
>> Phillip
>>
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
https://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to