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

Dag Sverre Seljebotn dagss@student.matnat.uio...
Sat Jul 5 08:44:58 CDT 2008


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?

I'm implementing native Cython support/syntax candy for PEP 3118 [2] and 
at the same time a backwards-compatability wrapper so that Cython can 
access NumPy through PEP 3118 for Python <= 2.5 (essentially I implement 
PyObject_GetBuffer etc. on behalf of NumPy for Python <= 2.5). Then the 
question arise about how to deal with complex datatypes.

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]

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.)

- 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.

- Does NumPy plan to support PEP3118, and if so, will complex numbers be 
exported as "dd" or "Zd"?

-
[1]: http://www.python.org/dev/peps/pep-3118/
[2]: http://wiki.cython.org/enhancements/buffer

-- 
Dag Sverre


More information about the Numpy-discussion mailing list