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
fsolve seems to be messing with the returned fvec array, because when it is a "reused" array, fsolve have a strange behavior (after some function evaluations it passes weird x values, and diverges).
x: [1.], y: [0.30685282]
x: [1.], y: [0.30685282]
x: [1.], y: [0.30685282]
x: [1.00000001], y: [0.30685284]
test_scipy.py:7: RuntimeWarning: invalid value encountered in log
y[:] = x * x - np.log(x + 1)
x: [-99.], y: [nan]
x: [nan], y: [nan]
x: [1.00000001], y: [0.30685284]
x: [-99.], y: [nan]
x: [nan], y: [nan]
x: [nan], y: [nan]
x: [nan], y: [nan]
x: [nan], y: [nan]
x: [nan], y: [nan]
x: [nan], y: [nan]
x: [nan], y: [nan]
(...)\site-packages\scipy\optimize\minpack.py:162: RuntimeWarning: The iteration is not making good progress, as measured by the improvement from the last ten iterations.
warnings.warn(msg, RuntimeWarning)
final x: [1.]
It passed a -99. at the fifth function evaluation above. Compare with the results if we change y[:] = x * x - np.log(x + 1) for y = x * x - np.log(x + 1) (so, creating a new fvec array every time):
This is probably because of internal behavior from minpack's HYBRJ, e.g.:
IF IFLAG = 1 CALCULATE THE FUNCTIONS AT X AND
RETURN THIS VECTOR IN FVEC. DO NOT ALTER FJAC.
IF IFLAG = 2 CALCULATE THE JACOBIAN AT X AND
RETURN THIS MATRIX IN FJAC. DO NOT ALTER FVEC.
but if it's too hard to circumvent (detect that fvec still uses the same buffer and making a copy in that case, etc.), perhaps a note could be added to the documentation.
Just as a note, I tried to reproduce this issue locally. Instead using fsolve, I used root, which is stated as the up-to-date interface for root-finding methods in scipy.optimize. The same behavior is observed with method="hybr". However, when you choose method="krylov", the strange behavior is not observed. Below I provide the MWE:
fsolve
seems to be messing with the returnedfvec
array, because when it is a "reused" array,fsolve
have a strange behavior (after some function evaluations it passes weirdx
values, and diverges).Reproducing code example:
Output:
It passed a
-99.
at the fifth function evaluation above. Compare with the results if we changey[:] = x * x - np.log(x + 1)
fory = x * x - np.log(x + 1)
(so, creating a new fvec array every time):Scipy/Numpy/Python version information:
The text was updated successfully, but these errors were encountered: