我正在尝试使用 cython 进行复数计算。 在示例代码中,我想计算复数的复指数函数。问题是我不知道如何将我的整数乘以虚数单位。 python的虚数单位1.0j乘以cython执行时报错。
这是我的代码:
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
这是错误信息:
complex.c:678:3: note: expected ‘complex double’ but argument is of type ‘__pyx_t_double_complex’
最佳答案
首先,尝试报告编译的完整堆栈跟踪。在我的机器上,我得到了一个非常有用的信息:
implicit declaration of function ‘cexp’ [-Wimplicit-function-declaration]
事实证明您包含了错误的 header 。 cexp
在 <complex.h>
中声明, 请参阅 docs .
只需将您的代码更改为
cdef extern from "complex.h":
double complex cexp(double complex)
你会没事的。
关于cython - 使用 Cython 进行复值计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27906862/