[Numpy-discussion] failure to register ufunc loops for user defined types

Charles R Harris charlesr.harris at gmail.com
Sun Dec 4 12:29:45 EST 2011


On Sat, Dec 3, 2011 at 8:14 PM, Geoffrey Irving <irving at naml.us> wrote:

> Hello,
>
> I'm trying to add a fixed precision rational number dtype to numpy,
> and am running into an issue trying to register ufunc loops.  The code
> in question looks like
>
>    int npy_rational = PyArray_RegisterDataType(&rational_descr);
>    PyObject* equal = ... // extract equal object from the imported numpy
> module
>    int types[3] = {npy_rational,npy_rational,NPY_BOOL};
>    if
> (PyUFunc_RegisterLoopForType((PyUFuncObject*)ufunc,npy_rational,rational_ufunc_##name,_types,0)<0)
>        return 0;
>
> In Python 2.6.7 with the latest numpy from git, I get
>
>    >>> from rational import *
>    >>> i = array([rational(5,3)])
>    >>> i
>    array([5/3], dtype=rational)
>    >>> equal(i,i)
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in <module>
>    TypeError: ufunc 'equal' not supported for the input types, and
> the inputs could not be safely coerced to any supported types
> according to the casting rule ''safe''
>
> The same thing happens with (rational,rational)->rational ufuncs like
> multiply.
>
> The full extension module code is here:
>
>    https://github.com/girving/poker/blob/rational/rational.cpp
>
> I realize this isn't much information to go on, but let me know if
> anything comes to mind in terms of possible reasons or further tests
> to run.  Unfortunately it looks like the ufunc ntypes and types
> properties aren't updated based on user-defined loops, so I'm not yet
> sure if the problem is in registry or resolution.
>
> It's also possible someone else hit this before:
> http://projects.scipy.org/numpy/ticket/1913.
>
>
I haven't tried adding a new type and can't offer any suggestions. But
there was a recent implementation of a quaternion type that might be worth
looking at for comparison. You can find it here <http://tinyurl.com/83kesqd>.


Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20111204/219fecc2/attachment.html>


More information about the NumPy-Discussion mailing list