There are no C-level APIs for __array_function__ or __array_ufunc__, so yes, at a high-level Python methods will be invoked by NumPy.
That said, NumPy's logic for handling __array_function__ and __array_ufunc__ methods is written in highly optimized C. If you wrote your own __array_function__ and __array_ufunc__ methods on Quantity using C, and there should be very little overhead from Python. I would guess you might see a considerable speed-ups due to moving highly dynamic Python logic into a compiled language. On Mon, Jan 10, 2022 at 12:56 PM Juan Luis Cano Rodríguez < hello@juanlu.space> wrote: > Hi all, > > I am a long time user of astropy.units, which allows one to define > quantities with physical units as follows: > > >>> from astropy import units as u > >>> 10 << u.cm > <Quantity 10. cm> > >>> np.sqrt(4 << u.m ** 2) > <Quantity 2. m> > >>> ([1, 1, 0] << u.m) @ ([0, 10, 20] << u.cm / u.s) > <Quantity 10. cm m / s> > >>> (([1, 1, 0] << u.m) * ([0, 10, 20] << u.cm / u.s)).to(u.m ** 2 / u.s) > <Quantity [0. , 0.1, 0. ] m2 / s> > > The mechanism works by subclassing numpy.ndarray and leveraging > __array_function__ support aka NEP 18. Internally it is something like this: > > >>> v = np.array(10, dtype=np.float64, copy=False, order=None, subok=True, > ndmin=0) > >>> vu = v.view(u.Quantity) > >>> vu._set_unit(u.cm) > >>> vu > <Quantity 10. cm> > > However, over the years I have been constantly annoyed by the fact that it > is tremendously slow. I'm not critizing Astropy devs, the problem seems > objectively difficult: although some code paths could be optimized at the > cost of losing some syntactic sugar or breaking backwards compatibility, > `isinstance` calls and introspection in general are slow. > > Setting aside the question of trying to make astropy.units faster (which > may or may not be possible), I was thinking how feasible could it be to > implement something similar, but using a compiled language instead (C, > Cython, Rust, whatever) and leveraging "modern" dispatch mechanisms. But > after reading about NEP 18, NEP 47, uarray, and various pull requests and > issues here and there ( > https://labs.quansight.org/blog/2021/11/pydata-extensibility-vision/ and > https://github.com/scipy/scipy/issues/10204#issuecomment-787067947 among > others) I don't fully grasp the differences between the approaches, and I > don't know if what I am proposing is feasible at all. Since IIUC the numpy > function or ufunc is passed to __array_function__ and __array_ufunc__ > respectively, I am not sure how would that interact with the code being in > a foreign language (I assume the NumPy C API would have to be used). > > If folks have advice, ideas, or suggestions for a direction, I'll be happy > to read them. > > Best! > _______________________________________________ > NumPy-Discussion mailing list -- numpy-discussion@python.org > To unsubscribe send an email to numpy-discussion-le...@python.org > https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ > Member address: sho...@gmail.com >
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: arch...@mail-archive.com