python - SWIG 输入文件和带有 numpy 的 vector 。使用%申请?

标签 python c++ arrays numpy swig

我正在尝试让我的 C++ 代码使用 Swig 传递一个 numpy 数组。一切都设置得很好,但是当我通过 python 运行我的代码时,我得到了一个 SwigPyObject。我似乎想不出正确的 SWIG 输入文件。我的函数如下所示:

    double*** runshapes(vector<vector<vector<double> > > &array3d,
            double T,
            double lam,
            double Vel){...}

我的 .i 文件如下所示:

%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"

它似乎理解 vector ,但输出(应该是 3D 数组)作为 SwigPyObject 出现。

如果这样可以简化问题,我可以将输出设为 3D vector :)

感谢您的帮助! 克里斯蒂娜

最佳答案

我通常使用 NumPy 类型映射使用 POD 类型包装接口(interface),如下所示。

vector3.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);

vector3.cpp

#include "vector3.h"

#include <stdio.h>
#include <malloc.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) {
  // Note this one allocates
  size_t nx = 10;
  size_t ny = 20;
  size_t nz = 30;

  *odata = (double*) malloc(sizeof(double)*nx*ny*nz);
  *onx = nx;
  *ony = ny;
  *onz = nz;

  // Do whatever
  printf("inx,iny,inz: %zu, %zu, %zu\n",nx,ny,nz);
  return 0;
}

vector3.i

%module(docstring="This is a Python wrapper for Sofus") swig_vector
%{
  #define SWIG_FILE_WITH_INIT  
  #include "vector3.h"
%}

%include "numpy.i"

%init
%{
  import_array();
%}

%apply (double** ARGOUTVIEWM_ARRAY3, size_t* DIM1, size_t* DIM2, size_t* DIM3) {(double** odata, size_t* onx, size_t* ony, size_t* onz)}

%apply (double* IN_ARRAY3, int DIM1, int DIM2, int DIM3) {(const double* idata, const size_t inx, const size_t iny, const size_t inz)};

%include "vector3.h"

请注意,类型映射 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_ext --inplace 执行最后一个脚本生成准备好测试功能的库

关于python - SWIG 输入文件和带有 numpy 的 vector 。使用%申请?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32701212/

相关文章:

c++ - 计算模式最大值

python - 出现 "integer is required (got type tuple)"错误,使用 cv2 绘制矩形

python - Pandas:复制结构并设置所有元素

c# - .NET 包装器到 C++ 代码 : CS0122 is inaccessible due to its protection level error

C++:在子进程上使用 cin

arrays - 在 ocamlre 中的两个数组之间迭代构造函数

python - Pandas value_counts() for loop 因 lambda 而失败

python - 一个按钮绑定(bind)两个命令

c++ - 主动模式和被动模式下的 FTP 服务器端口

javascript - jQuery 在单击时将数组中的项目移动到索引 0