Extended Ufunc Support

22 views
Skip to first unread message

Feiyang

unread,
Mar 29, 2024, 11:30:11 AMMar 29
to cython-users
Hi cython team,

I recently came across the ufunc decorator and found it extremely useful. I would like to thank the developers for providing this functionality and especially DA Woods for helping the community on this topic. 

I want to ask if the following are fundamental limitations of numpy ufunc api, or they might be implemented in near future?
1. Can we use ufuncs on methods of cdef classes in the future? For example, some object methods which cache into attributes of the object.
2. Can ufunc methods take default(optional) arguments in the future?

3. Can ufunc decorated functions be used inside other cdef nogil functions by some kind of overloading? Suppose I have cdef funa nogil, and cdef funb nogil which depends on funa. I want to expose both funa and funb as a ufunc for python users, but once I have decorated funa, it cannot be used inside funb anymore . I currently write the atomic functions in an implementation.pyx file, declare them in an implementation.pxd file, then cimport implementation in a ufuncs.pyx file and decorate them, copying all the function signatures. Could this model be simplified?

Thanks!

da-woods

unread,
Mar 31, 2024, 10:48:22 AMMar 31
to cython...@googlegroups.com
On 29/03/2024 14:34, Feiyang wrote:
> I recently came across the ufunc decorator and found it extremely useful.
Glad you're finding it useful useful.
>
> I want to ask if the following are fundamental limitations of numpy
> ufunc api, or they might be implemented in near future?
> 1. Can we use ufuncs on methods of cdef classes in the future? For
> example, some object methods which cache into attributes of the object.

Maybe? The ufunc API does have space for an arbitrary "data" input. This
could be used to implement inner ufuncs that capture variables. The same
idea could be used to bind a `self` argument. It isn't obvious to me
that bound ufuncs are a good idea (inner ufuncs probably are though).

> 2. Can ufunc methods take default(optional) arguments in the future?

Probably not. I suspect you could implement it yourself with a wrapper
around the ufunc, but I don't think Cython would want to support that.

>
> 3. Can ufunc decorated functions be used inside other cdef nogil
> functions by some kind of overloading? Suppose I have cdef funa nogil,
> and cdef funb nogil which depends on funa. I want to expose both funa
> and funb as a ufunc for python users, but once I have decorated funa,
> it cannot be used inside funb anymore . I currently write the atomic
> functions in an implementation.pyx file, declare them in an
> implementation.pxd file, then cimport implementation in a ufuncs.pyx
> file and decorate them, copying all the function signatures. Could
> this model be simplified?

Yes - this sounds possible - it should be fairly easily to access the
underlying C function directly for the all-scalar case. I wouldn't like
to make any promises about when though.

Reply all
Reply to author
Forward
0 new messages