[Numpy-discussion] object-arrays and isnan

ahalda@cs.mcgi... ahalda@cs.mcgi...
Tue Jun 3 11:29:36 CDT 2008


Hi

I'm trying to make a python class to be used in object arrays
(specifically, an mpfr type for multiprecision). Numpy lets me create
member functions like 'cos' which get called elemenwise when I call cos(a)
on an object array a. However, this doesn't work for some functions, like
isnan.

Looking in the numpy code, I see in
numpy/core/code_generators/generate_umath.py that the type description for
isnan does not include objects (type 'M'). Every math ufunc takes objects
in the type description except the following ones:

isnan
isfinite
isinf
signbit
modf

Is there a reason for this? I want to be able to use my mpfr type with
scipy functions, but for example I can't use optimize.fmin_bfgs because it
calls isnan. Other scipy functions work great though,.

If there is a reason, how can I get around this?



In case I was not clear, here is a demonstration:


from scipy import cos, isnan, array

class myNumber:
        def __init__(self, n):
                self.n = n

        def cos(self):
                return cos(self.n)

        def isnan(self):
                return isnan(self.n)

a = array([myNumber(3)])

print "cos: %s"  % str( cos(a)   )
print "isnan %s" % str( isnan(a) )


which prints:


cos: [-0.9899924966]
Traceback (most recent call last):
  File "./test.py", line 18, in <module>
    print "isnan %s" % str( isnan(a) )
TypeError: function not supported for these types, and can't coerce safely
to supported types


Thanks,
Allan





More information about the Numpy-discussion mailing list