Comparison operator overloading doesn't work with sparse matrices #4819

Open
SteveDiamond opened this Issue May 6, 2015 · 4 comments

Projects

None yet

6 participants

@SteveDiamond

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.

@argriffing
Contributor

The currently active discussion at numpy/numpy#5844 looks related.

@SteveDiamond

A clarification: what I really want is a clean way to make <scipy sparse matrix>.__binop__(<cvxpy object>) return NotImplemented for all binops. Ideally this would be via a flag in cvxpy objects.

@pv pv added the easy-fix label Feb 15, 2016
@ashwinpathak20

I was solving this issue, so shall I include various cases for dense matrix, sparse matrix ,etc: in the binary operator function that are there in add , subtract functions or should I just raise the error.

@perimosocordiae
Member

I'm not sure what you mean. Feel free to open a pull request with some partial fixes and mark it WIP. That way we can discuss the code directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment