-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_ad.py
242 lines (197 loc) · 7.5 KB
/
test_ad.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
================================================================================
ad test suite
================================================================================
Author: Abraham Lee
Copyright: 2013
"""
import ad
from ad import *
from ad.admath import *
import math
import cmath
from unittest import TestCase, TestSuite
try:
import numpy
numpy_installed = True
except ImportError:
numpy_installed = False
################################################################################
class AdTest:
def setUp(self):
self.x = adnumber(self.xi, tag='x')
self.y = adnumber(self.yi)
def test_tags(self):
'test tag property'
self.assertEqual(self.x.tag, 'x')
self.assertTrue(self.y.tag is None)
def test_comparisons(self):
'test object comparisons'
x,y = self.x, self.y
self.assertEqual(x, 2)
self.assertNotEqual(x, 1)
self.assertTrue(x) # nonzero
self.assertTrue(x < 3)
self.assertTrue(x <= 2)
self.assertTrue(x > 1)
self.assertTrue(x >= 2)
self.assertEqual(y, 3)
self.assertNotEqual(y, 2)
self.assertTrue(y) # nonzero
self.assertTrue(y < 4)
self.assertTrue(y <= 3)
self.assertTrue(y > 2)
self.assertTrue(y >= 3)
# test underlying object comparisons
self.assertEqual(x.x, 2)
self.assertEqual(y.x, 3)
def test_ADV_derivs(self):
"test derivatives of ADV (independent variable) objects"
x,y = self.x, self.y
self.assertEqual(x.d(x), 1)
self.assertEqual(y.d(y), 1)
self.assertEqual(y.d(x), 0)
self.assertEqual(x.d(y), 0)
self.assertEqual(x.d(1), 0)
self.assertEqual(y.d(1), 0)
self.assertEqual(x.d2(x), 0)
self.assertEqual(y.d2(y), 0)
self.assertEqual(x.d2(y), 0)
self.assertEqual(y.d2(x), 0)
def test_ADF_derivs(self):
'test derivatives of ADF (dependent variable) objects'
x, y = self.x, self.y
xi, yi = self.xi, self.yi
z_add = x + y
self.assertEqual(z_add, xi + yi)
self.assertEqual(z_add.d(x), 1)
self.assertEqual(z_add.d(y), 1)
# dependent variables not traced
self.assertEqual(z_add.d(z_add), 0)
self.assertEqual(z_add.d2(x), 0)
self.assertEqual(z_add.d2(y), 0)
self.assertEqual(z_add.d2c(x, y), 0)
self.assertEqual(z_add.d2c(y, x), z_add.d2c(x, y))
self.assertEqual(z_add.d2c(x, z_add), 0)
self.assertEqual(z_add.gradient([x, 1, y]), [1, 0, 1])
z_sub = x - y
self.assertEqual(z_sub, xi - yi)
self.assertEqual(z_sub.d(x), 1)
self.assertEqual(z_sub.d(y), -1)
self.assertEqual(z_sub.d2(x), 0)
self.assertEqual(z_sub.d2(y), 0)
self.assertEqual(z_sub.d2c(x, y), 0)
self.assertEqual(z_sub.gradient([x, y, z_add]), [1, -1, 0])
z_mul = x*y
self.assertEqual(z_mul, xi*yi)
self.assertEqual(z_mul.d(x), 3)
self.assertEqual(z_mul.d(y), 2)
self.assertEqual(z_mul.d2(x), 0)
self.assertEqual(z_mul.d2(y), 0)
self.assertEqual(z_mul.d2c(x, y), 1)
z_div = x/y
self.assertEqual(z_div, xi/yi)
self.assertEqual(z_div.d(x), 1./yi)
self.assertEqual(z_div.d(y), -xi/(yi**2))
self.assertEqual(z_div.d2(x), 0)
self.assertEqual(z_div.d2(y), 2*xi/(yi**3))
self.assertEqual(z_div.d2c(x, y), -1./9)
z_pow = x**y
self.assertEqual(z_pow, xi**yi)
self.assertEqual(z_pow.d(x), 12)
self.assertEqual(z_pow.d(y), (8*math.log(2)))
self.assertEqual(z_pow.d2(x), 12)
self.assertEqual(z_pow.d2(y), (8*math.log(2)**2))
self.assertEqual(z_pow.d2c(x, y), (4 + 12*math.log(2)))
self.assertEqual(z_pow.hessian([z_mul, y, x]), [
[0, 0, 0],
[0, 8*math.log(2)**2, 4 + 12*math.log(2)],
[0, 4 + 12*math.log(2), 12]])
for base in (2, 10, math.e):
z_log = log(x, base)
self.assertEqual(z_log.d(x), 1./(x*ln(base)))
self.assertEqual(z_log.d2(x), -1./(x**2*ln(base)))
z_mod = x%y
self.assertEqual(z_mod, (x - y*ad._floor(x/y)))
z_neg = -x
self.assertEqual(z_neg, -1*x.x)
z_pos = +x
self.assertEqual(z_pos, x.x)
z_inv = ~x
self.assertEqual(z_inv, -(x+1))
z_abs = abs(-x.x)
self.assertEqual(z_abs, x)
def test_coercion(self):
'test coercion methods'
x = self.x
if isinstance(x.x, (int, float)):
msg = '{0:} and {1:}'.format(int(x), type(int(x)))
self.assertEqual(int(x), 2, msg)
self.assertTrue(isinstance(int(x), int), msg)
msg = '{0:} and {1:}'.format(float(x), type(float(x)))
self.assertEqual(float(x), 2.0)
self.assertTrue(isinstance(float(x), float))
msg = '{0:} and {1:}'.format(complex(x), type(complex(x)))
self.assertEqual(complex(x), 2+0j)
self.assertTrue(isinstance(complex(x), complex))
def test_trace(self):
'test trace_me'
z_add = self.x + self.y
z_add.trace_me()
self.assertEqual(z_add.d(z_add), 1)
self.assertEqual(z_add.d2(z_add), 0)
def test_gh(self):
'test gh function wrapper'
x, y = self.x, self.y
def test_func(x, a):
return (x[0] + x[1])**a
testg, testh = gh(test_func)
self.assertEqual(testg([x, y], 3), ((x + y)**3).gradient([x, y]))
self.assertEqual(testh([x, y], 3), ((x + y)**3).hessian([x, y]))
def test_jacobian(self):
'test jacobian function'
x, y = self.x, self.y
self.assertEqual(jacobian([x*y, x+y], [x, 1, y]),
[[3.0, 0.0, 2.0], [1.0, 0.0, 1.0]])
class AdTestInt(AdTest, TestCase):
xi, yi = (2, 3)
class AdTestFloat(AdTest, TestCase):
xi, yi = (2.0, 3.0)
if numpy_installed:
import numpy as np
import numpy.testing
def assert_allclose(a, b):
a = np.array(a, dtype=float)
b = np.array(b, dtype=float)
return numpy.testing.assert_allclose(a, b)
class NPTests(TestCase):
def setUp(self):
self.x = adnumber(2)
self.x_row = adnumber(np.linspace(0, 2, 5))
self.y = np.logspace(0,4,5)
def test_deriv(self):
"""Test ad.d() function"""
z = self.y * self.x
dz = ad.d(z, self.x)
assert_allclose(dz, self.y)
z = self.x_row ** 2
dz = ad.d(z, self.x_row)
assert_allclose(dz, [0.0, 1.0, 2.0, 3.0, 4.0])
z = self.y * self.x_row
dz = ad.d(z, self.x_row)
assert_allclose(dz, self.y)
def test_d2(self):
"""Test ad.d2() function"""
z = self.y * exp(-2*self.x)
dz = ad.d(z, self.x)
ddz = ad.d2(z, self.x)
assert_allclose(dz, -2*z)
assert_allclose(ddz, 4*z)
z = self.x_row ** 2
ddz = ad.d2(z, self.x_row)
assert_allclose(ddz, 2.)
z = self.y * sin(2*self.x_row)
ddz = ad.d2(z, self.x_row)
assert_allclose(ddz, -4*z)
if __name__ == '__main__':
unittest.main()