0

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.

Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
  • I agree it's broken. I don't think there's a workable workround. Perhaps submit a bug report to https://github.com/cython/cython/issues? – DavidW Mar 27 '17 at 07:37
  • @DavidW Done, issue [#1643](https://github.com/cython/cython/issues/1643) – axil Mar 27 '17 at 07:58
  • @AntonyHatchkins That's the first time anyone's ever done it when I suggested they submit a bug report :)! – DavidW Mar 27 '17 at 08:10
  • 1
    @DavidW haha :) Maybe one day the world will be a better place to live :) – Antony Hatchkins Mar 27 '17 at 08:25
  • AntonyHatchkins @DavidW I've submitted a [related question](http://stackoverflow.com/questions/43041856/cython-counter-tutorial-behavior) which provides sort of workaround yet raises new issues. – axil Mar 27 '17 at 09:05

1 Answers1

2

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 *

axil
  • 1,558
  • 16
  • 17