SWIG:将std::vector<std::vector<double>>指针传递给python

2024-09-27 00:16:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用SWIG调用Python的C++程序,有三个参数。最后一个参数也用于返回值。在

分析。i

%module coverage
%{
//#include "../SeqMCMC/src/funeval_base.hpp"
  #include "coverage.hpp"
%}

%include "std_vector.i"

void coverage(std::vector<double> *Pypar,  std::vector<std::vector<double> > *Pyxpass,   std::vector<std::vector<double> > *Pyypass);

在覆盖率.hpp在

^{pr2}$

在覆盖率.cpp在

#include "isofuneval.hpp"
#include "funeval_base.hpp"
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp> 

void coverage (std::vector<double> *Pypar,
               std::vector<std::vector<double>> *Pyxpass,
               std::vector<std::vector<double>> *Pyypass){

  //Convert the par, xpass and ypass vectors as ublas
  boost::numeric::ublas::vector<double> par((*Pypar).size());
  boost::numeric::ublas::matrix<double> xpass((*Pyxpass).size(),(*Pyxpass)[0].size());
  boost::numeric::ublas::matrix<double> ypass((*Pyypass).size(),(*Pyypass)[0].size());

  for (size_t i = 0; i < (*Pypar).size(); i++){
    par(i) = (*Pypar)[i];
  }

  for (size_t i = 0; i < (*Pyxpass).size(); i++){
    for (size_t j = 0; j <(*Pyxpass)[0].size(); j++){
      xpass(i,j) = (*Pyxpass)[i][j] ;}
  }

  for (size_t i = 0; i < (*Pyypass).size(); i++){
    for (size_t j = 0; j <(*Pyypass)[0].size(); j++){
      ypass(i,j) = (*Pyypass)[i][j];}
  }

  isofuneval CoveragePlot;

  CoveragePlot.function(par, xpass, ypass); //These should actually be references and or pointers if I expect ypass to have the result

  for (size_t i = 0; i < (*Pyypass).size(); i++){
    for (size_t j = 0; j <(*Pyypass)[0].size(); j++){
      (*Pyypass)[i][j] = ypass(i,j) ;
    }
  }
}

它编译并加载模块,但当我运行它时:

import _coverage as     coverage
coverage.coverage([3, 2, 1 ],[[4, 8423] , [4, 12] ],[[24,234 ], [23, 23] ])

我得到以下错误:

TypeError:在方法“coverage”中,类型为“std::vector<;double,std::allocator<;double>;*”的参数1


Tags: forsizeincludecoveragestddoublevectorhpp

热门问题