5 #ifndef __eigenpy_sparse_eigen_from_python_hpp__
6 #define __eigenpy_sparse_eigen_from_python_hpp__
15 template <
typename SparseMatrixType>
17 Eigen::SparseMatrixBase<SparseMatrixType> > {
19 PyTypeObject
const *py_type = ScipyType::get_pytype<SparseMatrixType>();
30 template <
typename Scalar,
int Options,
typename StorageIndex>
31 struct expected_pytype_for_arg<
32 Eigen::SparseMatrix<Scalar, Options, StorageIndex> >
34 Eigen::SparseMatrix<Scalar, Options, StorageIndex> > {};
36 template <
typename Scalar,
int Options,
typename StorageIndex>
37 struct rvalue_from_python_data<
38 Eigen::SparseMatrix<Scalar, Options, StorageIndex> const &>
40 Eigen::SparseMatrix<Scalar, Options, StorageIndex> const &> {
41 typedef Eigen::SparseMatrix<Scalar, Options, StorageIndex>
T;
45 template <
typename Derived>
46 struct rvalue_from_python_data<
Eigen::SparseMatrixBase<Derived> const &>
79 template <
typename SparseMatrixType>
81 Eigen::SparseMatrixBase<SparseMatrixType> > {
82 typedef typename SparseMatrixType::Scalar
Scalar;
89 bp::converter::rvalue_from_python_stage1_data *memory);
94 template <
typename SparseMatrixType>
97 Eigen::SparseMatrixBase<SparseMatrixType> >::convertible(PyObject *pyObj) {
98 const PyTypeObject *type = Py_TYPE(pyObj);
99 const PyTypeObject *sparse_matrix_py_type =
100 ScipyType::get_pytype<SparseMatrixType>();
101 typedef typename SparseMatrixType::Scalar
Scalar;
103 if (type != sparse_matrix_py_type)
return 0;
105 bp::object obj(bp::handle<>(bp::borrowed(pyObj)));
109 if (!np_type_is_convertible_into_scalar<Scalar>(type_num))
return 0;
114 template <
typename MatOrRefType>
116 PyObject *pyObj, bp::converter::rvalue_from_python_stage1_data *memory) {
117 typedef typename MatOrRefType::Scalar Scalar;
118 typedef typename MatOrRefType::StorageIndex StorageIndex;
120 typedef Eigen::Map<MatOrRefType> MapMatOrRefType;
122 bp::converter::rvalue_from_python_storage<MatOrRefType> *storage =
124 bp::converter::rvalue_from_python_storage<MatOrRefType> *
>(
125 reinterpret_cast<void *
>(memory));
126 void *raw_ptr = storage->storage.bytes;
128 bp::object obj(bp::handle<>(bp::borrowed(pyObj)));
131 const int type_num_eigen_sparse_matrix = Register::getTypeCode<Scalar>();
133 if (type_num_eigen_sparse_matrix == type_num_python_sparse_matrix) {
134 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> DataVector;
136 DataVector
data = bp::extract<DataVector>(obj.attr(
"data"));
137 bp::tuple shape = bp::extract<bp::tuple>(obj.attr(
"shape"));
138 typedef Eigen::Matrix<StorageIndex, Eigen::Dynamic, 1> StorageIndexVector;
141 StorageIndexVector indices =
142 bp::extract<StorageIndexVector>(obj.attr(
"indices"));
143 StorageIndexVector indptr =
144 bp::extract<StorageIndexVector>(obj.attr(
"indptr"));
146 const Eigen::Index
m = bp::extract<Eigen::Index>(shape[0]),
147 n = bp::extract<Eigen::Index>(shape[1]),
148 nnz = bp::extract<Eigen::Index>(obj.attr(
"nnz"));
151 Scalar *data_ptr =
nullptr;
152 StorageIndex *indices_ptr =
nullptr;
154 data_ptr =
data.data();
155 indices_ptr = indices.data();
157 MapMatOrRefType sparse_map(
m, n, nnz, indptr.data(), indices_ptr, data_ptr);
159 new (raw_ptr) MatOrRefType(sparse_map);
162 memory->convertible = storage->storage.bytes;
165 template <
typename SparseMatrixType>
166 void eigen_from_py_impl<SparseMatrixType,
167 Eigen::SparseMatrixBase<SparseMatrixType> >::
168 construct(PyObject *pyObj,
169 bp::converter::rvalue_from_python_stage1_data *memory) {
170 eigen_sparse_matrix_from_py_construct<SparseMatrixType>(pyObj, memory);
173 template <
typename SparseMatrixType>
176 Eigen::SparseMatrixBase<SparseMatrixType> >::registration() {
177 bp::converter::registry::push_back(
180 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
187 template <
typename SparseMatrixType>
189 SparseMatrixType,
Eigen::SparseMatrixBase<SparseMatrixType> > {
194 typedef Eigen::SparseMatrixBase<SparseMatrixType> SparseMatrixBase;
207 template <
typename SparseMatrixType>
211 typedef Eigen::SparseMatrixBase<SparseMatrixType>
Base;
214 bp::converter::registry::push_back(
217 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
274 #endif // __eigenpy_sparse_eigen_from_python_hpp__