You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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
changed the title
NumPy functions error triggering incorrectly
Mutating NumPy array inplace with cp.Expression should have a clear error message
Nov 5, 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: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 asx = 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
The text was updated successfully, but these errors were encountered: