python如何调用c++numpy.ndarray代码_python – 在cython中使用numpy:定义ndarray数据类型/ ndims...

我正在尝试编写一些cython代码来进行numpy数组的计算. Cython似乎不喜欢我见过的所有示例中使用的[]来定义数据类型和维数.

例如,我有一个文件test.pyx:

cimport numpy as np

import numpy as np

ctypedef np.ndarray[np.float64_t, ndim=2] mymatrix

cpdef mymatrix hat (mymatrix x):

a = np.zeros((3,3));

a[0,1] = x[2,0];

a[0,2] = -x[1,0];

a[1,2] = x[0,0];

a[1,0] = -x[2,0];

a[2,0] = x[1,0];

a[2,1] = -x[0,0];

return a;

我使用setup.py编译它(参见结尾),我使用“python setup.py build_ext –inplace”运行

我得到以下输出:

running build_ext

cythoning test.pyx to test.c

Error converting Pyrex file to C:

------------------------------------------------------------

...

cimport numpy as np

import numpy as np

ctypedef np.ndarray[np.float64_t, ndim=2] mymatrix

^

------------------------------------------------------------

test.pyx:4:42: Syntax error in ctypedef statement

而如果我删除“[np.float64_t,ndim = 2]”部分,它可以正常工作.

有没有人有任何想法?

至于我的系统设置:

操作系统:Windows XP

完整,完整的pythonxy安装,版本2.6.5.1(此时最新)

据说pythonxy附带了cython,但我最终从这个站点安装了Python 2.6的cython版本0.12.1:http://www.lfd.uci.edu/~gohlke/pythonlibs/#cython

我怀疑我在某种程度上错过了一条路径或东西:我通过明确地将numpy头文件目录添加到mingw使用的包含路径来解决了一些问题(请参阅下面的setup.py文件)

这是我提到的setup.py文件:

from distutils.core import setup

from distutils.extension import Extension

from distutils.sysconfig import get_python_inc

from Cython.Distutils import build_ext

import os.path

inc_base = get_python_inc( plat_specific=1 );

incdir = os.path.join( get_python_inc( plat_specific=1 ), );

#libraries=['math'],

ext_modules = [Extension("test",

["test.pyx"],

include_dirs = [

os.path.join(inc_base,'..\\Lib\\site-packages\\numpy\\core\\include\\numpy'),

]

)

]

setup(

name = 'test',

cmdclass = {'build_ext': build_ext},

ext_modules = ext_modules

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值