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 hidden or 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are passing a function with side effects to
jacobian
, while it probably expects that each evaluation is independent.