使用numpy SWIG输入文件和向量。使用%apply? 我试图让我的C++代码使用SWIG来传递一个NUMPY数组。一切都设置得很好,但是当我通过python运行代码时,我得到了一个SwigPyObject。我似乎找不到正确的SWIG输入文件。我的函数如下所示: double*** runshapes(vector<vector<vector<double> > > &array3d, double T, double lam, double Vel){...} %module runshapes %{ #define SWIG_FILE_WITH_INIT #include "runshapes.h" %} %include "numpy.i" %init %{ import_array(); %} %include "std_vector.i" %include "std_string.i" // Instantiate templates used by example namespace std { %template(DoubleVector) vector<double>; %template(VecVecdouble) vector< vector<double> >; %template(VecVecVecdouble) vector< vector< vector<double> > >; } %include "runshapes.h" double***运行形状(矢量和阵列3D、, 双T, 双林, 双电平){…}
我的.i文件如下所示:使用numpy SWIG输入文件和向量。使用%apply? 我试图让我的C++代码使用SWIG来传递一个NUMPY数组。一切都设置得很好,但是当我通过python运行代码时,我得到了一个SwigPyObject。我似乎找不到正确的SWIG输入文件。我的函数如下所示: double*** runshapes(vector<vector<vector<double> > > &array3d, double T, double lam, double Vel){...} %module runshapes %{ #define SWIG_FILE_WITH_INIT #include "runshapes.h" %} %include "numpy.i" %init %{ import_array(); %} %include "std_vector.i" %include "std_string.i" // Instantiate templates used by example namespace std { %template(DoubleVector) vector<double>; %template(VecVecdouble) vector< vector<double> >; %template(VecVecVecdouble) vector< vector< vector<double> > >; } %include "runshapes.h" double***运行形状(矢量和阵列3D、, 双T, 双林, 双电平){…},python,c++,arrays,numpy,swig,Python,C++,Arrays,Numpy,Swig,我的.i文件如下所示: double*** runshapes(vector<vector<vector<double> > > &array3d, double T, double lam, double Vel){...} %module runshapes %{ #define SWIG_FILE_WITH_INIT #include "runshapes.h" %
double*** runshapes(vector<vector<vector<double> > > &array3d,
double T,
double lam,
double Vel){...}
%module runshapes
%{
#define SWIG_FILE_WITH_INIT
#include "runshapes.h"
%}
%include "numpy.i"
%init %{
import_array();
%}
%include "std_vector.i"
%include "std_string.i"
// Instantiate templates used by example
namespace std {
%template(DoubleVector) vector<double>;
%template(VecVecdouble) vector< vector<double> >;
%template(VecVecVecdouble) vector< vector< vector<double> > >;
}
%include "runshapes.h"
%模块运行形状
%{
#使用_INIT定义SWIG_文件
#包括“runshapes.h”
%}
%包括“numpy.i”
%初始%{
导入数组();
%}
%包括“标准向量i”
%包括“std_string.i”
//实例化示例使用的模板
名称空间标准{
%模板(双向量)向量;
%模板(VecDouble)向量;
%模板(VecDouble)向量<向量<向量>>;
}
%包括“runshapes.h”
它似乎理解向量,但输出(应该是一个3D数组)显示为SwigPyObject
如果可以简化问题,我可以将输出设置为3D向量:)
谢谢你的帮助!
Christina我通常使用如下NumPy类型图使用POD类型包装接口 向量3.h
#pragma once
#include <stddef.h>
int runshapes_wrap(const double* idata,
const size_t inx,
const size_t iny,
const size_t inz,
double** odata,
size_t* onx,
size_t* ony,
size_t* onz);
请注意,typemap ARGOUTVIEWM_ARRAY3确保在Python中删除相应的NumPy数组时删除分配的数据。使用模板,可以使其非常紧凑,但每个模板实例化都需要一个类型映射
# setup.py
from distutils.core import setup, Extension
setup(name="swig_vector",
py_modules=['swig_vector'],
ext_modules=[Extension("_swig_vector",
["vector3.i", "vector3.cpp"],
swig_opts=['-c++'],
extra_compile_args=['--std=c++11']
)]
)
使用
python setup.py build\u ext--inplace执行最后一个脚本将生成准备好测试功能的库。使用std::vector
您总是会复制数据,如果不进行复制,就无法使用指针创建std::vector
。我通常使用llvm
中的ArrayRef
或为std::vector制作包装,并使用numpy.I
中的常规方法进行swiging。如果你愿意,我可以发布一个解决方案。它很整洁