-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_differentiate.nim
155 lines (134 loc) · 5.4 KB
/
test_differentiate.nim
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
import std/[unittest, math]
import numericalnim
import arraymancer
proc f_1d(x: float): float = 2 * sin(x) * cos(x) # same as sin(2*x)
proc exact_deriv_1d(x: float): float = 2*cos(2*x)
proc exact_secondderiv_1d(x: float): float = -4*sin(2*x)
suite "1D numeric differentiation":
test "Forward difference":
for x in numericalnim.linspace(0, 10, 100):
let numDiff = diff1dForward(f_1d, x)
let exact = exact_deriv_1d(x)
check abs(numDiff - exact) < 3e-6
test "Backward difference":
for x in numericalnim.linspace(0, 10, 100):
let numDiff = diff1dBackward(f_1d, x)
let exact = exact_deriv_1d(x)
check abs(numDiff - exact) < 3e-6
test "Central difference":
for x in numericalnim.linspace(0, 10, 100):
let numDiff = diff1dCentral(f_1d, x)
let exact = exact_deriv_1d(x)
check abs(numDiff - exact) < 2e-9
test "Forward second difference":
for x in numericalnim.linspace(0, 10, 100):
let numDiff = secondDiff1dForward(f_1d, x)
let exact = exact_secondderiv_1d(x)
check abs(numDiff - exact) < 4e-3
test "Backward second difference":
for x in numericalnim.linspace(0, 10, 100):
let numDiff = secondDiff1dBackward(f_1d, x)
let exact = exact_secondderiv_1d(x)
check abs(numDiff - exact) < 4e-3
test "Central second difference":
for x in numericalnim.linspace(0, 10, 100):
let numDiff = secondDiff1dCentral(f_1d, x)
let exact = exact_secondderiv_1d(x)
check abs(numDiff - exact) < 4e-4
proc fScalar(x: Tensor[float]): float =
# This will be a function of three variables
# f(x0, x1, x2) = x0^2 + 2 * x0 * x1 + sin(x2)
result = x[0]*x[0] + 2 * x[0] * x[1] + sin(x[2])
proc scalarGradient(x: Tensor[float]): Tensor[float] =
# Gradient is (2*x0 + 2*x1, 2*x0, cos(x2))
result = zeros_like(x)
result[0] = 2*x[0] + 2*x[1]
result[1] = 2*x[0]
result[2] = cos(x[2])
proc scalarHessian(x: Tensor[float]): Tensor[float] =
result = zeros[float](3, 3)
result[0,0] = 2
result[0,1] = 2
result[1,0] = 2
result[0,2] = 0
result[2,0] = 0
result[1,1] = 0
result[1,2] = 0
result[2,1] = 0
result[2,2] = -sin(x[2])
proc fMultidim(x: Tensor[float]): Tensor[float] =
# Function will have 3 inputs and 2 outputs (important that they aren't the same for testing!)
# f(x0, x1, x2) = (x0*x1*x2^2, x1*sin(2*x2))
result = zeros[float](2)
result[0] = x[0]*x[1]*x[2]*x[2]
result[1] = x[1] * sin(2*x[2])
proc multidimGradient(x: Tensor[float]): Tensor[float] =
# The gradient (Jacobian transposed) is:
# x1*x2^2 0
# x0*x2^2 sin(2*x2)
# 2*x0*x1*x2 2*x1*cos(2*x2)
result = zeros[float](3, 2)
result[0, 0] = x[1]*x[2]*x[2]
result[0, 1] = 0
result[1, 0] = x[0]*x[2]*x[2]
result[1, 1] = sin(2*x[2])
result[2, 0] = 2*x[0]*x[1]*x[2]
result[2, 1] = 2*x[1]*cos(2*x[2])
suite "Multi dimensional numeric gradients":
test "Scalar valued function of 3 variables":
for x in numericalnim.linspace(0, 1, 10):
for y in numericalnim.linspace(0, 1, 10):
for z in numericalnim.linspace(0, 1, 10):
let x0 = [x, y, z].toTensor
let numGrad = tensorGradient(fScalar, x0)
let exact = scalarGradient(x0)
for err in abs(numGrad - exact):
check err < 5e-10
test "Scalar valued function of 3 variables (fast mode)":
for x in numericalnim.linspace(0, 1, 10):
for y in numericalnim.linspace(0, 1, 10):
for z in numericalnim.linspace(0, 1, 10):
let x0 = [x, y, z].toTensor
let numGrad = tensorGradient(fScalar, x0, fastMode=true)
let exact = scalarGradient(x0)
for err in abs(numGrad - exact):
check err < 5e-6
test "Multi-dimensional function of 3 variables":
for x in numericalnim.linspace(0, 1, 10):
for y in numericalnim.linspace(0, 1, 10):
for z in numericalnim.linspace(0, 1, 10):
let x0 = [x, y, z].toTensor
let numGrad = tensorGradient(fMultidim, x0)
let exact = multidimGradient(x0)
for err in abs(numGrad - exact):
check err < 1e-10
test "Multi-dimensional function of 3 variables (fast mode)":
for x in numericalnim.linspace(0, 1, 10):
for y in numericalnim.linspace(0, 1, 10):
for z in numericalnim.linspace(0, 1, 10):
let x0 = [x, y, z].toTensor
let numGrad = tensorGradient(fMultidim, x0, fastMode=true)
let exact = multidimGradient(x0)
for err in abs(numGrad - exact):
check err < 2e-6
test "Jacobian":
for x in numericalnim.linspace(0, 1, 10):
for y in numericalnim.linspace(0, 1, 10):
for z in numericalnim.linspace(0, 1, 10):
let x0 = [x, y, z].toTensor
let numJacobian = tensorJacobian(fMultidim, x0)
let exact = multidimGradient(x0).transpose
for err in abs(numJacobian - exact):
check err < 1e-10
test "checkGradient":
check checkGradient(fScalar, scalarGradient, [0.5, 0.5, 0.5].toTensor, 6e-11)
check checkGradient(fMultidim, multidimGradient, [0.5, 0.5, 0.5].toTensor, 4e-12)
test "Hessian scalar valued function":
for x in numericalnim.linspace(0, 1, 10):
for y in numericalnim.linspace(0, 1, 10):
for z in numericalnim.linspace(0, 1, 10):
let x0 = [x, y, z].toTensor
let numHessian = tensorHessian(fScalar, x0)
let exact = scalarHessian(x0)
for err in abs(numHessian - exact):
check err < 3e-4