Cython PXD似乎并没有用于PYX文件
Cython PXD does not seem to be used for PYX file
我正试图通过实现从C++到Python的线性插值器来学习cython。我正在尝试将PXD头文件用于最终的Integrator对象,这样它就可以在以后的其他方法/类中重用,所以我希望PXD头可以使用。
我有一个cpp_linear_interpolation.cpp和cpp_linear _interpolation.h,它们运行良好,插值器以两个双(x和y(向量作为输入进行实例化。
有我的文件
cy_linear_interpolation.pxd
# distutils: language = c++
import cython
import numpy as np
cimport numpy as np
from libcpp.vector cimport vector
cdef extern from "cpp_linear_interpolation.h":
cdef cppclass cppLinearInterpolation:
cppLinearInterpolation(vector[double], vector[double]) except +
vector[double] interp(vector[double]) except +
vector[double] m_x
vector[double] m_y
int m_len
double m_x_min
double m_x_max
py_linear_interpolation.pxd
from cy_linear_interpolation cimport cppLinearInterpolation
cdef class LinearInterpolation:
cdef cppLinearInterpolation * thisptr
py_linear_interpolation.pyx
import cython
import numpy as np
cimport numpy as np
from libcpp.vector cimport vector
from cy_linear_interpolation cimport cppLinearInterpolation
cdef class LinearInterpolation:
# cdef cppLinearInterpolation *thisptr
def __cinit__(self,vector[double] x,vector[double] y):
self.thisptr = new cppLinearInterpolation(x, y)
def __dealloc__(self):
del self.thisptr
def interpolate(self,vector[double] x_new):
return self.thisptr.interp(x_new)
设置.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy
setup( name = 'Cython_LinInt',
ext_modules=[Extension("cython_linear_interpolation",
sources=["py_linear_interpolation.pyx", "cpp_linear_interpolation.cpp",],
language="c++",
include_dirs=[numpy.get_include()])
],
cmdclass = {'build_ext': build_ext},
)
使用Microsoft(R(C/C++优化编译器15.00.30729.01版x64 进行编译
我收到错误信息
无法将"cppLinearInterpolation*"转换为Python对象
如果我移动
cdef cppLinearInterpolation * _thisptr
到pyx文件(py_linear_interpolation.pyx中注释掉的行(,它编译并运行,但随后我无法从另一个cython文件访问指针。理想情况下,我可以从python实例化插值器,并将其用作其他python/cython函数的参数。我确信我一定在做一些愚蠢的事情,但我在这个问题上被阻止了一段时间,还没有找到解决方案。。。
编辑:py_linear_interpolation.pyx中有一个拼写错误,现已更正EDIT 2:py_linear_interpolation.pyd中有相同的类型,成员名称为thisptr,代码仍然没有编译,我得到了相同的错误。cython编译器似乎没有识别self.thisptr不是python对象,应该是指向cppLinearInterpolation 的指针
更改此项:
self.thisptr = new cppLinearInterpolation(x, y)
收件人:
self._thisptr = new cppLinearInterpolation(x, y)
我会尝试将__cinit__
更改为
def __init__(self, x, y):
self.thisptr = new cppLinearInterpolation(x, y)
由于没有提供cpp_linear_interpolation.h
和其他文件,我无法自己测试。
- 1d 智能指针不适用于语法 (*)++
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 用于访问容器<T>数据成员的正确 API
- 重载操作程序时出错>>用于类中的字符串 memebr
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- C++中的cin.ignore()函数不适用于整个流
- 没有用于初始化C++中的变量模板的匹配构造函数
- 用于C++中带有数组和指针的循环
- 为什么它不适用于Visual 2019的原因
- 使用在用于SFINAE的void_t中具有参数的方法
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 重载==不适用于二进制树
- Insert函数不适用于2 if语句C++
- 用于矢量处理的多个线程
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 为什么模数运算符不适用于该代码
- 并行用于C++17中数组索引范围内的循环
- 将fold表达式与std::一起用于两个元组
- Cython PXD似乎并没有用于PYX文件