Skip to content

Commit fc3b681

Browse files
authored
restore value to mutated input on error
1 parent c7a4624 commit fc3b681

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/grad.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ function jacobian(fdm, f, x::Vector{<:Real}; len=nothing)
1515
ẏs = map(eachindex(x)) do n
1616
return fdm(zero(eltype(x))) do ε
1717
xn = x[n]
18-
x[n] = xn + ε
19-
ret = copy(first(to_vec(f(x)))) # copy required incase `f(x)` returns something that aliases `x`
20-
x[n] = xn # Can't do `x[n] -= ϵ` as floating-point math is not associative
21-
return ret
18+
try
19+
x[n] = xn + ε
20+
return copy(first(to_vec(f(x)))) # copy required incase `f(x)` returns something that aliases `x`
21+
finally
22+
x[n] = xn # Can't do `x[n] -= ϵ` as floating-point math is not associative
23+
end
2224
end
2325
end
2426
return (reduce(hcat, ẏs), )

0 commit comments

Comments
 (0)