Use SWIG to wrap C++ <vector> as python NumPy array -
i have c++ library defines following (and more them) types:
typedef std::vector< double > doublevec; typedef std::vector< doublevec > doublevecvec; typedef std::vector< int > intvec; typedef std::vector< intvec > intvecvec;
i trying create python interface library handles objects that. title states, interface convert to/from c++ std::vector
, numpy ndarray
.
i have seen both numpy.i
provided numpy people , std_vector.i
swig people. problems numpy.i
created handle c/c++ arrays (not c++ vectors) , std_vector.i
doesn't conversion directly to/from numpy arrays.
any ideas?
i have seen fenics project has done this, project large having hard time finding out how specific task.
try starting point.
%include "numpy.i" %apply (size_t dim1, double* in_array1) {(size_t len_, double* vec_)} %rename (foo) my_foo; %inline %{ int my_foo(size_t len_, double* vec_) { std::vector<double> v; v.insert(v.end(), vec_, vec_ + len_); return foo(v); } %} %apply (size_t dim1, size_t dim2, double* in_array2) {(size_t len1_, size_t len2_, double* vec_)} %rename (bar) my_bar; %inline %{ int my_bar(size_t len1_, size_t len2_, double* vec_) { std::vector< std::vector<double> > v (len1_); (size_t = 0; < len1_; ++i) { v[i].insert(v[i].end(), vec_ + i*len2_, vec_ + (i+1)*len2_); } return bar(v); } %}
Comments
Post a Comment