Changeset 5902
- Timestamp:
- Nov 6, 2008, 3:32:23 PM (16 years ago)
- Location:
- anuga_core/source_numpy_conversion/anuga/utilities
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source_numpy_conversion/anuga/utilities/cg_solve.py
r5889 r5902 4 4 class ConvergenceError(exceptions.Exception): pass 5 5 6 ##from numpy.oldnumeric import dot, array, Float, zeros 7 from numpy import dot, array, float, zeros 6 import numpy 8 7 9 8 import logging, logging.config … … 26 25 27 26 if x0 is None: 28 x0 = zeros(b.shape, dtype=float)27 x0 = numpy.zeros(b.shape, dtype=numpy.float) 29 28 else: 30 x0 = array(x0, dtype=float)29 x0 = numpy.array(x0, dtype=numpy.float) 31 30 32 b = array(b, dtype=float)31 b = numpy.array(b, dtype=numpy.float) 33 32 if len(b.shape) != 1 : 34 33 … … 59 58 60 59 61 b = array(b, dtype=float)60 b = numpy.array(b, dtype=numpy.float) 62 61 if len(b.shape) != 1 : 63 62 raise VectorShapeError, 'input vector should consist of only one column' 64 63 65 64 if x0 is None: 66 x0 = zeros(b.shape, dtype=float)65 x0 = numpy.zeros(b.shape, dtype=numpy.float) 67 66 else: 68 x0 = array(x0, dtype=float)67 x0 = numpy.array(x0, dtype=numpy.float) 69 68 70 69 … … 77 76 r = b - A*x 78 77 d = r 79 rTr = dot(r,r)78 rTr = numpy.dot(r,r) 80 79 rTr0 = rTr 81 80 82 81 while (i<imax and rTr>tol**2*rTr0): 83 82 q = A*d 84 alpha = rTr/ dot(d,q)83 alpha = rTr/numpy.dot(d,q) 85 84 x = x + alpha*d 86 85 if i%50 : … … 89 88 r = r - alpha*q 90 89 rTrOld = rTr 91 rTr = dot(r,r)90 rTr = numpy.dot(r,r) 92 91 bt = rTr/rTrOld 93 92 -
anuga_core/source_numpy_conversion/anuga/utilities/compile.py
r5889 r5902 12 12 13 13 #NumPy ------------------------------------ 14 # these lines recommended in "Converting from NUMARRAY to NUMPY"14 # Something like these lines recommended in "Converting from NUMARRAY to NUMPY" 15 15 import numpy 16 import numpy.numarray as nn17 16 numpyincludedirs = numpy.get_include() 18 numarrayincludedirs = nn.get_numarray_include_dirs()19 17 I_dirs = '-I"%s" ' % numpyincludedirs 20 for d in numarrayincludedirs:21 I_dirs += '-I"%s" ' % d22 18 #NumPy ------------------------------------ 23 19 -
anuga_core/source_numpy_conversion/anuga/utilities/interp.py
r5889 r5902 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/python -tt 4 2 #======================================================================= … … 140 138 """ 141 139 import arrayfns 142 ## import numpy.oldnumeric.ma as MA143 ## import numpy.oldnumeric as N144 140 import numpy.ma as MA 145 141 import numpy as N -
anuga_core/source_numpy_conversion/anuga/utilities/numerical_tools.py
r5889 r5902 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 numpy.oldnumeric import ArrayType, array, sum, innerproduct, ravel, sqrt, 17 #searchsorted, sort, concatenate, Float, arange 18 19 ##from numpy.oldnumeric import ArrayType, array, sum, innerproduct, ravel, sqrt,\ 20 ## searchsorted, sort, concatenate, Float, arange 21 from numpy import ndarray, array, sum, inner, ravel, sqrt, searchsorted, sort, concatenate, float, arange 9 ##from numpy import ndarray, array, sum, inner, ravel, sqrt, searchsorted, sort, concatenate, float, arange 10 import numpy 22 11 23 12 # Getting an infinite number to use when using Numeric 24 13 #INF = (array([1])/0.)[0] 25 14 26 NAN = ( array([1])/0.)[0]15 NAN = (numpy.array([1])/0.)[0] 27 16 # Note, INF is used instead of NAN (Not a number), since Numeric has no NAN 28 17 # if we use a package that has NAN, this should be updated to use NAN. … … 90 79 v2 = [1.0, 0.0] # Unit vector along the x-axis 91 80 92 v1 = ensure_numeric(v1, float)93 v2 = ensure_numeric(v2, float)81 v1 = ensure_numeric(v1, numpy.float) 82 v2 = ensure_numeric(v2, numpy.float) 94 83 95 84 # Normalise 96 v1 = v1/ sqrt(sum(v1**2))97 v2 = v2/ sqrt(sum(v2**2))85 v1 = v1/numpy.sqrt(numpy.sum(v1**2)) 86 v2 = v2/numpy.sqrt(numpy.sum(v2**2)) 98 87 99 88 # Compute angle 100 p = inner(v1, v2)101 c = inner(v1, normal_vector(v2)) # Projection onto normal89 p = numpy.inner(v1, v2) 90 c = numpy.inner(v1, normal_vector(v2)) # Projection onto normal 102 91 # (negative cross product) 103 92 … … 141 130 """ 142 131 143 return array([-v[1], v[0]],float)132 return numpy.array([-v[1], v[0]], numpy.float) 144 133 145 134 … … 151 140 """Mean value of a vector 152 141 """ 153 return(float( sum(x))/len(x))142 return(float(numpy.sum(x))/len(x)) 154 143 155 144 … … 172 161 cy = y - mean(y) 173 162 174 p = inner(cx,cy)/N163 p = numpy.inner(cx,cy)/N 175 164 return(p) 176 165 … … 221 210 """ 222 211 223 y = ravel(x)224 p = sqrt(inner(y,y))212 y = numpy.ravel(x) 213 p = numpy.sqrt(numpy.inner(y,y)) 225 214 return p 226 215 … … 264 253 if typecode is None: 265 254 ##NumPy if isinstance(A, ArrayType): 266 if type(A) == n darray:255 if type(A) == numpy.ndarray: 267 256 return A 268 257 else: 269 return array(A)258 return numpy.array(A) 270 259 else: 271 260 ##NumPy if isinstance(A, ArrayType): 272 if type(A) == n darray:261 if type(A) == numpy.ndarray: 273 262 ##NumPy if A.typecode == typecode: 274 263 if A.dtype == typecode: 275 return array(A) #FIXME: Shouldn't this just return A?264 return numpy.array(A) #FIXME: Shouldn't this just return A? 276 265 else: 277 return array(A, typecode)266 return numpy.array(A, typecode) 278 267 else: 279 268 import types ## 280 269 from numpy import str ## 281 270 if isinstance(A, types.StringType): ## 282 return array(A, dtype=int) ##283 return array(A, typecode)271 return numpy.array(A, dtype=int) ## 272 return numpy.array(A, typecode) 284 273 285 274 … … 292 281 """ 293 282 294 n = searchsorted(sort(a), bins)295 n = concatenate( [n, [len(a)]] )283 n = numpy.searchsorted(numpy.sort(a), bins) 284 n = numpy.concatenate( [n, [len(a)]] ) 296 285 297 286 hist = n[1:]-n[:-1] 298 287 299 288 if relative is True: 300 hist = hist/float( sum(hist))289 hist = hist/float(numpy.sum(hist)) 301 290 302 291 return hist … … 308 297 """ 309 298 310 mx = max(data)311 mn = min(data)299 mx = numpy.max(data) 300 mn = numpy.min(data) 312 301 313 302 if mx == mn: 314 bins = array([mn])303 bins = numpy.array([mn]) 315 304 else: 316 305 if number_of_bins is None: 317 306 number_of_bins = 10 318 307 319 bins = arange(mn, mx, (mx-mn)/number_of_bins)308 bins = numpy.arange(mn, mx, (mx-mn)/number_of_bins) 320 309 321 310 return bins -
anuga_core/source_numpy_conversion/anuga/utilities/polygon.py
r5889 r5902 10 10 # #print 'Could not find scipy - using Numeric' 11 11 12 from numpy import float, int, zeros, ones, array, concatenate, reshape, dot, allclose, newaxis, ascontiguousarray 12 ##from numpy import float, int, zeros, ones, array, concatenate, reshape, dot, allclose, newaxis, ascontiguousarray 13 import numpy 13 14 14 15 … … 76 77 # FIXME (Ole): Write this in C 77 78 78 line0 = ensure_numeric(line0, float)79 line1 = ensure_numeric(line1, float)79 line0 = ensure_numeric(line0, numpy.float) 80 line1 = ensure_numeric(line1, numpy.float) 80 81 81 82 x0 = line0[0,0]; y0 = line0[0,1] … … 89 90 u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0) 90 91 91 if allclose(denom, 0.0):92 if numpy.allclose(denom, 0.0): 92 93 # Lines are parallel - check if they coincide on a shared a segment 93 94 94 if allclose( [u0, u1], 0.0 ):95 if numpy.allclose( [u0, u1], 0.0 ): 95 96 # We now know that the lines if continued coincide 96 97 # The remaining check will establish if the finite lines share a segment … … 120 121 if line0_starts_on_line1 and line0_ends_on_line1: 121 122 # Shared segment is line0 fully included in line1 122 segment = array([[x0, y0], [x1, y1]])123 segment = numpy.array([[x0, y0], [x1, y1]]) 123 124 124 125 if line1_starts_on_line0 and line1_ends_on_line0: 125 126 # Shared segment is line1 fully included in line0 126 segment = array([[x2, y2], [x3, y3]])127 segment = numpy.array([[x2, y2], [x3, y3]]) 127 128 128 129 … … 130 131 if line0_starts_on_line1 and line1_ends_on_line0: 131 132 # Shared segment from line0 start to line 1 end 132 segment = array([[x0, y0], [x3, y3]])133 segment = numpy.array([[x0, y0], [x3, y3]]) 133 134 134 135 if line1_starts_on_line0 and line0_ends_on_line1: 135 136 # Shared segment from line1 start to line 0 end 136 segment = array([[x2, y2], [x1, y1]])137 segment = numpy.array([[x2, y2], [x1, y1]]) 137 138 138 139 … … 140 141 if line0_starts_on_line1 and line1_starts_on_line0: 141 142 # Shared segment from line0 start to line 1 end 142 segment = array([[x0, y0], [x2, y2]])143 segment = numpy.array([[x0, y0], [x2, y2]]) 143 144 144 145 if line0_ends_on_line1 and line1_ends_on_line0: 145 146 # Shared segment from line0 start to line 1 end 146 segment = array([[x3, y3], [x1, y1]])147 segment = numpy.array([[x3, y3], [x1, y1]]) 147 148 148 149 … … 161 162 162 163 # Sanity check - can be removed to speed up if needed 163 assert allclose(x, x2 + u1*(x3-x2))164 assert allclose(y, y2 + u1*(y3-y2))164 assert numpy.allclose(x, x2 + u1*(x3-x2)) 165 assert numpy.allclose(y, y2 + u1*(y3-y2)) 165 166 166 167 # Check if point found lies within given line segments … … 168 169 # We have intersection 169 170 170 return 1, array([x, y])171 return 1, numpy.array([x, y]) 171 172 else: 172 173 # No intersection … … 203 204 204 205 205 line0 = ensure_numeric(line0, float)206 line1 = ensure_numeric(line1, float)206 line0 = ensure_numeric(line0, numpy.float) 207 line1 = ensure_numeric(line1, numpy.float) 207 208 208 209 status, value = _intersection(line0[0,0], line0[0,1], … … 270 271 if len(points.shape) == 1: 271 272 # Only one point was passed in. Convert to array of points 272 points = reshape(points, (1,2))273 points = numpy.reshape(points, (1,2)) 273 274 274 275 indices, count = separate_points_by_polygon(points, polygon, … … 312 313 #if verbose: print 'Checking input to outside_polygon' 313 314 try: 314 points = ensure_numeric(points, float)315 points = ensure_numeric(points, numpy.float) 315 316 except NameError, e: 316 317 raise NameError, e … … 320 321 321 322 try: 322 polygon = ensure_numeric(polygon, float)323 polygon = ensure_numeric(polygon, numpy.float) 323 324 except NameError, e: 324 325 raise NameError, e … … 330 331 if len(points.shape) == 1: 331 332 # Only one point was passed in. Convert to array of points 332 points = reshape(points, (1,2))333 points = numpy.reshape(points, (1,2)) 333 334 334 335 indices, count = separate_points_by_polygon(points, polygon, … … 339 340 if count == len(indices): 340 341 # No points are outside 341 return array([])342 return numpy.array([]) 342 343 else: 343 344 return indices[count:][::-1] #return reversed … … 354 355 #if verbose: print 'Checking input to outside_polygon' 355 356 try: 356 points = ensure_numeric(points, float)357 points = ensure_numeric(points, numpy.float) 357 358 except NameError, e: 358 359 raise NameError, e … … 362 363 363 364 try: 364 polygon = ensure_numeric(polygon, float)365 polygon = ensure_numeric(polygon, numpy.float) 365 366 except NameError, e: 366 367 raise NameError, e … … 371 372 if len(points.shape) == 1: 372 373 # Only one point was passed in. Convert to array of points 373 points = reshape(points, (1,2))374 points = numpy.reshape(points, (1,2)) 374 375 375 376 … … 441 442 ## print 'Before: points=%s, flags=%s' % (type(points), str(points.flags)) 442 443 try: 443 ## points = ascontiguousarray(ensure_numeric(points,float))444 points = ensure_numeric(points, float)444 ## points = numpy.ascontiguousarray(ensure_numeric(points, numpy.float)) 445 points = ensure_numeric(points, numpy.float) 445 446 except NameError, e: 446 447 raise NameError, e … … 452 453 #if verbose: print 'Checking input to separate_points_by_polygon 2' 453 454 try: 454 ## polygon = ascontiguousarray(ensure_numeric(polygon,float))455 polygon = ensure_numeric(polygon, float)455 ## polygon = numpy.ascontiguousarray(ensure_numeric(polygon, numpy.float)) 456 polygon = ensure_numeric(polygon, numpy.float) 456 457 except NameError, e: 457 458 raise NameError, e … … 475 476 # Only one point was passed in. 476 477 # Convert to array of points 477 points = reshape(points, (1,2))478 points = numpy.reshape(points, (1,2)) 478 479 479 480 … … 494 495 495 496 496 indices = zeros( M,int )497 indices = numpy.zeros( M, numpy.int ) 497 498 498 499 count = _separate_points_by_polygon(points, polygon, indices, … … 621 622 622 623 try: 623 polygon = ensure_numeric(polygon, float)624 polygon = ensure_numeric(polygon, numpy.float) 624 625 except NameError, e: 625 626 raise NameError, e … … 630 631 x = polygon[:,0] 631 632 y = polygon[:,1] 632 x = concatenate((x, [polygon[0,0]]), axis = 0)633 y = concatenate((y, [polygon[0,1]]), axis = 0)633 x = numpy.concatenate((x, [polygon[0,0]]), axis = 0) 634 y = numpy.concatenate((y, [polygon[0,1]]), axis = 0) 634 635 635 636 return x, y … … 732 733 733 734 def __call__(self, x, y): 734 x = array(x).astype(float)735 y = array(y).astype(float)735 x = numpy.array(x).astype(numpy.float) 736 y = numpy.array(y).astype(numpy.float) 736 737 737 738 assert len(x.shape) == 1 and len(y.shape) == 1 … … 740 741 assert y.shape[0] == N 741 742 742 points = ascontiguousarray(concatenate( (x[:,newaxis], y[:,newaxis]), axis=1 ))743 points = numpy.ascontiguousarray(numpy.concatenate( (x[:,numpy.newaxis], y[:,numpy.newaxis]), axis=1 )) 743 744 744 745 if callable(self.default): 745 746 z = self.default(x, y) 746 747 else: 747 z = ones(N,float) * self.default748 z = numpy.ones(N, numpy.float) * self.default 748 749 749 750 for polygon, value in self.regions: … … 753 754 if callable(value): 754 755 for i in indices: 755 xx = array([x[i]])756 yy = array([y[i]])756 xx = numpy.array([x[i]]) 757 yy = numpy.array([y[i]]) 757 758 z[i] = value(xx, yy)[0] 758 759 else: -
anuga_core/source_numpy_conversion/anuga/utilities/quad.py
r5889 r5902 223 223 triangles = {} 224 224 verts = self.retrieve_vertices() 225 print "verts", verts226 225 for vert in verts: 227 226 triangle_list = self.mesh.get_triangles_and_vertices_per_node(vert) 228 print 'triangle_list=%s' % str(triangle_list)229 227 for k, _ in triangle_list: 230 228 if not triangles.has_key(k): … … 436 434 """ 437 435 438 from numpy import minimum, maximum439 440 441 436 #Make root cell 442 437 #print mesh.coordinates -
anuga_core/source_numpy_conversion/anuga/utilities/sparse.py
r5889 r5902 3 3 """ 4 4 5 import numpy 5 6 6 7 class Sparse: … … 18 19 19 20 if len(args) == 1: 20 from numpy import array21 21 try: 22 A = array(args[0])22 A = numpy.array(args[0]) 23 23 except: 24 24 raise 'Input must be convertable to a Numeric array' … … 93 93 94 94 def todense(self): 95 from numpy import zeros, float 96 97 D = zeros( (self.M, self.N), float) 95 D = numpy.zeros( (self.M, self.N), numpy.float) 98 96 99 97 for i in range(self.M): … … 110 108 """ 111 109 112 from numpy import array, zeros, float113 114 110 try: 115 B = array(other)111 B = numpy.array(other) 116 112 except: 117 113 msg = 'FIXME: Only Numeric types implemented so far' … … 131 127 assert B.shape[0] == self.N, msg 132 128 133 R = zeros(self.M,float) #Result129 R = numpy.zeros(self.M, numpy.float) #Result 134 130 135 131 # Multiply nonzero elements … … 141 137 142 138 143 R = zeros((self.M, B.shape[1]),float) #Result matrix139 R = numpy.zeros((self.M, B.shape[1]), numpy.float) #Result matrix 144 140 145 141 # Multiply nonzero elements … … 197 193 """ 198 194 199 from numpy import array, zeros, float200 201 195 try: 202 B = array(other)196 B = numpy.array(other) 203 197 except: 204 198 print 'FIXME: Only Numeric types implemented so far' … … 211 205 assert B.shape[0] == self.M, 'Mismatching dimensions' 212 206 213 R = zeros((self.N,),float) #Result207 R = numpy.zeros((self.N,), numpy.float) #Result 214 208 215 209 #Multiply nonzero elements … … 248 242 """ 249 243 250 from numpy import zeros, float, int251 252 244 if isinstance(A,Sparse): 253 245 … … 255 247 keys.sort() 256 248 nnz = len(keys) 257 data = zeros ( (nnz,),float)258 colind = zeros ( (nnz,),int)259 row_ptr = zeros ( (A.M+1,),int)249 data = numpy.zeros ( (nnz,), numpy.float) 250 colind = numpy.zeros ( (nnz,), numpy.int) 251 row_ptr = numpy.zeros ( (A.M+1,), numpy.int) 260 252 current_row = -1 261 253 k = 0 … … 295 287 296 288 def todense(self): 297 from numpy.oldnumeric import zeros, Float 298 299 D = zeros( (self.M, self.N), Float) 289 290 D = numpy.zeros( (self.M, self.N), numpy.float) 300 291 301 292 for i in range(self.M): … … 310 301 """ 311 302 312 from numpy.oldnumeric import array, zeros, Float313 314 303 try: 315 B = array(other)304 B = numpy.array(other) 316 305 except: 317 306 print 'FIXME: Only Numeric types implemented so far' … … 329 318 if __name__ == '__main__': 330 319 # A little selftest 331 332 from numpy.oldnumeric import allclose, array, Float333 334 320 A = Sparse(3,3) 335 321 … … 362 348 u = A*v 363 349 print u 364 assert allclose(u, [6,14,4])350 assert numpy.allclose(u, [6,14,4]) 365 351 366 352 u = A.trans_mult(v) 367 353 print u 368 assert allclose(u, [6,6,10])354 assert numpy.allclose(u, [6,6,10]) 369 355 370 356 #Right hand side column 371 v = array([[2,4],[3,4],[4,4]])357 v = numpy.array([[2,4],[3,4],[4,4]]) 372 358 373 359 u = A*v[:,0] 374 assert allclose(u, [6,14,4])360 assert numpy.allclose(u, [6,14,4]) 375 361 376 362 #u = A*v[:,1] -
anuga_core/source_numpy_conversion/anuga/utilities/test_cg_solve.py
r5889 r5902 5 5 import unittest 6 6 7 8 from numpy import dot, allclose, array, transpose, arange, ones, float 7 import numpy 9 8 from anuga.utilities.cg_solve import * 10 9 from anuga.utilities.cg_solve import _conjugate_gradient … … 30 29 x = conjugate_gradient(A,b,x,iprint=0) 31 30 32 assert allclose(x,xe)31 assert numpy.allclose(x,xe) 33 32 34 33 def test_max_iter(self): … … 61 60 A = Sparse(n,n) 62 61 63 for i in arange(0,n):62 for i in numpy.arange(0,n): 64 63 A[i,i] = 1.0 65 64 if i > 0 : … … 68 67 A[i,i+1] = -0.5 69 68 70 xe = ones( (n,),float)69 xe = numpy.ones( (n,), numpy.float) 71 70 72 71 b = A*xe 73 72 x = conjugate_gradient(A,b,b,tol=1.0e-5,iprint=1) 74 73 75 assert allclose(x,xe)74 assert numpy.allclose(x,xe) 76 75 77 76 def test_solve_large_2d(self): … … 83 82 A = Sparse(m*n, m*n) 84 83 85 for i in arange(0,n):86 for j in arange(0,m):84 for i in numpy.arange(0,n): 85 for j in numpy.arange(0,m): 87 86 I = j+m*i 88 87 A[I,I] = 4.0 … … 96 95 A[I,I+1] = -1.0 97 96 98 xe = ones( (n*m,),float)97 xe = numpy.ones( (n*m,), numpy.float) 99 98 100 99 b = A*xe 101 100 x = conjugate_gradient(A,b,b,iprint=0) 102 101 103 assert allclose(x,xe)102 assert numpy.allclose(x,xe) 104 103 105 104 def test_solve_large_2d_csr_matrix(self): … … 112 111 A = Sparse(m*n, m*n) 113 112 114 for i in arange(0,n):115 for j in arange(0,m):113 for i in numpy.arange(0,n): 114 for j in numpy.arange(0,m): 116 115 I = j+m*i 117 116 A[I,I] = 4.0 … … 125 124 A[I,I+1] = -1.0 126 125 127 xe = ones( (n*m,),float)126 xe = numpy.ones( (n*m,), numpy.float) 128 127 129 128 # Convert to csr format … … 134 133 x = conjugate_gradient(A,b,b,iprint=20) 135 134 136 assert allclose(x,xe)135 assert numpy.allclose(x,xe) 137 136 138 137 … … 145 144 A = Sparse(m*n, m*n) 146 145 147 for i in arange(0,n):148 for j in arange(0,m):146 for i in numpy.arange(0,n): 147 for j in numpy.arange(0,m): 149 148 I = j+m*i 150 149 A[I,I] = 4.0 … … 158 157 A[I,I+1] = -1.0 159 158 160 xe = ones( (n*m,),float)159 xe = numpy.ones( (n*m,), numpy.float) 161 160 162 161 b = A*xe 163 162 x = conjugate_gradient(A,b) 164 163 165 assert allclose(x,xe)164 assert numpy.allclose(x,xe) 166 165 167 166 … … 202 201 x = conjugate_gradient(A,b,x,iprint=0) 203 202 204 assert allclose(x,xe)203 assert numpy.allclose(x,xe) 205 204 206 205 #------------------------------------------------------------- -
anuga_core/source_numpy_conversion/anuga/utilities/test_data_audit.py
r5889 r5902 3 3 4 4 import unittest 5 from numpy.oldnumeric import zeros, array, allclose6 5 from tempfile import mkstemp 7 6 import os -
anuga_core/source_numpy_conversion/anuga/utilities/test_numerical_tools.py
r5889 r5902 3 3 4 4 import unittest 5 from numpy import zeros, array, allclose 6 from numpy import ndarray, float, int, array, alltrue 5 import numpy 7 6 8 7 from math import sqrt, pi … … 23 22 """Test angles between one vector and the x-axis 24 23 """ 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)24 assert numpy.allclose(angle([1.0, 0.0])/pi*180, 0.0) 25 assert numpy.allclose(angle([1.0, 1.0])/pi*180, 45.0) 26 assert numpy.allclose(angle([0.0, 1.0])/pi*180, 90.0) 27 assert numpy.allclose(angle([-1.0, 1.0])/pi*180, 135.0) 28 assert numpy.allclose(angle([-1.0, 0.0])/pi*180, 180.0) 29 assert numpy.allclose(angle([-1.0, -1.0])/pi*180, 225.0) 30 assert numpy.allclose(angle([0.0, -1.0])/pi*180, 270.0) 31 assert numpy.allclose(angle([1.0, -1.0])/pi*180, 315.0) 33 32 34 33 … … 37 36 """ 38 37 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)38 assert numpy.allclose(angle([1.0, 0.0], [1.0, 1.0])/pi*180, 315.0) 39 assert numpy.allclose(angle([1.0, 1.0], [1.0, 0.0])/pi*180, 45.0) 41 40 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)41 assert numpy.allclose(angle([-1.0, -1.0], [1.0, 1.0])/pi*180, 180) 42 assert numpy.allclose(angle([-1.0, -1.0], [-1.0, 1.0])/pi*180, 90.0) 44 43 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)44 assert numpy.allclose(angle([-1.0, 0.0], [1.0, 1.0])/pi*180, 135.0) 45 assert numpy.allclose(angle([0.0, -1.0], [1.0, 1.0])/pi*180, 225.0) 47 46 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)47 assert numpy.allclose(angle([1.0, -1.0], [1.0, 1.0])/pi*180, 270.0) 48 assert numpy.allclose(angle([1.0, 0.0], [0.0, 1.0])/pi*180, 270.0) 50 49 51 50 #From test_get_boundary_polygon_V 52 51 v_prev = [-0.5, -0.5] 53 52 vc = [ 0.0, -0.5] 54 assert allclose(angle(vc, v_prev)/pi*180, 45.0)53 assert numpy.allclose(angle(vc, v_prev)/pi*180, 45.0) 55 54 56 55 vc = [ 0.5, 0.0] 57 assert allclose(angle(vc, v_prev)/pi*180, 135.0)56 assert numpy.allclose(angle(vc, v_prev)/pi*180, 135.0) 58 57 59 58 vc = [ -0.5, 0.5] 60 assert allclose(angle(vc, v_prev)/pi*180, 270.0)59 assert numpy.allclose(angle(vc, v_prev)/pi*180, 270.0) 61 60 62 61 63 62 def test_anglediff(self): 64 assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0)63 assert numpy.allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0) 65 64 66 65 … … 68 67 A = [1,2,3,4] 69 68 B = ensure_numeric(A) 70 assert isinstance(B, n darray)69 assert isinstance(B, numpy.ndarray) 71 70 assert B.dtype.char == 'l' 72 71 assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4 … … 74 73 A = [1,2,3.14,4] 75 74 B = ensure_numeric(A) 76 assert isinstance(B, n darray)75 assert isinstance(B, numpy.ndarray) 77 76 assert B.dtype.char == 'd' 78 77 assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4 79 78 80 79 A = [1,2,3,4] 81 B = ensure_numeric(A, float)82 assert isinstance(B, n darray)80 B = ensure_numeric(A, numpy.float) 81 assert isinstance(B, numpy.ndarray) 83 82 assert B.dtype.char == 'd' 84 83 assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 85 84 86 85 A = [1,2,3,4] 87 B = ensure_numeric(A, float)88 assert isinstance(B, n darray)86 B = ensure_numeric(A, numpy.float) 87 assert isinstance(B, numpy.ndarray) 89 88 assert B.dtype.char == 'd' 90 89 assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 91 90 92 A = array([1,2,3,4])91 A = numpy.array([1,2,3,4]) 93 92 B = ensure_numeric(A) 94 assert isinstance(B, n darray)93 assert isinstance(B, numpy.ndarray) 95 94 assert B.dtype.char == 'l' 96 assert alltrue(A == B)95 assert numpy.alltrue(A == B) 97 96 assert A is B #Same object 98 97 99 A = array([1,2,3,4])100 B = ensure_numeric(A, float)101 assert isinstance(B, n darray)98 A = numpy.array([1,2,3,4]) 99 B = ensure_numeric(A, numpy.float) 100 assert isinstance(B, numpy.ndarray) 102 101 assert B.dtype.char == 'd' 103 assert alltrue(A == B)102 assert numpy.alltrue(A == B) 104 103 assert A is not B #Not the same object 105 104 106 105 # Check scalars 107 106 A = 1 108 B = ensure_numeric(A, float)107 B = ensure_numeric(A, numpy.float) 109 108 #print A, B[0], len(B), type(B) 110 109 #print B.shape 111 assert alltrue(A == B)112 113 B = ensure_numeric(A, int)110 assert numpy.alltrue(A == B) 111 112 B = ensure_numeric(A, numpy.int) 114 113 #print A, B 115 114 #print B.shape 116 assert alltrue(A == B)115 assert numpy.alltrue(A == B) 117 116 118 117 def NO_test_ensure_numeric_char(self): 119 118 # Error situation 120 B = ensure_numeric('hello', int)119 B = ensure_numeric('hello', numpy.int) 121 120 print 'B=%s %s' % (str(B), type(B)) 122 assert allclose(B, [104, 101, 108, 108, 111])121 assert numpy.allclose(B, [104, 101, 108, 108, 111]) 123 122 124 123 … … 209 208 #There are four elements greater than or equal to 3 210 209 bins = [3] 211 assert allclose(histogram(a, bins), [4])210 assert numpy.allclose(histogram(a, bins), [4]) 212 211 213 212 bins = [ min(a) ] 214 assert allclose(histogram(a, bins), [len(a)])213 assert numpy.allclose(histogram(a, bins), [len(a)]) 215 214 216 215 bins = [ max(a)+0.00001 ] 217 assert allclose(histogram(a, bins), [0])216 assert numpy.allclose(histogram(a, bins), [0]) 218 217 219 218 bins = [1,2,3,4] 220 assert allclose(histogram(a, bins), [8,3,3,1])219 assert numpy.allclose(histogram(a, bins), [8,3,3,1]) 221 220 222 221 bins = [1.1,2,3.1,4] 223 222 #print histogram(a, bins) 224 assert allclose(histogram(a, bins), [0,6,0,1])223 assert numpy.allclose(histogram(a, bins), [0,6,0,1]) 225 224 226 225 bins = [0,1.5,2,3] 227 assert allclose(histogram(a, bins), [8,0,3,4])228 assert allclose(histogram(a, [0,3]), histogram(a, [-0.5,3]))226 assert numpy.allclose(histogram(a, bins), [8,0,3,4]) 227 assert numpy.allclose(histogram(a, [0,3]), histogram(a, [-0.5,3])) 229 228 230 229 # Check situation with #bins >= #datapoints 231 230 a = [1.7] 232 231 bins = [0,1.5,2,3] 233 assert allclose(histogram(a, bins), [0,1,0,0])232 assert numpy.allclose(histogram(a, bins), [0,1,0,0]) 234 233 235 234 a = [1.7] 236 235 bins = [0] 237 assert allclose(histogram(a, bins), [1])236 assert numpy.allclose(histogram(a, bins), [1]) 238 237 239 238 a = [-1.7] 240 239 bins = [0] 241 assert allclose(histogram(a, bins), [0])240 assert numpy.allclose(histogram(a, bins), [0]) 242 241 243 242 a = [-1.7] 244 243 bins = [-1.7] 245 assert allclose(histogram(a, bins), [1])244 assert numpy.allclose(histogram(a, bins), [1]) 246 245 247 246 -
anuga_core/source_numpy_conversion/anuga/utilities/test_polygon.py
r5889 r5902 3 3 4 4 import unittest 5 from numpy import zeros, array, allclose, alltrue 5 import numpy 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)], verbose=False ) 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 numpy.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 numpy.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 numpy.allclose(z, [2,1,0,2]) 59 59 60 60 … … 79 79 z = f([430000,480000], [490000,7720000]) # first outside, second inside 80 80 81 assert allclose(z, [0,10])81 assert numpy.allclose(z, [0,10]) 82 82 83 83 def test_polygon_function_georef(self): … … 97 97 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 98 98 99 assert allclose(z, [1,1,0,0])99 assert numpy.allclose(z, [1,1,0,0]) 100 100 101 101 102 102 f = Polygon_function( [(p2, 2.0)], geo_reference=geo) 103 103 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2 104 assert allclose(z, [2,0,0,2])104 assert numpy.allclose(z, [2,0,0,2]) 105 105 106 106 … … 108 108 f = Polygon_function( [(p1, 1.0), (p2, 2.0)], geo_reference=geo) 109 109 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 110 assert allclose(z, [2,1,0,2])110 assert numpy.allclose(z, [2,1,0,2]) 111 111 112 112 … … 114 114 f = Polygon_function( [(p1, 1.0), (p2, 2.0)]) 115 115 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 116 assert not allclose(z, [2,1,0,2])116 assert not numpy.allclose(z, [2,1,0,2]) 117 117 118 118 … … 127 127 f = Polygon_function( [(p1, test_function)] ) 128 128 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 129 assert allclose(z, [10,14,0,0])129 assert numpy.allclose(z, [10,14,0,0]) 130 130 131 131 # Combined 132 132 f = Polygon_function( [(p1, test_function), (p2, 2.0)] ) 133 133 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 134 assert allclose(z, [2,14,0,2])134 assert numpy.allclose(z, [2,14,0,2]) 135 135 136 136 … … 138 138 f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14) 139 139 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 140 assert allclose(z, [2,14,3.14,2])140 assert numpy.allclose(z, [2,14,3.14,2]) 141 141 142 142 … … 145 145 default = test_function) 146 146 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 147 assert allclose(z, [2,14,35,2])147 assert numpy.allclose(z, [2,14,35,2]) 148 148 149 149 … … 217 217 res = inside_polygon(points, polygon) 218 218 assert len(res) == 2 219 assert allclose(res, [0,1]) ## alltrue?219 assert numpy.allclose(res, [0,1]) ## alltrue? 220 220 221 221 … … 312 312 res = inside_polygon( points, polygon, verbose=False ) 313 313 314 assert allclose( res, [0,1,2] )314 assert numpy.allclose( res, [0,1,2] ) 315 315 316 316 def test_outside_polygon(self): … … 324 324 325 325 indices = outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) 326 assert allclose( indices, [1] )326 assert numpy.allclose( indices, [1] ) 327 327 328 328 # One more test of vector formulation returning indices … … 331 331 res = outside_polygon( points, polygon ) 332 332 333 assert allclose( res, [3, 4] )333 assert numpy.allclose( res, [3, 4] ) 334 334 335 335 … … 339 339 res = outside_polygon( points, polygon ) 340 340 341 assert allclose( res, [0, 4, 5] )341 assert numpy.allclose( res, [0, 4, 5] ) 342 342 343 343 def test_outside_polygon2(self): … … 362 362 #print indices, count 363 363 assert count == 0 #None inside 364 assert allclose(indices, [3,2,1,0])364 assert numpy.allclose(indices, [3,2,1,0]) 365 365 366 366 indices = outside_polygon(points, U, closed = True) 367 assert allclose(indices, [0,1,2,3])367 assert numpy.allclose(indices, [0,1,2,3]) 368 368 369 369 indices = inside_polygon(points, U, closed = True) 370 assert allclose(indices, [])370 assert numpy.allclose(indices, []) 371 371 372 372 … … 382 382 indices, count = separate_points_by_polygon(points, U) 383 383 assert count == 3 #All inside 384 assert allclose(indices, [0,1,2])384 assert numpy.allclose(indices, [0,1,2]) 385 385 386 386 indices = outside_polygon(points, U, closed = True) 387 assert allclose(indices, [])387 assert numpy.allclose(indices, []) 388 388 389 389 indices = inside_polygon(points, U, closed = True) 390 assert allclose(indices, [0,1,2])390 assert numpy.allclose(indices, [0,1,2]) 391 391 392 392 … … 395 395 396 396 indices, count = separate_points_by_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) 397 assert allclose( indices, [0,2,1] )397 assert numpy.allclose( indices, [0,2,1] ) 398 398 assert count == 2 399 399 … … 403 403 res, count = separate_points_by_polygon( points, polygon ) 404 404 405 assert allclose( res, [0,1,2,4,3] )405 assert numpy.allclose( res, [0,1,2,4,3] ) 406 406 assert count == 3 407 407 … … 411 411 res, count = separate_points_by_polygon( points, polygon ) 412 412 413 assert allclose( res, [1,2,3,5,4,0] )413 assert numpy.allclose( res, [1,2,3,5,4,0] ) 414 414 assert count == 3 415 415 … … 595 595 status, value = intersection(line0, line1) 596 596 assert status == 1 597 assert allclose(value, [0.0, 0.0])597 assert numpy.allclose(value, [0.0, 0.0]) 598 598 599 599 def test_intersection2(self): … … 603 603 status, value = intersection(line0, line1) 604 604 assert status == 1 605 assert allclose(value, [12.0, 6.0])605 assert numpy.allclose(value, [12.0, 6.0]) 606 606 607 607 # Swap direction of one line … … 610 610 status, value = intersection(line0, line1) 611 611 assert status == 1 612 assert allclose(value, [12.0, 6.0])612 assert numpy.allclose(value, [12.0, 6.0]) 613 613 614 614 # Swap order of lines 615 615 status, value = intersection(line1, line0) 616 616 assert status == 1 617 assert allclose(value, [12.0, 6.0])617 assert numpy.allclose(value, [12.0, 6.0]) 618 618 619 619 def test_intersection3(self): … … 623 623 status, value = intersection(line0, line1) 624 624 assert status == 1 625 assert allclose(value, [14.068965517, 7.0344827586])625 assert numpy.allclose(value, [14.068965517, 7.0344827586]) 626 626 627 627 # Swap direction of one line … … 630 630 status, value = intersection(line0, line1) 631 631 assert status == 1 632 assert allclose(value, [14.068965517, 7.0344827586])632 assert numpy.allclose(value, [14.068965517, 7.0344827586]) 633 633 634 634 # Swap order of lines 635 635 status, value = intersection(line1, line0) 636 636 assert status == 1 637 assert allclose(value, [14.068965517, 7.0344827586])637 assert numpy.allclose(value, [14.068965517, 7.0344827586]) 638 638 639 639 … … 648 648 status, value = intersection(line0, line1) 649 649 assert status == 1 650 assert allclose(value, [1.0, 1.0])650 assert numpy.allclose(value, [1.0, 1.0]) 651 651 652 652 … … 656 656 status, value = intersection(line0, line1) 657 657 assert status == 1 658 assert allclose(value, [1.0, 1.0])658 assert numpy.allclose(value, [1.0, 1.0]) 659 659 660 660 … … 686 686 common_end_point[1]) 687 687 msg += ' gave %s, \nbut when reversed we got %s' %(p1, p2) 688 assert allclose(p1, p2), msg688 assert numpy.allclose(p1, p2), msg 689 689 690 690 # Swap order of lines … … 692 692 assert status == 1 693 693 msg = 'Order of lines gave different results' 694 assert allclose(p1, p3), msg694 assert numpy.allclose(p1, p3), msg 695 695 696 696 … … 732 732 status, value = intersection(line0, line1) 733 733 assert status == 2 734 assert allclose(value, [[0,0], [3,0]])734 assert numpy.allclose(value, [[0,0], [3,0]]) 735 735 736 736 # Overlap 2 … … 740 740 status, value = intersection(line0, line1) 741 741 assert status == 2 742 assert allclose(value, [[-3, 0], [5,0]])742 assert numpy.allclose(value, [[-3, 0], [5,0]]) 743 743 744 744 # Inclusion 1 … … 748 748 status, value = intersection(line0, line1) 749 749 assert status == 2 750 assert allclose(value, line1)750 assert numpy.allclose(value, line1) 751 751 752 752 # Inclusion 2 … … 756 756 status, value = intersection(line0, line1) 757 757 assert status == 2 758 assert allclose(value, line0)758 assert numpy.allclose(value, line0) 759 759 760 760 … … 775 775 status, value = intersection(line0, line1) 776 776 assert status == 2 777 assert allclose(value, [[1, 7], [7, 19]])777 assert numpy.allclose(value, [[1, 7], [7, 19]]) 778 778 779 779 status, value = intersection(line1, line0) 780 780 assert status == 2 781 assert allclose(value, [[1, 7], [7, 19]])781 assert numpy.allclose(value, [[1, 7], [7, 19]]) 782 782 783 783 # Swap direction … … 786 786 status, value = intersection(line0, line1) 787 787 assert status == 2 788 assert allclose(value, [[7, 19], [1, 7]])788 assert numpy.allclose(value, [[7, 19], [1, 7]]) 789 789 790 790 line0 = [[0,5], [7,19]] … … 792 792 status, value = intersection(line0, line1) 793 793 assert status == 2 794 assert allclose(value, [[1, 7], [7, 19]])794 assert numpy.allclose(value, [[1, 7], [7, 19]]) 795 795 796 796 … … 800 800 status, value = intersection(line0, line1) 801 801 assert status == 2 802 assert allclose(value, [[1,7], [7, 19]])802 assert numpy.allclose(value, [[1,7], [7, 19]]) 803 803 804 804 line0 = [[0,5], [10,25]] … … 806 806 status, value = intersection(line0, line1) 807 807 assert status == 2 808 assert allclose(value, [[1,7], [7, 19]])808 assert numpy.allclose(value, [[1,7], [7, 19]]) 809 809 810 810 … … 813 813 status, value = intersection(line0, line1) 814 814 assert status == 2 815 assert allclose(value, [[7, 19], [1, 7]])815 assert numpy.allclose(value, [[7, 19], [1, 7]]) 816 816 817 817 … … 850 850 res = inside_polygon(points, polygon) 851 851 assert len(res) == 2 852 assert allclose(res, [0,1])852 assert numpy.allclose(res, [0,1]) 853 853 854 854 def test_polygon_area(self): -
anuga_core/source_numpy_conversion/anuga/utilities/test_quad.py
r5889 r5902 1 1 import unittest 2 from numpy import array, allclose 2 ##import numpy 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 numpy.allclose(numpy.array(results),[[[ 2., 1.], 239 239 #[ 4., 1.], [ 4., 4.]], [[ 4., 1.],[ 5., 4.],[ 4., 4.]]] ) 240 240 results = Q.search(5,4.) … … 253 253 [ 5., 4.], 254 254 [ 4., 4.]] 255 #assert allclose(array(results),[[[ 2., 1.],[ 4., 1.], [ 4., 4.]]255 #assert numpy.allclose(numpy.array(results),[[[ 2., 1.],[ 4., 1.], [ 4., 4.]] 256 256 # ,[[ 2., 1.],[ 4., 4.], [ 2., 4.]], 257 257 #[[ 4., 1.], [ 5., 4.], [ 4., 4.]], -
anuga_core/source_numpy_conversion/anuga/utilities/test_sparse.py
r5889 r5902 5 5 6 6 from sparse import * 7 from numpy.oldnumeric import allclose, array, transpose 7 import numpy 8 8 9 9 class Test_Sparse(unittest.TestCase): … … 43 43 C = Sparse(B) 44 44 45 assert allclose(C.todense(), B)45 assert numpy.allclose(C.todense(), B) 46 46 47 47 … … 50 50 A[1,1] = 4 51 51 52 assert allclose(A.todense(), [[0,0,0], [0,4,0], [0,0,0], [0,0,0]])52 assert numpy.allclose(A.todense(), [[0,0,0], [0,4,0], [0,0,0], [0,0,0]]) 53 53 54 54 … … 63 63 64 64 assert len(A) == 0 65 assert allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]])65 assert numpy.allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]]) 66 66 67 67 #Set an existing zero element to zero 68 68 A[1,2] = 0 69 69 assert len(A) == 0 70 assert allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]])70 assert numpy.allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]]) 71 71 72 72 def test_sparse_multiplication_vector(self): … … 82 82 83 83 u = A*v 84 assert allclose(u, [6,14,4])84 assert numpy.allclose(u, [6,14,4]) 85 85 86 86 #Right hand side column 87 v = array([[2,4],[3,4],[4,4]])87 v = numpy.array([[2,4],[3,4],[4,4]]) 88 88 89 89 u = A*v[:,0] 90 assert allclose(u, [6,14,4])90 assert numpy.allclose(u, [6,14,4]) 91 91 92 92 u = A*v[:,1] 93 assert allclose(u, [12,16,4])93 assert numpy.allclose(u, [12,16,4]) 94 94 95 95 … … 103 103 104 104 #Right hand side matrix 105 v = array([[2,4],[3,4],[4,4]])105 v = numpy.array([[2,4],[3,4],[4,4]]) 106 106 107 107 u = A*v 108 assert allclose(u, [[6,12], [14,16], [4,4]])108 assert numpy.allclose(u, [[6,12], [14,16], [4,4]]) 109 109 110 110 … … 122 122 123 123 u = A.trans_mult(v) 124 assert allclose(u, [6,6,10])124 assert numpy.allclose(u, [6,6,10]) 125 125 126 126 … … 137 137 138 138 B = 3*A 139 assert allclose(B.todense(), 3*A.todense())139 assert numpy.allclose(B.todense(), 3*A.todense()) 140 140 141 141 B = A*3 142 assert allclose(B.todense(), 3*A.todense())142 assert numpy.allclose(B.todense(), 3*A.todense()) 143 143 144 144 try: … … 166 166 C = A+B 167 167 168 assert allclose(C.todense(), [[12,0,0], [2,8,8], [0,0,4]])168 assert numpy.allclose(C.todense(), [[12,0,0], [2,8,8], [0,0,4]]) 169 169 170 170 def test_sparse_tocsr(self): … … 190 190 C = [1, 2, 3] 191 191 192 assert allclose(B*C, [15.0, 10.0 ,8.0, 0.0])192 assert numpy.allclose(B*C, [15.0, 10.0 ,8.0, 0.0]) 193 193 194 194 C2 = [[1,2],[2,4],[3,6]] … … 196 196 #print B*C2 197 197 198 assert allclose(B*C2, [[15.0, 30.0],[10.0, 20.0],[8.0, 16.0],[0.0, 0.0]])198 assert numpy.allclose(B*C2, [[15.0, 30.0],[10.0, 20.0],[8.0, 16.0],[0.0, 0.0]]) 199 199 200 200 -
anuga_core/source_numpy_conversion/anuga/utilities/test_system_tools.py
r5889 r5902 1 1 #!/usr/bin/env python 2 2 3 4 3 import unittest 5 from numpy import zeros, array, allclose, float, dtype 4 import numpy 6 5 import zlib 7 6 from os.path import join, split, sep … … 81 80 pass 82 81 else: 83 test_array = array([[7.0, 3.14], [-31.333, 0.0]])82 test_array = numpy.array([[7.0, 3.14], [-31.333, 0.0]]) 84 83 85 84 # First file … … 87 86 fid = NetCDFFile(filename1, 'w') 88 87 fid.createDimension('two', 2) 89 fid.createVariable('test_array', dtype(float).char, ('two', 'two'))88 fid.createVariable('test_array', numpy.dtype(numpy.float).char, ('two', 'two')) 90 89 fid.variables['test_array'][:] = test_array 91 90 fid.close() … … 95 94 fid = NetCDFFile(filename2, 'w') 96 95 fid.createDimension('two', 2) 97 fid.createVariable('test_array', dtype(float).char, ('two', 'two'))96 fid.createVariable('test_array', numpy.dtype(numpy.float).char, ('two', 'two')) 98 97 fid.variables['test_array'][:] = test_array 99 98 fid.close()
Note: See TracChangeset
for help on using the changeset viewer.