Eigen: Demonstrate and fix issue with passing scipy.sparse matrices with unsorted indices
#981
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi,
I've just encountered an issue when the user passes a scipy sparse matrix with unsorted inner indices to Eigen.
This issue manifests in two ways:
eigen_assert()is triggered when compiling in Debug,It seems until relatively recently it was not explicitly stated that
Eigen::Map<SparseMatrix>objects expect the inner indices to be sorted. This issue really pops up when you try to evaluate the map into a plain sparse matrix object, like here:nanobind/include/nanobind/eigen/sparse.h
Line 89 in 10eafbc
Eigen's master branch has a nice
a.sortInnerIndices()function and some helpers to check if the indices are sorted, for compressed storage types. For Eigen's released version (3.4.0 and below), for now I've added a call to SciPy's API to sort the indices of the passed sparse matrix.This might be relevant for #782.
Also related: stack-of-tasks/eigenpy#538 (in Boost.Python world)