1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from Numeric import zeros, array, allclose |
---|
6 | from math import sqrt, pi |
---|
7 | from pyvolution.config import epsilon |
---|
8 | |
---|
9 | from numerical_tools import * |
---|
10 | |
---|
11 | def test_function(x, y): |
---|
12 | return x+y |
---|
13 | |
---|
14 | class Test_Numerical_Tools(unittest.TestCase): |
---|
15 | def setUp(self): |
---|
16 | pass |
---|
17 | |
---|
18 | def tearDown(self): |
---|
19 | pass |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | def test_ensure_numeric(self): |
---|
25 | from numerical_tools import ensure_numeric |
---|
26 | from Numeric import ArrayType, Float, array |
---|
27 | |
---|
28 | A = [1,2,3,4] |
---|
29 | B = ensure_numeric(A) |
---|
30 | assert type(B) == ArrayType |
---|
31 | assert B.typecode() == 'l' |
---|
32 | assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4 |
---|
33 | |
---|
34 | |
---|
35 | A = [1,2,3.14,4] |
---|
36 | B = ensure_numeric(A) |
---|
37 | assert type(B) == ArrayType |
---|
38 | assert B.typecode() == 'd' |
---|
39 | assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4 |
---|
40 | |
---|
41 | |
---|
42 | A = [1,2,3,4] |
---|
43 | B = ensure_numeric(A, Float) |
---|
44 | assert type(B) == ArrayType |
---|
45 | assert B.typecode() == 'd' |
---|
46 | assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 |
---|
47 | |
---|
48 | |
---|
49 | A = [1,2,3,4] |
---|
50 | B = ensure_numeric(A, Float) |
---|
51 | assert type(B) == ArrayType |
---|
52 | assert B.typecode() == 'd' |
---|
53 | assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 |
---|
54 | |
---|
55 | |
---|
56 | A = array([1,2,3,4]) |
---|
57 | B = ensure_numeric(A) |
---|
58 | assert type(B) == ArrayType |
---|
59 | assert B.typecode() == 'l' |
---|
60 | assert A == B |
---|
61 | assert A is B #Same object |
---|
62 | |
---|
63 | |
---|
64 | A = array([1,2,3,4]) |
---|
65 | B = ensure_numeric(A, Float) |
---|
66 | assert type(B) == ArrayType |
---|
67 | assert B.typecode() == 'd' |
---|
68 | assert A == B |
---|
69 | assert A is not B #Not the same object |
---|
70 | |
---|
71 | |
---|
72 | def test_gradient(self): |
---|
73 | x0 = 0.0; y0 = 0.0; z0 = 0.0 |
---|
74 | x1 = 1.0; y1 = 0.0; z1 = -1.0 |
---|
75 | x2 = 0.0; y2 = 1.0; z2 = 0.0 |
---|
76 | |
---|
77 | zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2) |
---|
78 | |
---|
79 | assert zx == -1.0 |
---|
80 | assert zy == 0.0 |
---|
81 | |
---|
82 | def test_gradient_more(self): |
---|
83 | x0 = 2.0/3; y0 = 2.0/3 |
---|
84 | x1= 8.0/3; y1 = 2.0/3 |
---|
85 | x2 = 2.0/3; y2 = 8.0/3 |
---|
86 | |
---|
87 | q0 = 2.0+2.0/3 |
---|
88 | q1 = 8.0+2.0/3 |
---|
89 | q2 = 2.0+8.0/3 |
---|
90 | |
---|
91 | #Gradient of fitted pwl surface |
---|
92 | a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
93 | |
---|
94 | assert abs(a - 3.0) < epsilon |
---|
95 | assert abs(b - 1.0) < epsilon |
---|
96 | |
---|
97 | |
---|
98 | def test_gradient2(self): |
---|
99 | """Test two-point gradient |
---|
100 | """ |
---|
101 | |
---|
102 | x0 = 5.0; y0 = 5.0; z0 = 10.0 |
---|
103 | x1 = 8.0; y1 = 2.0; z1 = 1.0 |
---|
104 | x2 = 8.0; y2 = 8.0; z2 = 10.0 |
---|
105 | |
---|
106 | #Reference |
---|
107 | zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2) |
---|
108 | a, b = gradient2(x0, y0, x1, y1, z0, z1) |
---|
109 | |
---|
110 | assert zx == a |
---|
111 | assert zy == b |
---|
112 | |
---|
113 | z2_computed = z0 + a*(x2-x0) + b*(y2-y0) |
---|
114 | assert z2_computed == z2 |
---|
115 | |
---|
116 | def test_gradient2_more(self): |
---|
117 | """Test two-point gradient more |
---|
118 | """ |
---|
119 | x0 = 2.0; y0 = 2.0 |
---|
120 | x1 = 8.0; y1 = 3.0 |
---|
121 | x2 = 1.0; y2 = 8.0 |
---|
122 | |
---|
123 | q0 = 2.0 |
---|
124 | q1 = 8.0 |
---|
125 | q2 = q0 |
---|
126 | |
---|
127 | #Gradient of fitted pwl surface |
---|
128 | a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
129 | a, b = gradient2(x0, y0, x1, y1, q0, q1) |
---|
130 | |
---|
131 | assert a == a_ref |
---|
132 | assert b == b_ref |
---|
133 | |
---|
134 | |
---|
135 | def test_that_C_extension_compiles(self): |
---|
136 | FN = 'util_ext.c' |
---|
137 | try: |
---|
138 | import util_ext |
---|
139 | except: |
---|
140 | from compile import compile |
---|
141 | |
---|
142 | try: |
---|
143 | compile(FN) |
---|
144 | except: |
---|
145 | raise 'Could not compile %s' %FN |
---|
146 | else: |
---|
147 | import util_ext |
---|
148 | |
---|
149 | |
---|
150 | def test_gradient_C_extension(self): |
---|
151 | from util_ext import gradient as gradient_c |
---|
152 | |
---|
153 | x0 = 2.0/3; y0 = 2.0/3 |
---|
154 | x1= 8.0/3; y1 = 2.0/3 |
---|
155 | x2 = 2.0/3; y2 = 8.0/3 |
---|
156 | |
---|
157 | q0 = 2.0+2.0/3 |
---|
158 | q1 = 8.0+2.0/3 |
---|
159 | q2 = 2.0+8.0/3 |
---|
160 | |
---|
161 | #Gradient of fitted pwl surface |
---|
162 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
163 | |
---|
164 | assert abs(a - 3.0) < epsilon |
---|
165 | assert abs(b - 1.0) < epsilon |
---|
166 | |
---|
167 | |
---|
168 | def test_gradient_C_extension3(self): |
---|
169 | from util_ext import gradient as gradient_c |
---|
170 | |
---|
171 | from RandomArray import uniform, seed |
---|
172 | seed(17, 53) |
---|
173 | |
---|
174 | x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6) |
---|
175 | |
---|
176 | q0 = uniform(0.0, 10.0, 4) |
---|
177 | q1 = uniform(1.0, 3.0, 4) |
---|
178 | q2 = uniform(7.0, 20.0, 4) |
---|
179 | |
---|
180 | |
---|
181 | for i in range(4): |
---|
182 | #Gradient of fitted pwl surface |
---|
183 | a_ref, b_ref = gradient_python(x0, y0, x1, y1, x2, y2, |
---|
184 | q0[i], q1[i], q2[i]) |
---|
185 | |
---|
186 | #print a_ref, b_ref |
---|
187 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, |
---|
188 | q0[i], q1[i], q2[i]) |
---|
189 | |
---|
190 | #print a, a_ref, b, b_ref |
---|
191 | assert abs(a - a_ref) < epsilon |
---|
192 | assert abs(b - b_ref) < epsilon |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | #------------------------------------------------------------- |
---|
198 | if __name__ == "__main__": |
---|
199 | suite = unittest.makeSuite(Test_Numerical_Tools,'test') |
---|
200 | runner = unittest.TextTestRunner() |
---|
201 | runner.run(suite) |
---|