Skip to content
Snippets Groups Projects
Commit cd6e8a48 authored by Nicolas Mansard's avatar Nicolas Mansard Committed by nmansard
Browse files

Convertion from Eigen to Numpy::Matrix.

parent 211ab1b2
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ verbose = False ...@@ -5,7 +5,7 @@ verbose = False
if verbose: print "===> From MatrixXd to Py" if verbose: print "===> From MatrixXd to Py"
M = eigenpy.naturals(3,3,verbose) M = eigenpy.naturals(3,3,verbose)
Mcheck = np.reshape(np.array(range(9),np.double),[3,3]) Mcheck = np.reshape(np.matrix(range(9),np.double),[3,3])
assert np.array_equal(Mcheck,M) assert np.array_equal(Mcheck,M)
if verbose: print "===> From Matrix3d to Py" if verbose: print "===> From Matrix3d to Py"
...@@ -14,13 +14,13 @@ assert np.array_equal(Mcheck,M33) ...@@ -14,13 +14,13 @@ assert np.array_equal(Mcheck,M33)
if verbose: print "===> From VectorXd to Py" if verbose: print "===> From VectorXd to Py"
v = eigenpy.naturalsX(3,verbose) v = eigenpy.naturalsX(3,verbose)
vcheck = np.array([range(3),],np.double).T vcheck = np.matrix([range(3),],np.double).T
assert np.array_equal(vcheck ,v) assert np.array_equal(vcheck ,v)
if verbose: print "===> From Py to Eigen::MatrixXd" if verbose: print "===> From Py to Eigen::MatrixXd"
if verbose: print "===> From Py to Eigen::MatrixXd" if verbose: print "===> From Py to Eigen::MatrixXd"
if verbose: print "===> From Py to Eigen::MatrixXd" if verbose: print "===> From Py to Eigen::MatrixXd"
Mref = np.reshape(np.array(range(64),np.double),[8,8]) Mref = np.reshape(np.matrix(range(64),np.double),[8,8])
if verbose: print "===> Matrix 8x8" if verbose: print "===> Matrix 8x8"
M = Mref M = Mref
...@@ -68,7 +68,7 @@ if verbose: print "===> From Py to Eigen::VectorXd" ...@@ -68,7 +68,7 @@ if verbose: print "===> From Py to Eigen::VectorXd"
if verbose: print "===> Block Vector 0:6:2x1 1 dim" if verbose: print "===> Block Vector 0:6:2x1 1 dim"
M = Mref[0:6:2,1].T M = Mref[0:6:2,1].T
assert( np.array_equal(np.array([M,]).T,eigenpy.reflexV(M,verbose)) ); assert( np.array_equal(M.T,eigenpy.reflexV(M,verbose)) );
if verbose: print "===> Block Vector 0:6:2x1" if verbose: print "===> Block Vector 0:6:2x1"
M = Mref[0:6:2,1:2] M = Mref[0:6:2,1:2]
......
...@@ -42,6 +42,31 @@ namespace eigenpy ...@@ -42,6 +42,31 @@ namespace eigenpy
template <> struct NumpyEquivalentType<int> { enum { type_code = NPY_INT };}; template <> struct NumpyEquivalentType<int> { enum { type_code = NPY_INT };};
template <> struct NumpyEquivalentType<float> { enum { type_code = NPY_FLOAT };}; template <> struct NumpyEquivalentType<float> { enum { type_code = NPY_FLOAT };};
struct PyMatrixType
{
boost::python::object pyMatrixType;
boost::python::object pyModule;
PyMatrixType()
{
pyModule = boost::python::import("numpy");
pyMatrixType = pyModule.attr("matrix");
}
operator boost::python::object () { return pyMatrixType; }
boost::python::object make(PyArrayObject* pyArray, bool copy = false)
{ return make((PyObject*)pyArray,copy); }
boost::python::object make(PyObject* pyObj, bool copy = false)
{
boost::python::object m
= pyMatrixType( boost::python::object(boost::python::handle<>(pyObj)),
boost::python::object(), copy );
Py_INCREF(m.ptr());
return m;
}
};
static PyMatrixType pyMatrixType = PyMatrixType();
/* --- TO PYTHON -------------------------------------------------------------- */ /* --- TO PYTHON -------------------------------------------------------------- */
template< typename MatType,typename EquivalentEigenType > template< typename MatType,typename EquivalentEigenType >
...@@ -60,15 +85,7 @@ namespace eigenpy ...@@ -60,15 +85,7 @@ namespace eigenpy
MapNumpy<EquivalentEigenType>::map(pyArray) = mat; MapNumpy<EquivalentEigenType>::map(pyArray) = mat;
return pyMatrixType.make(pyArray).ptr();
boost::python::object pyModule = boost::python::import("numpy");
boost::python::object pyMatrixType = pyModule.attr("matrix");
boost::python::object m
= pyMatrixType( boost::python::object(boost::python::handle<>((PyObject*)pyArray)),
boost::python::object(), true );
Py_INCREF(m.ptr());
return m.ptr();
} }
}; };
...@@ -198,5 +215,4 @@ namespace eigenpy ...@@ -198,5 +215,4 @@ namespace eigenpy
} }
} // namespace eigenpy } // namespace eigenpy
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment