-
Notifications
You must be signed in to change notification settings - Fork 149
Derivative of inplace functions #218
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
Comments
How can you make an in place version of something that returns a scalar? |
No it takes in a scalar and returns a vector...
…On Apr 20, 2017 10:43 PM, "Kristoffer Carlsson" ***@***.***> wrote:
How can you make an in place version of something that returns a scalar?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#218 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABuunrx_Na6D1NbnyFJ2fY8FiHrLQXv4ks5ryEH1gaJpZM4NDxwO>
.
|
Hm, I thought |
I agree, this would be an appropriate addition to the API. @ChrisRackauckas Would this fix JuliaDiff/FiniteDiff.jl#3, since you could then do |
It doesn't fix all of JuliaDiff/FiniteDiff.jl#3, but it would quite a bit. derivative!(dt, (du, t) -> f(t, u, du), du, t) If this is in a loop, is it generating the closure every time? That's why I am using call-overloaded types and then just making the # Setup
type VectorF{F,SizeType,uType,rateType} <: Function
f::F
u::uType
du::rateType
end
function (p::VectorF)(t)
p.f(t,reshape(uprev,size(p.u)),reshape(du,size(p.u)))
du = vec(du)
end
# Now build the function that can be differentiated
# Note that `du` is a cache that needs to be typed for duals
f_diff = VectorF(f,some_u_cache,du)
# Usage
for i in ...
f_diff.u .= u # update internal cache
derivative!(dt,f_diff,t)
end I think that since the time I wrote this, the reshaping is now done internally by ForwardDiff? (I found a commit that added a bunch of I need to test this closure usage more, and also see that if the closure is behind a function yet in the loop if it also would get replaced. |
Anonymous functions are only "generated" once and are given a unique type at creation. They should lower to something similar that you have done manually here. Always gotta worry about the |
That's good to know. So then if ForwardDiff does the https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/blob/master/src/derivative_wrappers.jl Can be deleted and we can do straight calls to ForwadDiff.jl, which would make me more than happy. This doesn't fix the usage of NLsolve + autodifferentiation + inplace functions (#136), but I am not sure that has an elegant fix and will require specialty code.
Yes, I think before I ran into JuliaLang/julia#15276. |
Right, arrays of any shape should be usable now. IIRC, this was always the case for
Like Kristoffer said, the anonymous function is lowered into a callable type where the fields correspond to closed-over variables. In practice, Julia will sometimes box these fields unnecessarily because it can't determine that they're type-constant, so I've taken to defining a callable |
There is a
jacobian!
forf!(y,x)
, but is there noderivative!
forf(y,t)
wheret
is a scalar?The text was updated successfully, but these errors were encountered: