在pyx文件中,我定义了两个C函数并用python函数包装。
它被成功编译并输出了一个名为Rand_Motion.cpython-34m.so
的文件。
但是,我无法使用import Rand_Motion访问主文件中pyx文件中定义的任何一个函数。这是我的Rand_Motion.pyx
:
cdef extern from "math.h":
float cosf(float theta)
float sinf(float theta)
float acosf(float theta)
float sqrt(float x)
cdef object randmotion_c(int Dimension,float Diffcoe, float pos_x,float pos_y, float pos_z,float drift_l,float drift_a,float drift_b,float a,float b):
cdef float length=sqrt(2*Dimension*Diffcoe)
cdef float pi= 3.1415926535
cdef float e= 2.7182818284
cdef float a_d=a*2*pi
cdef float b_d=acosf(1-b*2)
pos_x+=length*sinf(b_d) *cosf(a_d)
pos_y+=length*sinf(b_d) *sinf(a_d)
pos_z+=length*cosf(b_d)
if drift_l>0:
drift_a=(drift_a/180)*pi
drift_b=(drift_b/180)*pi
pos_x+=drift_l*sinf(drift_a) *cosf(drift_b)
pos_y+=drift_l*sinf(drift_a) *sinf(drift_b)
pos_z+=drift_l*cosf(drift_a)
return pos_x,pos_y,pos_z
def randmotion(Dimension,Diffcoe, pos_x,pos_y,pos_z,drift_l,drift_a,drift_b,a,b):
return randmotion_c(Dimension,Diffcoe, pos_x,pos_y,pos_z,drift_l,drift_a,drift_b,a,b)
cdef dis_cal_c(float x,float y,float z):
return sqrt(x*x+y*y+z*z)
def dis_cal(x,y,z):
return dis_cal_c(x,y,z)
我将代码复制/粘贴到
aaa.pyx
文件中,然后运行:它似乎工作得很好(注意,我在与共享对象相同的目录下运行python)。 也许你想安装你的共享库?看看cython documentation about compilation或python documentation about installing modules。在
相关问题 更多 >
编程相关推荐