简体   繁体   中英

Cython: error adding complex[double] to double

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.

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 *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM