<br><br><div class="gmail_quote">On Sat, Dec 3, 2011 at 8:14 PM, Geoffrey Irving <span dir="ltr"><<a href="mailto:irving@naml.us">irving@naml.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello,<br>
<br>
I'm trying to add a fixed precision rational number dtype to numpy,<br>
and am running into an issue trying to register ufunc loops.  The code<br>
in question looks like<br>
<br>
    int npy_rational = PyArray_RegisterDataType(&rational_descr);<br>
    PyObject* equal = ... // extract equal object from the imported numpy module<br>
    int types[3] = {npy_rational,npy_rational,NPY_BOOL};<br>
    if (PyUFunc_RegisterLoopForType((PyUFuncObject*)ufunc,npy_rational,rational_ufunc_##name,_types,0)<0)<br>
        return 0;<br>
<br>
In Python 2.6.7 with the latest numpy from git, I get<br>
<br>
    >>> from rational import *<br>
    >>> i = array([rational(5,3)])<br>
    >>> i<br>
    array([5/3], dtype=rational)<br>
    >>> equal(i,i)<br>
    Traceback (most recent call last):<br>
      File "<stdin>", line 1, in <module><br>
    TypeError: ufunc 'equal' not supported for the input types, and<br>
the inputs could not be safely coerced to any supported types<br>
according to the casting rule ''safe''<br>
<br>
The same thing happens with (rational,rational)->rational ufuncs like multiply.<br>
<br>
The full extension module code is here:<br>
<br>
    <a href="https://github.com/girving/poker/blob/rational/rational.cpp" target="_blank">https://github.com/girving/poker/blob/rational/rational.cpp</a><br>
<br>
I realize this isn't much information to go on, but let me know if<br>
anything comes to mind in terms of possible reasons or further tests<br>
to run.  Unfortunately it looks like the ufunc ntypes and types<br>
properties aren't updated based on user-defined loops, so I'm not yet<br>
sure if the problem is in registry or resolution.<br>
<br>
It's also possible someone else hit this before:<br>
<a href="http://projects.scipy.org/numpy/ticket/1913" target="_blank">http://projects.scipy.org/numpy/ticket/1913</a>.<br>
<br></blockquote><div><br>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 <a href="http://tinyurl.com/83kesqd">here</a>. <br>
<br>Chuck<br></div><br></div>