vendredi 27 mars 2015

How do I call a python function in C++ using SWIG?


I have a the following C++



class myfun{
public:
virtual double eval(arma::vec& x){};
};

double op(myfun* f, arma::vec& x){
return f->eval(x);
}


where arma::vec is an armadillo c++ vector for which I am using armanpy.i to translate a numpy array as an arma::vec object and vice versa.


After enabling the swig directors, and creating a SWIG interface for the above C++ code, I create a python class which redefine's the eval virtual method:



class f(mymodule.myfun):
def __init__(self):
super(f,self).__init__()
def eval(self,x):
print x
return 3.14


In python, I first create an instance of f, which inherits myfun:



b = f()


Then, I pass this to op:



mymodule.op(b,array([3.,4.]))


which outputs



<Swig Object of type 'arma::vec *' at 0x10e6dcc30>
3.14


Python is not interpreting arma::vec * as a numpy array, which I thought it was doing in the armanpy.i file. Any ideas? I am not married to arma::vec and I could replace arma::vec with a numpy vector.


Thanks!




Aucun commentaire:

Enregistrer un commentaire