From 244431fa3b0c1193173e0f335394c5b5eaa8245b Mon Sep 17 00:00:00 2001 From: Zhen NI Date: Fri, 10 Jul 2026 21:10:45 +0800 Subject: [PATCH] Support scipy csc_array to suppress redundant conversion warnings --- src/osqp/interface.py | 12 ++++++++++-- src/osqppurepy/interface.py | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/osqp/interface.py b/src/osqp/interface.py index 67b659af..477cbb84 100644 --- a/src/osqp/interface.py +++ b/src/osqp/interface.py @@ -25,6 +25,14 @@ OSQP_ALGEBRA_BACKEND = os.environ.get('OSQP_ALGEBRA_BACKEND') # If envvar is set, that algebra is used by default +def is_csc(sp_mat): + """Check if the sparse matrix or array is in the csc format.""" + if hasattr(spa, 'csc_array'): + return isinstance(sp_mat, (spa.csc_matrix, spa.csc_array)) + else: + return isinstance(sp_mat, spa.csc_matrix) + + def algebra_available(algebra): assert algebra in _ALGEBRAS, f'Unknown algebra {algebra}' module = _ALGEBRA_MODULES[algebra] @@ -222,10 +230,10 @@ def _infer_mnpqalu(self, P=None, q=None, A=None, l=None, u=None): P = spa.triu(P, format='csc') # Convert matrices in CSC form to individual pointers - if not spa.isspmatrix_csc(P): + if not is_csc(P): warnings.warn('Converting sparse P to a CSC matrix. This may take a while...') P = P.tocsc() - if not spa.isspmatrix_csc(A): + if not is_csc(A): warnings.warn('Converting sparse A to a CSC matrix. This may take a while...') A = A.tocsc() diff --git a/src/osqppurepy/interface.py b/src/osqppurepy/interface.py index 0e8fd18a..28a6dab8 100644 --- a/src/osqppurepy/interface.py +++ b/src/osqppurepy/interface.py @@ -8,6 +8,14 @@ from scipy import sparse +def is_csc(sp_mat): + """Check if the sparse matrix or array is in the csc format.""" + if hasattr(sparse, 'csc_array'): + return isinstance(sp_mat, (sparse.csc_matrix, sparse.csc_array)) + else: + return isinstance(sp_mat, sparse.csc_matrix) + + class OSQP(object): def __init__(self): self._model = _osqp.OSQP() @@ -104,10 +112,10 @@ def setup(self, P=None, q=None, A=None, l=None, u=None, **settings): raise TypeError('A is required to be a sparse matrix') # Convert matrices in CSC form and to individual pointers - if not sparse.isspmatrix_csc(P): + if not is_csc(P): warn('Converting sparse P to a CSC ' + '(compressed sparse column) matrix. (It may take a while...)') P = P.tocsc() - if not sparse.isspmatrix_csc(A): + if not is_csc(A): warn('Converting sparse A to a CSC ' + '(compressed sparse column) matrix. (It may take a while...)') A = A.tocsc()