2 #include <boost/numpy.hpp> 7 namespace bpn = boost::numpy;
12 static PyObject*
convert(Eigen::VectorXd
const&
v)
14 Py_intptr_t shape[1] = { v.size() };
15 bpn::matrix result(bpn::zeros(1, shape, bpn::dtype::get_builtin<double>()));
16 std::copy(v.data(), v.data()+v.size(),
reinterpret_cast<double*
>(result.get_data()));
17 return bp::incref(result.ptr());
25 bp::converter::registry
26 ::push_back(&convertible,
28 bp::type_id<Eigen::VectorXd>());
36 bp::object obj(bp::handle<>(bp::borrowed(obj_ptr)));
37 std::auto_ptr<bpn::ndarray>
38 array(
new bpn::ndarray(bpn::from_object(obj,
39 bpn::dtype::get_builtin<double>(),
40 bpn::ndarray::V_CONTIGUOUS)));
42 if( (array->get_nd()==1)
43 || ( (array->get_nd()==2) && (array->get_shape()[1]==1) ))
44 return array.release();
47 }
catch (bp::error_already_set & err) {
48 bp::handle_exception();
55 bp::converter::rvalue_from_python_stage1_data* memory)
58 std::auto_ptr<bpn::ndarray>
59 array(reinterpret_cast<bpn::ndarray*>(memory->convertible));
60 const int nrow = array->get_shape()[0];
61 std::cout <<
"nrow = " << nrow << std::endl;
65 = ((bp::converter::rvalue_from_python_storage<Eigen::VectorXd>*)memory)
69 Eigen::VectorXd &
res = *
new (storage) Eigen::VectorXd(nrow);
72 double *
data = (
double*)array->get_data();
73 for(
int i=0;i<nrow;++i)
77 memory->convertible = storage;
84 Eigen::VectorXd
v = Eigen::VectorXd::Random(5);
85 std::cout << v.transpose() << std::endl;
91 std::cout <<
"test2: dim = " << v.size() <<
" ||| v[0] = " << v[0] << std::endl;
96 namespace bpn = boost::numpy;
100 bp::to_python_converter<Eigen::VectorXd,
104 bp::def(
"test",
test);
105 bp::def(
"test2",
test2);
Eigenvec_from_python_array()
boost::python::object matrix()
void test2(Eigen::VectorXd v)
BOOST_PYTHON_MODULE(libeigen)
static void construct(PyObject *, bp::converter::rvalue_from_python_stage1_data *memory)
static PyObject * convert(Eigen::VectorXd const &v)
static void * convertible(PyObject *obj_ptr)