Skip to content

Perturbation confusion #238

Open
Open

Description

@YingboMa

I have two examples in real use cases that can trigger perturbation confusion error in ForwardDiff.jl.

The first example (Lagrangian):

using ForwardDiff
m,g = 1, 9.8
t = 1
q = [1,2]
q̇ = [3,4]
L(t,q,q̇) = m/2 * dot(q̇,q̇) - m*g*q[2]

∂L∂q̇(L, t, q, q̇) = ForwardDiff.gradient(a->L(t,q,a), q̇)
Dqq̇(L, t, q, q̇) = ForwardDiff.jacobian(a->∂L∂q̇(L,t,a,q̇), q)
Dqq̇(L, t, q, q̇)  # ERROR: potential perturbation confusion...

The second one (Hamiltonian):

using ForwardDiff
m,g = 1, 9.8
t = 1
p = [5,6]
q = [1,2]

function Legendre_transformation(F, w)
    wv = a->ForwardDiff.gradient(F, a)
    z = zeros(w)
    M = ForwardDiff.jacobian(wv, z)
    b = wv(z)
    v = Symmetric(M)\(w-b)
    w'v - F(v)
end

function Lagrangian2Hamiltonian(Lagrangian, t, q, p)
    L = q̇ -> Lagrangian(t, q, q̇)
    Legendre_transformation(L, p)
end
Lagrangian2Hamiltonian(L, t, q, p)  # This is OK
ForwardDiff.gradient(a->Lagrangian2Hamiltonian(L, t, a, p), q)
# ERROR: potential perturbation confusion...

P.S.: I am on ForwardDiff master branch

julia> Pkg.status("ForwardDiff")
 - ForwardDiff                   0.5.0+             master

Activity

  1. simonbyrne commented on Sep 22, 2017

    @simonbyrne
    Contributor

    I've just tried these on #247 branch (I had to get rid of the Symmetric note since there is no generic Bunch-Kaufman method). I get

    julia> Dqq̇(L, t, q, q̇)
    2×2 Array{Float64,2}:
     0.0  0.0
     0.0  0.0
    
    julia> ForwardDiff.gradient(a->Lagrangian2Hamiltonian(L, t, a, p), q)
    2-element Array{Float64,1}:
     -0.0
      9.8
    

    Can you verify that those are correct?

  2. YingboMa commented on Sep 24, 2017

    @YingboMa
    MemberAuthor

    @simonbyrne Yes, those are correct.

  3. added a commit that references this issue on Sep 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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