如何使用SWIG将fortran命令2d numpy数组传递给c ++

时间:2014-04-01 01:21:47

标签: python c++ arrays numpy swig

我在c ++中有一个函数

myfun(double* array, int n1, int n2);

我在python中与numpy接口。在我的界面文件中我有

%apply (double* INPLACE_FARRAY2, int DIM1, int DIM2) {(double* inarray, int n1, int n2)}

现在,我想在python中将数组b = array([[3,27.0],[2,9],[10,1]],order='F')传递给myfun,但是我收到以下错误

TypeError: Array must be contiguous.  A non-contiguous array was given. 

我做错了什么?我的%apply语句中的双数据类型是否不正确?

1 个答案:

答案 0 :(得分:0)

Fortran order(order ='F')可能是问题,因为这与C顺序相反,我不确定因为我无法找到conumpuous与non for numpy的明确定义。所以值得尝试:

b = array([[3,27.0],[2,9],[10,1]],order='C')

此外,可能值得尝试

myfun( numpy.ascontiguousarray(b) )

我发现numpy swig reference doc对了解INPLACE_IN_字体图之间的区别非常有用。基本上后者是在c函数只读取而不是写入时使用的,因为长输入是某种类型的序列,SWIG代码将能够处理它。前者表示c函数将写入数组,因此原始容器中的排序必须匹配。