forked from JuliaDiff/ForwardDiff.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfusionTest.jl
59 lines (43 loc) · 1.28 KB
/
ConfusionTest.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module ConfusionTest
using Compat
using Compat.Test
using ForwardDiff
# Perturbation Confusion (Issue #83) #
#------------------------------------#
D = ForwardDiff.derivative
@test D(x -> x * D(y -> x + y, 1), 1) == 1
@test ForwardDiff.gradient(v -> sum(v) * D(y -> y * norm(v), 1), [1]) == ForwardDiff.gradient(v -> sum(v) * norm(v), [1])
const A = rand(10,8)
y = rand(10)
x = rand(8)
@test A == ForwardDiff.jacobian(x) do x
ForwardDiff.gradient(y) do y
dot(y, A*x)
end
end
# Issue #238 #
#------------------------------------#
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)
@test Dqq̇(L, t, q, q̇) == fill(0.0, 2, 2)
q = [1,2]
p = [5,6]
function Legendre_transformation(F, w)
z = fill(0.0, size(w))
M = ForwardDiff.hessian(F, z)
b = ForwardDiff.gradient(F, z)
v = cholfact(M)\(w-b)
dot(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)
@test ForwardDiff.gradient(a->Lagrangian2Hamiltonian(L, t, a, p), q) == [0.0,g]
end # module