49 template <
typename SimilarMatrixType>
50 static PyObject *allocate(
51 const Eigen::SparseCompressedBase<SimilarMatrixType> &mat_,
53 EIGENPY_UNUSED_VARIABLE(copy);
54 typedef typename SimilarMatrixType::Scalar Scalar;
55 typedef typename SimilarMatrixType::StorageIndex StorageIndex;
57 enum { IsRowMajor = SimilarMatrixType::IsRowMajor };
59 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> DataVector;
60 typedef const Eigen::Map<const DataVector> MapDataVector;
61 typedef Eigen::Matrix<StorageIndex, Eigen::Dynamic, 1> StorageIndexVector;
62 typedef Eigen::Matrix<int32_t, Eigen::Dynamic, 1> ScipyStorageIndexVector;
63 typedef const Eigen::Map<const StorageIndexVector> MapStorageIndexVector;
65 SimilarMatrixType &mat = mat_.const_cast_derived();
66 bp::object scipy_sparse_matrix_type =
67 ScipyType::get_pytype_object<SimilarMatrixType>();
69 MapDataVector data(mat.valuePtr(), mat.nonZeros());
70 MapStorageIndexVector outer_indices(
71 mat.outerIndexPtr(), (IsRowMajor ? mat.rows() : mat.cols()) + 1);
72 MapStorageIndexVector inner_indices(mat.innerIndexPtr(), mat.nonZeros());
74 bp::object scipy_sparse_matrix;
76 if (mat.rows() == 0 &&
86 scipy_sparse_matrix = scipy_sparse_matrix_type(
87 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>(0, 0));
88 }
else if (mat.nonZeros() == 0) {
90 scipy_sparse_matrix_type(bp::make_tuple(mat.rows(), mat.cols()));
92 scipy_sparse_matrix = scipy_sparse_matrix_type(bp::make_tuple(
94 ScipyStorageIndexVector(inner_indices.template
cast<int32_t>()),
95 ScipyStorageIndexVector(
100 Py_INCREF(scipy_sparse_matrix.ptr());
101 return scipy_sparse_matrix.ptr();