如何使用SWIG将Fortran有序的2d numpy数组传递到c ++中
How to pass a fortran ordered 2d numpy array into c++ using 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)}
现在,我想将数组b = array([[3,27.0],[2,9],[10,1]],order='F')
传递到 python 中的 myfun 中,但出现以下错误
TypeError: Array must be contiguous. A non-contiguous array was given.
我做错了什么?我的 %apply
语句中的双精度数据类型不正确吗?
Fortran 顺序 (order='F') 可能是问题所在,因为这与 C 顺序相反,我不确定,因为我无法找到 numpy 的意外与非的明确定义。因此,可能值得尝试:
b = array([[3,27.0],[2,9],[10,1]],order='C')
另外,可能值得一试
myfun( numpy.ascontiguousarray(b) )
我发现 numpy swig 参考文档有助于理解INPLACE_
和IN_
类型图之间的区别。基本上,当c函数只读取而不写入时,使用后者,只要输入是某种序列,SWIG代码就可以处理它。前者表示 c 函数将写入数组,因此原始容器中的顺序必须匹配。
相关文章:
- C++返回 Numpy 数组的 Python 扩展模块
- 在 Cython 中将C++向量转换为 numpy 数组,而无需复制
- 如何通过 cython 将 numpy 数组列表传递给 C++
- Pybind11 默认参数 numpy 数组或 None
- Pybind11:在C++中使用Pybind11转换numpy数组的问题
- 使用SWIG将numpy数组元素(int)传递给c++int
- 如何将 numpy 二维数组作为一种可以用C++读取的二进制格式存储在磁盘上
- 从 numpy 数组获取指针以将图像发送到C++
- 从结构的 C 数组到带有 ctypes 的 NumPy 数组的高效转换
- pybind11 返回 numpy 对象数组
- 使用 Boost.Python 将 numpy 数组传递给C++
- 如何使用pybind传递numpy数组列表
- 通过 Pybind11 返回 numpy 数组
- 如何在C 中创建类似于Python的Numpy数组的数组
- Xtensor将numpy数组传递到XT :: XTENSOR参数类型
- Cython:从参考获得时缺少两个前元素的 Numpy 数组
- 将 numpy 数组公开为C++向量
- Numpy C API - 使用 PyArray_Descr 创建数组会导致段错误
- 带有Boost Python的Wrap C 功能 - Numpy数组类型
- 在C 中,可以用语法[i,j,..]实现访问数组元素的功能,而语法就像numpy中的ndarray样式一样