本文主要是介绍C++ 扩展python(四)传递numpy(使用SWIG),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前期准备:
numpy.i文件,有些conda下载的numpy库里面自带,而有些没有(比如我的,,),可以去下载numpy.i源码。
我们实现一个cos_doubles模块的cos_double函数来对numpy数组求cos, 并返回他的cos值到一个新的numpy数组中:
测试样例test.py如下:
import numpy as np
import cos_doublesx = np.arange(0, 2 * np.pi, 0.1)
y = np.empty_like(x)cos_doubles.cos_doubles_func(x, y)
print(x)
print(y)
首先我们先实现cos_doubles 的C版本: cos_doubles.h
void cos_doubles(double * in_array, double * out_array, int size);
cos_doubles.c
#include <math.h>
#include "cos_doubles.h"/* Compute the cosine of each element in in_array, storing the result in* out_array. */
void cos_doubles(double * in_array, double * out_array, int size){int i;for(i=0;i<size;i++){out_array[i] = cos(in_array[i]);}
}
然后我们借助swig的接口文件 cos_doubles.i 实现扩展:
/* Example of wrapping a C function that takes a C double array as input using* numpy typemaps for SWIG. */%module cos_doubles
%{/* the resulting C file should be built as a python extension */#define SWIG_FILE_WITH_INIT/* Includes the header in the wrapper code */#include "cos_doubles.h"
%}/* include the numpy typemaps */
%include "numpy.i"
/* need this for correct module initialization */
%init %{import_array();
%}/* typemaps for the two arrays, the second will be modified in-place */
%apply (double* IN_ARRAY1, int DIM1) {(double * in_array, int size_in)}
%apply (double* INPLACE_ARRAY1, int DIM1) {(double * out_array, int size_out)}/* Wrapper for cos_doubles that massages the types */
%inline %{/* takes as input two numpy arrays */void cos_doubles_func(double * in_array, int size_in, double * out_array, int size_out) {/* calls the original funcion, providing only the size of the first */cos_doubles(in_array, out_array, size_in);}
%}
...我一开始忘了写import_array后面的;
- 为了使用Numpy类型映射,需要
numpy.i
文件。 - 观察
import_array()
的调用 - 因为类型映射仅仅支持参数
ARRAY, SIZE
我们需要包裹cos_doubles
为cos_doubles_func
,该函数接受两个数组包含各自大小作为输入。 - 相对于简单SWIG的例子,我们不需要包含
cos_doubles.h
头文件,因为我们通过cos_doubles_func
暴露这个功能,我们没有其它东西想暴露给Python。
然后通过distutils来包装他,如果在你的numpy库中包含了numpy.i,则需要include_dirs来指定位置。否则你可以把numpy.i放在当前目录下。
from distutils.core import setup, Extension
import numpysetup(ext_modules=[Extension("_cos_doubles",sources=["cos_doubles.c", "cos_doubles.i"],include_dirs=[numpy.get_include()])])
之前每次编译都出错,说我没有实现cos_doubels()函数。
后来才发现是因为我把函数写到了cpp文件里面,而上述的setup的方式,只能够实现c的转换,而对于c++的源码,可以采用下述方式:
swig -c++ -python cos_doubles.i
可以看到生成了两个文件cos_doubles.py与cos_doubles_wrap.cxx,然后setup.py修改为:
from distutils.core import setup, Extension
import numpysetup(ext_modules=[Extension("_cos_doubles",sources=["cos_doubles.cpp", "cos_doubles_wrap.cxx"],include_dirs=[numpy.get_include()])])
然后运行命令
python setup.py build_ext --inplace
运行测试代码test.py就可以发现现在cos_module可以正常使用了。
本文借鉴自
https://segmentfault.com/a/1190000000479951
https://blog.csdn.net/ustczhang/article/details/78147215
https://stackoverflow.com/questions/58460470/how-to-use-numpy-i-in-swig
https://stackoverflow.com/questions/51598137/swig-function-that-modifies-its-argument-from-c-to-python/51614104#51614104
这篇关于C++ 扩展python(四)传递numpy(使用SWIG)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!