Example #1
0
class W_AbstractObjectWithIdentityHash(W_Object):
    """Object with explicit hash (ie all except small
    ints and floats)."""
    _attrs_ = ['hash']

    #XXX maybe this is too extreme, but it's very random
    hash_generator = rrandom.Random()
    UNASSIGNED_HASH = sys.maxint

    hash = UNASSIGNED_HASH # default value

    def setchar(self, n0, character):
        raise NotImplementedError()

    def gethash(self):
        if self.hash == self.UNASSIGNED_HASH:
            self.hash = hash = intmask(self.hash_generator.genrand32()) // 2
            return hash
        return self.hash

    def invariant(self):
        return isinstance(self.hash, int)

    def _become(self, w_other):
        self.hash, w_other.hash = w_other.hash, self.hash
Example #2
0
def entry_point(argv):
    # Adjust the amount of work we do based on command-line arguments.
    # NUM_INPUTS increases the number of loop iterations.
    # INPUT_LENGTH increases the amount of work done per loop iteration.
    NUM_INPUTS = 1000
    INPUT_LENGTH = 50
    if len(argv) > 1:
        NUM_INPUTS = int(argv[1])
    if len(argv) > 2:
        INPUT_LENGTH = int(argv[2])
    if len(argv) > 3:
        raise RuntimeError("too many arguments")

    # Build up the regex pattern.
    # Target pattern: (a|b)*a(a|b){20}a(a|b)*
    # For now we use the same pattern every time, but it must be
    # dynamically constructed or it gets eliminated at compile-time.
    prefix = Sequence(Repetition(Alternative(Char("a"), Char("b"))), Char("a"))
    suffix = Sequence(Char("a"), Repetition(Alternative(Char("a"), Char("b"))))
    pattern = prefix
    for _ in xrange(20):
        pattern = Sequence(pattern,  Alternative(Char("a"), Char("b")))
    pattern = Sequence(pattern, suffix)

    # Generate "random input" to match against the pattern.
    # Ideally this would come from the outside world, but stdio
    # on pypy.js doesn't seem to work just yet.
    print "Generating", NUM_INPUTS, "strings of length", INPUT_LENGTH, "..."
    inputs = [None] * NUM_INPUTS
    r = rrandom.Random(42)
    for i in xrange(len(inputs)):
        s = []
        for _ in xrange(INPUT_LENGTH):
            if r.random() > 0.5:
                s.append("a")
            else:
                s.append("b")
        inputs[i] = "".join(s)

    # Run each input string through the regex.
    # Time how long it takes for the total run.
    print "Matching all strings against the regex..."
    ts = clock()
    for i in xrange(len(inputs)):
        # No output, we just want to exercise the loop.
        matched = match(pattern, inputs[i])
    tdiff = clock() - ts
    print "Done!"
    print "Matching time for %d strings: %f" % (len(inputs), tdiff)
    print "Performed %f matches per second." % (len(inputs) / tdiff,)
    return 0
Example #3
0
class W_AbstractObjectWithIdentityHash(W_Object):
    """Object with explicit hash (ie all except small
    ints and floats)."""
    _attrs_ = ['hash']
    repr_classname = "W_AbstractObjectWithIdentityHash"

    hash_generator = rrandom.Random()
    UNASSIGNED_HASH = sys.maxint
    hash = UNASSIGNED_HASH # default value

    def fillin(self, space, g_self):
        self.hash = g_self.get_hash()

    def setchar(self, n0, character):
        raise NotImplementedError()

    def gethash(self):
        if self.hash == self.UNASSIGNED_HASH:
            self.hash = hash = intmask(self.hash_generator.genrand32()) // 2
            return hash
        return self.hash

    def invariant(self):
        return isinstance(self.hash, int)

    def become(self, w_other):
        if not self.can_become(w_other):
            return False
        if self.is_same_object(w_other):
            return False
        self._become(w_other)
        return True

    def can_become(self, w_other):
        # TODO -- what about become: with a Float and a CompiledMethod etc.?
        # We might be in trouble regarding W_LargePositiveInteger1Word, too.
        return self.__class__ is w_other.__class__

    def _become(self, w_other):
        assert isinstance(w_other, W_AbstractObjectWithIdentityHash)
        self.hash, w_other.hash = w_other.hash, self.hash
Example #4
0
    if isnan(x):
        return NAN

    if x == INFINITY:
        return INFINITY

    if x == -INFINITY:
        return 0

    return math.exp(x)


import time
from rpython.rlib import rrandom

random = rrandom.Random(int(time.time()))


# 15.8.2.14
@w_return
def js_random(this, args):
    return random.random()


# 15.8.1.1
E = math.e

# 15.8.1.2
LN10 = math.log(10)

# 15.8.1.3
Example #5
0
 def __init__(self, space, w_anything):
     self._rnd = rrandom.Random()
     self.seed(space, w_anything)
def run(n, qry):
    ksize = max(2, n)
    rg = rrandom.Random()
    a = []
    for i in xrange(n):
        #b = [r_ushort(int(rg.random() * pow(2, 15) - 1)) for elem0 in xrange(32)]
        b = [int(rg.random() * pow(2, 15) - 1) for elem0 in xrange(32)]
        # b.sort()
        TimSort(b).sort()
        # a.append(b)
        a.append([r_ushort(elem) for elem in b])
        # a.append(d)
    #a = [TimSort([r_ushort(int(rg.random() * pow(2, 15) - 1)) for elem0 in xrange(32)]).sort() for elem1 in xrange(n)]
    # print 'short add', a[0][0] + a[0][0]
    u = p = 0
    Atmp = range(6)
    dna = 'atgcgc'
    qsort(Atmp, dna)
    print 'qsort', Atmp, [dna[elem] for elem in Atmp]
    for i in xrange(1):
        x = [0] * 32
        y = [0] * 32
        # for j in xrange(32):
        #    x[j] = rg.random()
        #    y[j] = rg.random()
        #x = [rg.random() for elem in xrange(32)]
        #y = [rg.random() for elem in xrange(32)]
        #x = [intmask(elem) for elem in [r_ushort(0)] * 32]
        #x = map(intmask, [0] * 32)
        y = [intmask(elem) for elem in xrange(32)]
        #rgs = range(32)
        #y = map(intmask, rgs)
        #x = y = [0] * 32
        u, p = mannwhitneyu(x, y, True)
        p = pearson(x, y)

    print 'p value is', u, p

    print k2n('ta' * 12), intmask(int('123'))
    test_seq = 'tgatcgctgtagctgatgctcatgctatgctatcgtagtcgtgctagctagcatcgatcgatcgctagaaacagctgcgtatctatctatatatatattaggagaatgtgagaga'
    test_n = seq2n(test_seq)
    # for i in test_n:
    #    print 'seq2n test', i
    canopy(a)

    print [r_uint(elem) for elem in test_n]
    buck = [[r_ushort(0) for elem0 in xrange(pow(scale, ksize))]
            for elem1 in xrange(len(qry))]
    f0 = open(qry, 'r')
    f1 = open(qry, 'r')
    seqs0 = parse(f0)
    seqs1 = parse(f1)
    for seq0, seq1 in izip([seqs0, seqs1]):
        # print seq0.seq, seq1.seq
        print 'test seq2n'
        # print [nb for nb in seq2n(seq0.seq)]
        kmc(seq0.seq, buck[0], ksize, scale)
        kmc(seq1.seq, buck[0], ksize, scale)

    f0.close()
    f1.close()

    print [intmask(elem) for elem in buck[0][:15]]
    '''
from rpython.rlib import rmmap
from rpython.rlib.listsort import TimSort
from rpython.rlib import listsort
#from struct import pack
from time import time, sleep
import gc
from heapq import heappush, heappop, heapreplace, heapify


# define some constant number
MIN = 7
#MIN = 13
MED = 23
MAX = 41

random = rrandom.Random(10).random

# swap 2 selected elem in a list


def swap(x, i, j):
    x[i], x[j] = x[j], x[i]

# in-place sort


def insort(x, l, r, key=lambda x: x):
    for i in xrange(l, r):
        v = x[i]
        pivot = key(v)
        j = i - 1
Example #8
0
 def init(self, seed):
     self.sthread = rstacklet.StackletThread()
     self.random = rrandom.Random(seed)
Example #9
0
from rpython.rlib import rrandom, rarithmetic

from pycket.prims.expose import default, expose, expose_val
from pycket import values, values_parameter
from pycket import vector as values_vector
from pycket.error import SchemeException

# XXX for now just always use a global rng

rng = rrandom.Random()


@expose("random")
def random(args):
    if not args:
        # random flonum
        return values.W_Flonum(rng.random())
    a1 = args[0]
    if isinstance(a1, values.W_Fixnum):
        upper = a1.value
        return values.W_Fixnum(int(rng.random() * upper))
    if isinstance(a1, values.W_PseudoRandomGenerator):
        return values.W_Flonum(rng.random())
    raise SchemeException("random: invalid arguments")


@expose(["flrandom", "unsafe-flrandom"], [values.W_PseudoRandomGenerator])
def flrandom(gen):
    return values.W_Flonum(rng.random())

Example #10
0
# Neil Schemenauer <*****@*****.**>
#
# Modifications to the original (Armin Rigo):
#   * import random from PyPy's lib, which is Python 2.2's plain
#     Python implementation
#   * print a doc about how to start the Translator

import sys
import math
import time

from rpython.rlib import rrandom

PRINT_IT = True

random = rrandom.Random(1)


# calculate a random number where:  a <= rand < b
def rand(a, b):
    return (b - a) * random.random() + a


# Make a matrix (we could use NumPy to speed this up)
def makeMatrix(I, J, fill=0.0):
    m = []
    for i in range(I):
        m.append([fill] * J)
    return m