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 | |
---|
15 | import types |
---|
16 | import numpy as num |
---|
17 | |
---|
18 | if isinstance(x, num.ndarray): |
---|
19 | return num.sum(x) |
---|
20 | elif isinstance(x, (list, tuple)): |
---|
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 | |
---|
30 | import numpy as num |
---|
31 | |
---|
32 | x = num.reshape(x, (A.shape[1], 1)) #Make x a column vector |
---|
33 | |
---|
34 | return num.dot(A, x) |
---|
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 | |
---|
52 | import numpy as num |
---|
53 | |
---|
54 | d = len(N) |
---|
55 | |
---|
56 | X = [] |
---|
57 | for s in range(d): |
---|
58 | local_shape = num.ones(d) |
---|
59 | local_shape[s] = N[s] |
---|
60 | a = num.arange(N[s]) |
---|
61 | if N[s] > 1: |
---|
62 | a = a.astype(num.float)/(N[s]-1) |
---|
63 | else: |
---|
64 | a = a.astype(num.float) |
---|
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: |
---|
73 | e.append(num.ones(N[t])) |
---|
74 | |
---|
75 | # Take kronecker product of all dimensions |
---|
76 | # |
---|
77 | x = 1 |
---|
78 | for t in range(d): |
---|
79 | x=num.multiply.outer(x,e[t]) |
---|
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 | |
---|
92 | import numpy as num |
---|
93 | |
---|
94 | assert isinstance(x, num.ndarray) |
---|
95 | assert isinstance(mask, num.ndarray) |
---|
96 | #FIXME: Assert that mask contains only ones and zeros |
---|
97 | assert len(x) == num.sum(mask), 'Number of ones in mask must equal length of x' |
---|
98 | |
---|
99 | d = len(mask) |
---|
100 | y = num.zeros(d, x.dtype) |
---|
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 |
---|