<div dir="ltr"><div><div><div>Hello Nathaniel,<br><br>It is hard to say what is normative practice with NumPy, because there are at least three paradigms:<br><br></div>(1) Some operations are implemented as methods of the `ndarray` class.  `sum` and `mean` are examples.<br><br></div></div>(2) Some operations are implemented via functions that invoke a private method of the class.  `abs` is an example of this:<br><br><div><div><div>In [8]: x= array([1+1J])<br>In [9]: x.__abs__()<br>Out[9]: array([ 1.41421356])<br><br>(3) Some operations are implemented as functions that operate directly on the array, e.g., RMS (root-mean-square).<br><br></div><div>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).<br><br></div><div>Phillip<br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Oct 8, 2015 at 3:05 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">Hi Phillip,</p>
<p dir="ltr">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.</p>
<p dir="ltr">Good luck,<br>
-n</p>
<div class="gmail_quote"><div><div class="h5">On Oct 5, 2015 21:08, "Phillip Feldman" <<a href="mailto:phillip.m.feldman@gmail.com" target="_blank">phillip.m.feldman@gmail.com</a>> wrote:<br type="attribution"></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><div>My apologies for the slow response; I was experiencing some technical problems with e-mail.<br><br>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.<br><br>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.<br><br></div>Phillip<br><div><br></div></div>
<br></div></div>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br></div>