Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutating NumPy array inplace with cp.Expression should have a clear error message #2282

Closed
PTNobel opened this issue Oct 31, 2023 · 1 comment

Comments

@PTNobel
Copy link
Collaborator

PTNobel commented Oct 31, 2023

Currently, when calling a NumPy function on a CVXPY variable such as np.log(cp.Variable()), we raise an error telling people to use a CVXPY atom inside. This error incorrectly triggers on the following code:

import numpy as np
import cvxpy as cp

x = np.array([0])
x += cp.Variable()

This errors because of this line, since NumPy sets kwargs = {'out': x}.

Expected behavior

It isn't clear to me what we want to do here. On the one hand people might expect this to mutate x the way it would for a non-NumPy array. It may not possible to both keep the error and make this have the same behaviour as x = x + cp.Variable().

We could add a new error message that says to do x = x + expression in this case, I think this is probably better than trying to get this to work because of my concerns about people expecting mutation. It is possible to write a function that works when given a NumPy array but which depends on mutation that doesn't have the same behaviour when given a CVXPY expression and I think we want to make this an error.

Version

  • OS: Linux
  • CVXPY Version: 1.4.1
@rileyjmurray
Copy link
Collaborator

rileyjmurray commented Nov 4, 2023

I think the most we'd want to do is raise an error saying x = x + expression. The idea of an in-place assignment on a numerical array with changing data types doesn't even make sense. Consider the following code as a non-cvxpy example:

>>> import numpy as np
>>> a = np.array([0],'d')
>>> a
array([0.])
>>> a += 1j
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
numpy.core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'add' output from dtype('complex128') to dtype('float64') with casting rule 'same_kind'

I suggest we look into adding an informative error message to help users who try to do this. Until we get around to that, I suggest we change the title of this GitHub issue.

Oh, and one more thing, @PTNobel. I believe you're either using cvxpy 1.4.1 or you're using the version on master (1.5.0+commit identifier).

@PTNobel PTNobel changed the title NumPy functions error triggering incorrectly Mutating NumPy array inplace with cp.Expression should have a clear error message Nov 5, 2023
@PTNobel PTNobel closed this as completed May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants