Skip to content
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

Perturbation confusion #238

Open
YingboMa opened this issue Jun 23, 2017 · 2 comments
Open

Perturbation confusion #238

YingboMa opened this issue Jun 23, 2017 · 2 comments

Comments

@YingboMa
Copy link
Member

YingboMa commented Jun 23, 2017

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 =-> 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
@simonbyrne
Copy link
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?

@YingboMa
Copy link
Member Author

@simonbyrne Yes, those are correct.

simonbyrne added a commit to simonbyrne/ForwardDiff.jl that referenced this issue Sep 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants