Skip to content
Open
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
12 changes: 10 additions & 2 deletions src/osqp/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()

Expand Down
12 changes: 10 additions & 2 deletions src/osqppurepy/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down