3

I am trying to do complex number calculations using cython. In the example code I would like to calculate the complex exponential function of a complex number. The problem is that I do not know how to multiply my integer by the imaginary unit. Multiplying by python's imaginary unit 1.0j raises errors while executing cython.

Here is my code:

cdef extern from "math.h":
    double complex cexp(double complex)

def testfunction():
    cdef double n
    n=3
    cdef double complex res
    res=cexp(n*1.0j)
    return res

And here is the error message:

complex.c:678:3: note: expected ‘complex double’ but argument is of type ‘__pyx_t_double_complex’

laolux
  • 1,445
  • 1
  • 17
  • 28

1 Answers1

6

First of all, try reporting the full stack trace of the compilation. On my machine, I get a very helpful:

implicit declaration of function ‘cexp’ [-Wimplicit-function-declaration]

It turns out you're including the wrong header. cexp is declared in <complex.h>, see the docs.

Simply change your code to

cdef extern from "complex.h":
    double complex cexp(double complex)

and you'll be fine.

gg349
  • 21,996
  • 5
  • 54
  • 64