Repository navigation
Expand file tree
/
Copy pathtestFunctions.py
More file actions
119 lines (97 loc) · 3.83 KB
/
Copy pathtestFunctions.py
File metadata and controls
119 lines (97 loc) · 3.83 KB
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
#!/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 testApproxJacobianQuadratic(self):
f=lambda x: 2*x**2
x0 = 3.
dx = 1.e-6
Df_x = F.ApproximateJacobian(f, x0, dx)
self.assertEqual(Df_x.shape, (1,1))
self.assertAlmostEqual(Df_x, 4.*x0)"""
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 testCubicPolynomial(self):
#my testNewton test for x^3 wasn't close enough to zero so I wanted to check that the implementation in polynomial is fine
p = F.Polynomial([1, 0, 0, 0])
for x in N.linspace(-2,2,101):
self.assertEqual(p(x), x**3)
def testQuadraticJacobian(self):
p=F.Polynomial([1,-1,6]) #x^2-x+6 has derivative 2x-1
for x in N.linspace(-2,2,11):
D=F.ApproximateJacobian(p, x)
self.assertEqual(D.shape, (1,1))
N.testing.assert_array_almost_equal(D, N.matrix([[2*x-1]]))
def testBivariateQuadratic2D(self):
q=F.BivariateQuadratic2D([1,2,3,4,5,6],[7,8,9,10,11,12])
for x in N.linspace(-2,2,11):
for y in N.linspace(-2,2,11):
xs=N.matrix([[x],[y]])
fs=q(xs)
self.assertEqual(fs.shape, xs.shape)
self.assertEqual(fs.item(0), x**2+2*y**2+3*x+4*y+5*x*y+6)
self.assertEqual(fs.item(1), 7*x**2+8*y**2+9*x+10*y+11*x*y+12)
def testBivariateQuadratic2DApproxJacobian(self):
q=F.BivariateQuadratic2D([1,0,0,1,0,1],[0,1,0,0,3,0])
xs1=N.matrix("5;3")
xs2=N.matrix("0;0")
xs3=N.matrix("0;1")
#actual Jacobian at xs1, xs2 calculated analytically by hand
J1=N.matrix("10 1; 9 21")
J2=N.matrix("0 1; 0 0")
J3=N.matrix("0 1; 3 2")
D1=F.ApproximateJacobian(q, xs1)
D2=F.ApproximateJacobian(q, xs2)
D3=F.ApproximateJacobian(q, xs3)
self.assertEqual(D1.shape, (2,2))
self.assertEqual(D2.shape, (2,2))
self.assertEqual(D3.shape, (2,2))
N.testing.assert_array_almost_equal(D1, J1)
N.testing.assert_array_almost_equal(D2, J2)
N.testing.assert_array_almost_equal(D3, J3)
def testBivariateQuadratic2DExactJacobian(self):
q=F.BivariateQuadratic2D([1,0,0,1,0,1],[0,1,0,0,3,0])
xs1=N.matrix("5;3")
xs2=N.matrix("0;0")
xs3=N.matrix("0;1")
#actual Jacobian at xs1, xs2 calculated analytically by hand
J1=N.matrix("10 1; 9 21")
J2=N.matrix("0 1; 0 0")
J3=N.matrix("0 1; 3 2")
D1=q.Jacobian(xs1)
D2=q.Jacobian(xs2)
D3=q.Jacobian(xs3)
self.assertEqual(D1.shape, (2,2))
self.assertEqual(D2.shape, (2,2))
self.assertEqual(D3.shape, (2,2))
N.testing.assert_array_almost_equal(D1, J1)
N.testing.assert_array_almost_equal(D2, J2)
N.testing.assert_array_almost_equal(D3, J3)
def testPolynomialExactJacobian(self):
p = F.Polynomial([1, 2, 3])
for x in N.linspace(-2,2,11):
self.assertEqual(p.Jacobian(x), 2*x + 2)
if __name__ == '__main__':
unittest.main()