scipy: Comparison operator overloading doesn't work with sparse matrices

I’m the developer of a modeling framework for optimization problems called cvxpy. I’d like to support operator overloading between scipy sparse matrices and cvxpy objects.

Currently operator overloading works for arithmetic operations, but I also need it to work for comparison operators. The issue is that all comparison operators route through the _inequality function in scipy/sparse/compressed.py, and there’s no code path in that function that returns NotImplemented.

Could you add some code path that returns NotImplemented like in the arithmetic operators? That way the reflection will be called on the cvxpy object. For example, if A is a sparse matrix and x is a cvxpy variable, the expression A <= x will first call A.__le__(x). This needs to return NotImplemented so Python calls x.__ge__(A), which creates a cvxpy constraint object.

The same issues apply to the __eq__ method in scipy/sparse/compressed.py. There should be some code path that returns NotImplemented.

Another solution would be to check __array_priority__ like in Numpy. The object with the higher __array_priority__ would have its comparison operator called first.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 2
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Closed by gh-19593!

Hi, I’d like to work on this bug if nobody is working on it currently. The last activity seems to be a year ago?

If it’s possible, a point in the right direction (in terms of what files to understand and what code to modify) would be a big help.