forked from JuliaDiff/ForwardDiff.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscTest.jl
157 lines (117 loc) · 4.51 KB
/
MiscTest.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
module MiscTest
import NaNMath
using Compat
using Compat.Test
using ForwardDiff
using DiffTests
include(joinpath(dirname(@__FILE__), "utils.jl"))
##########################
# Nested Differentiation #
##########################
# README example #
#----------------#
x = rand(5)
f = x -> sum(sin, x) + prod(tan, x) * sum(sqrt, x)
g = x -> ForwardDiff.gradient(f, x)
j = x -> ForwardDiff.jacobian(g, x)
@test isapprox(ForwardDiff.hessian(f, x), j(x))
# higher-order derivatives #
#--------------------------#
function tensor(f, x)
n = length(x)
out = ForwardDiff.jacobian(y -> ForwardDiff.hessian(f, y), x)
return reshape(out, n, n, n)
end
test_tensor_output = reshape([240.0 -400.0 0.0;
-400.0 0.0 0.0;
0.0 0.0 0.0;
-400.0 0.0 0.0;
0.0 480.0 -400.0;
0.0 -400.0 0.0;
0.0 0.0 0.0;
0.0 -400.0 0.0;
0.0 0.0 0.0], 3, 3, 3)
@test isapprox(tensor(DiffTests.rosenbrock_1, [0.1, 0.2, 0.3]), test_tensor_output)
test_nested_jacobian_output = [-sin(1) 0.0 0.0;
-0.0 -0.0 -0.0;
-0.0 -0.0 -0.0;
0.0 0.0 0.0;
-0.0 -sin(2) -0.0;
-0.0 -0.0 -0.0;
0.0 0.0 0.0;
-0.0 -0.0 -0.0;
-0.0 -0.0 -sin(3)]
sin_jacobian = x -> ForwardDiff.jacobian(y -> broadcast(sin, y), x)
@test isapprox(ForwardDiff.jacobian(sin_jacobian, [1., 2., 3.]), test_nested_jacobian_output)
# Issue #59 example #
#-------------------#
x = rand(2)
f = x -> sin(x)/3 * cos(x)/2
df = x -> ForwardDiff.derivative(f, x)
testdf = x -> (((cos(x)^2)/3) - (sin(x)^2)/3) / 2
f2 = x -> df(x[1]) * f(x[2])
testf2 = x -> testdf(x[1]) * f(x[2])
@test isapprox(ForwardDiff.gradient(f2, x), ForwardDiff.gradient(testf2, x))
######################################
# Higher-Dimensional Differentiation #
######################################
x = rand(5, 5)
@test isapprox(ForwardDiff.jacobian(inv, x), -kron(inv(x'), inv(x)))
#########################################
# Differentiation with non-Array inputs #
#########################################
x = rand(5,5)
# Sparse
f = x -> sum(sin, x) + prod(tan, x) * sum(sqrt, x)
gfx = ForwardDiff.gradient(f, x)
@test isapprox(gfx, ForwardDiff.gradient(f, sparse(x)))
# Views
jinvx = ForwardDiff.jacobian(inv, x)
@test isapprox(jinvx, ForwardDiff.jacobian(inv, view(x, 1:5, 1:5)))
########################
# Conversion/Promotion #
########################
# target function returns a literal (Issue #71) #
#-----------------------------------------------#
@test ForwardDiff.derivative(x->zero(x), rand()) == ForwardDiff.derivative(x->1.0, rand())
@test ForwardDiff.gradient(x->zero(x[1]), [rand()]) == ForwardDiff.gradient(x->1.0, [rand()])
@test ForwardDiff.hessian(x->zero(x[1]), [rand()]) == ForwardDiff.hessian(x->1.0, [rand()])
@test ForwardDiff.jacobian(x->[zero(x[1])], [rand()]) == ForwardDiff.jacobian(x->[1.0], [rand()])
# arithmetic element-wise functions #
#-----------------------------------#
N = 4
a = fill(1.0, N)
jac0 = reshape(vcat([[fill(0.0, N*(i-1)); a; fill(0.0, N^2-N*i)] for i = 1:N]...), N^2, N)
for op in (:-, :+, :.-, :.+, :./, :.*)
f = @eval x -> [$op(x[1], a); $op(x[2], a); $op(x[3], a); $op(x[4], a)]
jac = @eval ForwardDiff.jacobian(f, a)
@test isapprox(jac0, jac)
end
# NaNs #
#------#
@test ForwardDiff.partials(NaNMath.pow(ForwardDiff.Dual(-2.0,1.0),ForwardDiff.Dual(2.0,0.0)),1) == -4.0
# Partials{0} #
#-------------#
x, y = rand(3), rand(3)
h = ForwardDiff.hessian(y -> sum(hypot.(x, y)), y)
@test h[1, 1] ≈ (x[1]^2) / (x[1]^2 + y[1]^2)^(3/2)
@test h[2, 2] ≈ (x[2]^2) / (x[2]^2 + y[2]^2)^(3/2)
@test h[3, 3] ≈ (x[3]^2) / (x[3]^2 + y[3]^2)^(3/2)
for i in 1:3, j in 1:3
i != j && (@test h[i, j] ≈ 0.0)
end
########
# misc #
########
# issue 267
@noinline f267(z, x) = x[1]
z267 = ([(1, (2), [(3, (4, 5, [1, 2, (3, (4, 5), [5])]), (5))])])
let z = z267
g = x -> f267(z, x)
h = x -> g(x)
@test ForwardDiff.hessian(h, [1.]) == fill(0.0, 1, 1)
end
# issue #290
@test ForwardDiff.derivative(x -> rem2pi(x, RoundUp), rand()) == 1
@test ForwardDiff.derivative(x -> rem2pi(x, RoundDown), rand()) == 1
end # module