On Apr 3, 2009, at 10:21 PM, Vic Kelson wrote:

> Greetings all,
>
> Sorry for the incomplete message, I'm re-sending it.
>
> I'm in need of fast computations on complex numbers from Cython.  
> I've been converting a Python groundwater flow model code to  
> Cython, but I'm reaching the point where I'd really prefer to have  
> native C complex math. Up to now, I've used some little C routines  
> that I can call with multiple floats (the C code converts to/from  
> complex for my purposes). However, I'm nearly to the point where  
> I'll need to store series coefficients for performance purposes,  
> and then I'll either need to hide them in float arrays (and that's  
> extremely nasty), or something like that.
>
> I'd like to say something like
>
> cdef class Foo:
>     cdef cfloat z
>
>     def __cinit__(self, float x, float y):
>         self.z = cfloat(x, y)
>
>     cdef cfloat func(Foo self, float x, float y):
>         return-some-gnarly-function of x, y, and self.z
>
>
> Or something like that, where 'cfloat' maps to the appropriate type  
> in the C compiler, e.g. "double _Complex" in gcc.
>
> I've seen this discussed in the archives. Has it been implemented?  
> Is there some pre-release code that essentially works? I only need  
> the four math functions, conjugate, abs, and log.

Yes, I've started this, as it comes up in my research too (number  
theory, computing values of L-functions). I hope to have something  
releasable soon. In the meantime, you can do

cdef extern from "complex.h":
     ctypedef double cdouble "double complex"
     cdef double creal(cdouble)
     cdef double cimag(cdouble)
     cdef cdouble _Complex_I

cdef inline cdouble  new_cdouble(double x, double y):
     return x + _Complex_I*y

Of course, the ctypedef is wrong, it will think it can go back to  
doubles (and Python objects) without any problem, but it works for now.

> THANKS! I think Cython will revolutionize my research work!

That's the goal :) Please cite us if it does.

- Robert

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

Reply via email to