Changeset 6158
- Timestamp:
- Jan 14, 2009, 9:48:37 AM (16 years ago)
- Location:
- anuga_core/source/anuga/utilities
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/utilities/cg_solve.py
r5897 r6158 3 3 class ConvergenceError(exceptions.Exception): pass 4 4 5 from Numeric import dot, array, Float, zeros 5 import Numeric as num 6 6 7 7 import logging, logging.config … … 24 24 25 25 if x0 is None: 26 x0 = zeros(b.shape, typecode=Float)26 x0 = num.zeros(b.shape, typecode=num.Float) 27 27 else: 28 x0 = array(x0, typecode=Float)28 x0 = num.array(x0, typecode=num.Float) 29 29 30 b = array(b, typecode=Float)30 b = num.array(b, typecode=num.Float) 31 31 if len(b.shape) != 1 : 32 32 … … 57 57 58 58 59 b = array(b, typecode=Float)59 b = num.array(b, typecode=num.Float) 60 60 if len(b.shape) != 1 : 61 61 raise VectorShapeError, 'input vector should consist of only one column' 62 62 63 63 if x0 is None: 64 x0 = zeros(b.shape, typecode=Float)64 x0 = num.zeros(b.shape, typecode=num.Float) 65 65 else: 66 x0 = array(x0, typecode=Float)66 x0 = num.array(x0, typecode=num.Float) 67 67 68 68 … … 75 75 r = b - A*x 76 76 d = r 77 rTr = dot(r,r)77 rTr = num.dot(r,r) 78 78 rTr0 = rTr 79 79 80 80 while (i<imax and rTr>tol**2*rTr0): 81 81 q = A*d 82 alpha = rTr/ dot(d,q)82 alpha = rTr/num.dot(d,q) 83 83 x = x + alpha*d 84 84 if i%50 : … … 87 87 r = r - alpha*q 88 88 rTrOld = rTr 89 rTr = dot(r,r)89 rTr = num.dot(r,r) 90 90 bt = rTr/rTrOld 91 91 -
anuga_core/source/anuga/utilities/numerical_tools.py
r6119 r6158 7 7 from warnings import warn 8 8 9 #Establish which Numeric package to use 10 #(this should move to somewhere central) 11 #try: 12 # from scipy import ArrayType, array, sum, innerproduct, ravel, sqrt, 13 # searchsorted, sort, concatenate, Float, arange 14 #except: 15 # #print 'Could not find scipy - using Numeric' 16 # from Numeric import ArrayType, array, sum, innerproduct, ravel, sqrt, 17 #searchsorted, sort, concatenate, Float, arange 18 19 from Numeric import ArrayType, array, sum, innerproduct, ravel, sqrt,\ 20 searchsorted, sort, concatenate, Float, arange 21 22 # Getting an infinite number to use when using Numeric 23 #INF = (array([1])/0.)[0] 24 25 NAN = (array([1])/0.)[0] 26 # Note, INF is used instead of NAN (Not a number), since Numeric has no NAN 9 import Numeric as num 10 11 NAN = (num.array([1])/0.)[0] 27 12 # if we use a package that has NAN, this should be updated to use NAN. 28 13 … … 89 74 v2 = [1.0, 0.0] # Unit vector along the x-axis 90 75 91 v1 = ensure_numeric(v1, Float)92 v2 = ensure_numeric(v2, Float)76 v1 = ensure_numeric(v1, num.Float) 77 v2 = ensure_numeric(v2, num.Float) 93 78 94 79 # Normalise 95 v1 = v1/ sqrt(sum(v1**2))96 v2 = v2/ sqrt(sum(v2**2))80 v1 = v1/num.sqrt(num.sum(v1**2)) 81 v2 = v2/num.sqrt(num.sum(v2**2)) 97 82 98 83 # Compute angle 99 p = innerproduct(v1, v2)100 c = innerproduct(v1, normal_vector(v2)) # Projection onto normal84 p = num.innerproduct(v1, v2) 85 c = num.innerproduct(v1, normal_vector(v2)) # Projection onto normal 101 86 # (negative cross product) 102 87 … … 140 125 """ 141 126 142 return array([-v[1], v[0]],Float)127 return num.array([-v[1], v[0]], num.Float) 143 128 144 129 … … 150 135 """Mean value of a vector 151 136 """ 152 return(float( sum(x))/len(x))137 return(float(num.sum(x))/len(x)) 153 138 154 139 … … 171 156 cy = y - mean(y) 172 157 173 p = innerproduct(cx,cy)/N158 p = num.innerproduct(cx,cy)/N 174 159 return(p) 175 160 … … 220 205 """ 221 206 222 y = ravel(x)223 p = sqrt(innerproduct(y,y))207 y = num.ravel(x) 208 p = num.sqrt(num.innerproduct(y,y)) 224 209 return p 225 210 … … 262 247 263 248 if typecode is None: 264 if type(A) == ArrayType:249 if type(A) == num.ArrayType: 265 250 return A 266 251 else: 267 return array(A)252 return num.array(A) 268 253 else: 269 if type(A) == ArrayType:254 if type(A) == num.ArrayType: 270 255 if A.typecode == typecode: 271 return array(A) #FIXME: Shouldn't this just return A?256 return num.array(A) #FIXME: Shouldn't this just return A? 272 257 else: 273 return array(A,typecode)258 return num.array(A,typecode) 274 259 else: 275 return array(A,typecode)260 return num.array(A,typecode) 276 261 277 262 … … 285 270 """ 286 271 287 n = searchsorted(sort(a), bins)288 n = concatenate( [n, [len(a)]] )272 n = num.searchsorted(num.sort(a), bins) 273 n = num.concatenate( [n, [len(a)]] ) 289 274 290 275 hist = n[1:]-n[:-1] 291 276 292 277 if relative is True: 293 hist = hist/float( sum(hist))278 hist = hist/float(num.sum(hist)) 294 279 295 280 return hist … … 305 290 306 291 if mx == mn: 307 bins = array([mn])292 bins = num.array([mn]) 308 293 else: 309 294 if number_of_bins is None: 310 295 number_of_bins = 10 311 296 312 bins = arange(mn, mx, (mx-mn)/number_of_bins)297 bins = num.arange(mn, mx, (mx-mn)/number_of_bins) 313 298 314 299 return bins -
anuga_core/source/anuga/utilities/polygon.py
r6119 r6158 1 1 #!/usr/bin/env python 2 2 """Polygon manipulations 3 4 3 """ 5 4 6 7 #try: 8 # from scipy import Float, Int, zeros, ones, array, concatenate, reshape, dot 9 #except: 10 # #print 'Could not find scipy - using Numeric' 11 12 from Numeric import Float, Int, zeros, ones, array, concatenate, reshape, dot, allclose 13 5 import Numeric as num 14 6 15 7 from math import sqrt … … 49 41 # result functions for possible states 50 42 def lines_dont_coincide(p0,p1,p2,p3): return (3, None) 51 def lines_0_fully_included_in_1(p0,p1,p2,p3): return (2, array([p0,p1]))52 def lines_1_fully_included_in_0(p0,p1,p2,p3): return (2, array([p2,p3]))53 def lines_overlap_same_direction(p0,p1,p2,p3): return (2, array([p0,p3]))54 def lines_overlap_same_direction2(p0,p1,p2,p3): return (2, array([p2,p1]))55 def lines_overlap_opposite_direction(p0,p1,p2,p3): return (2, array([p0,p2]))56 def lines_overlap_opposite_direction2(p0,p1,p2,p3): return (2, array([p3,p1]))43 def lines_0_fully_included_in_1(p0,p1,p2,p3): return (2, num.array([p0,p1])) 44 def lines_1_fully_included_in_0(p0,p1,p2,p3): return (2, num.array([p2,p3])) 45 def lines_overlap_same_direction(p0,p1,p2,p3): return (2, num.array([p0,p3])) 46 def lines_overlap_same_direction2(p0,p1,p2,p3): return (2, num.array([p2,p1])) 47 def lines_overlap_opposite_direction(p0,p1,p2,p3): return (2, num.array([p0,p2])) 48 def lines_overlap_opposite_direction2(p0,p1,p2,p3): return (2, num.array([p3,p1])) 57 49 58 50 # this function called when an impossible state is found … … 108 100 # FIXME (Ole): Write this in C 109 101 110 line0 = ensure_numeric(line0, Float)111 line1 = ensure_numeric(line1, Float)102 line0 = ensure_numeric(line0, num.Float) 103 line1 = ensure_numeric(line1, num.Float) 112 104 113 105 x0 = line0[0,0]; y0 = line0[0,1] … … 121 113 u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0) 122 114 123 if allclose(denom, 0.0, rtol=rtol, atol=atol):115 if num.allclose(denom, 0.0, rtol=rtol, atol=atol): 124 116 # Lines are parallel - check if they are collinear 125 if allclose([u0, u1], 0.0, rtol=rtol, atol=atol):117 if num.allclose([u0, u1], 0.0, rtol=rtol, atol=atol): 126 118 # We now know that the lines are collinear 127 119 state_tuple = (point_on_line([x0, y0], line1, rtol=rtol, atol=atol), … … 143 135 144 136 # Sanity check - can be removed to speed up if needed 145 assert allclose(x, x2 + u1*(x3-x2), rtol=rtol, atol=atol)146 assert allclose(y, y2 + u1*(y3-y2), rtol=rtol, atol=atol)137 assert num.allclose(x, x2 + u1*(x3-x2), rtol=rtol, atol=atol) 138 assert num.allclose(y, y2 + u1*(y3-y2), rtol=rtol, atol=atol) 147 139 148 140 # Check if point found lies within given line segments 149 141 if 0.0 <= u0 <= 1.0 and 0.0 <= u1 <= 1.0: 150 142 # We have intersection 151 return 1, array([x, y])143 return 1, num.array([x, y]) 152 144 else: 153 145 # No intersection … … 184 176 185 177 186 line0 = ensure_numeric(line0, Float)187 line1 = ensure_numeric(line1, Float)178 line0 = ensure_numeric(line0, num.Float) 179 line1 = ensure_numeric(line1, num.Float) 188 180 189 181 status, value = _intersection(line0[0,0], line0[0,1], … … 251 243 if len(points.shape) == 1: 252 244 # Only one point was passed in. Convert to array of points 253 points = reshape(points, (1,2))245 points = num.reshape(points, (1,2)) 254 246 255 247 indices, count = separate_points_by_polygon(points, polygon, … … 293 285 #if verbose: print 'Checking input to outside_polygon' 294 286 try: 295 points = ensure_numeric(points, Float)287 points = ensure_numeric(points, num.Float) 296 288 except NameError, e: 297 289 raise NameError, e … … 301 293 302 294 try: 303 polygon = ensure_numeric(polygon, Float)295 polygon = ensure_numeric(polygon, num.Float) 304 296 except NameError, e: 305 297 raise NameError, e … … 311 303 if len(points.shape) == 1: 312 304 # Only one point was passed in. Convert to array of points 313 points = reshape(points, (1,2))305 points = num.reshape(points, (1,2)) 314 306 315 307 indices, count = separate_points_by_polygon(points, polygon, … … 320 312 if count == len(indices): 321 313 # No points are outside 322 return array([])314 return num.array([]) 323 315 else: 324 316 return indices[count:][::-1] #return reversed … … 335 327 #if verbose: print 'Checking input to outside_polygon' 336 328 try: 337 points = ensure_numeric(points, Float)329 points = ensure_numeric(points, num.Float) 338 330 except NameError, e: 339 331 raise NameError, e … … 343 335 344 336 try: 345 polygon = ensure_numeric(polygon, Float)337 polygon = ensure_numeric(polygon, num.Float) 346 338 except NameError, e: 347 339 raise NameError, e … … 352 344 if len(points.shape) == 1: 353 345 # Only one point was passed in. Convert to array of points 354 points = reshape(points, (1,2))346 points = num.reshape(points, (1,2)) 355 347 356 348 … … 422 414 423 415 try: 424 points = ensure_numeric(points, Float)416 points = ensure_numeric(points, num.Float) 425 417 except NameError, e: 426 418 raise NameError, e … … 431 423 #if verbose: print 'Checking input to separate_points_by_polygon 2' 432 424 try: 433 polygon = ensure_numeric(polygon, Float)425 polygon = ensure_numeric(polygon, num.Float) 434 426 except NameError, e: 435 427 raise NameError, e … … 453 445 # Only one point was passed in. 454 446 # Convert to array of points 455 points = reshape(points, (1,2))447 points = num.reshape(points, (1,2)) 456 448 457 449 … … 472 464 473 465 474 indices = zeros( M,Int )466 indices = num.zeros( M, num.Int ) 475 467 476 468 count = _separate_points_by_polygon(points, polygon, indices, … … 607 599 608 600 try: 609 polygon = ensure_numeric(polygon, Float)601 polygon = ensure_numeric(polygon, num.Float) 610 602 except NameError, e: 611 603 raise NameError, e … … 616 608 x = polygon[:,0] 617 609 y = polygon[:,1] 618 x = concatenate((x, [polygon[0,0]]), axis = 0)619 y = concatenate((y, [polygon[0,1]]), axis = 0)610 x = num.concatenate((x, [polygon[0,0]]), axis = 0) 611 y = num.concatenate((y, [polygon[0,1]]), axis = 0) 620 612 621 613 return x, y … … 716 708 717 709 def __call__(self, x, y): 718 x = array(x).astype(Float)719 y = array(y).astype(Float)710 x = num.array(x).astype(num.Float) 711 y = num.array(y).astype(num.Float) 720 712 721 713 N = len(x) 722 714 assert len(y) == N 723 715 724 points = concatenate( (reshape(x, (N, 1)),725 reshape(y, (N, 1))), axis=1 )716 points = num.concatenate( (num.reshape(x, (N, 1)), 717 num.reshape(y, (N, 1))), axis=1 ) 726 718 727 719 if callable(self.default): 728 720 z = self.default(x,y) 729 721 else: 730 z = ones(N,Float) * self.default722 z = num.ones(N, num.Float) * self.default 731 723 732 724 for polygon, value in self.regions: … … 736 728 if callable(value): 737 729 for i in indices: 738 xx = array([x[i]])739 yy = array([y[i]])730 xx = num.array([x[i]]) 731 yy = num.array([y[i]]) 740 732 z[i] = value(xx, yy)[0] 741 733 else: -
anuga_core/source/anuga/utilities/quad.py
r5897 r6158 6 6 from treenode import TreeNode 7 7 import string, types, sys 8 8 9 9 10 #FIXME verts are added one at a time. … … 435 436 """ 436 437 437 from Numeric import minimum, maximum438 439 438 440 439 #Make root cell -
anuga_core/source/anuga/utilities/sparse.py
r5897 r6158 1 1 """Proof of concept sparse matrix code 2 2 """ 3 4 import Numeric as num 3 5 4 6 … … 17 19 18 20 if len(args) == 1: 19 from Numeric import array20 21 try: 21 A = array(args[0])22 A = num.array(args[0]) 22 23 except: 23 24 raise 'Input must be convertable to a Numeric array' … … 92 93 93 94 def todense(self): 94 from Numeric import zeros, Float 95 96 D = zeros( (self.M, self.N), Float) 95 D = num.zeros( (self.M, self.N), num.Float) 97 96 98 97 for i in range(self.M): … … 109 108 """ 110 109 111 from Numeric import array, zeros, Float112 113 110 try: 114 B = array(other)111 B = num.array(other) 115 112 except: 116 113 msg = 'FIXME: Only Numeric types implemented so far' … … 130 127 assert B.shape[0] == self.N, msg 131 128 132 R = zeros(self.M,Float) #Result129 R = num.zeros(self.M, num.Float) #Result 133 130 134 131 # Multiply nonzero elements … … 140 137 141 138 142 R = zeros((self.M, B.shape[1]),Float) #Result matrix139 R = num.zeros((self.M, B.shape[1]), num.Float) #Result matrix 143 140 144 141 # Multiply nonzero elements … … 162 159 """ 163 160 164 from Numeric import array, zeros, Float165 166 161 new = other.copy() 167 162 for key in self.Data.keys(): … … 177 172 """ 178 173 179 from Numeric import array, zeros, Float180 181 174 try: 182 175 other = float(other) … … 200 193 """ 201 194 202 from Numeric import array, zeros, Float203 204 195 try: 205 B = array(other)196 B = num.array(other) 206 197 except: 207 198 print 'FIXME: Only Numeric types implemented so far' … … 214 205 assert B.shape[0] == self.M, 'Mismatching dimensions' 215 206 216 R = zeros((self.N,),Float) #Result207 R = num.zeros((self.N,), num.Float) #Result 217 208 218 209 #Multiply nonzero elements … … 251 242 """ 252 243 253 from Numeric import array, Float, Int254 255 244 if isinstance(A,Sparse): 256 245 257 from Numeric import zeros258 246 keys = A.Data.keys() 259 247 keys.sort() 260 248 nnz = len(keys) 261 data = zeros ( (nnz,),Float)262 colind = zeros ( (nnz,),Int)263 row_ptr = zeros ( (A.M+1,),Int)249 data = num.zeros ( (nnz,), num.Float) 250 colind = num.zeros ( (nnz,), num.Int) 251 row_ptr = num.zeros ( (A.M+1,), num.Int) 264 252 current_row = -1 265 253 k = 0 … … 299 287 300 288 def todense(self): 301 from Numeric import zeros, Float 302 303 D = zeros( (self.M, self.N), Float) 289 D = num.zeros( (self.M, self.N), num.Float) 304 290 305 291 for i in range(self.M): … … 314 300 """ 315 301 316 from Numeric import array, zeros, Float317 318 302 try: 319 B = array(other)303 B = num.array(other) 320 304 except: 321 305 print 'FIXME: Only Numeric types implemented so far' … … 334 318 # A little selftest 335 319 336 from Numeric import allclose, array, Float337 338 320 A = Sparse(3,3) 339 321 … … 366 348 u = A*v 367 349 print u 368 assert allclose(u, [6,14,4])350 assert num.allclose(u, [6,14,4]) 369 351 370 352 u = A.trans_mult(v) 371 353 print u 372 assert allclose(u, [6,6,10])354 assert num.allclose(u, [6,6,10]) 373 355 374 356 #Right hand side column 375 v = array([[2,4],[3,4],[4,4]])357 v = num.array([[2,4],[3,4],[4,4]]) 376 358 377 359 u = A*v[:,0] 378 assert allclose(u, [6,14,4])360 assert num.allclose(u, [6,14,4]) 379 361 380 362 #u = A*v[:,1] -
anuga_core/source/anuga/utilities/test_cg_solve.py
r5897 r6158 6 6 7 7 8 from Numeric import dot, allclose, array, transpose, arange, ones, Float 8 import Numeric as num 9 9 from anuga.utilities.cg_solve import * 10 10 from anuga.utilities.cg_solve import _conjugate_gradient … … 30 30 x = conjugate_gradient(A,b,x,iprint=0) 31 31 32 assert allclose(x,xe)32 assert num.allclose(x,xe) 33 33 34 34 def test_max_iter(self): … … 61 61 A = Sparse(n,n) 62 62 63 for i in arange(0,n):63 for i in num.arange(0,n): 64 64 A[i,i] = 1.0 65 65 if i > 0 : … … 68 68 A[i,i+1] = -0.5 69 69 70 xe = ones( (n,),Float)70 xe = num.ones( (n,), num.Float) 71 71 72 72 b = A*xe 73 73 x = conjugate_gradient(A,b,b,tol=1.0e-5,iprint=1) 74 74 75 assert allclose(x,xe)75 assert num.allclose(x,xe) 76 76 77 77 def test_solve_large_2d(self): … … 83 83 A = Sparse(m*n, m*n) 84 84 85 for i in arange(0,n):86 for j in arange(0,m):85 for i in num.arange(0,n): 86 for j in num.arange(0,m): 87 87 I = j+m*i 88 88 A[I,I] = 4.0 … … 96 96 A[I,I+1] = -1.0 97 97 98 xe = ones( (n*m,),Float)98 xe = num.ones( (n*m,), num.Float) 99 99 100 100 b = A*xe 101 101 x = conjugate_gradient(A,b,b,iprint=0) 102 102 103 assert allclose(x,xe)103 assert num.allclose(x,xe) 104 104 105 105 def test_solve_large_2d_csr_matrix(self): … … 112 112 A = Sparse(m*n, m*n) 113 113 114 for i in arange(0,n):115 for j in arange(0,m):114 for i in num.arange(0,n): 115 for j in num.arange(0,m): 116 116 I = j+m*i 117 117 A[I,I] = 4.0 … … 125 125 A[I,I+1] = -1.0 126 126 127 xe = ones( (n*m,),Float)127 xe = num.ones( (n*m,), num.Float) 128 128 129 129 # Convert to csr format … … 134 134 x = conjugate_gradient(A,b,b,iprint=20) 135 135 136 assert allclose(x,xe)136 assert num.allclose(x,xe) 137 137 138 138 … … 145 145 A = Sparse(m*n, m*n) 146 146 147 for i in arange(0,n):148 for j in arange(0,m):147 for i in num.arange(0,n): 148 for j in num.arange(0,m): 149 149 I = j+m*i 150 150 A[I,I] = 4.0 … … 158 158 A[I,I+1] = -1.0 159 159 160 xe = ones( (n*m,),Float)160 xe = num.ones( (n*m,), num.Float) 161 161 162 162 b = A*xe 163 163 x = conjugate_gradient(A,b) 164 164 165 assert allclose(x,xe)165 assert num.allclose(x,xe) 166 166 167 167 … … 202 202 x = conjugate_gradient(A,b,x,iprint=0) 203 203 204 assert allclose(x,xe)204 assert num.allclose(x,xe) 205 205 206 206 #------------------------------------------------------------- -
anuga_core/source/anuga/utilities/test_data_audit.py
r5897 r6158 1 1 #!/usr/bin/env python 2 2 3 4 3 import unittest 5 from Numeric import zeros, array, allclose, Float6 4 from tempfile import mkstemp 7 5 import os 8 6 9 7 from data_audit import * 8 10 9 11 10 class Test_data_audit(unittest.TestCase): -
anuga_core/source/anuga/utilities/test_numerical_tools.py
r5897 r6158 3 3 4 4 import unittest 5 from Numeric import zeros, array, allclose 6 from Numeric import ArrayType, Float, Int, array, alltrue 5 import Numeric as num 7 6 8 7 from math import sqrt, pi … … 10 9 from numerical_tools import * 11 10 11 12 12 def test_function(x, y): 13 13 return x+y … … 23 23 """Test angles between one vector and the x-axis 24 24 """ 25 assert allclose(angle([1.0, 0.0])/pi*180, 0.0)26 assert allclose(angle([1.0, 1.0])/pi*180, 45.0)27 assert allclose(angle([0.0, 1.0])/pi*180, 90.0)28 assert allclose(angle([-1.0, 1.0])/pi*180, 135.0)29 assert allclose(angle([-1.0, 0.0])/pi*180, 180.0)30 assert allclose(angle([-1.0, -1.0])/pi*180, 225.0)31 assert allclose(angle([0.0, -1.0])/pi*180, 270.0)32 assert allclose(angle([1.0, -1.0])/pi*180, 315.0)25 assert num.allclose(angle([1.0, 0.0])/pi*180, 0.0) 26 assert num.allclose(angle([1.0, 1.0])/pi*180, 45.0) 27 assert num.allclose(angle([0.0, 1.0])/pi*180, 90.0) 28 assert num.allclose(angle([-1.0, 1.0])/pi*180, 135.0) 29 assert num.allclose(angle([-1.0, 0.0])/pi*180, 180.0) 30 assert num.allclose(angle([-1.0, -1.0])/pi*180, 225.0) 31 assert num.allclose(angle([0.0, -1.0])/pi*180, 270.0) 32 assert num.allclose(angle([1.0, -1.0])/pi*180, 315.0) 33 33 34 34 … … 37 37 """ 38 38 39 assert allclose(angle([1.0, 0.0], [1.0, 1.0])/pi*180, 315.0)40 assert allclose(angle([1.0, 1.0], [1.0, 0.0])/pi*180, 45.0)39 assert num.allclose(angle([1.0, 0.0], [1.0, 1.0])/pi*180, 315.0) 40 assert num.allclose(angle([1.0, 1.0], [1.0, 0.0])/pi*180, 45.0) 41 41 42 assert allclose(angle([-1.0, -1.0], [1.0, 1.0])/pi*180, 180)43 assert allclose(angle([-1.0, -1.0], [-1.0, 1.0])/pi*180, 90.0)42 assert num.allclose(angle([-1.0, -1.0], [1.0, 1.0])/pi*180, 180) 43 assert num.allclose(angle([-1.0, -1.0], [-1.0, 1.0])/pi*180, 90.0) 44 44 45 assert allclose(angle([-1.0, 0.0], [1.0, 1.0])/pi*180, 135.0)46 assert allclose(angle([0.0, -1.0], [1.0, 1.0])/pi*180, 225.0)45 assert num.allclose(angle([-1.0, 0.0], [1.0, 1.0])/pi*180, 135.0) 46 assert num.allclose(angle([0.0, -1.0], [1.0, 1.0])/pi*180, 225.0) 47 47 48 assert allclose(angle([1.0, -1.0], [1.0, 1.0])/pi*180, 270.0)49 assert allclose(angle([1.0, 0.0], [0.0, 1.0])/pi*180, 270.0)48 assert num.allclose(angle([1.0, -1.0], [1.0, 1.0])/pi*180, 270.0) 49 assert num.allclose(angle([1.0, 0.0], [0.0, 1.0])/pi*180, 270.0) 50 50 51 51 #From test_get_boundary_polygon_V 52 52 v_prev = [-0.5, -0.5] 53 53 vc = [ 0.0, -0.5] 54 assert allclose(angle(vc, v_prev)/pi*180, 45.0)54 assert num.allclose(angle(vc, v_prev)/pi*180, 45.0) 55 55 56 56 vc = [ 0.5, 0.0] 57 assert allclose(angle(vc, v_prev)/pi*180, 135.0)57 assert num.allclose(angle(vc, v_prev)/pi*180, 135.0) 58 58 59 59 vc = [ -0.5, 0.5] 60 assert allclose(angle(vc, v_prev)/pi*180, 270.0)60 assert num.allclose(angle(vc, v_prev)/pi*180, 270.0) 61 61 62 62 63 63 def test_anglediff(self): 64 assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0)64 assert num.allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0) 65 65 66 66 … … 68 68 A = [1,2,3,4] 69 69 B = ensure_numeric(A) 70 assert type(B) == ArrayType70 assert type(B) == num.ArrayType 71 71 assert B.typecode() == 'l' 72 72 assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4 … … 74 74 A = [1,2,3.14,4] 75 75 B = ensure_numeric(A) 76 assert type(B) == ArrayType76 assert type(B) == num.ArrayType 77 77 assert B.typecode() == 'd' 78 78 assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4 79 79 80 80 A = [1,2,3,4] 81 B = ensure_numeric(A, Float)82 assert type(B) == ArrayType81 B = ensure_numeric(A, num.Float) 82 assert type(B) == num.ArrayType 83 83 assert B.typecode() == 'd' 84 84 assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 85 85 86 86 A = [1,2,3,4] 87 B = ensure_numeric(A, Float)88 assert type(B) == ArrayType87 B = ensure_numeric(A, num.Float) 88 assert type(B) == num.ArrayType 89 89 assert B.typecode() == 'd' 90 90 assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 91 91 92 A = array([1,2,3,4])92 A = num.array([1,2,3,4]) 93 93 B = ensure_numeric(A) 94 assert type(B) == ArrayType94 assert type(B) == num.ArrayType 95 95 assert B.typecode() == 'l' 96 assert alltrue(A == B)96 assert num.alltrue(A == B) 97 97 assert A is B #Same object 98 98 99 A = array([1,2,3,4])100 B = ensure_numeric(A, Float)101 assert type(B) == ArrayType99 A = num.array([1,2,3,4]) 100 B = ensure_numeric(A, num.Float) 101 assert type(B) == num.ArrayType 102 102 assert B.typecode() == 'd' 103 assert alltrue(A == B)103 assert num.alltrue(A == B) 104 104 assert A is not B #Not the same object 105 105 106 106 # Check scalars 107 107 A = 1 108 B = ensure_numeric(A, Float)108 B = ensure_numeric(A, num.Float) 109 109 #print A, B[0], len(B), type(B) 110 110 #print B.shape 111 assert alltrue(A == B)112 113 B = ensure_numeric(A, Int)111 assert num.alltrue(A == B) 112 113 B = ensure_numeric(A, num.Int) 114 114 #print A, B 115 115 #print B.shape 116 assert alltrue(A == B)116 assert num.alltrue(A == B) 117 117 118 118 # Error situation 119 119 120 B = ensure_numeric('hello', Int)121 assert allclose(B, [104, 101, 108, 108, 111])120 B = ensure_numeric('hello', num.Int) 121 assert num.allclose(B, [104, 101, 108, 108, 111]) 122 122 123 123 … … 208 208 #There are four elements greater than or equal to 3 209 209 bins = [3] 210 assert allclose(histogram(a, bins), [4])210 assert num.allclose(histogram(a, bins), [4]) 211 211 212 212 bins = [ min(a) ] 213 assert allclose(histogram(a, bins), [len(a)])213 assert num.allclose(histogram(a, bins), [len(a)]) 214 214 215 215 bins = [ max(a)+0.00001 ] 216 assert allclose(histogram(a, bins), [0])216 assert num.allclose(histogram(a, bins), [0]) 217 217 218 218 bins = [1,2,3,4] 219 assert allclose(histogram(a, bins), [8,3,3,1])219 assert num.allclose(histogram(a, bins), [8,3,3,1]) 220 220 221 221 bins = [1.1,2,3.1,4] 222 222 #print histogram(a, bins) 223 assert allclose(histogram(a, bins), [0,6,0,1])223 assert num.allclose(histogram(a, bins), [0,6,0,1]) 224 224 225 225 bins = [0,1.5,2,3] 226 assert allclose(histogram(a, bins), [8,0,3,4])227 assert allclose(histogram(a, [0,3]), histogram(a, [-0.5,3]))226 assert num.allclose(histogram(a, bins), [8,0,3,4]) 227 assert num.allclose(histogram(a, [0,3]), histogram(a, [-0.5,3])) 228 228 229 229 # Check situation with #bins >= #datapoints 230 230 a = [1.7] 231 231 bins = [0,1.5,2,3] 232 assert allclose(histogram(a, bins), [0,1,0,0])232 assert num.allclose(histogram(a, bins), [0,1,0,0]) 233 233 234 234 a = [1.7] 235 235 bins = [0] 236 assert allclose(histogram(a, bins), [1])236 assert num.allclose(histogram(a, bins), [1]) 237 237 238 238 a = [-1.7] 239 239 bins = [0] 240 assert allclose(histogram(a, bins), [0])240 assert num.allclose(histogram(a, bins), [0]) 241 241 242 242 a = [-1.7] 243 243 bins = [-1.7] 244 assert allclose(histogram(a, bins), [1])244 assert num.allclose(histogram(a, bins), [1]) 245 245 246 246 -
anuga_core/source/anuga/utilities/test_polygon.py
r6000 r6158 3 3 4 4 import unittest 5 from Numeric import zeros, array, allclose 5 import Numeric as num 6 6 from math import sqrt, pi 7 7 from anuga.utilities.numerical_tools import ensure_numeric … … 45 45 f = Polygon_function( [(p1, 1.0)] ) 46 46 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 47 assert allclose(z, [1,1,0,0])47 assert num.allclose(z, [1,1,0,0]) 48 48 49 49 50 50 f = Polygon_function( [(p2, 2.0)] ) 51 51 z = f([5, 5, 27, 35], [5, 9, 8, -5]) # First and last inside p2 52 assert allclose(z, [2,0,0,2])52 assert num.allclose(z, [2,0,0,2]) 53 53 54 54 … … 56 56 f = Polygon_function( [(p1, 1.0), (p2, 2.0)] ) 57 57 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 58 assert allclose(z, [2,1,0,2])58 assert num.allclose(z, [2,1,0,2]) 59 59 60 60 def test_polygon_function_csvfile(self): … … 72 72 z = f([430000,480000], [490000,7720000]) # first outside, second inside 73 73 74 assert allclose(z, [0,10])74 assert num.allclose(z, [0,10]) 75 75 76 76 def test_polygon_function_georef(self): … … 90 90 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 91 91 92 assert allclose(z, [1,1,0,0])92 assert num.allclose(z, [1,1,0,0]) 93 93 94 94 95 95 f = Polygon_function( [(p2, 2.0)], geo_reference=geo) 96 96 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2 97 assert allclose(z, [2,0,0,2])97 assert num.allclose(z, [2,0,0,2]) 98 98 99 99 … … 101 101 f = Polygon_function( [(p1, 1.0), (p2, 2.0)], geo_reference=geo) 102 102 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 103 assert allclose(z, [2,1,0,2])103 assert num.allclose(z, [2,1,0,2]) 104 104 105 105 … … 107 107 f = Polygon_function( [(p1, 1.0), (p2, 2.0)]) 108 108 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 109 assert not allclose(z, [2,1,0,2])109 assert not num.allclose(z, [2,1,0,2]) 110 110 111 111 … … 120 120 f = Polygon_function( [(p1, test_function)] ) 121 121 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 122 assert allclose(z, [10,14,0,0])122 assert num.allclose(z, [10,14,0,0]) 123 123 124 124 # Combined 125 125 f = Polygon_function( [(p1, test_function), (p2, 2.0)] ) 126 126 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 127 assert allclose(z, [2,14,0,2])127 assert num.allclose(z, [2,14,0,2]) 128 128 129 129 … … 131 131 f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14) 132 132 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 133 assert allclose(z, [2,14,3.14,2])133 assert num.allclose(z, [2,14,3.14,2]) 134 134 135 135 … … 138 138 default = test_function) 139 139 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 140 assert allclose(z, [2,14,35,2])140 assert num.allclose(z, [2,14,35,2]) 141 141 142 142 … … 210 210 res = inside_polygon(points, polygon) 211 211 assert len(res) == 2 212 assert allclose(res, [0,1])212 assert num.allclose(res, [0,1]) 213 213 214 214 … … 305 305 res = inside_polygon( points, polygon, verbose=False ) 306 306 307 assert allclose( res, [0,1,2] )307 assert num.allclose( res, [0,1,2] ) 308 308 309 309 def test_outside_polygon(self): … … 317 317 318 318 indices = outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) 319 assert allclose( indices, [1] )319 assert num.allclose( indices, [1] ) 320 320 321 321 # One more test of vector formulation returning indices … … 324 324 res = outside_polygon( points, polygon ) 325 325 326 assert allclose( res, [3, 4] )326 assert num.allclose( res, [3, 4] ) 327 327 328 328 … … 332 332 res = outside_polygon( points, polygon ) 333 333 334 assert allclose( res, [0, 4, 5] )334 assert num.allclose( res, [0, 4, 5] ) 335 335 336 336 def test_outside_polygon2(self): … … 355 355 #print indices, count 356 356 assert count == 0 #None inside 357 assert allclose(indices, [3,2,1,0])357 assert num.allclose(indices, [3,2,1,0]) 358 358 359 359 indices = outside_polygon(points, U, closed = True) 360 assert allclose(indices, [0,1,2,3])360 assert num.allclose(indices, [0,1,2,3]) 361 361 362 362 indices = inside_polygon(points, U, closed = True) 363 assert allclose(indices, [])363 assert num.allclose(indices, []) 364 364 365 365 … … 375 375 indices, count = separate_points_by_polygon(points, U) 376 376 assert count == 3 #All inside 377 assert allclose(indices, [0,1,2])377 assert num.allclose(indices, [0,1,2]) 378 378 379 379 indices = outside_polygon(points, U, closed = True) 380 assert allclose(indices, [])380 assert num.allclose(indices, []) 381 381 382 382 indices = inside_polygon(points, U, closed = True) 383 assert allclose(indices, [0,1,2])383 assert num.allclose(indices, [0,1,2]) 384 384 385 385 … … 388 388 389 389 indices, count = separate_points_by_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) 390 assert allclose( indices, [0,2,1] )390 assert num.allclose( indices, [0,2,1] ) 391 391 assert count == 2 392 392 … … 396 396 res, count = separate_points_by_polygon( points, polygon ) 397 397 398 assert allclose( res, [0,1,2,4,3] )398 assert num.allclose( res, [0,1,2,4,3] ) 399 399 assert count == 3 400 400 … … 404 404 res, count = separate_points_by_polygon( points, polygon ) 405 405 406 assert allclose( res, [1,2,3,5,4,0] )406 assert num.allclose( res, [1,2,3,5,4,0] ) 407 407 assert count == 3 408 408 … … 588 588 status, value = intersection(line0, line1) 589 589 assert status == 1 590 assert allclose(value, [0.0, 0.0])590 assert num.allclose(value, [0.0, 0.0]) 591 591 592 592 def test_intersection2(self): … … 596 596 status, value = intersection(line0, line1) 597 597 assert status == 1 598 assert allclose(value, [12.0, 6.0])598 assert num.allclose(value, [12.0, 6.0]) 599 599 600 600 # Swap direction of one line … … 603 603 status, value = intersection(line0, line1) 604 604 assert status == 1 605 assert allclose(value, [12.0, 6.0])605 assert num.allclose(value, [12.0, 6.0]) 606 606 607 607 # Swap order of lines 608 608 status, value = intersection(line1, line0) 609 609 assert status == 1 610 assert allclose(value, [12.0, 6.0])610 assert num.allclose(value, [12.0, 6.0]) 611 611 612 612 def test_intersection3(self): … … 616 616 status, value = intersection(line0, line1) 617 617 assert status == 1 618 assert allclose(value, [14.068965517, 7.0344827586])618 assert num.allclose(value, [14.068965517, 7.0344827586]) 619 619 620 620 # Swap direction of one line … … 623 623 status, value = intersection(line0, line1) 624 624 assert status == 1 625 assert allclose(value, [14.068965517, 7.0344827586])625 assert num.allclose(value, [14.068965517, 7.0344827586]) 626 626 627 627 # Swap order of lines 628 628 status, value = intersection(line1, line0) 629 629 assert status == 1 630 assert allclose(value, [14.068965517, 7.0344827586])630 assert num.allclose(value, [14.068965517, 7.0344827586]) 631 631 632 632 … … 641 641 status, value = intersection(line0, line1) 642 642 assert status == 1 643 assert allclose(value, [1.0, 1.0])643 assert num.allclose(value, [1.0, 1.0]) 644 644 645 645 … … 649 649 status, value = intersection(line0, line1) 650 650 assert status == 1 651 assert allclose(value, [1.0, 1.0])651 assert num.allclose(value, [1.0, 1.0]) 652 652 653 653 … … 713 713 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 714 714 (str(status), str(value))) 715 self.failUnless( allclose(value, line0))715 self.failUnless(num.allclose(value, line0)) 716 716 717 717 # line0 fully within line1, same direction … … 725 725 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 726 726 (str(status), str(value))) 727 self.failUnless( allclose(value, line0))727 self.failUnless(num.allclose(value, line0)) 728 728 729 729 # line0 fully within line1, opposite direction … … 737 737 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 738 738 (str(status), str(value))) 739 self.failUnless( allclose(value, line0))739 self.failUnless(num.allclose(value, line0)) 740 740 741 741 # line0 fully within line1, opposite direction … … 749 749 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 750 750 (str(status), str(value))) 751 self.failUnless( allclose(value, line0))751 self.failUnless(num.allclose(value, line0)) 752 752 753 753 #---------------------------------------------------------------------- … … 763 763 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 764 764 (str(status), str(value))) 765 self.failUnless( allclose(value, line1))765 self.failUnless(num.allclose(value, line1)) 766 766 767 767 # line1 fully within line0, same direction … … 775 775 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 776 776 (str(status), str(value))) 777 self.failUnless( allclose(value, line1))777 self.failUnless(num.allclose(value, line1)) 778 778 779 779 # line1 fully within line0, opposite direction … … 787 787 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 788 788 (str(status), str(value))) 789 self.failUnless( allclose(value, line1))789 self.failUnless(num.allclose(value, line1)) 790 790 791 791 # line1 fully within line0, opposite direction … … 799 799 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 800 800 (str(status), str(value))) 801 self.failUnless( allclose(value, line1))801 self.failUnless(num.allclose(value, line1)) 802 802 803 803 #---------------------------------------------------------------------- … … 813 813 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 814 814 (str(status), str(value))) 815 self.failUnless( allclose(value, [line1[0],line0[1]]))815 self.failUnless(num.allclose(value, [line1[0],line0[1]])) 816 816 817 817 # line in same direction, partial overlap … … 825 825 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 826 826 (str(status), str(value))) 827 self.failUnless( allclose(value, [line0[0],line1[1]]))827 self.failUnless(num.allclose(value, [line0[0],line1[1]])) 828 828 829 829 # line in opposite direction, partial overlap … … 837 837 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 838 838 (str(status), str(value))) 839 self.failUnless( allclose(value, [line0[0],line1[0]]))839 self.failUnless(num.allclose(value, [line0[0],line1[0]])) 840 840 841 841 # line in opposite direction, partial overlap … … 849 849 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 850 850 (str(status), str(value))) 851 self.failUnless( allclose(value, [line1[1],line0[1]]))851 self.failUnless(num.allclose(value, [line1[1],line0[1]])) 852 852 853 853 #---------------------------------------------------------------------- … … 863 863 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 864 864 (str(status), str(value))) 865 self.failUnless( allclose(value, [line0[0],line1[1]]))865 self.failUnless(num.allclose(value, [line0[0],line1[1]])) 866 866 867 867 # line in same direction, partial overlap … … 875 875 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 876 876 (str(status), str(value))) 877 self.failUnless( allclose(value, [line1[0],line0[1]]))877 self.failUnless(num.allclose(value, [line1[0],line0[1]])) 878 878 879 879 # line in opposite direction, partial overlap … … 887 887 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 888 888 (str(status), str(value))) 889 self.failUnless( allclose(value, [line1[1],line0[1]]))889 self.failUnless(num.allclose(value, [line1[1],line0[1]])) 890 890 891 891 # line in opposite direction, partial overlap … … 899 899 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 900 900 (str(status), str(value))) 901 self.failUnless( allclose(value, [line0[0],line1[0]]))901 self.failUnless(num.allclose(value, [line0[0],line1[0]])) 902 902 903 903 #---------------------------------------------------------------------- … … 913 913 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 914 914 (str(status), str(value))) 915 self.failUnless( allclose(value, line0))915 self.failUnless(num.allclose(value, line0)) 916 916 917 917 # line in same direction, same left point, line1 longer … … 925 925 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 926 926 (str(status), str(value))) 927 self.failUnless( allclose(value, line0))927 self.failUnless(num.allclose(value, line0)) 928 928 929 929 # line in opposite direction, same left point, line1 longer … … 937 937 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 938 938 (str(status), str(value))) 939 self.failUnless( allclose(value, line0))939 self.failUnless(num.allclose(value, line0)) 940 940 941 941 # line in opposite direction, same start point, line1 longer … … 949 949 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 950 950 (str(status), str(value))) 951 self.failUnless( allclose(value, line0))951 self.failUnless(num.allclose(value, line0)) 952 952 953 953 #---------------------------------------------------------------------- … … 963 963 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 964 964 (str(status), str(value))) 965 self.failUnless( allclose(value, line0))965 self.failUnless(num.allclose(value, line0)) 966 966 967 967 # line in same direction, same left point, same right point … … 975 975 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 976 976 (str(status), str(value))) 977 self.failUnless( allclose(value, line0))977 self.failUnless(num.allclose(value, line0)) 978 978 979 979 # line in opposite direction, same left point, same right point … … 987 987 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 988 988 (str(status), str(value))) 989 self.failUnless( allclose(value, line0))989 self.failUnless(num.allclose(value, line0)) 990 990 991 991 # line in opposite direction, same left point, same right point … … 999 999 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1000 1000 (str(status), str(value))) 1001 self.failUnless( allclose(value, line0))1001 self.failUnless(num.allclose(value, line0)) 1002 1002 1003 1003 #---------------------------------------------------------------------- … … 1013 1013 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1014 1014 (str(status), str(value))) 1015 self.failUnless( allclose(value, line0))1015 self.failUnless(num.allclose(value, line0)) 1016 1016 1017 1017 # line in same direction, same right point, line1 longer … … 1025 1025 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1026 1026 (str(status), str(value))) 1027 self.failUnless( allclose(value, line0))1027 self.failUnless(num.allclose(value, line0)) 1028 1028 1029 1029 # line in opposite direction, same right point, line1 longer … … 1037 1037 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1038 1038 (str(status), str(value))) 1039 self.failUnless( allclose(value, line0))1039 self.failUnless(num.allclose(value, line0)) 1040 1040 1041 1041 # line in opposite direction, same right point, line1 longer … … 1049 1049 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1050 1050 (str(status), str(value))) 1051 self.failUnless( allclose(value, line0))1051 self.failUnless(num.allclose(value, line0)) 1052 1052 1053 1053 #---------------------------------------------------------------------- … … 1063 1063 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1064 1064 (str(status), str(value))) 1065 self.failUnless( allclose(value, line1))1065 self.failUnless(num.allclose(value, line1)) 1066 1066 1067 1067 # line in same direction, same left point, line0 longer … … 1075 1075 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1076 1076 (str(status), str(value))) 1077 self.failUnless( allclose(value, line1))1077 self.failUnless(num.allclose(value, line1)) 1078 1078 1079 1079 # line in opposite direction, same left point, line0 longer … … 1087 1087 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1088 1088 (str(status), str(value))) 1089 self.failUnless( allclose(value, line1))1089 self.failUnless(num.allclose(value, line1)) 1090 1090 1091 1091 # line in opposite direction, same left point, line0 longer … … 1099 1099 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1100 1100 (str(status), str(value))) 1101 self.failUnless( allclose(value, line1))1101 self.failUnless(num.allclose(value, line1)) 1102 1102 1103 1103 #---------------------------------------------------------------------- … … 1113 1113 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1114 1114 (str(status), str(value))) 1115 self.failUnless( allclose(value, line1))1115 self.failUnless(num.allclose(value, line1)) 1116 1116 1117 1117 # line in same direction, same right point, line0 longer … … 1125 1125 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1126 1126 (str(status), str(value))) 1127 self.failUnless( allclose(value, line1))1127 self.failUnless(num.allclose(value, line1)) 1128 1128 1129 1129 # line in opposite direction, same right point, line0 longer … … 1137 1137 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1138 1138 (str(status), str(value))) 1139 self.failUnless( allclose(value, line1))1139 self.failUnless(num.allclose(value, line1)) 1140 1140 1141 1141 # line in opposite direction, same right point, line0 longer … … 1149 1149 self.failIf(status!=2, 'Expected status 2, got status=%s, value=%s' % 1150 1150 (str(status), str(value))) 1151 self.failUnless( allclose(value, line1))1151 self.failUnless(num.allclose(value, line1)) 1152 1152 1153 1153 … … 1374 1374 common_end_point[1]) 1375 1375 msg += ' gave %s, \nbut when reversed we got %s' %(p1, p2) 1376 assert allclose(p1, p2), msg1376 assert num.allclose(p1, p2), msg 1377 1377 1378 1378 # Swap order of lines … … 1380 1380 assert status == 1 1381 1381 msg = 'Order of lines gave different results' 1382 assert allclose(p1, p3), msg1382 assert num.allclose(p1, p3), msg 1383 1383 1384 1384 … … 1420 1420 status, value = intersection(line0, line1) 1421 1421 assert status == 2 1422 assert allclose(value, [[0,0], [3,0]])1422 assert num.allclose(value, [[0,0], [3,0]]) 1423 1423 1424 1424 # Overlap 2 … … 1428 1428 status, value = intersection(line0, line1) 1429 1429 assert status == 2 1430 assert allclose(value, [[-3, 0], [5,0]])1430 assert num.allclose(value, [[-3, 0], [5,0]]) 1431 1431 1432 1432 # Inclusion 1 … … 1436 1436 status, value = intersection(line0, line1) 1437 1437 assert status == 2 1438 assert allclose(value, line1)1438 assert num.allclose(value, line1) 1439 1439 1440 1440 # Inclusion 2 … … 1444 1444 status, value = intersection(line0, line1) 1445 1445 assert status == 2 1446 assert allclose(value, line0)1446 assert num.allclose(value, line0) 1447 1447 1448 1448 … … 1463 1463 status, value = intersection(line0, line1) 1464 1464 assert status == 2 1465 assert allclose(value, [[1, 7], [7, 19]])1465 assert num.allclose(value, [[1, 7], [7, 19]]) 1466 1466 1467 1467 status, value = intersection(line1, line0) 1468 1468 assert status == 2 1469 assert allclose(value, [[1, 7], [7, 19]])1469 assert num.allclose(value, [[1, 7], [7, 19]]) 1470 1470 1471 1471 # Swap direction … … 1474 1474 status, value = intersection(line0, line1) 1475 1475 assert status == 2 1476 assert allclose(value, [[7, 19], [1, 7]])1476 assert num.allclose(value, [[7, 19], [1, 7]]) 1477 1477 1478 1478 line0 = [[0,5], [7,19]] … … 1480 1480 status, value = intersection(line0, line1) 1481 1481 assert status == 2 1482 assert allclose(value, [[1, 7], [7, 19]])1482 assert num.allclose(value, [[1, 7], [7, 19]]) 1483 1483 1484 1484 … … 1488 1488 status, value = intersection(line0, line1) 1489 1489 assert status == 2 1490 assert allclose(value, [[1,7], [7, 19]])1490 assert num.allclose(value, [[1,7], [7, 19]]) 1491 1491 1492 1492 line0 = [[0,5], [10,25]] … … 1494 1494 status, value = intersection(line0, line1) 1495 1495 assert status == 2 1496 assert allclose(value, [[1,7], [7, 19]])1496 assert num.allclose(value, [[1,7], [7, 19]]) 1497 1497 1498 1498 … … 1501 1501 status, value = intersection(line0, line1) 1502 1502 assert status == 2 1503 assert allclose(value, [[7, 19], [1, 7]])1503 assert num.allclose(value, [[7, 19], [1, 7]]) 1504 1504 1505 1505 … … 1538 1538 res = inside_polygon(points, polygon) 1539 1539 assert len(res) == 2 1540 assert allclose(res, [0,1])1540 assert num.allclose(res, [0,1]) 1541 1541 1542 1542 def test_polygon_area(self): -
anuga_core/source/anuga/utilities/test_quad.py
r5897 r6158 1 1 import unittest 2 from Numeric import array, allclose 2 import Numeric as num 3 3 4 4 from quad import Cell, build_quadtree … … 236 236 [ 0., -1.]] 237 237 238 # assert allclose(array(results),[[[ 2., 1.],238 # assert num.allclose(num.array(results),[[[ 2., 1.], 239 239 #[ 4., 1.], [ 4., 4.]], [[ 4., 1.],[ 5., 4.],[ 4., 4.]]] ) 240 240 results = Q.search(5,4.) -
anuga_core/source/anuga/utilities/test_sparse.py
r5897 r6158 5 5 6 6 from sparse import * 7 from Numeric import allclose, array, transpose, Float 7 import Numeric as num 8 8 9 9 10 class Test_Sparse(unittest.TestCase): … … 43 44 C = Sparse(B) 44 45 45 assert allclose(C.todense(), B)46 assert num.allclose(C.todense(), B) 46 47 47 48 … … 50 51 A[1,1] = 4 51 52 52 assert allclose(A.todense(), [[0,0,0], [0,4,0], [0,0,0], [0,0,0]])53 assert num.allclose(A.todense(), [[0,0,0], [0,4,0], [0,0,0], [0,0,0]]) 53 54 54 55 … … 63 64 64 65 assert len(A) == 0 65 assert allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]])66 assert num.allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]]) 66 67 67 68 #Set an existing zero element to zero 68 69 A[1,2] = 0 69 70 assert len(A) == 0 70 assert allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]])71 assert num.allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]]) 71 72 72 73 def test_sparse_multiplication_vector(self): … … 82 83 83 84 u = A*v 84 assert allclose(u, [6,14,4])85 assert num.allclose(u, [6,14,4]) 85 86 86 87 #Right hand side column 87 v = array([[2,4],[3,4],[4,4]])88 v = num.array([[2,4],[3,4],[4,4]]) 88 89 89 90 u = A*v[:,0] 90 assert allclose(u, [6,14,4])91 assert num.allclose(u, [6,14,4]) 91 92 92 93 u = A*v[:,1] 93 assert allclose(u, [12,16,4])94 assert num.allclose(u, [12,16,4]) 94 95 95 96 … … 103 104 104 105 #Right hand side matrix 105 v = array([[2,4],[3,4],[4,4]])106 v = num.array([[2,4],[3,4],[4,4]]) 106 107 107 108 u = A*v 108 assert allclose(u, [[6,12], [14,16], [4,4]])109 assert num.allclose(u, [[6,12], [14,16], [4,4]]) 109 110 110 111 … … 122 123 123 124 u = A.trans_mult(v) 124 assert allclose(u, [6,6,10])125 assert num.allclose(u, [6,6,10]) 125 126 126 127 … … 137 138 138 139 B = 3*A 139 assert allclose(B.todense(), 3*A.todense())140 assert num.allclose(B.todense(), 3*A.todense()) 140 141 141 142 B = A*3 142 assert allclose(B.todense(), 3*A.todense())143 assert num.allclose(B.todense(), 3*A.todense()) 143 144 144 145 try: … … 166 167 C = A+B 167 168 168 assert allclose(C.todense(), [[12,0,0], [2,8,8], [0,0,4]])169 assert num.allclose(C.todense(), [[12,0,0], [2,8,8], [0,0,4]]) 169 170 170 171 def test_sparse_tocsr(self): … … 190 191 C = [1, 2, 3] 191 192 192 assert allclose(B*C, [15.0, 10.0 ,8.0, 0.0])193 assert num.allclose(B*C, [15.0, 10.0 ,8.0, 0.0]) 193 194 194 195 C2 = [[1,2],[2,4],[3,6]] … … 196 197 #print B*C2 197 198 198 assert allclose(B*C2, [[15.0, 30.0],[10.0, 20.0],[8.0, 16.0],[0.0, 0.0]])199 assert num.allclose(B*C2, [[15.0, 30.0],[10.0, 20.0],[8.0, 16.0],[0.0, 0.0]]) 199 200 200 201 -
anuga_core/source/anuga/utilities/test_system_tools.py
r6086 r6158 3 3 4 4 import unittest 5 from Numeric import zeros, array, allclose, Float 5 import Numeric as num 6 6 import zlib 7 7 from os.path import join, split, sep … … 82 82 pass 83 83 else: 84 test_array = array([[7.0, 3.14], [-31.333, 0.0]])84 test_array = num.array([[7.0, 3.14], [-31.333, 0.0]]) 85 85 86 86 # First file … … 88 88 fid = NetCDFFile(filename1, netcdf_mode_w) 89 89 fid.createDimension('two', 2) 90 fid.createVariable('test_array', Float,90 fid.createVariable('test_array', num.Float, 91 91 ('two', 'two')) 92 92 fid.variables['test_array'][:] = test_array … … 97 97 fid = NetCDFFile(filename2, netcdf_mode_w) 98 98 fid.createDimension('two', 2) 99 fid.createVariable('test_array', Float,99 fid.createVariable('test_array', num.Float, 100 100 ('two', 'two')) 101 101 fid.variables['test_array'][:] = test_array -
anuga_core/source/anuga/utilities/test_xml_tools.py
r5897 r6158 3 3 4 4 import unittest 5 from Numeric import zeros, array, allclose, Float6 5 from tempfile import mkstemp, mktemp 7 6
Note: See TracChangeset
for help on using the changeset viewer.