NumPy, buffers (PEP 3118), complex floats, and Cython
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
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
Travis E. Oliphant wrote:
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.
I guess my confusion comes from assuming the wrong thing about what "complex double" refers to in the PEP; since that's the only reference I found to the concept in the PEP I assumed it corresponded to the C99 float. Can I assume then that Zd means that the data always has the form of npy_cdouble and friends? (I guess this might seem "obvious", but as you see I've already had one wrong guess, and I can't seem to find it defined anywhere...) -- Dag Sverre
participants (2)
-
Dag Sverre Seljebotn
-
Travis E. Oliphant