JuliaDiff / ForwardDiff.jl

Forward Mode Automatic Differentiation for Julia
Other
888 stars 141 forks source link

Replace values of Duals with other numbers #526

Open jwscook opened 3 years ago

jwscook commented 3 years ago

See my gist for what I'm trying to achieve.

I'm using NLsolve to find the cartesian coordinates given curvilinear coordinates and a function that returns curvilinear coordinates given cartesian coordinates (the reciprocal function is not usually easy / possible to write down). I want to be able to perform multiple AD passes through the solver function as though I had written down the complicated forward transform. I hope that makes sense - the gist should help.

I can't think of a good way of manipulating the values inside a Dual to acheive the desired outcome. Is there a good way?

mcabbott commented 3 years ago

The most straightforward answer is that you can't, they are immutable. And your gist doesn't try, despite its name changevalue!(x::Dual, ... returns an altered dual, without changing its argument. And changevalue!(x::AbstractVector ... writes what it returns into the (mutable) array x. Which sounds fine.