[1910] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | import unittest |
---|
| 5 | from Numeric import zeros, array, allclose |
---|
| 6 | from math import sqrt, pi |
---|
[3514] | 7 | from anuga.config import epsilon |
---|
[1910] | 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 | |
---|
[2704] | 21 | def test_angle1(self): |
---|
| 22 | """Test angles between one vector and the x-axis |
---|
| 23 | """ |
---|
| 24 | assert allclose(angle([1.0, 0.0])/pi*180, 0.0) |
---|
[2526] | 25 | assert allclose(angle([1.0, 1.0])/pi*180, 45.0) |
---|
[2704] | 26 | assert allclose(angle([0.0, 1.0])/pi*180, 90.0) |
---|
| 27 | assert allclose(angle([-1.0, 1.0])/pi*180, 135.0) |
---|
| 28 | assert allclose(angle([-1.0, 0.0])/pi*180, 180.0) |
---|
| 29 | assert allclose(angle([-1.0, -1.0])/pi*180, 225.0) |
---|
| 30 | assert allclose(angle([0.0, -1.0])/pi*180, 270.0) |
---|
[5561] | 31 | assert allclose(angle([1.0, -1.0])/pi*180, 315.0) |
---|
[2704] | 32 | |
---|
| 33 | |
---|
| 34 | def test_angle2(self): |
---|
| 35 | """Test angles between two arbitrary vectors |
---|
| 36 | """ |
---|
| 37 | |
---|
| 38 | assert allclose(angle([1.0, 0.0], [1.0, 1.0])/pi*180, 315.0) |
---|
| 39 | assert allclose(angle([1.0, 1.0], [1.0, 0.0])/pi*180, 45.0) |
---|
| 40 | |
---|
| 41 | assert allclose(angle([-1.0, -1.0], [1.0, 1.0])/pi*180, 180) |
---|
| 42 | assert allclose(angle([-1.0, -1.0], [-1.0, 1.0])/pi*180, 90.0) |
---|
| 43 | |
---|
| 44 | assert allclose(angle([-1.0, 0.0], [1.0, 1.0])/pi*180, 135.0) |
---|
| 45 | assert allclose(angle([0.0, -1.0], [1.0, 1.0])/pi*180, 225.0) |
---|
| 46 | |
---|
| 47 | assert allclose(angle([1.0, -1.0], [1.0, 1.0])/pi*180, 270.0) |
---|
[2709] | 48 | assert allclose(angle([1.0, 0.0], [0.0, 1.0])/pi*180, 270.0) |
---|
| 49 | |
---|
| 50 | #From test_get_boundary_polygon_V |
---|
| 51 | v_prev = [-0.5, -0.5] |
---|
| 52 | vc = [ 0.0, -0.5] |
---|
| 53 | assert allclose(angle(vc, v_prev)/pi*180, 45.0) |
---|
| 54 | |
---|
| 55 | vc = [ 0.5, 0.0] |
---|
| 56 | assert allclose(angle(vc, v_prev)/pi*180, 135.0) |
---|
| 57 | |
---|
| 58 | vc = [ -0.5, 0.5] |
---|
| 59 | assert allclose(angle(vc, v_prev)/pi*180, 270.0) |
---|
| 60 | |
---|
| 61 | |
---|
[2526] | 62 | def test_anglediff(self): |
---|
| 63 | assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0) |
---|
| 64 | |
---|
[1910] | 65 | |
---|
| 66 | def test_ensure_numeric(self): |
---|
| 67 | from numerical_tools import ensure_numeric |
---|
[4551] | 68 | from Numeric import ArrayType, Float, Int, array |
---|
[1910] | 69 | |
---|
| 70 | A = [1,2,3,4] |
---|
| 71 | B = ensure_numeric(A) |
---|
| 72 | assert type(B) == ArrayType |
---|
| 73 | assert B.typecode() == 'l' |
---|
| 74 | assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4 |
---|
| 75 | |
---|
| 76 | A = [1,2,3.14,4] |
---|
| 77 | B = ensure_numeric(A) |
---|
| 78 | assert type(B) == ArrayType |
---|
| 79 | assert B.typecode() == 'd' |
---|
| 80 | assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4 |
---|
| 81 | |
---|
| 82 | A = [1,2,3,4] |
---|
| 83 | B = ensure_numeric(A, Float) |
---|
| 84 | assert type(B) == ArrayType |
---|
| 85 | assert B.typecode() == 'd' |
---|
| 86 | assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 |
---|
| 87 | |
---|
| 88 | A = [1,2,3,4] |
---|
| 89 | B = ensure_numeric(A, Float) |
---|
| 90 | assert type(B) == ArrayType |
---|
| 91 | assert B.typecode() == 'd' |
---|
| 92 | assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 |
---|
| 93 | |
---|
| 94 | A = array([1,2,3,4]) |
---|
| 95 | B = ensure_numeric(A) |
---|
| 96 | assert type(B) == ArrayType |
---|
| 97 | assert B.typecode() == 'l' |
---|
| 98 | assert A == B |
---|
| 99 | assert A is B #Same object |
---|
| 100 | |
---|
| 101 | A = array([1,2,3,4]) |
---|
| 102 | B = ensure_numeric(A, Float) |
---|
| 103 | assert type(B) == ArrayType |
---|
| 104 | assert B.typecode() == 'd' |
---|
| 105 | assert A == B |
---|
[2509] | 106 | assert A is not B #Not the same object |
---|
[1910] | 107 | |
---|
[4551] | 108 | # Check scalars |
---|
| 109 | A = 1 |
---|
| 110 | B = ensure_numeric(A, Float) |
---|
| 111 | #print A, B[0], len(B), type(B) |
---|
| 112 | #print B.shape |
---|
| 113 | assert A == B |
---|
[1910] | 114 | |
---|
[4551] | 115 | B = ensure_numeric(A, Int) |
---|
| 116 | #print A, B |
---|
| 117 | #print B.shape |
---|
| 118 | assert A == B |
---|
| 119 | |
---|
| 120 | # Error situation |
---|
| 121 | |
---|
| 122 | B = ensure_numeric('hello', Int) |
---|
| 123 | assert allclose(B, [104, 101, 108, 108, 111]) |
---|
| 124 | |
---|
[5561] | 125 | |
---|
[2509] | 126 | def test_gradient(self): |
---|
| 127 | x0 = 0.0; y0 = 0.0; z0 = 0.0 |
---|
| 128 | x1 = 1.0; y1 = 0.0; z1 = -1.0 |
---|
| 129 | x2 = 0.0; y2 = 1.0; z2 = 0.0 |
---|
| 130 | |
---|
| 131 | zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2) |
---|
| 132 | |
---|
| 133 | assert zx == -1.0 |
---|
| 134 | assert zy == 0.0 |
---|
| 135 | |
---|
[5561] | 136 | |
---|
[2509] | 137 | def test_gradient_more(self): |
---|
| 138 | x0 = 2.0/3; y0 = 2.0/3 |
---|
| 139 | x1= 8.0/3; y1 = 2.0/3 |
---|
| 140 | x2 = 2.0/3; y2 = 8.0/3 |
---|
| 141 | |
---|
| 142 | q0 = 2.0+2.0/3 |
---|
| 143 | q1 = 8.0+2.0/3 |
---|
| 144 | q2 = 2.0+8.0/3 |
---|
| 145 | |
---|
| 146 | #Gradient of fitted pwl surface |
---|
| 147 | a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
| 148 | |
---|
| 149 | assert abs(a - 3.0) < epsilon |
---|
| 150 | assert abs(b - 1.0) < epsilon |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | def test_gradient2(self): |
---|
| 154 | """Test two-point gradient |
---|
| 155 | """ |
---|
| 156 | |
---|
| 157 | x0 = 5.0; y0 = 5.0; z0 = 10.0 |
---|
| 158 | x1 = 8.0; y1 = 2.0; z1 = 1.0 |
---|
| 159 | x2 = 8.0; y2 = 8.0; z2 = 10.0 |
---|
| 160 | |
---|
| 161 | #Reference |
---|
| 162 | zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2) |
---|
| 163 | a, b = gradient2(x0, y0, x1, y1, z0, z1) |
---|
| 164 | |
---|
| 165 | assert zx == a |
---|
| 166 | assert zy == b |
---|
| 167 | |
---|
| 168 | z2_computed = z0 + a*(x2-x0) + b*(y2-y0) |
---|
| 169 | assert z2_computed == z2 |
---|
[5561] | 170 | |
---|
[2509] | 171 | |
---|
| 172 | def test_gradient2_more(self): |
---|
| 173 | """Test two-point gradient more |
---|
| 174 | """ |
---|
| 175 | x0 = 2.0; y0 = 2.0 |
---|
| 176 | x1 = 8.0; y1 = 3.0 |
---|
| 177 | x2 = 1.0; y2 = 8.0 |
---|
| 178 | |
---|
| 179 | q0 = 2.0 |
---|
| 180 | q1 = 8.0 |
---|
| 181 | q2 = q0 |
---|
| 182 | |
---|
| 183 | #Gradient of fitted pwl surface |
---|
| 184 | a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
| 185 | a, b = gradient2(x0, y0, x1, y1, q0, q1) |
---|
| 186 | |
---|
| 187 | assert a == a_ref |
---|
| 188 | assert b == b_ref |
---|
| 189 | |
---|
| 190 | |
---|
[3849] | 191 | def test_machine_precision(self): |
---|
| 192 | """test_machine_precision(self): |
---|
| 193 | Test the function that calculates epsilon. As this varies on |
---|
| 194 | different machines, this is only an indication. |
---|
| 195 | """ |
---|
| 196 | |
---|
| 197 | eps = get_machine_precision() |
---|
| 198 | |
---|
| 199 | assert eps < 1.0e-12, 'Machine precision should be better than 1.0e-12' |
---|
| 200 | assert eps > 0.0 |
---|
| 201 | assert 1.0+eps/2 == 1.0 |
---|
| 202 | |
---|
| 203 | |
---|
[2533] | 204 | def test_histogram(self): |
---|
| 205 | """Test histogram with different bin boundaries |
---|
| 206 | """ |
---|
| 207 | |
---|
| 208 | a = [1,1,1,1,1,2,1,3,2,3,1,2,3,4,1] |
---|
| 209 | |
---|
| 210 | #There are four elements greater than or equal to 3 |
---|
| 211 | bins = [3] |
---|
| 212 | assert allclose(histogram(a, bins), [4]) |
---|
| 213 | |
---|
| 214 | bins = [ min(a) ] |
---|
| 215 | assert allclose(histogram(a, bins), [len(a)]) |
---|
| 216 | |
---|
| 217 | bins = [ max(a)+0.00001 ] |
---|
| 218 | assert allclose(histogram(a, bins), [0]) |
---|
| 219 | |
---|
| 220 | bins = [1,2,3,4] |
---|
| 221 | assert allclose(histogram(a, bins), [8,3,3,1]) |
---|
| 222 | |
---|
[2534] | 223 | bins = [1.1,2,3.1,4] |
---|
| 224 | #print histogram(a, bins) |
---|
| 225 | assert allclose(histogram(a, bins), [0,6,0,1]) |
---|
| 226 | |
---|
| 227 | bins = [0,1.5,2,3] |
---|
[2533] | 228 | assert allclose(histogram(a, bins), [8,0,3,4]) |
---|
[2971] | 229 | assert allclose(histogram(a, [0,3]), histogram(a, [-0.5,3])) |
---|
[2533] | 230 | |
---|
[2971] | 231 | # Check situation with #bins >= #datapoints |
---|
| 232 | a = [1.7] |
---|
| 233 | bins = [0,1.5,2,3] |
---|
| 234 | assert allclose(histogram(a, bins), [0,1,0,0]) |
---|
[2533] | 235 | |
---|
[2971] | 236 | a = [1.7] |
---|
| 237 | bins = [0] |
---|
| 238 | assert allclose(histogram(a, bins), [1]) |
---|
[2534] | 239 | |
---|
[2971] | 240 | a = [-1.7] |
---|
| 241 | bins = [0] |
---|
| 242 | assert allclose(histogram(a, bins), [0]) |
---|
[2534] | 243 | |
---|
[2971] | 244 | a = [-1.7] |
---|
| 245 | bins = [-1.7] |
---|
[5561] | 246 | assert allclose(histogram(a, bins), [1]) |
---|
[2533] | 247 | |
---|
| 248 | |
---|
[2509] | 249 | def test_that_C_extension_compiles(self): |
---|
| 250 | FN = 'util_ext.c' |
---|
| 251 | try: |
---|
| 252 | import util_ext |
---|
| 253 | except: |
---|
| 254 | from compile import compile |
---|
| 255 | |
---|
| 256 | try: |
---|
| 257 | compile(FN) |
---|
| 258 | except: |
---|
| 259 | raise 'Could not compile %s' %FN |
---|
| 260 | else: |
---|
| 261 | import util_ext |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | def test_gradient_C_extension(self): |
---|
| 265 | from util_ext import gradient as gradient_c |
---|
| 266 | |
---|
| 267 | x0 = 2.0/3; y0 = 2.0/3 |
---|
| 268 | x1= 8.0/3; y1 = 2.0/3 |
---|
| 269 | x2 = 2.0/3; y2 = 8.0/3 |
---|
| 270 | |
---|
| 271 | q0 = 2.0+2.0/3 |
---|
| 272 | q1 = 8.0+2.0/3 |
---|
| 273 | q2 = 2.0+8.0/3 |
---|
| 274 | |
---|
| 275 | #Gradient of fitted pwl surface |
---|
| 276 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
| 277 | |
---|
| 278 | assert abs(a - 3.0) < epsilon |
---|
| 279 | assert abs(b - 1.0) < epsilon |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | def test_gradient_C_extension3(self): |
---|
| 283 | from util_ext import gradient as gradient_c |
---|
| 284 | |
---|
| 285 | from RandomArray import uniform, seed |
---|
| 286 | seed(17, 53) |
---|
| 287 | |
---|
| 288 | x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6) |
---|
| 289 | |
---|
| 290 | q0 = uniform(0.0, 10.0, 4) |
---|
| 291 | q1 = uniform(1.0, 3.0, 4) |
---|
| 292 | q2 = uniform(7.0, 20.0, 4) |
---|
| 293 | |
---|
| 294 | for i in range(4): |
---|
| 295 | #Gradient of fitted pwl surface |
---|
[2516] | 296 | a_ref, b_ref = gradient_python(x0, y0, x1, y1, x2, y2, |
---|
| 297 | q0[i], q1[i], q2[i]) |
---|
[2509] | 298 | |
---|
| 299 | #print a_ref, b_ref |
---|
| 300 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, |
---|
| 301 | q0[i], q1[i], q2[i]) |
---|
| 302 | |
---|
| 303 | #print a, a_ref, b, b_ref |
---|
| 304 | assert abs(a - a_ref) < epsilon |
---|
| 305 | assert abs(b - b_ref) < epsilon |
---|
| 306 | |
---|
[5561] | 307 | |
---|
| 308 | def test_err(self): |
---|
| 309 | x = [2,5] # diff at first position = 4, 4^2 = 16 |
---|
| 310 | y = [6,7] # diff at secnd position = 2, 2^2 = 4 |
---|
| 311 | # 16 + 4 = 20 |
---|
[2509] | 312 | |
---|
[5561] | 313 | # If there is x and y, n=2 and relative=False, this will calc; |
---|
| 314 | # sqrt(sum_over_x&y((xi - yi)^2)) |
---|
| 315 | err__1 = err(x,y,2,False) |
---|
| 316 | assert err__1 == sqrt(20) |
---|
| 317 | #print "err_", err_ |
---|
| 318 | #rmsd_1 = err__1*sqrt(1./len(x)) |
---|
| 319 | #print "err__1*sqrt(1./len(x))", err__1*sqrt(1./len(x)) |
---|
| 320 | #print "sqrt(10)", sqrt(10) |
---|
| 321 | |
---|
| 322 | x = [2,7,100] |
---|
| 323 | y = [5,10,103] |
---|
| 324 | err__2 = err(x,y,2,False) |
---|
| 325 | assert err__2 == sqrt(27) |
---|
| 326 | #rmsd_2 = err__2*sqrt(1./len(x)) |
---|
| 327 | #print "err__2*sqrt(1./len(x))", err__2*sqrt(1./len(x)) |
---|
[2509] | 328 | |
---|
[5561] | 329 | x = [2,5,2,7,100] |
---|
| 330 | y = [6,7,5,10,103] |
---|
| 331 | err_3 = err(x,y,2,False) |
---|
| 332 | assert err_3 == sqrt(47) |
---|
| 333 | |
---|
| 334 | #rmsd_3 = err_3*sqrt(1./len(x)) |
---|
| 335 | #print "err__3*sqrt(1./len(x))", err__3*sqrt(1./len(x)) |
---|
| 336 | #print "rmsd_3", rmsd_3 |
---|
| 337 | #print "sqrt(err_1*err__1+err__2*err__2)/sqrt(5)", \ |
---|
| 338 | # sqrt(err__1*err__1+err__2*err__2)/sqrt(5) |
---|
| 339 | #print "(rmsd_1 + rmsd_2)/2.", (rmsd_1 + rmsd_2)/2. |
---|
| 340 | #print "sqrt((rmsd_1*rmsd_1 + rmsd_2*rmsd_2))/2.", \ |
---|
| 341 | #sqrt((rmsd_1*rmsd_1 + rmsd_2*rmsd_2))/2. |
---|
| 342 | |
---|
[2509] | 343 | |
---|
[5561] | 344 | |
---|
| 345 | |
---|
[1910] | 346 | #------------------------------------------------------------- |
---|
| 347 | if __name__ == "__main__": |
---|
| 348 | suite = unittest.makeSuite(Test_Numerical_Tools,'test') |
---|
[5561] | 349 | #suite = unittest.makeSuite(Test_Numerical_Tools,'test_err') |
---|
[1910] | 350 | runner = unittest.TextTestRunner() |
---|
| 351 | runner.run(suite) |
---|