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
31 import numpy.testing
32 _last_m = numpy.testing.memusage()
39
41 return sum(a*b for (a,b) in izip(seq1,seq2))
42
44 """Filter away .[] from indexed array names."""
45 name = name.replace(".","_")
46 name = name.replace("[","_")
47 name = name.replace("]","_")
48 return name
49
51 if isinstance(i, int):
52 return str(i)
53 return "_".join(str(j) for j in i)
54
56 sfc_error("FIXME")
57
58 if not isinstance(fe, str):
59 fe = strings.finite_element_classname(fe)
60
61 if "_" in fe:
62 fe = fe.split('_')[1]
63
64 return fe in ["DiscontinuousLagrange",
65 "VectorDiscontinuousLagrange",
66 "TensorDiscontinuousLagrange",
67 "ArnoldFalkWintherWeakSymU",
68 "ArnoldFalkWintherWeakSymP"]
69
71 sfc_error("FIXME")
72
73 if not isinstance(fe, str):
74 fe = strings.finite_element_classname(fe)
75
76 if "_" in fe:
77 fe = fe.split('_')[1]
78
79 return fe in ["ArnoldFalkWintherWeakSymSigma",
80 "Nedelec2Hdiv",
81 "Nedelec2HdivPtv",
82 "Nedelec",
83 "RaviartThomas",
84 "Robust",
85 "RobustPtv"]
86
88 """Check that i is in [a,b), raise exception otherwise."""
89 if (i < a or i >= b) and (a != b):
90 raise ValueError(msg)
91
93 s = set()
94 for i in sequence:
95 if not i in s:
96 s.add(i)
97 yield i
98
100 newindices = []
101 for ii in indices:
102 jj = tuple((j if keep[i] else None) for (i,j) in enumerate(ii))
103 newindices.append(jj)
104 return tuple(unique(newindices))
105
107
108
109 return tuple(dims)
110
112 """Returns a permutation of all multiindices within the range of a rank 0, 1, or 2 tensor shape."""
113 if len(shape) == 0:
114 return [(0,)]
115 if len(shape) == 1:
116 return [(k,) for k in range(shape[0])]
117 if len(shape) == 2:
118 return [(k1,k2) for k1 in range(shape[0]) for k2 in range(shape[1])]
119 raise ValueError("Shapes with rank 3 or higher not implemented in permute(shape)")
120
122 return zip( range(len(l)), l )
123
125 """If x is not a list type, it is repeated in a list wantlen times.
126 Otherwise checks it x the correct length. Always returns a list
127 with length wantlen or raises an exception."""
128 if isinstance(x, tuple):
129 x = list(x)
130 if not isinstance(x, list):
131 x = [x,]
132 if len(x) == 1:
133 x = wantlen*x
134 if len(x) != (wantlen):
135 raise ValueError("Got list with size " + str(len(x)) + ", need " + str(wantlen) + ".")
136 return x
137
139 return [m[i] for i in range(len(m))]
140
142 if len(l) != m*n:
143 raise ValueError("Invalid sizes: %d * %d != %d" % (m, n, l))
144 return swiginac.matrix(m, n, l)
145
147 return swiginac.matrix(len(l), 1, l)
148
150 return hasattr(f, 'func_name')
151
153 if hasattr(f, 'func_name'):
154 return False
155 return hasattr(f, '__call__')
156
158 """Get the func_code object from a function or functor object."""
159 if not callable(f):
160 raise RuntimeError("Object is not callable!")
161 fc = None
162 if is_function(f):
163 fc = f.func_code
164 if is_functor(f):
165 fc = f.__call__.im_func.func_code
166 return fc
167
170
173