2 #include <numpy/arrayobject.h> 8 template <
typename SCALAR>
struct NumpyEquivalentType {};
9 template <>
struct NumpyEquivalentType<double> {
enum { type_code = NPY_DOUBLE };};
10 template <>
struct NumpyEquivalentType<int> {
enum { type_code = NPY_INT };};
11 template <>
struct NumpyEquivalentType<float> {
enum { type_code = NPY_FLOAT };};
14 template<
typename MatType >
15 struct EigenMatrix_to_python_matrix
19 typedef typename MatType::Scalar T;
20 const int R = mat.rows(), C = mat.cols();
22 npy_intp shape[2] = {
R,C };
23 PyArrayObject* pyArray = (PyArrayObject*)
24 PyArray_SimpleNew(2, shape,
27 T* pyData = (T*)PyArray_DATA(pyArray);
28 Eigen::Map< Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> > pyMatrix(pyData,R,C);
31 return (PyObject*)pyArray;
36 template<
typename MatType>
42 bp::converter::registry
43 ::push_back(&convertible,
45 bp::type_id<MatType>());
51 typedef typename MatType::Scalar T;
53 if (!PyArray_Check(obj_ptr))
return 0;
55 std::cout <<
"Until here ok. ndim = " << PyArray_NDIM(obj_ptr) <<
" isvec " << MatType::IsVectorAtCompileTime << std::endl;
56 if (PyArray_NDIM(obj_ptr) != 2)
57 if ( (PyArray_NDIM(obj_ptr) !=1) || (! MatType::IsVectorAtCompileTime) )
59 std::cout <<
"Until here ok." << std::endl;
64 if (!(PyArray_FLAGS(obj_ptr) & NPY_ALIGNED))
66 std::cerr <<
"NPY non-aligned matrices are not implemented." << std::endl;
75 bp::converter::rvalue_from_python_stage1_data* memory)
77 typedef typename MatType::Scalar T;
78 using namespace Eigen;
80 std::cout <<
"Until here ok. Constructing..." << std::endl;
81 PyArrayObject * pyArray =
reinterpret_cast<PyArrayObject*
>(pyObj);
83 if ( PyArray_NDIM(pyArray) == 2 )
85 int R = MatType::RowsAtCompileTime;
86 int C = MatType::ColsAtCompileTime;
87 if (R == Eigen::Dynamic) R = PyArray_DIMS(pyArray)[0];
88 else assert(PyArray_DIMS(pyArray)[0]==R);
90 if (C == Eigen::Dynamic) C = PyArray_DIMS(pyArray)[1];
91 else assert(PyArray_DIMS(pyArray)[1]==C);
93 T* pyData =
reinterpret_cast<T*
>(PyArray_DATA(pyArray));
95 int itemsize = PyArray_ITEMSIZE(pyArray);
96 int stride1 = PyArray_STRIDE(pyArray, 0) / itemsize;
97 int stride2 = PyArray_STRIDE(pyArray, 1) / itemsize;
98 std::cout <<
"STRIDE = " << stride1 <<
" x " << stride2 << std::endl;
99 Eigen::Map<MatType,0,Eigen::Stride<Eigen::Dynamic,Eigen::Dynamic> >
100 pyMap( pyData, R,C, Eigen::Stride<Eigen::Dynamic,Eigen::Dynamic>(stride2,stride1) );
101 std::cout <<
"Map = " << pyMap << std::endl;
103 void* storage = ((bp::converter::rvalue_from_python_storage<MatType>*)
104 (memory))->storage.bytes;
105 MatType &
mat = * new (storage) MatType(R,C);
108 memory->convertible = storage;
112 int R = MatType::MaxSizeAtCompileTime, C=1;
113 if(R==Eigen::Dynamic) R = PyArray_DIMS(pyArray)[0];
114 else assert(PyArray_DIMS(pyArray)[0]==R);
116 T* pyData =
reinterpret_cast<T*
>(PyArray_DATA(pyArray));
118 int itemsize = PyArray_ITEMSIZE(pyArray);
119 int stride = PyArray_STRIDE(pyArray, 0) / itemsize;
120 Eigen::Stride<Eigen::Dynamic,Eigen::Dynamic> s(stride,0);
121 Eigen::Map<MatType,0,Eigen::InnerStride<Eigen::Dynamic> >
122 pyMap( pyData, R, 1, Eigen::InnerStride<Eigen::Dynamic>(stride) );
123 std::cout <<
"Map = " << pyMap << std::endl;
125 void* storage = ((bp::converter::rvalue_from_python_storage<MatType>*)
126 (memory))->storage.bytes;
127 MatType &
mat = * new (storage) MatType(R,C);
130 memory->convertible = storage;
140 Eigen::MatrixXd
mat = Eigen::MatrixXd::Random(3,6);
141 std::cout <<
"EigenMAt = " << mat << std::endl;
146 Eigen::VectorXd
mat = Eigen::VectorXd::Random(6);
147 std::cout <<
"EigenVec = " << mat << std::endl;
153 std::cout <<
"Test2 mat = " << mat << std::endl;
157 std::cout <<
"Test2 vec = " << v << std::endl;
164 bp::to_python_converter<Eigen::MatrixXd,
168 bp::to_python_converter<Eigen::VectorXd,
172 bp::def(
"test",
test);
174 bp::def(
"test2",
test2);
void test2(Eigen::MatrixXd mat)
static void * convertible(PyObject *obj_ptr)
static PyObject * convert(MatType const &mat)
static void construct(PyObject *pyObj, bp::converter::rvalue_from_python_stage1_data *memory)
void test2Vec(Eigen::VectorXd v)
EigenMatrix_from_python_array()
Eigen::VectorXd testVec()
BOOST_PYTHON_MODULE(libeigentemplate)