Cython: error adding complex[double] to double

巧了我就是萌 提交于 2019-12-01 12:11:57

问题


According to libcpp/complex.pxd adding T to complex[T] is supported:

    complex[T] operator+(complex[T]&, T&)
    complex[T] operator+(T&, complex[T]&)

But it doesn't work:

a.pyx:

# distutils: language = c++

cimport libcpp.complex

def f():
    libcpp.complex.complex[double](1,2) + libcpp.complex.complex[double](2,3) # ok
    libcpp.complex.complex[double](1,2) + 5. # Cannot assign type 'double' to 'complex[double]' 
    5. + libcpp.complex.complex[double](1,2) # Invalid operand types for '+' (double; complex[double])

setup.pyx:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    name = "demo",
    ext_modules = cythonize('a.pyx'),
)

Àny idea how to fix it?

Moving declaration

complex[T] operator+(complex[T]&, T&)

out of cppclass and changing it to

complex[T] operator+[T](complex[T]&, T&)

looks more legimate but still does not work.


回答1:


I've got it working. See cython ticket https://github.com/cython/cython/issues/1643

It is a combination of moving

complex[T] operator+(complex[T]&, T&) 

out of cppclass definition, changing it to

complex[T] operator+[T](complex[T]&, T&)

as suggested in the question and @DavidW's idea of cimport *



来源:https://stackoverflow.com/questions/43038496/cython-error-adding-complexdouble-to-double

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!