David Cournapeau wrote:
> Hi,
>
> Is there a way to generate simple C code from complex numbers with
> cython. For example, something like
>
> cdef complex foo(complex a, complex b):
>         return a  - b
>
> which would become in C:
>
> complex foo(complex a, complex b)
> {
>         return a - b;
> }
>
> And ideally, an easy way to handle complex/imaginary as members
> (a.imag, etc...); assuming the C compiler supports complex.h, of
> course.
>   
There seems to be a positive attitude towards including complex support 
in Cython natively, it's just about lack of developer time...

You can fake it, but it is a bit dangerous:

cdef extern from *:
    ctypedef float complex

Problems to watch out:
1) This means that Cython will think that complex numbers are 
automatically castable to float and it will be C that raises any error. 
(Especially for float* vs. complex*)
2) You need to declare seperate functions for getting real and imag parts.
3) No auto-coercion to/from Python complex, must do it manually (and use 
the functions from 2) in the process).

I think this is as good as it gets, at least I can't think of anything 
else within the Cython that's there today.

Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to