Created
June 30, 2016 20:01
-
-
Save mauro3/d8a127ab798d8655c3151f3a21a6fa85 to your computer and use it in GitHub Desktop.
forwarddiff with in-place functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ForwardDiff | |
const T = Float64 # the datatype used, probably Float64 | |
const dof = 4 | |
fn!(;T_::Type=T, dof_=dof) = zeros(T_,dof_) | |
function fn!(res, y)#, dydt) | |
@show typeof(res), typeof(y) | |
for i=1:dof | |
res[i] = 3*y[i]#+dydt[i] | |
end | |
nothing | |
end | |
const res = fn!() | |
function fn_ex(y) | |
fn!(res, y) | |
return res | |
end | |
ad_jacobian(F) = (t,y)->ForwardDiff.jacobian(F,y) | |
const out = fn!() | |
ad_jacobian2(F!) = (t,y)->ForwardDiff.jacobian(F!,out,y) | |
ad_jacobian2!(F!) = (outJ,t,y)->ForwardDiff.jacobian!(outJ,F!,out,y) | |
J = ad_jacobian(fn_ex) | |
J! = ad_jacobian2(fn!) | |
J!! = ad_jacobian2!(fn!) | |
ic = zeros(T,dof) | |
typeof(J(0.0,ic)) | |
typeof(J!(0.0,ic)) | |
const outJ = similar(out, length(out), length(out)) | |
J!!(outJ,0,ic) | |
typeof(outJ) |
This looks like a bad style to me. fn_ex
should become fn_ex!
and accept res
as an argument, then you actually can't use line 23 as the implementation of a Jacobian. Lines 25 and 26 work perfectly well.
You are passing a function with side effects to jacobian
, while it probably expects that each evaluation is independent.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
J(0.0,ic)
errors because it tries to fillres
with dual numbers.J!
andJ!!
work.