-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestFunctions.py
executable file
·150 lines (121 loc) · 5.08 KB
/
testFunctions.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
#!/usr/bin/env python
import functions as F
import numpy as N
import unittest
class TestFunctions(unittest.TestCase):
def testApproxJacobian1(self):
slope = 3.0
def f(x):
return slope * x + 5.0
x0 = 2.0
dx = 1.e-3
Df_x = F.ApproximateJacobian(f, x0, dx)
self.assertEqual(Df_x.shape, (1,1))
self.assertAlmostEqual(Df_x, slope)
def testApproxJacobian2(self):
A = N.matrix("1. 2.; 3. 4.")
def f(x):
return A * x
x0 = N.matrix("5; 6")
dx = 1.e-6
Df_x = F.ApproximateJacobian(f, x0, dx)
self.assertEqual(Df_x.shape, (2,2))
N.testing.assert_array_almost_equal(Df_x, A)
def testPolynomial(self):
# p(x) = x^2 + 2x + 3
p = F.Polynomial([1, 2, 3])
for x in N.linspace(-2,2,11):
self.assertEqual(p(x), x**2 + 2*x + 3)
def testAprroxJacobian3(self):
def f(x = N.matrix(N.zeros((2,1)))):
ans = N.matrix(N.zeros((2,1)))
ans[0,0] = x[0,0]**2 + 3.0*x[0,0]*x[1,0] + 4.0*x[1,0]**2
ans[1,0] = -2.0*x[0,0]**2 + x[1,0]**2
return ans
x0 = N.matrix("1.; 2.")
dx = 1.e-8
Df_x = F.ApproximateJacobian(f, x0, dx)
A = N.matrix("8. 19.; -4. 4.")
N.testing.assert_array_almost_equal(Df_x, A)
def testCompare(self):
def f(x = N.matrix(N.zeros((2,1)))):
ans = N.matrix(N.zeros((2,1)))
ans[0,0] = x[0,0]**2 + 4.0*x[1,0]**2
ans[1,0] = -2.0*x[0,0]**2 + x[1,0]**2
return ans
x0 = N.matrix("1.; 2.")
dx = 1.e-8
x = []
x.append([N.matrix("1.0, 4.0"), N.matrix("2.0, 0.0; 0.0, 2.0")])
x.append([N.matrix("-2.0, 1.0"), N.matrix("2.0, 0.0; 0.0, 2.0")])
Df_x = F.AnalyticJacobian(x0, dx, ["Polynomial", x[0], x[1]])
Df_x_A = F.ApproximateJacobian(f, x0, dx)
N.testing.assert_array_almost_equal(Df_x, Df_x_A)
def testAnalyticJacobian1(self):
f = lambda x: x**2 - x + 2
x0 = N.matrix('1.0')
dx = 1.e-3
Df_x = F.AnalyticJacobian(x0, dx, ["Polynomial", [N.matrix('1.0, -1.0'), N.matrix('2.0, -1')]])
self.assertEqual(Df_x, 3.0)
def testAnalyticJacobian2(self):
f = lambda x: x**4 - x**2 + 2
x0 = N.matrix('1.0')
dx = 1.e-3
Df_x = F.AnalyticJacobian(x0, dx, ["Polynomial", [N.matrix('1.0, -1.0'), N.matrix('4.0, 2.0')]])
self.assertEqual(Df_x, 2.0)
def testAnalyticJacobian3(self):
def f(x = N.matrix(N.zeros((2,1)))):
ans = N.matrix(N.zeros((2,1)))
ans[0,0] = x[0,0]**2 + 4.0*x[1,0]**2
ans[1,0] = -2.0*x[0,0]**2 + x[1,0]**2
return ans
x0 = N.matrix("1.; 2.")
dx = 1.e-8
x = []
x.append([N.matrix("1.0, 4.0"), N.matrix("2.0, 0.0; 0.0, 2.0")])
x.append([N.matrix("-2.0, 1.0"), N.matrix("2.0, 0.0; 0.0, 2.0")])
Df_x = F.AnalyticJacobian(x0, dx, ["Polynomial", x[0], x[1]])
exact = N.matrix("2. 16.; -4. 4.")
N.testing.assert_array_almost_equal(Df_x, exact)
def testAnalyticJacobian4(self):
def f(x = N.matrix(N.zeros((2,1)))):
ans = N.matrix(N.zeros((2,1)))
ans[0,0] = x[0,0]**2 + 3.0*x[0,0]*x[1,0] + 4.0*x[1,0]**2
ans[1,0] = -2.0*x[0,0]**2 - x[0,0]*x[1,0] + x[1,0]**2
return ans
x0 = N.matrix("1.; 2.")
dx = 1.e-8
x = []
x.append([N.matrix("1.0, 3.0, 4.0"), N.matrix("2.0, 1.0, 0.0; 0.0, 1.0, 2.0")])
x.append([N.matrix("-2.0, -1.0, 1.0"), N.matrix("2.0, 1.0, 0.0; 0.0, 1.0, 2.0")])
Df_x = F.AnalyticJacobian(x0, dx, ["Polynomial", x[0], x[1]])
exact = N.matrix("8. 19.; -6. 3.")
N.testing.assert_array_almost_equal(Df_x, exact)
def testAnalyticJacobian5(self):
def f(x = N.matrix(N.zeros((2,1)))):
ans = N.matrix(N.zeros((2,1)))
ans[0,0] = M.pow(x[0,0], 0.5) + 2.0*M.pow(x[1,0], 0.5)
ans[1,0] = 3.0*M.pow(x[0,0], 0.5) - M.pow(x[1,0], 0.5)
x0 = N.matrix("1.0; 1.0")
dx = 1.e-8
x = []
x.append([N.matrix("1.0, 2.0"), N.matrix("0.5, 0; 0, 0.5")])
x.append([N.matrix("3.0, -1.0"), N.matrix("0.5, 0; 0, 0.5")])
Df_x = F.AnalyticJacobian(x0, dx, ["Polynomial", x[0], x[1]])
exact = N.matrix("0.5 1.; 1.5 -0.5")
N.testing.assert_array_almost_equal(Df_x, exact)
def testAnalyticJacobian6(self):
def f(x = N.matrix(N.zeros((2,1)))):
ans = N.matrix(N.zeros((2,1)))
ans[0,0] = M.sin(x[0,0]) + 2.0*M.cos(x[1,0]) - M.pow(x[0,0], 2.0)
ans[1,0] = 3.0*M.tan(x[0,0], 0.5) - M.tan(x[1,0])
x0 = N.matrix("1.0; 1.0")
dx = 1.e-12
x = []
x.append([{"sin": 1, 2: -1}, {"cos": 2}])
x.append([{"tan": 3}, {"tan": -1}])
Df_x = F.AnalyticJacobian(x0, dx, ["SimpleComplex", x[0], x[1]])
exact = N.matrix("-0.45969769 -1.68294197; 7.27655646 -2.42551882")
N.testing.assert_array_almost_equal(Df_x, exact)
if __name__ == '__main__':
unittest.main()