JuliaMath / JuliaMath/Calculus.jl
finite_difference routines temporarily mutate inputs
- Dominant language
- Julia
- Stars
- 285
- Forks
- 78
- PR merge metrics
- No merged PRs in 30d
Description
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 input `f` function holds a reference to `x` and the user will not suspect this will happen. A copy of `x` 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:
```
function curve_fit(model::Function, xpts, ydata, p0; kwargs...)
```
and inputted the same array for `ydata` and `p0`. 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 because `curve_fit` makes a function that includes `ydata` as a closure that is used in `finite_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.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.