2016-03-14 88 views
3

我想通過實現從C++到Python的線性插值器來學習cython。 我想爲最終的Intepolator對象使用PXD頭文件,以便它可以在其他方法/類下重用,所以我想讓PXD頭文件可用。Cython PXD似乎不用於PYX文件

我有一個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) 

setup.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中存在一個錯誤,現在更正 編輯2:相同的類型是在py_linear_interpolation.pyd中,成員名稱是thisptr,代碼仍然沒有編譯,我得到相同的錯誤。它似乎並不該用Cython編譯器識別出self.thisptr不是一個Python的對象,應該是一個指向cppLinearInterpolation

回答

1

更改此:

self.thisptr = new cppLinearInterpolation(x, y) 

要:

self._thisptr = new cppLinearInterpolation(x, y) 
+0

謝謝你的答案Czarek。這是我的一種類型,代碼仍然沒有正確的thisptr名稱 – Damien022

+0

@ Damien022更新回答 –

+0

與最新的變化,我現在得到py_linear_interpolation.pyx:12:5:C類'LinearInterlation'被聲明,但沒有定義爲 – Damien022

0

我會嘗試改變__cinit__

def __init__(self, x, y): 
    self.thisptr = new cppLinearInterpolation(x, y) 

由於cpp_linear_interpolation.h和其他文件沒有給出,我無法自己測試這個。

+0

謝謝你。我仍然沒有找到解決這個問題的辦法,我會嘗試在SO上寫一個小而完整的新問題。 – Damien022

+0

我還沒有得到關於編譯錯誤的[Cython問題](http://stackoverflow.com/questions/41926482/cython-compileerror-when-attempting-to-compile-extension-type)上的響應。你可以幫忙。 – Phillip