[Cython] Cython and complex floats

Dag Sverre Seljebotn dagss at student.matnat.uio.no
Tue Jul 29 11:36:07 CEST 2008


Buffers and Python has the complex datatype, while I'm reasonably sure I 
haven't found anything about it in Cython (?). So here's the three 
options I see ordered by amount of work, comments welcome.

These are incremental so it is not about picking one, but about whether 
anyone has any objections to 2) or 3) going into Cython, as well as 
supporting non-C99-compilers is either a) mandatory or b) a waste of time.

(Also I don't know whether I'll get around to more than 1) this time 
around, will have to see what you say.)

1) Complex numbers can be pulled out into a struct consisting of two 
floats. Like this:

cdef struct cdouble:
   double real
   double imag

def f(object[cdouble] buf):
   ...

Operations on the fields must happen manually.

2) In addition, Cython gets support for the C99 "complex" float modifier.

def complex double sum(object[complex double] buf):
   cdef complex double result = 0 + 0j
   ...

Here, operations between complex datatypes are done natively by the 
compiler. However the code generated is illegal on non-C99-compilers.

3) Also support older compilers by doing all operations through 
conditional macros where complex operations are defined for other 
compilers as well. (I guess, given 2) this is not much work)


-- 
Dag Sverre


More information about the Cython-dev mailing list