python - Cython - 如何使用引用正确调用运算符

标签 python c++ cython

我的 Cython 包装器中有以下代码到 C++ 代码:

# distutils: language = c++
# distutils: sources = symbolic.cpp
from libcpp.vector cimport vector
from libcpp.pair cimport pair
from libcpp.string cimport string
from libcpp cimport bool

cdef extern from "symbolic.h" namespace "metadiff::symbolic":
    cdef cppclass SymbolicMonomial:
        vector[pair[int, int]] powers
        long long coefficient;
        SymbolicMonomial()
        SymbolicMonomial(long)
        SymbolicMonomial(const SymbolicMonomial&)
        bool is_constant()
        long long int eval(vector[int]&)
        long long int eval()
        string to_string()
        string to_string_with_star() const

    cdef SymbolicMonomial mul_mm"operator*"(const SymbolicMonomial&, const SymbolicMonomial&)
    # SymbolicMonomial operator*(long long, const SymbolicMonomial&)
    # SymbolicMonomial operator*(const SymbolicMonomial&, long long)


cdef class SymMonomial:
    cdef SymbolicMonomial* thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self):
        self.thisptr = new SymbolicMonomial()
    def __cinit__(self, int value):
        self.thisptr = new SymbolicMonomial(value)
    def __dealloc__(self):
        del self.thisptr
    def is_constant(self):
        return self.thisptr.is_constant()
    def eval(self):
        return self.thisptr.eval()
    def __str__(self):
        return self.to_string_with_star()
    def to_string(self):
        return self.thisptr.to_string().decode('UTF-8')
    def to_string_with_star(self):
        return self.thisptr.to_string_with_star().decode('UTF-8')
    def __mul__(self, other):
        return mul_mm(self.thisptr, other)
def variable(variable_id):
    monomial = SymMonomial()
    monomial.thisptr.powers.push_back((variable_id, 1))
    return monomial

但是,我从来没有弄清楚如何正确调用mul_mm 方法。它一直说 Cannot convert 'SymbolicMonomial' to Python object 反之亦然。问题是我需要能够以这种方式将两个 SymMonomials 相乘。但是由于某种原因,我无法掌握如何正确执行它的窍门。有什么建议吗?

最佳答案

你有很多问题:

  1. 您不能将 C++ 对象直接返回给 Python - 您需要返回您的包装器类型(分配给包装器的 thisptr)

  2. 你也不能保证 selfother在调用函数时是正确的类型(请参阅 http://docs.cython.org/src/userguide/special_methods.html#arithmetic-methods 中关于如何以任意顺序使用操作数调用方法的注释)。要使用 Cython 类的 C/C++ 成员,您需要确保 Cython 知道该对象确实属于该类。我建议使用 <Classname?>样式转换(注意问号),如果不匹配则抛出异常。

  3. 你需要得到 thisptr来自 other也可以,而不仅仅是将 Python 包装器类传递给您的 C++ 函数。

以下应该有效。

def __mul__(self,other):
    cdef SymMonomial tmp = SymMonomial()
    cdef SymMonomial self2, other2
    try:
        self2 = <SymMonomial?>self
        other2 = <SymMonomial?>other
    except TypeError:
        return NotImplemented # this is what Python expects for operators
                      # that don't know what to do
    tmp.thisptr[0] = mul_mm(self2.thisptr[0],other2.thisptr[0])
    return tmp

关于python - Cython - 如何使用引用正确调用运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37765864/

相关文章:

python - 我是否需要安装 Hadoop 才能使用 Pyspark 的所有功能?

python - lambda - 'QuerySet' 对象没有属性 'xxx'

c++ - 通过参数传递变量并在稍后的类中对其进行修改

c++ - 安全蓄能器真的这么复杂吗?

python - Cython 使用 gmp 算术

python - 无法使用新观察更新 StatsModels SARIMAX (ValueError)

python - 使用 BeautifulSoup 搜索具有多个空格和通配符的类标签

c++ - 我如何使用 LDA 对信号进行分类

python - 如何在 Cython 中使用不同的 C++ 编译器?

python - 从 C++ 函数 Cython 返回包含 PyObject 的复杂对象