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
I'm not sure this is intentional or if it is for numerical efficiency reasons, and I may be out of my league discussing this, but I figured I should point this out. When trying to copy the DualNumbers.jl structure into the HyperDualNumbers.jl structure, I noticed that there is a problem with how abs is dealt with. I believe the problem is that abs(x) returns x if x >= 0 (in DualNumbers.jl, HyperDualNumbers.jl, and I believe in ForwardDiff.jl's dual implementation).
Here is a small example where this causes problems. I use ForwardDiff.hessian to evaluate the second derivative of abs(-x^2), which should be the same as the second derivative of x^2, which is always 2:
(I used hessian and a single element Vector because I did not know how to get to second derivatives of scalars using ForwardDiff.)
I come with a suggestion: What about dealing with abs in the "physical" way, that is thinking of the absolute value of some x plus or minus some very small term ε. In other words, treating it recursively via something like:
functionmyabs(d::Dual)
x, y =value(d), epsilon(d)
if x >0return d
elseif x <0return-d
elseif y ≥0return d
elsereturn-d
endendend
For hyper duals, something like
functionmyabs(h::Hyper)
x, y, z, w =value(h), epsilon1(h), epsilon2(h), epsilon12(h)
if x >0return h
elseif x <0return-h
elseif y+z >0return h
elseif y+z <0return-h
elseif z ≥0return h
else z <0return-h
endendendend
Now I know this is not DualNumbers.jl or HyperDualNumbers.jl, to which the above example suggestions apply, but I haven't really figured out how ForwardDiff.jl implements hyperduals so this was the best way I could convey my suggestions. I also believe that it is very likely that one can code the suggested abs in a much more efficient way than my if statements allow. But regardless of that, is that something worse paying some attention?
The text was updated successfully, but these errors were encountered:
Hi there,
I'm not sure this is intentional or if it is for numerical efficiency reasons, and I may be out of my league discussing this, but I figured I should point this out. When trying to copy the DualNumbers.jl structure into the HyperDualNumbers.jl structure, I noticed that there is a problem with how
abs
is dealt with. I believe the problem is thatabs(x)
returnsx
ifx >= 0
(in DualNumbers.jl, HyperDualNumbers.jl, and I believe in ForwardDiff.jl's dual implementation).Here is a small example where this causes problems. I use
ForwardDiff.hessian
to evaluate the second derivative ofabs(-x^2)
, which should be the same as the second derivative ofx^2
, which is always2
:(I used
hessian
and a single elementVector
because I did not know how to get to second derivatives of scalars using ForwardDiff.)I come with a suggestion: What about dealing with
abs
in the "physical" way, that is thinking of the absolute value of somex
plus or minus some very small termε
. In other words, treating it recursively via something like:For hyper duals, something like
Now I know this is not DualNumbers.jl or HyperDualNumbers.jl, to which the above example suggestions apply, but I haven't really figured out how ForwardDiff.jl implements hyperduals so this was the best way I could convey my suggestions. I also believe that it is very likely that one can code the suggested
abs
in a much more efficient way than myif
statements allow. But regardless of that, is that something worse paying some attention?The text was updated successfully, but these errors were encountered: