.. _program_listing_file__tmp_ws_src_eigenpy_include_eigenpy_eigen-to-python.hpp: Program Listing for File eigen-to-python.hpp ============================================ |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/eigenpy/include/eigenpy/eigen-to-python.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // // Copyright (c) 2014-2023 CNRS INRIA // #ifndef __eigenpy_eigen_to_python_hpp__ #define __eigenpy_eigen_to_python_hpp__ #include #include "eigenpy/fwd.hpp" #include "eigenpy/eigen-allocator.hpp" #include "eigenpy/numpy-allocator.hpp" #include "eigenpy/numpy-type.hpp" #include "eigenpy/registration.hpp" namespace boost { namespace python { template struct to_python_indirect_eigen { template inline PyObject* operator()(U const& mat) const { return eigenpy::EigenToPy::convert(const_cast(mat)); } #ifndef BOOST_PYTHON_NO_PY_SIGNATURES inline PyTypeObject const* get_pytype() const { return converter::registered_pytype::get_pytype(); } #endif }; template struct to_python_indirect< Eigen::Matrix&, MakeHolder> : to_python_indirect_eigen< Eigen::Matrix&, MakeHolder> {}; template struct to_python_indirect< const Eigen::Matrix&, MakeHolder> : to_python_indirect_eigen< const Eigen::Matrix&, MakeHolder> {}; } // namespace python } // namespace boost namespace eigenpy { EIGENPY_DOCUMENTATION_START_IGNORE template ::type> struct eigen_to_py_impl; template struct eigen_to_py_impl_matrix; template struct eigen_to_py_impl > : eigen_to_py_impl_matrix {}; template struct eigen_to_py_impl > : eigen_to_py_impl_matrix {}; template struct eigen_to_py_impl > : eigen_to_py_impl_matrix {}; template struct eigen_to_py_impl > : eigen_to_py_impl_matrix {}; template struct eigen_to_py_impl_matrix { static PyObject* convert( typename boost::add_reference< typename boost::add_const::type>::type mat) { typedef typename boost::remove_const< typename boost::remove_reference::type>::type MatrixDerived; assert((mat.rows() < INT_MAX) && (mat.cols() < INT_MAX) && "Matrix range larger than int ... should never happen."); const npy_intp R = (npy_intp)mat.rows(), C = (npy_intp)mat.cols(); PyArrayObject* pyArray; // Allocate Python memory if ((((!(C == 1) != !(R == 1)) && !MatrixDerived::IsVectorAtCompileTime) || MatrixDerived::IsVectorAtCompileTime)) // Handle array with a single // dimension { npy_intp shape[1] = {C == 1 ? R : C}; pyArray = NumpyAllocator::allocate( const_cast(mat.derived()), 1, shape); } else { npy_intp shape[2] = {R, C}; pyArray = NumpyAllocator::allocate( const_cast(mat.derived()), 2, shape); } // Create an instance (either np.array or np.matrix) return NumpyType::make(pyArray).ptr(); } }; #ifdef EIGENPY_WITH_TENSOR_SUPPORT template struct eigen_to_py_impl_tensor; template struct eigen_to_py_impl > : eigen_to_py_impl_tensor {}; template struct eigen_to_py_impl > : eigen_to_py_impl_tensor {}; template struct eigen_to_py_impl_tensor { static PyObject* convert( typename boost::add_reference< typename boost::add_const::type>::type tensor) { // typedef typename boost::remove_const< // typename boost::remove_reference::type>::type // TensorDerived; static const int NumIndices = TensorType::NumIndices; npy_intp shape[NumIndices]; for (int k = 0; k < NumIndices; ++k) shape[k] = tensor.dimension(k); PyArrayObject* pyArray = NumpyAllocator::allocate( const_cast(tensor), NumIndices, shape); // Create an instance (either np.array or np.matrix) return NumpyType::make(pyArray).ptr(); } }; #endif EIGENPY_DOCUMENTATION_END_IGNORE #ifdef EIGENPY_MSVC_COMPILER template struct EigenToPy::type::Scalar> #else template struct EigenToPy #endif : eigen_to_py_impl { static PyTypeObject const* get_pytype() { return getPyArrayType(); } }; template struct EigenToPyConverter { static void registration() { bp::to_python_converter, true>(); } }; } // namespace eigenpy #endif // __eigenpy_eigen_to_python_hpp__