Newer
Older
import math
import sys
import operator
import types
from pytools.decorator import decorator
Andreas Klöckner
committed
Andreas Klöckner
committed
def delta(x,y):
if x == y:
return 1
else:
return 0
Andreas Klöckner
committed
def factorial(n):
from operator import mul
Andreas Klöckner
committed
assert n == int(n)
return reduce(mul, (i for i in xrange(1,n+1)), 1)
def norm_1(iterable):
return sum(abs(x) for x in iterable)
def norm_2(iterable):
return sum(x**2 for x in iterable)**0.5
def norm_inf(iterable):
return max(abs(x) for x in iterable)
return sum(i**p for i in iterable)**(1/p)
class Norm(object):
def __init__(self, p):
self.p = p
def __call__(self, iterable):
return sum(i**self.p for i in iterable)**(1/self.p)
Andreas Klöckner
committed
# Data structures ------------------------------------------------------------
class Record(object):
def __init__(self, valuedict=None, exclude=["self"], **kwargs):
try:
fields = self.__class__.fields
except AttributeError:
self.__class__.fields = fields = set()
if valuedict is not None:
kwargs.update(valuedict)
for key, value in kwargs.iteritems():
if not key in exclude:
fields.add(key)
setattr(self, key, value)
def copy(self, **kwargs):
for f in self.__class__.fields:
if f not in kwargs:
kwargs[f] = getattr(self, f)
return self.__class__(**kwargs)
def __getstate__(self):
return dict(
(key, getattr(self, key))
for key in self.__class__.fields)
def __setstate__(self, valuedict):
try:
fields = self.__class__.fields
except AttributeError:
self.__class__.fields = fields = set()
for key, value in valuedict.iteritems():
fields.add(key)
setattr(self, key, value)
Andreas Klöckner
committed
class Reference(object):
def __init__( self, value ):
self.V = value
def get( self ):
return self.V
def set( self, value ):
self.V = value
def _attrsetdefault(obj, name, default_thunk):
"Similar to .setdefault in dictionaries."
try:
return getattr(obj, name)
except AttributeError:
default = default_thunk()
setattr(obj, name, default)
return default
@decorator
def memoize(func, *args):
# by Michele Simionato
# http://www.phyast.pitt.edu/~micheles/python/
dic = _attrsetdefault(func, "_memoize_dic", dict)
if args in dic:
return dic[args]
else:
result = func(*args)
dic[args] = result
return result
FunctionValueCache = memoize
Andreas Klöckner
committed
@decorator
def memoize_method(method, instance, *args):
dic = _attrsetdefault(instance, "_memoize_dic_"+method.__name__, dict)
try:
return dic[args]
except KeyError:
result = method(instance, *args)
dic[args] = result
return result
FunctionValueCache = memoize
Andreas Klöckner
committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
class DictionaryWithDefault(object):
def __init__(self, default_value_generator, start = {}):
self._Dictionary = dict(start)
self._DefaultGenerator = default_value_generator
def __getitem__(self, index):
try:
return self._Dictionary[index]
except KeyError:
value = self._DefaultGenerator(index)
self._Dictionary[index] = value
return value
def __setitem__(self, index, value):
self._Dictionary[index] = value
def __contains__(self, item):
return True
def iterkeys(self):
return self._Dictionary.iterkeys()
def __iter__(self):
return self._Dictionary.__iter__()
def iteritems(self):
return self._Dictionary.iteritems()
class FakeList(object):
def __init__(self, f, length):
self._Length = length
self._Function = f
def __len__(self):
return self._Length
def __getitem__(self, index):
try:
return [self._Function(i)
for i in range(*index.indices(self._Length))]
except AttributeError:
return self._Function(index)
class DependentDictionary(object):
def __init__(self, f, start = {}):
self._Function = f
self._Dictionary = start.copy()
def copy(self):
return DependentDictionary(self._Function, self._Dictionary)
Loading
Loading full blame...