I have a C++ header which returns a struct having 3 elements. How can I make python accept the struct variable properly?
This is what I have in the C++ function :
// Function name myfunc
struct velocity
{
std::vector< std::vector<double> > u;
std::vector< std::vector<double> > v;
std::vector< std::vector<double> > w;
};
velocity velo; //Creating object
velo.u = sum(umean,pu);
velo.v = sum(vmean,pv);
velo.w = sum(wmean,pw);
return(velo)
This is my Python implementation, after using SWIG
import numpy
from myfunc import * # importing C++ myfunc library
My = 100 # Matrix dimensions
Mz = 100
z = myfunc(My,Mz) # Supplying the matrix dimensions to the myfunc library
print(z)
The error message I get upon executing this:
<myfunc.velocity; proxy of <Swig Object of type 'velocity *' at 0x2951ae0> >
I know that I have to somehow define in SWIG to make python take a struct "as is". Is there any way? Or any alternative method you may suggest? Here is my SWIG file
%module myfunc
%{
#include "myfunc.h"
%}
%include "std_vector.i"
// Instantiate templates used by example
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
%template(twodvector) std::vector< std::vector<double> >;
}
struct velocity
{
std::vector< std::vector<double> > u;
std::vector< std::vector<double> > v;
std::vector< std::vector<double> > w;
};
%include "myfunc.h"
Note that I have declared a struct here. This compiles successfully on SWIG but I do not know how to use it in Python to actually get the C++ struct!
This is not an error message:
<myfunc.velocity; proxy of <Swig Object of type 'velocity *' at 0x2951ae0> >
It just indicates you have a SWIG proxy object that wraps a pointer to a velocity object. You can just access z.u[0][0]
, for example, to access an element of one of the double vectors.
Edit
Here's an example with typemaps defined for vector<vector<double>>
. They are not pretty, but allow assignment of Python "list of lists" to the velocity
members directly:
%module x
%begin %{
#pragma warning(disable:4127 4701 4706 4996)
#include <vector>
#include <algorithm>
#include <sstream>
%}
%include <std_vector.i>
%include <std_string.i>
%template(vector_double) std::vector<double>;
%template(vector_vector_double) std::vector<std::vector<double> >;
// Input typemap converts from Python object to C++ object.
// Note error checking not shown for brevity.
// $input is the Python object, $1 is the C++ result.
//
%typemap(in) std::vector<std::vector<double> >* (std::vector<std::vector<double> > tmp) %{
for(Py_ssize_t i = 0; i < PySequence_Size($input); ++i)
{
auto t = PySequence_GetItem($input,i);
std::vector<double> vd;
for(Py_ssize_t j = 0; j < PySequence_Size(t); ++j) {
auto d = PySequence_GetItem(t,j);
vd.push_back(PyFloat_AsDouble(d));
Py_DECREF(d);
}
Py_DECREF(t);
tmp.push_back(vd);
}
$1 = &tmp;
%}
// Output typemap converts from C++object to Python object.
// Note error checking not shown for brevity.
// $1 is the C++ object, $result is the Python result.
//
%typemap(out) std::vector<std::vector<double> >* %{
$result = PyList_New($1->size()); // Create outer Python list of correct size
for(size_t i = 0; i < $1->size(); ++i)
{
auto t = PyList_New((*$1)[i].size()); // Create inner Python list of correct size for this element.
for(size_t j = 0; j < (*$1)[i].size(); ++j) {
PyList_SET_ITEM(t,j,PyFloat_FromDouble((*$1)[i][j]));
}
PyList_SET_ITEM($result,i,t);
}
%}
%inline %{
struct velocity
{
std::vector<std::vector<double> > u;
std::vector<std::vector<double> > v;
std::vector<std::vector<double> > w;
};
// A test function with an in/out velocity parameter.
void myfunc(velocity& vel)
{
for(auto& v : vel.u)
std::transform(begin(v),end(v),begin(v),[](double d){return d*1.1;});
for(auto& v : vel.v)
std::transform(begin(v),end(v),begin(v),[](double d){return d*2.2;});
for(auto& v : vel.w)
std::transform(begin(v),end(v),begin(v),[](double d){return d*3.3;});
}
%}
Use example:
>>> import x
>>> vel=x.velocity()
>>> vel.u = [1,2,3],[4.5,6,7]
>>> vel.v = [1,2],[3,4,5]
>>> vel.w = [1],[2,3]
>>> vel.u
[[1.0, 2.0, 3.0], [4.5, 6.0, 7.0]]
>>> vel.v
[[1.0, 2.0], [3.0, 4.0, 5.0]]
>>> vel.w
[[1.0], [2.0, 3.0]]
>>> x.myfunc(vel)
>>> vel.u
[[1.1, 2.2, 3.3000000000000003], [4.95, 6.6000000000000005, 7.700000000000001]]
>>> vel.v
[[2.2, 4.4], [6.6000000000000005, 8.8, 11.0]]
>>> vel.w
[[3.3], [6.6, 9.899999999999999]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With