Cython:将complex [double]添加为double时出错 [英] Cython: error adding complex[double] to double

查看:86
本文介绍了Cython:将complex [double]添加为double时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据libcpp/complex.pxd支持将T添加到complex[T]:

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

但这不起作用:

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:

setup.pyx:

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

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

不知道如何解决?

移动声明

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.

推荐答案

我已经开始使用它了.请参阅cython票证 https://github.com/cython/cython/issues/1643

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

它是以下各项的组合

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

超出cppclass定义,将其更改为

out of cppclass definition, changing it to

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

在问题和@DavidW的cimport *

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

这篇关于Cython:将complex [double]添加为double时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆