Skip to content

Commit f528d72

Browse files
committed
sparse: fix handling of nnz==0
1 parent 02fe171 commit f528d72

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

include/eigenpy/scipy-allocator.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ struct scipy_allocator_impl_sparse_matrix {
8585
// scipy_sparse_matrix_type(*bp::make_tuple(0,0),**args);
8686
scipy_sparse_matrix = scipy_sparse_matrix_type(
8787
Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>(0, 0));
88+
} else if (mat.nonZeros() == 0) {
89+
scipy_sparse_matrix =
90+
scipy_sparse_matrix_type(bp::make_tuple(mat.rows(), mat.cols()));
8891
} else {
8992
scipy_sparse_matrix = scipy_sparse_matrix_type(bp::make_tuple(
9093
DataVector(data),

include/eigenpy/sparse/eigen-from-python.hpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,15 @@ void eigen_sparse_matrix_from_py_construct(
146146
const Eigen::Index m = bp::extract<Eigen::Index>(shape[0]),
147147
n = bp::extract<Eigen::Index>(shape[1]),
148148
nnz = bp::extract<Eigen::Index>(obj.attr("nnz"));
149-
MapMatOrRefType sparse_map(m, n, nnz, indptr.data(), indices.data(),
150-
data.data());
149+
150+
// Handle the specific case of the null matrix
151+
Scalar *data_ptr = nullptr;
152+
StorageIndex *indices_ptr = nullptr;
153+
if (nnz > 0) {
154+
data_ptr = data.data();
155+
indices_ptr = indices.data();
156+
}
157+
MapMatOrRefType sparse_map(m, n, nnz, indptr.data(), indices_ptr, data_ptr);
151158

152159
new (raw_ptr) MatOrRefType(sparse_map);
153160
}

0 commit comments

Comments
 (0)