Changeset 7276 for anuga_core/source/anuga/utilities/numerical_tools.py
- Timestamp:
- Jun 30, 2009, 2:07:41 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/utilities/numerical_tools.py
r6534 r7276 7 7 from warnings import warn 8 8 9 import Numeric as num 10 9 import numpy as num 10 11 #After having migrated to numpy we should use the native NAN. 12 #num.seterr(divide='warn') 13 num.seterr(divide='ignore') # Ignore division error here for the time being 11 14 NAN = (num.array([1])/0.)[0] 12 # if we use a package that has NAN, this should be updated to use NAN.13 15 14 16 # Static variable used by get_machine_precision … … 70 72 """ 71 73 72 # Prepare two Numeric vectors74 # Prepare two numeric vectors 73 75 if v2 is None: 74 76 v2 = [1.0, 0.0] # Unit vector along the x-axis 75 77 76 v1 = ensure_numeric(v1, num. Float)77 v2 = ensure_numeric(v2, num. Float)78 v1 = ensure_numeric(v1, num.float) 79 v2 = ensure_numeric(v2, num.float) 78 80 79 81 # Normalise … … 82 84 83 85 # Compute angle 84 p = num.inner product(v1, v2)85 c = num.inner product(v1, normal_vector(v2))# Projection onto normal86 86 p = num.inner(v1, v2) 87 c = num.inner(v1, normal_vector(v2)) # Projection onto normal 88 # (negative cross product) 87 89 88 90 theta = safe_acos(p) … … 125 127 """ 126 128 127 return num.array([-v[1], v[0]], num. Float)129 return num.array([-v[1], v[0]], num.float) 128 130 129 131 … … 156 158 cy = y - mean(y) 157 159 158 p = num.inner product(cx,cy)/N160 p = num.inner(cx,cy)/N 159 161 return(p) 160 162 … … 206 208 207 209 y = num.ravel(x) 208 p = num.sqrt(num.inner product(y,y))210 p = num.sqrt(num.inner(y,y)) 209 211 return p 210 212 … … 230 232 231 233 232 234 235 ## 236 # @brief Ensure that a sequence is a numeric array of the required type. 237 # @param A The sequence object to convert to a numeric array. 238 # @param typecode The required numeric type of object A (a numeric dtype). 239 # @return A numeric array of the required type. 233 240 def ensure_numeric(A, typecode=None): 234 241 """Ensure that sequence is a numeric array. 235 242 236 243 Inputs: 237 A: Sequence. If A is already a Numeric array it will be returned244 A: Sequence. If A is already a numeric array it will be returned 238 245 unaltered 239 If not, an attempt is made to convert it to a Numeric246 If not, an attempt is made to convert it to a numeric 240 247 array 241 A: Scalar. Return 0-dimensional array of length 1, containing that value 242 A: String. Array of ASCII values 243 typecode: Numeric type. If specified, use this in the conversion. 244 If not, let Numeric decide 248 A: Scalar. Return 0-dimensional array containing that value. Note 249 that a 0-dim array DOES NOT HAVE A LENGTH UNDER numpy. 250 A: String. Array of ASCII values (numpy can't handle this) 251 252 typecode: numeric type. If specified, use this in the conversion. 253 If not, let numeric package decide. 254 typecode will always be one of num.float, num.int, etc. 255 256 Note that num.array(A, dtype) will sometimes copy. Use 'copy=False' to 257 copy only when required. 245 258 246 259 This function is necessary as array(A) can cause memory overflow. 247 260 """ 248 261 262 # if isinstance(A, basestring): 263 # msg = 'Sorry, cannot handle strings in ensure_numeric()' 264 # raise Exception, msg 265 249 266 if typecode is None: 250 if type(A) == num.ArrayType:267 if isinstance(A, num.ndarray): 251 268 return A 252 269 else: 253 270 return num.array(A) 254 271 else: 255 if type(A) == num.ArrayType: 256 if A.typecode() == typecode: 257 return A 258 else: 259 return num.array(A, typecode) 260 else: 261 return num.array(A, typecode) 262 263 272 return num.array(A, dtype=typecode, copy=False) 264 273 265 274 266 275 def histogram(a, bins, relative=False): 267 """Standard histogram straight from the Numeric manual276 """Standard histogram straight from the numeric manual 268 277 269 278 If relative is True, values will be normalised againts the total and … … 272 281 273 282 n = num.searchsorted(num.sort(a), bins) 274 n = num.concatenate( [n, [len(a)]] )283 n = num.concatenate([n, [len(a)]], axis=0) #??default# 275 284 276 285 hist = n[1:]-n[:-1] … … 358 367 return a, b 359 368 369 ################################################################################ 370 # Decision functions for numeric package objects. 371 # It is a little tricky to decide if a numpy thing is of type float. 372 # These functions hide numpy-specific details of how we do this. 373 ################################################################################ 374 375 ## 376 # @brief Decide if an object is a numeric package object with datatype of float. 377 # @param obj The object to decide on. 378 # @return True if 'obj' is a numeric package object, and some sort of float. 379 def is_num_float(obj): 380 '''Is an object a numeric package float object?''' 381 382 try: 383 return obj.dtype.char in num.typecodes['Float'] 384 except AttributeError: 385 return False 386 387 ## 388 # @brief Decide if an object is a numeric package object with datatype of int. 389 # @param obj The object to decide on. 390 # @return True if 'obj' is a numeric package object, and some sort of int. 391 def is_num_int(obj): 392 '''Is an object a numeric package int object?''' 393 394 try: 395 return obj.dtype.char in num.typecodes['Integer'] 396 except AttributeError: 397 return False 398 360 399 361 400 #-----------------
Note: See TracChangeset
for help on using the changeset viewer.