[Numpy-discussion] ufunc for user-defined type
Neal Becker
ndbecker2 at gmail.com
Fri Feb 22 11:59:16 EST 2008
Travis E. Oliphant wrote:
> Neal Becker wrote:
>> Now that I have my user-defined type, I want to add some funcs.
>> According to the numpy book, I need to use:
>>
>> PyUFunc_RegisterLoopForType
>>
>> The book says I first need a ufunc. The only way I see to create one is
>>
>> PyUFunc_FromFuncAndData.
>>
>> Is the the correct procedure?
>>
> You'll have to do some digging because the API allowing low-level loops
> for user-defined types is not well tested or vetted. Also, don't be
> surprised if you uncover some bugs.
>
> The concept is that you create the UFunc with the "built-in" types using
> PyUFunc_FromFuncAndData and then you add loops for the user-defined type
> using RegisterLoopForType.
>
> So, you have the basic procedure down.
>
> -Travis
It looks like this works. Basically, the only thing used from
the ufunc obj is: name, nin, nout, nargs, and ntypes.
// Make a generic new ufunc obj
static PyUFuncObject * new_ufunc (char* name) {
PyUFuncObject * u = PyObject_New (PyUFuncObject, &PyUFunc_Type);
u->nin = 0;
u->nout = 0;
u->nargs = 0;
u->identity = 0;
u->functions = 0;
u->data = 0;
u->types = 0;
u->ntypes = 0;
u->check_return = 0;
u->ptr = NULL;
u->obj = NULL;
u->userloops=NULL;
u->name = name;
u->doc = "NULL";
return u;
}
//fill in required fields
PyUFuncObject * u = new_ufunc ("magsqr");
u->nin = 1;
u->nout = 1;
u->nargs = 2;
u->ntypes = 1;
{
int types[] = {d1->type_num, NPY_INT32};
register_unary_loop_func (u, d1->type_num, types, mag_sqr_impl<cmplx_int32_t>());
}
where 'register_unary_loop_func' will basically call:
PyUFunc_RegisterLoopForType (u, typenum, &loop1d_unary<in_t,out_t,unary_func>, types, 0);
More information about the NumPy-Discussion
mailing list