1
2
3 """
4 This module contains utility functions for various stuff.
5 """
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 from itertools import izip
28 import swiginac
29
30 from sfc.common.names import finite_element_classname
31 from sfc.common.output import sfc_error
32
33 try:
34 import numpy.testing
35 _last_m = numpy.testing.memusage()
36 except:
37 _last_m = None
39 global _last_m
40 if _last_m is None:
41 print "numpy memusage not available"
42 else:
43 m = numpy.testing.memusage()
44 diff = m - _last_m
45 print msg, diff, "b, ", diff/1024, "Kb, ", diff/1024**2, "Mb"
46 _last_m = m
47
49 return sum(a*b for (a,b) in izip(seq1,seq2))
50
52 """Filter away .[] from indexed array names."""
53 name = name.replace(".","_")
54 name = name.replace("[","_")
55 name = name.replace("]","_")
56 return name
57
59 if isinstance(i, int):
60 return str(i)
61 return "_".join(str(j) for j in i)
62
64 d = {}
65 k = 0
66 for e in entries:
67 if not e in d:
68 d[e] = k
69 k += 1
70 return d
71
73 sfc_error("FIXME")
74
75 if not isinstance(fe, str):
76 fe = finite_element_classname(fe)
77
78 if "_" in fe:
79 fe = fe.split('_')[1]
80
81 return fe in ["DiscontinuousLagrange",
82 "VectorDiscontinuousLagrange",
83 "TensorDiscontinuousLagrange",
84 "ArnoldFalkWintherWeakSymU",
85 "ArnoldFalkWintherWeakSymP"]
86
88 sfc_error("FIXME")
89
90 if not isinstance(fe, str):
91 fe = finite_element_classname(fe)
92
93 if "_" in fe:
94 fe = fe.split('_')[1]
95
96 return fe in ["ArnoldFalkWintherWeakSymSigma",
97 "Nedelec2Hdiv",
98 "Nedelec2HdivPtv",
99 "Nedelec",
100 "RaviartThomas",
101 "Robust",
102 "RobustPtv"]
103
105 """Check that i is in [a,b), raise exception otherwise."""
106 if (i < a or i >= b) and (a != b):
107 raise ValueError(msg)
108
110 s = set()
111 for i in sequence:
112 if not i in s:
113 s.add(i)
114 yield i
115
117 newindices = []
118 for ii in indices:
119 jj = tuple((j if keep[i] else None) for (i,j) in enumerate(ii))
120 newindices.append(jj)
121 return tuple(unique(newindices))
122
124
125
126 return tuple(dims)
127
129 """Returns a permutation of all multiindices within the range of a rank 0, 1, or 2 tensor shape."""
130 if len(shape) == 0:
131 return [(0,)]
132 if len(shape) == 1:
133 return [(k,) for k in range(shape[0])]
134 if len(shape) == 2:
135 return [(k1,k2) for k1 in range(shape[0]) for k2 in range(shape[1])]
136 raise ValueError("Shapes with rank 3 or higher not implemented in permute(shape)")
137
139 return zip( range(len(l)), l )
140
142 """If x is not a list type, it is repeated in a list wantlen times.
143 Otherwise checks it x the correct length. Always returns a list
144 with length wantlen or raises an exception."""
145 if isinstance(x, tuple):
146 x = list(x)
147 if not isinstance(x, list):
148 x = [x,]
149 if len(x) == 1:
150 x = wantlen*x
151 if len(x) != (wantlen):
152 raise ValueError("Got list with size " + str(len(x)) + ", need " + str(wantlen) + ".")
153 return x
154
156 return [m[i] for i in range(len(m))]
157
159 if len(l) != m*n:
160 raise ValueError("Invalid sizes: %d * %d != %d" % (m, n, l))
161 return swiginac.matrix(m, n, l)
162
164 return swiginac.matrix(len(l), 1, l)
165
167 return hasattr(f, 'func_name')
168
170 if hasattr(f, 'func_name'):
171 return False
172 return hasattr(f, '__call__')
173
175 """Get the func_code object from a function or functor object."""
176 if not callable(f):
177 raise RuntimeError("Object is not callable!")
178 fc = None
179 if is_function(f):
180 fc = f.func_code
181 if is_functor(f):
182 fc = f.__call__.im_func.func_code
183 return fc
184
187
190