[5897] | 1 | #!/usr/bin/env python |
---|
| 2 | """Auxiliary numerical tools |
---|
| 3 | |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | from math import acos, pi, sqrt |
---|
| 7 | from warnings import warn |
---|
| 8 | |
---|
[6304] | 9 | import numpy as num |
---|
[5897] | 10 | |
---|
[6158] | 11 | NAN = (num.array([1])/0.)[0] |
---|
[5897] | 12 | # if we use a package that has NAN, this should be updated to use NAN. |
---|
| 13 | |
---|
| 14 | # Static variable used by get_machine_precision |
---|
| 15 | machine_precision = None |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | def safe_acos(x): |
---|
| 19 | """Safely compute acos |
---|
| 20 | |
---|
| 21 | Protect against cases where input argument x is outside the allowed |
---|
| 22 | interval [-1.0, 1.0] by no more than machine precision |
---|
| 23 | """ |
---|
| 24 | |
---|
| 25 | error_msg = 'Input to acos is outside allowed domain [-1.0, 1.0].'+\ |
---|
| 26 | 'I got %.12f' %x |
---|
| 27 | warning_msg = 'Changing argument to acos from %.18f to %.1f' %(x, sign(x)) |
---|
| 28 | |
---|
| 29 | eps = get_machine_precision() # Machine precision |
---|
| 30 | if x < -1.0: |
---|
| 31 | if x < -1.0 - eps: |
---|
| 32 | raise ValueError, errmsg |
---|
| 33 | else: |
---|
| 34 | warn(warning_msg) |
---|
| 35 | x = -1.0 |
---|
| 36 | |
---|
| 37 | if x > 1.0: |
---|
| 38 | if x > 1.0 + eps: |
---|
| 39 | raise ValueError, errmsg |
---|
| 40 | else: |
---|
| 41 | print 'NOTE: changing argument to acos from %.18f to 1.0' %x |
---|
| 42 | x = 1.0 |
---|
| 43 | |
---|
| 44 | return acos(x) |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | def sign(x): |
---|
| 48 | if x > 0: return 1 |
---|
| 49 | if x < 0: return -1 |
---|
| 50 | if x == 0: return 0 |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | def is_scalar(x): |
---|
| 54 | """True if x is a scalar (constant numeric value) |
---|
| 55 | """ |
---|
| 56 | |
---|
| 57 | from types import IntType, FloatType |
---|
| 58 | if type(x) in [IntType, FloatType]: |
---|
| 59 | return True |
---|
| 60 | else: |
---|
| 61 | return False |
---|
| 62 | |
---|
| 63 | def angle(v1, v2=None): |
---|
| 64 | """Compute angle between 2D vectors v1 and v2. |
---|
| 65 | |
---|
| 66 | If v2 is not specified it will default |
---|
| 67 | to e1 (the unit vector in the x-direction) |
---|
| 68 | |
---|
| 69 | The angle is measured as a number in [0, 2pi] from v2 to v1. |
---|
| 70 | """ |
---|
| 71 | |
---|
[6304] | 72 | # Prepare two numeric vectors |
---|
[5897] | 73 | if v2 is None: |
---|
| 74 | v2 = [1.0, 0.0] # Unit vector along the x-axis |
---|
| 75 | |
---|
[6304] | 76 | v1 = ensure_numeric(v1, num.float) |
---|
| 77 | v2 = ensure_numeric(v2, num.float) |
---|
[5897] | 78 | |
---|
| 79 | # Normalise |
---|
[6158] | 80 | v1 = v1/num.sqrt(num.sum(v1**2)) |
---|
| 81 | v2 = v2/num.sqrt(num.sum(v2**2)) |
---|
[5897] | 82 | |
---|
| 83 | # Compute angle |
---|
[6304] | 84 | p = num.inner(v1, v2) |
---|
| 85 | c = num.inner(v1, normal_vector(v2)) # Projection onto normal |
---|
[5897] | 86 | # (negative cross product) |
---|
| 87 | |
---|
| 88 | theta = safe_acos(p) |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | # Correct if v1 is in quadrant 3 or 4 with respect to v2 (as the x-axis) |
---|
| 92 | # If v2 was the unit vector [1,0] this would correspond to the test |
---|
| 93 | # if v1[1] < 0: theta = 2*pi-theta |
---|
| 94 | # In general we use the sign of the projection onto the normal. |
---|
| 95 | if c < 0: |
---|
| 96 | #Quadrant 3 or 4 |
---|
| 97 | theta = 2*pi-theta |
---|
| 98 | |
---|
| 99 | return theta |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | def anglediff(v0, v1): |
---|
| 103 | """Compute difference between angle of vector v0 (x0, y0) and v1 (x1, y1). |
---|
| 104 | This is used for determining the ordering of vertices, |
---|
| 105 | e.g. for checking if they are counter clockwise. |
---|
| 106 | |
---|
| 107 | Always return a positive value |
---|
| 108 | """ |
---|
| 109 | |
---|
| 110 | from math import pi |
---|
| 111 | |
---|
| 112 | a0 = angle(v0) |
---|
| 113 | a1 = angle(v1) |
---|
| 114 | |
---|
| 115 | #Ensure that difference will be positive |
---|
| 116 | if a0 < a1: |
---|
| 117 | a0 += 2*pi |
---|
| 118 | |
---|
| 119 | return a0-a1 |
---|
| 120 | |
---|
| 121 | def normal_vector(v): |
---|
| 122 | """Normal vector to v. |
---|
| 123 | |
---|
| 124 | Returns vector 90 degrees counter clockwise to and of same length as v |
---|
| 125 | """ |
---|
| 126 | |
---|
[6304] | 127 | return num.array([-v[1], v[0]], num.float) |
---|
[5897] | 128 | |
---|
| 129 | |
---|
| 130 | #def crossproduct_length(v1, v2): |
---|
| 131 | # return v1[0]*v2[1]-v2[0]*v1[1] |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | def mean(x): |
---|
| 135 | """Mean value of a vector |
---|
| 136 | """ |
---|
[6158] | 137 | return(float(num.sum(x))/len(x)) |
---|
[5897] | 138 | |
---|
| 139 | |
---|
| 140 | def cov(x, y=None): |
---|
| 141 | """Covariance of vectors x and y. |
---|
| 142 | |
---|
| 143 | If y is None: return cov(x, x) |
---|
| 144 | """ |
---|
| 145 | |
---|
| 146 | if y is None: |
---|
| 147 | y = x |
---|
| 148 | |
---|
| 149 | x = ensure_numeric(x) |
---|
| 150 | y = ensure_numeric(y) |
---|
| 151 | msg = 'Lengths must be equal: len(x) == %d, len(y) == %d' %(len(x), len(y)) |
---|
| 152 | assert(len(x)==len(y)), msg |
---|
| 153 | |
---|
| 154 | N = len(x) |
---|
| 155 | cx = x - mean(x) |
---|
| 156 | cy = y - mean(y) |
---|
| 157 | |
---|
[6304] | 158 | p = num.inner(cx,cy)/N |
---|
[5897] | 159 | return(p) |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | def err(x, y=0, n=2, relative=True): |
---|
| 163 | """Relative error of ||x-y|| to ||y|| |
---|
| 164 | n = 2: Two norm |
---|
| 165 | n = None: Max norm |
---|
| 166 | |
---|
| 167 | If denominator evaluates to zero or |
---|
| 168 | if y is omitted or |
---|
| 169 | if keyword relative is False, |
---|
| 170 | absolute error is returned |
---|
| 171 | |
---|
| 172 | If there is x and y, n=2 and relative=False, this will calc; |
---|
| 173 | sqrt(sum_over_x&y((xi - yi)^2)) |
---|
| 174 | |
---|
| 175 | Given this value (err), to calc the root mean square deviation, do |
---|
| 176 | err/sqrt(n) |
---|
| 177 | where n is the number of elements,(len(x)) |
---|
| 178 | """ |
---|
| 179 | |
---|
| 180 | x = ensure_numeric(x) |
---|
| 181 | if y: |
---|
| 182 | y = ensure_numeric(y) |
---|
| 183 | |
---|
| 184 | if n == 2: |
---|
| 185 | err = norm(x-y) |
---|
| 186 | if relative is True: |
---|
| 187 | try: |
---|
| 188 | err = err/norm(y) |
---|
| 189 | except: |
---|
| 190 | pass |
---|
| 191 | |
---|
| 192 | else: |
---|
| 193 | err = max(abs(x-y)) |
---|
| 194 | if relative is True: |
---|
| 195 | try: |
---|
| 196 | err = err/max(abs(y)) |
---|
| 197 | except: |
---|
| 198 | pass |
---|
| 199 | |
---|
| 200 | return err |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | def norm(x): |
---|
| 204 | """2-norm of x |
---|
| 205 | """ |
---|
| 206 | |
---|
[6158] | 207 | y = num.ravel(x) |
---|
[6304] | 208 | p = num.sqrt(num.inner(y,y)) |
---|
[5897] | 209 | return p |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | def corr(x, y=None): |
---|
| 213 | """Correlation of x and y |
---|
| 214 | If y is None return autocorrelation of x |
---|
| 215 | """ |
---|
| 216 | |
---|
| 217 | from math import sqrt |
---|
| 218 | if y is None: |
---|
| 219 | y = x |
---|
| 220 | |
---|
| 221 | varx = cov(x) |
---|
| 222 | vary = cov(y) |
---|
| 223 | |
---|
| 224 | if varx == 0 or vary == 0: |
---|
| 225 | C = 0 |
---|
| 226 | else: |
---|
| 227 | C = cov(x,y)/sqrt(varx * vary) |
---|
| 228 | |
---|
| 229 | return(C) |
---|
| 230 | |
---|
| 231 | |
---|
[6304] | 232 | |
---|
| 233 | ## |
---|
| 234 | # @brief Ensure that a sequence is a numeric array of the required type. |
---|
| 235 | # @param A The sequence object to convert to a numeric array. |
---|
| 236 | # @param typecode The required numeric type of object A (a numeric dtype). |
---|
| 237 | # @return A numeric array of the required type. |
---|
| 238 | def ensure_numeric(A, typecode=None): |
---|
| 239 | """Ensure that sequence is a numeric array. |
---|
[5897] | 240 | Inputs: |
---|
[6304] | 241 | A: Sequence. If A is already a numeric array it will be returned |
---|
[5897] | 242 | unaltered |
---|
[6304] | 243 | If not, an attempt is made to convert it to a numeric |
---|
[5897] | 244 | array |
---|
[6304] | 245 | A: Scalar. Return 0-dimensional array containing that value. Note |
---|
| 246 | that a 0-dim array DOES NOT HAVE A LENGTH UNDER numpy. |
---|
| 247 | A: String. Array of ASCII values (numpy can't handle this) |
---|
[5897] | 248 | |
---|
[6304] | 249 | typecode: numeric type. If specified, use this in the conversion. |
---|
| 250 | If not, let numeric package decide. |
---|
[6441] | 251 | typecode will always be one of num.float, num.int, etc. |
---|
[6304] | 252 | |
---|
[6441] | 253 | Note that num.array(A, dtype) will sometimes copy. Use 'copy=False' to |
---|
| 254 | copy only when required. |
---|
| 255 | |
---|
[5897] | 256 | This function is necessary as array(A) can cause memory overflow. |
---|
| 257 | """ |
---|
| 258 | |
---|
[6462] | 259 | # if isinstance(A, basestring): |
---|
| 260 | # msg = 'Sorry, cannot handle strings in ensure_numeric()' |
---|
| 261 | # raise Exception, msg |
---|
[6304] | 262 | |
---|
[5897] | 263 | if typecode is None: |
---|
[6304] | 264 | if isinstance(A, num.ndarray): |
---|
[5897] | 265 | return A |
---|
| 266 | else: |
---|
[6158] | 267 | return num.array(A) |
---|
[5897] | 268 | else: |
---|
[6441] | 269 | return num.array(A, dtype=typecode, copy=False) |
---|
[5897] | 270 | |
---|
| 271 | |
---|
| 272 | def histogram(a, bins, relative=False): |
---|
[6304] | 273 | """Standard histogram straight from the numeric manual |
---|
[5897] | 274 | |
---|
| 275 | If relative is True, values will be normalised againts the total and |
---|
| 276 | thus represent frequencies rather than counts. |
---|
| 277 | """ |
---|
| 278 | |
---|
[6158] | 279 | n = num.searchsorted(num.sort(a), bins) |
---|
[6428] | 280 | n = num.concatenate([n, [len(a)]], axis=0) #??default# |
---|
[5897] | 281 | |
---|
| 282 | hist = n[1:]-n[:-1] |
---|
| 283 | |
---|
| 284 | if relative is True: |
---|
[6158] | 285 | hist = hist/float(num.sum(hist)) |
---|
[5897] | 286 | |
---|
| 287 | return hist |
---|
| 288 | |
---|
| 289 | def create_bins(data, number_of_bins = None): |
---|
| 290 | """Safely create bins for use with histogram |
---|
| 291 | If data contains only one point or is constant, one bin will be created. |
---|
| 292 | If number_of_bins in omitted 10 bins will be created |
---|
| 293 | """ |
---|
| 294 | |
---|
| 295 | mx = max(data) |
---|
| 296 | mn = min(data) |
---|
| 297 | |
---|
| 298 | if mx == mn: |
---|
[6158] | 299 | bins = num.array([mn]) |
---|
[5897] | 300 | else: |
---|
| 301 | if number_of_bins is None: |
---|
| 302 | number_of_bins = 10 |
---|
| 303 | |
---|
[6158] | 304 | bins = num.arange(mn, mx, (mx-mn)/number_of_bins) |
---|
[5897] | 305 | |
---|
| 306 | return bins |
---|
| 307 | |
---|
| 308 | |
---|
| 309 | |
---|
| 310 | def get_machine_precision(): |
---|
| 311 | """Calculate the machine precision for Floats |
---|
| 312 | |
---|
| 313 | Depends on static variable machine_precision in this module |
---|
| 314 | as this would otherwise require too much computation. |
---|
| 315 | """ |
---|
| 316 | |
---|
| 317 | global machine_precision |
---|
| 318 | |
---|
| 319 | if machine_precision is None: |
---|
| 320 | epsilon = 1. |
---|
| 321 | while epsilon/2 + 1. > 1.: |
---|
| 322 | epsilon /= 2 |
---|
| 323 | |
---|
| 324 | machine_precision = epsilon |
---|
| 325 | |
---|
| 326 | return machine_precision |
---|
| 327 | |
---|
| 328 | #################################################################### |
---|
| 329 | #Python versions of function that are also implemented in numerical_tools_ext.c |
---|
| 330 | # FIXME (Ole): Delete these and update tests |
---|
| 331 | # |
---|
| 332 | |
---|
| 333 | def gradient_python(x0, y0, x1, y1, x2, y2, q0, q1, q2): |
---|
| 334 | """ |
---|
| 335 | """ |
---|
| 336 | |
---|
| 337 | det = (y2-y0)*(x1-x0) - (y1-y0)*(x2-x0) |
---|
| 338 | a = (y2-y0)*(q1-q0) - (y1-y0)*(q2-q0) |
---|
| 339 | a /= det |
---|
| 340 | |
---|
| 341 | b = (x1-x0)*(q2-q0) - (x2-x0)*(q1-q0) |
---|
| 342 | b /= det |
---|
| 343 | |
---|
| 344 | return a, b |
---|
| 345 | |
---|
| 346 | |
---|
| 347 | def gradient2_python(x0, y0, x1, y1, q0, q1): |
---|
| 348 | """Compute radient based on two points and enforce zero gradient |
---|
| 349 | in the direction orthogonal to (x1-x0), (y1-y0) |
---|
| 350 | """ |
---|
| 351 | |
---|
| 352 | #Old code |
---|
| 353 | #det = x0*y1 - x1*y0 |
---|
| 354 | #if det != 0.0: |
---|
| 355 | # a = (y1*q0 - y0*q1)/det |
---|
| 356 | # b = (x0*q1 - x1*q0)/det |
---|
| 357 | |
---|
| 358 | #Correct code (ON) |
---|
| 359 | det = (x1-x0)**2 + (y1-y0)**2 |
---|
| 360 | if det != 0.0: |
---|
| 361 | a = (x1-x0)*(q1-q0)/det |
---|
| 362 | b = (y1-y0)*(q1-q0)/det |
---|
| 363 | |
---|
| 364 | return a, b |
---|
| 365 | |
---|
[6304] | 366 | ################################################################################ |
---|
| 367 | # Decision functions for numeric package objects. |
---|
| 368 | # It is a little tricky to decide if a numpy thing is of type float. |
---|
| 369 | # These functions hide numpy-specific details of how we do this. |
---|
| 370 | ################################################################################ |
---|
[5897] | 371 | |
---|
[6304] | 372 | ## |
---|
| 373 | # @brief Decide if an object is a numeric package object with datatype of float. |
---|
| 374 | # @param obj The object to decide on. |
---|
| 375 | # @return True if 'obj' is a numeric package object, and some sort of float. |
---|
| 376 | def is_num_float(obj): |
---|
| 377 | '''Is an object a numeric package float object?''' |
---|
| 378 | |
---|
| 379 | try: |
---|
| 380 | return obj.dtype.char in num.typecodes['Float'] |
---|
| 381 | except AttributeError: |
---|
| 382 | return False |
---|
| 383 | |
---|
| 384 | ## |
---|
| 385 | # @brief Decide if an object is a numeric package object with datatype of int. |
---|
| 386 | # @param obj The object to decide on. |
---|
| 387 | # @return True if 'obj' is a numeric package object, and some sort of int. |
---|
| 388 | def is_num_int(obj): |
---|
| 389 | '''Is an object a numeric package int object?''' |
---|
| 390 | |
---|
| 391 | try: |
---|
| 392 | return obj.dtype.char in num.typecodes['Integer'] |
---|
| 393 | except AttributeError: |
---|
| 394 | return False |
---|
| 395 | |
---|
| 396 | |
---|
[5897] | 397 | #----------------- |
---|
| 398 | #Initialise module |
---|
| 399 | |
---|
| 400 | from anuga.utilities import compile |
---|
| 401 | if compile.can_use_C_extension('util_ext.c'): |
---|
| 402 | from util_ext import gradient, gradient2 |
---|
| 403 | else: |
---|
| 404 | gradient = gradient_python |
---|
| 405 | gradient2 = gradient2_python |
---|
| 406 | |
---|
| 407 | |
---|
[6119] | 408 | if __name__ == '__main__': |
---|
[5897] | 409 | pass |
---|
| 410 | |
---|
| 411 | |
---|