[Numpy-discussion] NumPy, buffers (PEP 3118), complex floats, and Cython

Travis E. Oliphant oliphant at enthought.com
Sat Jul 5 14:50:53 EDT 2008


Dag Sverre Seljebotn wrote:
> I'd like some advice for what way people feel would be the best for 
> supporting complex datatypes in NumPy in Cython; as well as ask in what 
> way it is likely that NumPy will make use of PEP 3118.
>
> It seems like NumPy defines its complex data to be a struct of two 
> doubles, for instance:
>
> typedef struct { double real, imag; } npy_cdouble;
>
> According to PEP 3118 [1], it would be natural to export this as "dd" 
> (record of two doubles) rather than "Zd" (complex double), when 
> exporting ndarrays through a buffer interface. Right?
>   
No, it is more natural to use Zd because then you know you are dealing 
with complex numbers and not just two separate floats.
> For Cython, it seems most user-friendly to use the C99 standard for 
> complex numbers (not currently supported, but it wouldn't be much work) 
> and have code like this:
>
> cdef ndarray[complex double, 2] arr = ...
> cdef complex double item
> item = arr[34, 23]
>   
That is fine to use the C99 standard.  
> So the natural questions then are:
>
> - Is it ok to assume that (complex double*) can be safely casted to 
> (npy_cdouble*) on all platforms which both a) NumPy compiles and runs 
> on, and b) supports complex double? (It seems to be ok with GCC but I 
> wouldn't know about other compilers.)
>   
I think so, but I'm not 100% sure.
> - Will NumPy ever be rewritten to use the C99 complex datatype if 
> available? (I.e. #ifdef how npy_cdouble is defined, and define 
> corresponding #ifdef-ed macros for all complex operations). This would 
> remove the need for such a cast.
>   
Can't answer that one.   C99 is not supported by Microsoft's compilers 
and other compilers and so could not be used though I would have liked to.
> - Does NumPy plan to support PEP3118, and if so, will complex numbers be 
> exported as "dd" or "Zd"?
>   
Yes, it will support it eventually.   PEP3118 came from NumPy.   Complex 
numbers will be exported as "Zd"


-Travis




More information about the NumPy-Discussion mailing list