-
Notifications
You must be signed in to change notification settings - Fork 77
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
finite_difference routines temporarily mutate inputs #81
Comments
I think the way to do this is to require users to provide us with a buffer that we can use instead of I'm sorry you had such trouble tracking down your bug. Your problem is a great example of the reason that Rust's borrow checker is so helpful: Julia doesn't really let us express the fact that we borrow |
I'm not sure what you have in mind for an interface that allocates memory? But it sounds promising. |
We can just use the current interface you're used to in FiniteDiff while always allocating a buffer. I don't see any reason to hesitate doing that if we make the contract more explicit. I'm less happy doing that same thing for Calculus since it will make some code that's currently correct slower. |
Sounds good if FiniteDiff.jl is going to be the way forward. |
We ended up implementing non-allocating versions of this over in DiffEqDiffTools.jl |
tl;dr The finite_difference methods for the gradient and jacobian (but not hessian) temporarily mutate the input
x
vector in order to compute the derivatives. I think this is unwise since there is no way to know inside these functions if the inputf
function holds a reference tox
and the user will not suspect this will happen. A copy ofx
should be made to make these routines safer.More details for those interested:
This recently happened to me and it was difficult to track down. It occurred because I called the curve_fit routine from LsqFit.jl:
and inputted the same array for
ydata
andp0
. I know this seems unusual but it makes sense for the problem I was solving (I can provide details if you want). This caused the fit to fail becausecurve_fit
makes a function that includesydata
as a closure that is used infinite_difference_jacobian!
. I don't see how a user of curve_fit would suspect that somewhere under the hood an input array will be mutated (even temporarily).I see that the Calculus.jl routines are going to be superseded by the FiniteDiff.jl and this concern affects routines there as well (for computing the gradient and hessian; I don't see a jacobian routine there).
Thanks for your consideration.
The text was updated successfully, but these errors were encountered: