【发布时间】:2016-01-04 14:06:36
【问题描述】:
由于我有多维数组Passing memoryview to C function,因此以下线程中的答案对我没有帮助。
我有以下测试代码:
头文件:
//cm.h
double per(double **marr,int rows,int cols);
带有 C 函数的文件
//cm.c
#include <stdio.h>
double per(double **marr,int rows,int cols){
int i,j;
for (i=0;i<rows;i++){
for (j=0;j<cols;j++){
//Just update the array
marr[i][j] = (double)(i+1)*(j+1);
}
}
}
Cython 文件:
#m.pyz
import numpy as np
cimport numpy as np
from cython.view cimport array as cvarray
cdef extern from 'cm.h':
double per(double **marr,int rows,int cols);
def test(double[:,::1] x):
cdef int rows = x.shape[0]
cdef int cols = x.shape[1]
per(x,rows,cols)
for i in range(rows):
for j in range(cols):
print x[i,j]
以及错误信息:
Error compiling Cython file:
------------------------------------------------------------
...
double per(double **marr,int rows,int cols);
def test(double[:,::1] x):
cdef int rows = x.shape[0]
cdef int cols = x.shape[1]
per(x,rows,cols)
^
------------------------------------------------------------
m.pyx:12:9: Cannot assign type 'double[:, ::1]' to 'double **'
我读过类型化的 memoryviews 是在 Cython 中处理 Python 数组的最现代的方法,但我不知道如何做到这一点。我在 C 中有一些数值配方,它们在动态制作的大型多维数组上运行。 我尝试做的事情是完全错误的吗?
【问题讨论】:
标签: multidimensional-array cython typed