[Numpy-discussion] numpy.complex* functions do not call the __complex__ method

2008-09-25 Thread jason-sage
In creating an array of type numpy.complex128, I'm having problems 
passing in Sage types that should be considered complex numbers since 
they implement the standard __complex__ method.  However, numpy doesn't 
recognize that.  Here's a minimal example:

In [1]: class MyNum:
   ...: def __complex__(self):
   ...: return complex(1,3)
   ...:
   ...:

In [2]: import numpy

In [3]: a=MyNum()

In [4]: complex(a)
Out[4]: (1+3j)

In [5]: numpy.complex128(a)
---
type 'exceptions.AttributeError'Traceback (most recent call last)

/home/jason/download/numpy/numpy-1.1.0/src/numpy/ipython console in 
module()

type 'exceptions.AttributeError': MyNum instance has no attribute 
'__float__'



This is with numpy 1.1.  Is this a bug, or is this by design?

Thanks,

Jason

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy.complex* functions do not call the __complex__ method

2008-09-25 Thread Neal Becker
[EMAIL PROTECTED] wrote:

 In creating an array of type numpy.complex128, I'm having problems
 passing in Sage types that should be considered complex numbers since
 they implement the standard __complex__ method.  However, numpy doesn't
 recognize that.  Here's a minimal example:
 

I had tried to add my own complex_int class (using c++ code).  Although partly 
successful, I was not able to get x.real to work.  The reason is that 
PyArray_ISCOMPLEX is used in various places, and this is a hard-coded macro.  
There is no way to extend numpy's complex behavior to support user added types. 
 I wish there was.

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy.complex* functions do not call the __complex__ method

2008-09-25 Thread jason-sage
Neal Becker wrote:
 [EMAIL PROTECTED] wrote:

   
 In creating an array of type numpy.complex128, I'm having problems
 passing in Sage types that should be considered complex numbers since
 they implement the standard __complex__ method.  However, numpy doesn't
 recognize that.  Here's a minimal example:

 

 I had tried to add my own complex_int class (using c++ code).  Although 
 partly successful, I was not able to get x.real to work.  The reason is that 
 PyArray_ISCOMPLEX is used in various places, and this is a hard-coded macro.  
 There is no way to extend numpy's complex behavior to support user added 
 types.  I wish there was.
   

Is there any possibility of redefining the macro to first call the 
Python function that converts to a python complex?  (I don't know much 
about the python C API, just throwing an idea out).  My example works if 
I do:

numpy.complex128(complex(my_complex_number)),

 but not if I do

numpy.complex128(my_complex_number).

Can we make the call to python's complex (or maybe numpy's copy of it) 
automatic?

Thanks,

Jason

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion