[18] | 1 | """Functions for numerical computations |
---|
| 2 | """ |
---|
| 3 | |
---|
| 4 | epsilon = 1.0e-15 |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | def sum(x): |
---|
| 11 | """ |
---|
| 12 | Attempt to sum up elements in x |
---|
| 13 | """ |
---|
| 14 | |
---|
[6818] | 15 | import types |
---|
| 16 | import numpy as num |
---|
[18] | 17 | |
---|
[6818] | 18 | if isinstance(x, num.ndarray): |
---|
| 19 | return num.sum(x) |
---|
| 20 | elif isinstance(x, (list, tuple)): |
---|
[18] | 21 | s = x[0] |
---|
| 22 | for e in x[1:]: |
---|
| 23 | s += e |
---|
| 24 | return s |
---|
| 25 | |
---|
| 26 | def mvmul(A, x): |
---|
| 27 | """Multiply matrix A onto vector x |
---|
| 28 | """ |
---|
| 29 | |
---|
[6818] | 30 | import numpy as num |
---|
[18] | 31 | |
---|
[6818] | 32 | x = num.reshape(x, (A.shape[1], 1)) #Make x a column vector |
---|
[18] | 33 | |
---|
[6818] | 34 | return num.dot(A, x) |
---|
[18] | 35 | |
---|
| 36 | |
---|
| 37 | def all_equal(vec): |
---|
| 38 | |
---|
| 39 | equal = 1 |
---|
| 40 | v0 = vec[0] |
---|
| 41 | for v in vec: |
---|
| 42 | if v != v0: |
---|
| 43 | equal = 0 |
---|
| 44 | |
---|
| 45 | return equal |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | def meshgrid(N): |
---|
| 49 | """Make meshgrid (a' la Matlab) |
---|
| 50 | """ |
---|
| 51 | |
---|
[6818] | 52 | import numpy as num |
---|
[18] | 53 | |
---|
| 54 | d = len(N) |
---|
| 55 | |
---|
| 56 | X = [] |
---|
| 57 | for s in range(d): |
---|
[6818] | 58 | local_shape = num.ones(d) |
---|
[18] | 59 | local_shape[s] = N[s] |
---|
[6818] | 60 | a = num.arange(N[s]) |
---|
[18] | 61 | if N[s] > 1: |
---|
[6818] | 62 | a = a.astype(num.float)/(N[s]-1) |
---|
[18] | 63 | else: |
---|
[6818] | 64 | a = a.astype(num.float) |
---|
[18] | 65 | |
---|
| 66 | # Put ones in all other dimensions |
---|
| 67 | # |
---|
| 68 | e = [] |
---|
| 69 | for t in range(d): |
---|
| 70 | if s == t: |
---|
| 71 | e.append(a) |
---|
| 72 | else: |
---|
[6818] | 73 | e.append(num.ones(N[t])) |
---|
[18] | 74 | |
---|
| 75 | # Take kronecker product of all dimensions |
---|
| 76 | # |
---|
| 77 | x = 1 |
---|
| 78 | for t in range(d): |
---|
[6818] | 79 | x=num.multiply.outer(x,e[t]) |
---|
[18] | 80 | |
---|
| 81 | #print x, x.shape |
---|
| 82 | X.append(x) |
---|
| 83 | |
---|
| 84 | return X |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | def expand(x, mask): |
---|
| 88 | """Expand vector x into into vector of length equal to vector |
---|
| 89 | mask such that elements of x are placed where mask is one. |
---|
| 90 | Number of ones in mask must equal len(x).""" |
---|
| 91 | |
---|
[6818] | 92 | import numpy as num |
---|
[18] | 93 | |
---|
[6818] | 94 | assert isinstance(x, num.ndarray) |
---|
| 95 | assert isinstance(mask, num.ndarray) |
---|
[18] | 96 | #FIXME: Assert that mask contains only ones and zeros |
---|
[6818] | 97 | assert len(x) == num.sum(mask), 'Number of ones in mask must equal length of x' |
---|
[18] | 98 | |
---|
| 99 | d = len(mask) |
---|
[6818] | 100 | y = num.zeros(d, x.dtype) |
---|
[18] | 101 | |
---|
| 102 | i = 0 |
---|
| 103 | for s in range(d): |
---|
| 104 | if mask[s]: |
---|
| 105 | y[s] = x[i] |
---|
| 106 | i += 1 |
---|
| 107 | |
---|
| 108 | return y |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | def pwr2trunc(N, e = None, dir = 0): |
---|
| 112 | """N1 = pwr2trunc(N, e, dir) |
---|
| 113 | |
---|
| 114 | If e is None, let e be the largest integer such that N > 2**e. |
---|
| 115 | |
---|
| 116 | If dir = 0 (default) |
---|
| 117 | Compute the nearest number smaller than N divisible by 2^e |
---|
| 118 | if dir == 1 |
---|
| 119 | Compute the nearest number greater than N divisible by 2^e |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | """ |
---|
| 123 | |
---|
| 124 | import math |
---|
| 125 | |
---|
| 126 | if e is None: |
---|
| 127 | e = int(math.log(N)/math.log(2)) # Maximal exponent |
---|
| 128 | |
---|
| 129 | k = N % 2**e |
---|
| 130 | N1 = N - k |
---|
| 131 | if dir == 1: |
---|
| 132 | N1 = 2*N1 |
---|
| 133 | |
---|
| 134 | return N1, e |
---|