5#ifndef __eigenpy_eigen_to_python_hpp__ 6#define __eigenpy_eigen_to_python_hpp__ 8#include <boost/type_traits.hpp> 10#include "eigenpy/fwd.hpp" 12#include "eigenpy/eigen-allocator.hpp" 13#include "eigenpy/numpy-allocator.hpp" 14#include "eigenpy/scipy-allocator.hpp" 15#include "eigenpy/numpy-type.hpp" 16#include "eigenpy/scipy-type.hpp" 17#include "eigenpy/registration.hpp" 21EIGENPY_DOCUMENTATION_START_IGNORE
23template <
typename EigenType,
24 typename BaseType =
typename get_eigen_base_type<EigenType>::type>
27template <
typename MatType>
30template <
typename MatType>
34template <
typename MatType>
38template <
typename MatType>
42template <
typename MatType>
46template <
typename MatType>
48 static PyObject* convert(
49 typename boost::add_reference<
50 typename boost::add_const<MatType>::type>
::type mat) {
51 typedef typename boost::remove_const<
52 typename boost::remove_reference<MatType>::type>
::type MatrixDerived;
54 assert((mat.rows() < INT_MAX) && (mat.cols() < INT_MAX) &&
55 "Matrix range larger than int ... should never happen.");
56 const npy_intp R = (npy_intp)mat.rows(), C = (npy_intp)mat.cols();
58 PyArrayObject* pyArray;
60 if ((((!(C == 1) != !(R == 1)) && !MatrixDerived::IsVectorAtCompileTime) ||
61 MatrixDerived::IsVectorAtCompileTime))
64 npy_intp shape[1] = {C == 1 ? R : C};
66 const_cast<MatrixDerived&
>(mat.derived()), 1, shape);
68 npy_intp shape[2] = {R, C};
70 const_cast<MatrixDerived&
>(mat.derived()), 2, shape);
74 return NumpyType::make(pyArray).ptr();
77 static PyTypeObject
const* get_pytype() {
return getPyArrayType(); }
80template <
typename MatType>
83template <
typename MatType>
87template <
typename MatType>
91template <
typename MatType>
95template <
typename MatType>
99template <
typename MatType>
101 enum { IsRowMajor = MatType::IsRowMajor };
103 static PyObject* convert(
104 typename boost::add_reference<
105 typename boost::add_const<MatType>::type>
::type mat) {
106 typedef typename boost::remove_const<
107 typename boost::remove_reference<MatType>::type>
::type MatrixDerived;
116 static PyTypeObject
const* get_pytype() {
117 return IsRowMajor ? ScipyType::getScipyCSRMatrixType()
118 : ScipyType::getScipyCSCMatrixType();
122#ifdef EIGENPY_WITH_TENSOR_SUPPORT 123template <
typename TensorType>
124struct eigen_to_py_impl_tensor;
126template <
typename TensorType>
128 : eigen_to_py_impl_tensor<TensorType> {};
130template <
typename TensorType>
131struct eigen_to_py_impl<const TensorType, const Eigen::TensorBase<TensorType>>
132 : eigen_to_py_impl_tensor<const TensorType> {};
134template <
typename TensorType>
135struct eigen_to_py_impl_tensor {
136 static PyObject* convert(
137 typename boost::add_reference<
138 typename boost::add_const<TensorType>::type>::type tensor) {
143 static const int NumIndices = TensorType::NumIndices;
144 npy_intp shape[NumIndices];
145 for (
int k = 0; k < NumIndices; ++k) shape[k] = tensor.dimension(k);
147 PyArrayObject* pyArray = NumpyAllocator<TensorType>::allocate(
148 const_cast<TensorType&
>(tensor), NumIndices, shape);
151 return NumpyType::make(pyArray).ptr();
154 static PyTypeObject
const* get_pytype() {
return getPyArrayType(); }
158EIGENPY_DOCUMENTATION_END_IGNORE
160template <
typename EigenType,
162 typename boost::remove_reference<EigenType>::type::Scalar>
165template <
typename MatType>
167 static void registration() {
168 bp::to_python_converter<MatType, EigenToPy<MatType>,
true>();
177template <
typename MatrixRef,
class MakeHolder>
180 inline PyObject* operator()(U
const& mat)
const {
184#ifndef BOOST_PYTHON_NO_PY_SIGNATURES 185 inline PyTypeObject
const* get_pytype()
const {
186 return converter::registered_pytype<MatrixRef>::get_pytype();
191template <
typename Scalar,
int RowsAtCompileTime,
int ColsAtCompileTime,
192 int Options,
int MaxRowsAtCompileTime,
int MaxColsAtCompileTime,
194struct to_python_indirect<
195 Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options,
196 MaxRowsAtCompileTime, MaxColsAtCompileTime>&,
199 Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options,
200 MaxRowsAtCompileTime, MaxColsAtCompileTime>&,
203template <
typename Scalar,
int RowsAtCompileTime,
int ColsAtCompileTime,
204 int Options,
int MaxRowsAtCompileTime,
int MaxColsAtCompileTime,
206struct to_python_indirect<
207 const Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options,
208 MaxRowsAtCompileTime, MaxColsAtCompileTime>&,
211 const Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime,
212 Options, MaxRowsAtCompileTime,
213 MaxColsAtCompileTime>&,