Skip to content
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

Document why re-aliasing is important #17

Open
lassepe opened this issue Mar 27, 2020 · 1 comment
Open

Document why re-aliasing is important #17

lassepe opened this issue Mar 27, 2020 · 1 comment

Comments

@lassepe
Copy link

lassepe commented Mar 27, 2020

The documentation points out, that it really important to re-alias the result, e.g. in

result = ForwardDiff.hessian!(result, f, x);

It says that more information for why this is done can be found in the documentation of ForwardDiff.hessian!. Unfortunately, the according documentation does not talk about DiffResults at all so I remain curious why re-aliasing is necessary here. Is re-aliasing no longer needed?

@gdalle
Copy link
Member

gdalle commented Mar 15, 2024

Aliasing is still important, due to the updating code looking like this everywhere:

"""
value!(r::DiffResult, x)
Return `s::DiffResult` with the same data as `r`, except for `value(s) == x`.
This function may or may not mutate `r`. If `r::ImmutableDiffResult`, a totally new
instance will be created and returned, whereas if `r::MutableDiffResult`, then `r` will be
mutated in-place and returned. Thus, this function should be called as `r = value!(r, x)`.
"""
value!(r::MutableDiffResult, x::Number) = (r.value = x; return r)
value!(r::MutableDiffResult, x::AbstractArray) = (copyto!(value(r), x); return r)
value!(r::ImmutableDiffResult{O,V}, x::Union{Number,AbstractArray}) where {O,V} = ImmutableDiffResult(convert(V, x), r.derivs)

Basically, whenever you call res2 = do_stuff!(res1, args...), you're unsure that res1 is correct: all you know is that res2 is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants