Skip to content

Manually preventing perturbation confusion #423

@ArnoStrouwen

Description

@ArnoStrouwen

Some packages such as DifferentialEquations.jl require you to manually provide Dual numbers for trickier problems. When you have nested differentiation in such a situation you also need to provide ordering of the tags yourself. Could an example how to manually prevent perturbation confusion be provided?

using ForwardDiff
function f(u,p)
    u[1]^2*u[2]^2*p[1]^2
end
u0  = [2.0, 3.0]
p0 = [4.0]
dfdp = u->ForwardDiff.gradient(p->f(u,p),p0)
ForwardDiff.jacobian(dfdp,u0)
# ddf/dpdu1 = 2*u1*u2^2*2p1 = 2*2*3^2*2*4 = 288
# ddf/dpdu2 = u1^2*2*u2*2p1 = 2^2*2*3*2*4 = 192

using ForwardDiff:Dual
abstract type A end
abstract type B end
# ... define ordering on A and B here ...
u_test = [Dual{A}(2.0,1.0,0.0),Dual{A}(3.0,0.0,1.0)]
p_test = [Dual{B}(4.0,1.0)]
u_test[1]^2*u_test[2]^2*p_test[1]^2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions