replaced pattern with my own version

This commit is contained in:
Stephen McQuay 2011-05-04 21:36:42 -06:00
parent 09fc14b99d
commit 9a366b0428

View File

@ -5,6 +5,7 @@ import numpy as np
from functools import wraps from functools import wraps
import itertools import itertools
import interp
import logging import logging
log = logging.getLogger('interp') log = logging.getLogger('interp')
@ -202,30 +203,20 @@ def memoize(f):
return cache[x] return cache[x]
return memf return memf
def combinations_with_replacement(iterable, r):
"""
What I really need for the pattern function only
exists in python 2.7 and greater. The docs suggest
the implementation in this function as a
replacement.
source:
http://docs.python.org/library/itertools.html#itertools.combinations_with_replacement
"""
pool = tuple(iterable)
n = len(pool)
for indices in itertools.product(range(n), repeat=r):
if sorted(indices) == list(indices):
yield tuple(pool[i] for i in indices)
@memoize @memoize
def pattern(simplex_size, nu): def pattern(simplex_size, nu):
""" """
my useful docstring my useful docstring
""" """
log.debug("pattern: simplex: %d, order: %d" % (simplex_size, nu)) log.error("pattern: simplex: %d, order: %d" % (simplex_size, nu))
return [i for i in combinations_with_replacement(xrange(simplex_size), nu) if len(set(i)) != 1]
r = []
for i in itertools.product(xrange(simplex_size), repeat = nu):
if len(set(i)) !=1:
r.append(tuple(sorted(i)))
return list(set(r))
if __name__ == '__main__': if __name__ == '__main__':
print len(pattern(3, 2)), pattern(3, 2) print len(pattern(3, 2)), pattern(3, 2)
@ -236,6 +227,7 @@ if __name__ == '__main__':
print len(pattern(3, 4)), pattern(3, 4) print len(pattern(3, 4)), pattern(3, 4)
print len(pattern(4, 4)), pattern(4, 4) print len(pattern(4, 4)), pattern(4, 4)
print len(pattern(3, 2)), pattern(3, 2) print len(pattern(3, 2)), pattern(3, 2)
print len(pattern(4, 2)), pattern(4, 2) print len(pattern(4, 2)), pattern(4, 2)
@ -244,6 +236,7 @@ if __name__ == '__main__':
print len(pattern(3, 4)), pattern(3, 4) print len(pattern(3, 4)), pattern(3, 4)
print len(pattern(4, 4)), pattern(4, 4) print len(pattern(4, 4)), pattern(4, 4)
print len(pattern(3, 2)), pattern(3, 2) print len(pattern(3, 2)), pattern(3, 2)
print len(pattern(4, 2)), pattern(4, 2) print len(pattern(4, 2)), pattern(4, 2)
@ -252,6 +245,7 @@ if __name__ == '__main__':
print len(pattern(3, 4)), pattern(3, 4) print len(pattern(3, 4)), pattern(3, 4)
print len(pattern(4, 4)), pattern(4, 4) print len(pattern(4, 4)), pattern(4, 4)
print len(pattern(3, 2)), pattern(3, 2) print len(pattern(3, 2)), pattern(3, 2)
print len(pattern(4, 2)), pattern(4, 2) print len(pattern(4, 2)), pattern(4, 2)