Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue for sparse matrix conversions #538

Merged
merged 5 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Fixed

- Fix handling of non sorted sparse matrix ([#538](https://github.com/stack-of-tasks/eigenpy/pull/538))

## [3.10.3] - 2025-02-11

### Added
Expand Down
6 changes: 5 additions & 1 deletion include/eigenpy/sparse/eigen-from-python.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2024 INRIA
// Copyright (c) 2024-2025 INRIA
//

#ifndef __eigenpy_sparse_eigen_from_python_hpp__
Expand Down Expand Up @@ -156,6 +156,10 @@ void eigen_sparse_matrix_from_py_construct(
}
MapMatOrRefType sparse_map(m, n, nnz, indptr.data(), indices_ptr, data_ptr);

#if EIGEN_VERSION_AT_LEAST(3, 4, 90)
sparse_map.sortInnerIndices();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably at some point we would like to add a check (and throw an exception) if the indices are unsorted and the Eigen version is below 3.4.90 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sure.

#endif

new (raw_ptr) MatOrRefType(sparse_map);
}

Expand Down
12 changes: 12 additions & 0 deletions unittest/python/decompositions/sparse/test_SimplicialLLT.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import scipy
from scipy.sparse import csc_matrix

import eigenpy
Expand Down Expand Up @@ -30,3 +31,14 @@
llt.analyzePattern(A)
llt.factorize(A)
permutation = llt.permutationP()

X_sparse = scipy.sparse.random(dim, 10)
B_sparse = A.dot(X_sparse)
B_sparse = B_sparse.tocsc(True)

if not B_sparse.has_sorted_indices:
B_sparse.sort_indices()

X_est = llt.solve(B_sparse)
assert eigenpy.is_approx(X_est.toarray(), X_sparse.toarray())
assert eigenpy.is_approx(A.dot(X_est.toarray()), B_sparse.toarray())