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.
A <= x
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.
The currently active discussion at numpy/numpy#5844 looks related.
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.
<scipy sparse matrix>.__binop__(<cvxpy object>)
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.
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.