def wrapped_fun(*args, **kwargs): arg_seed = None if 'seed' in kwargs: arg_seed = kwargs['seed'] del kwargs['seed'] with seed(arg_seed): used_seed = initial_seed() if 'print_seed' in kwargs: if kwargs['print_seed']: print "Random seed: %d" % used_seed del kwargs['print_seed'] # I don't know if this line is necessary, but it can't # hurt; and it would be a real pity to lose the # information you need to reproduce a segfault because # it was missing... stdout.flush() try: fn(*args, **kwargs) except StandardError, e: # We treat any sort of StandardError as a doctest # failure. (We have to eat the exception, because if # doctesting sees an exception, it doesn't display # whatever was printed before the exception happened # -- so the text we print here would be lost.) Note # that KeyboardInterrupt is not a StandardError, so # pressing Control-C doesn't print this message. print "Random testing has revealed a problem in " + fn.__name__ print "Please report this bug! You may be the first" print "person in the world to have seen this problem." print "Please include this random seed in your bug report:" print "Random seed: %d" % used_seed print repr(e)
def __init__(self, checker=None, verbose=None, optionflags=0): optionflags |= ncadoctest.NORMALIZE_WHITESPACE optionflags |= ncadoctest.ELLIPSIS OrigDocTestRunner.__init__(self, checker=checker, verbose=verbose, optionflags=optionflags) self._collect_timeit_stats = True self._timeit_stats = {} self._reset_random_seed = True self._random_seed = randstate.seed(0)
def _cftp(self, start_row): """ Implement coupling from the past. ALGORITHM: The set of Gelfand-Tsetlin patterns can partially ordered by elementwise domination. The partial order has unique maximum and minimum elements that are computed by the methods :meth:`_cftp_upper` and :meth:`_cftp_lower`. We then run the Markov chain that randomly toggles each element up or down from the past until the state reached from the upper and lower start points coalesce as described in [Propp1997]_. EXAMPLES:: sage: G = GelfandTsetlinPatterns(3, 5) sage: G._cftp(0) # random [[5, 3, 2], [4, 2], [3]] sage: G._cftp(0) in G True """ from sage.misc.randstate import current_randstate from sage.misc.randstate import seed from sage.misc.randstate import random count = self._n * self._k seedlist = [(current_randstate().long_seed(), count)] upper = [] lower = [] while True: upper = self._cftp_upper() lower = self._cftp_lower() for currseed, count in seedlist: with seed(currseed): for _ in range(count): for row in range(start_row, self._n): for col in range(self._n - row): direction = random() % 2 self._toggle_markov_chain( upper, row, col, direction) self._toggle_markov_chain( lower, row, col, direction) if all( all(x == y for x, y in zip(l1, l2)) for l1, l2 in zip(upper, lower)): break count = seedlist[0][1] * 2 seedlist.insert(0, (current_randstate().long_seed(), count)) return GelfandTsetlinPattern(upper)
def _cftp(self, start_row): """ Implement coupling from the past. ALGORITHM: The set of Gelfand-Tsetlin patterns can partially ordered by elementwise domination. The partial order has unique maximum and minimum elements that are computed by the methods :meth:`_cftp_upper` and :meth:`_cftp_lower`. We then run the Markov chain that randomly toggles each element up or down from the past until the state reached from the upper and lower start points coalesce as described in [Propp1997]_. EXAMPLES:: sage: G = GelfandTsetlinPatterns(3, 5) sage: G._cftp(0) # random [[5, 3, 2], [4, 2], [3]] sage: G._cftp(0) in G True """ from sage.misc.randstate import current_randstate from sage.misc.randstate import seed from sage.misc.randstate import random count = self._n * self._k seedlist = [(current_randstate().long_seed(), count)] upper = [] lower = [] while True: upper = self._cftp_upper() lower = self._cftp_lower() for currseed, count in seedlist: with seed(currseed): for _ in range(count): for row in range(start_row, self._n): for col in range(self._n - row): direction = random() % 2 self._toggle_markov_chain(upper, row, col, direction) self._toggle_markov_chain(lower, row, col, direction) if all(all(x == y for x,y in zip(l1, l2)) for l1, l2 in zip(upper, lower)): break count = seedlist[0][1] * 2 seedlist.insert(0, (current_randstate().long_seed(), count)) return GelfandTsetlinPattern(upper)