예제 #1
0
def MulOp(x, y):  # (* x = x * y *)
    global RH
    if (x.mode == ORB.Const) and (y.mode == ORB.Const):
        x.a = x.a * y.a
    elif (y.mode == ORB.Const) and (y.a >= 2) and (log2(y.a) == 1):
        load(x)
        Put1(Lsl, x.r, x.r, log2(y.a))
    elif y.mode == ORB.Const:
        load(x)
        Put1a(Mul, x.r, x.r, y.a)
    elif (x.mode == ORB.Const) and (x.a >= 2) and (log2(x.a) == 1):
        load(y)
        Put1(Lsl, y.r, y.r, log2(x.a))
        x.mode = Reg
        x.r = y.r
    elif x.mode == ORB.Const:
        load(y)
        Put1a(Mul, y.r, y.r, x.a)
        x.mode = Reg
        x.r = y.r
    else:
        load(x)
        load(y)
        Put0(Mul, RH - 2, x.r, y.r)
        RH -= 1
        x.r = RH - 1
예제 #2
0
def _main(f):
    """
    >>> _main('abaab')
    unigram: nbits < 7.906891
    bigram:  nbits < 7.584963

    >>> _main('bababa')
    unigram: nbits < 9.129283
    bigram:  nbits < 6.584963
    """
    text = ''.join(l.strip() for l in f)
    A = len(set(text))

    k_unigram = defaultdict(int)
    logprob_unigram = 0
    for (n, ai) in enumerate(text):
        p = (k_unigram[ai] + 1) / (n + A)
        assert 0 <= p <= 1, 'probabilities must be in [0,1]'
        logprob_unigram += log2(p)
        k_unigram[ai] += 1
    print 'unigram: nbits < %f' % (2 - logprob_unigram)

    k_bigram = defaultdict(int)
    njs = defaultdict(int)
    logprob_bigram = log2(1.0 / A)
    for (aj, ai) in bigrams(text):
        kij = k_bigram[(ai, aj)]
        nj = njs[aj]
        p = (kij + 1) / (nj + A)
        assert 0 <= p <= 1, 'probabilities must be in [0,1]'
        logprob_bigram += log2(p)
        k_bigram[(ai, aj)] += 1
        njs[aj] += 1
    print 'bigram:  nbits < %f' % (2 - logprob_bigram)
예제 #3
0
def MulOp(x, y): # (* x = x * y *)
  global RH
  if (x.mode == ORB.Const) and (y.mode == ORB.Const):
    x.a = x.a * y.a
  elif (y.mode == ORB.Const) and (y.a >= 2) and (log2(y.a) == 1):
    load(x)
    Put1(Lsl, x.r, x.r, log2(y.a))
  elif y.mode == ORB.Const:
    load(x)
    Put1a(Mul, x.r, x.r, y.a)
  elif (x.mode == ORB.Const) and (x.a >= 2) and (log2(x.a) == 1):
    load(y)
    Put1(Lsl, y.r, y.r, log2(x.a))
    x.mode = Reg
    x.r = y.r
  elif x.mode == ORB.Const:
    load(y)
    Put1a(Mul, y.r, y.r, x.a)
    x.mode = Reg
    x.r = y.r
  else:
    load(x)
    load(y)
    Put0(Mul, RH-2, x.r, y.r)
    RH -= 1
    x.r = RH-1
예제 #4
0
def setup(label, ps):
    progress(label, "starting setup")
    try:
        with open(jar(label), "rb") as brine:
            ps = pickle.load(brine)
    except IOError:
        progress(label, "generating distributions")

    secret_l2_dist = ps.secret_l2_distribution()
    progress(label, "done secret l2 dist")
    query_l2_dist = ps.query_l2_distribution()
    progress(label, "done query l2 dist")
    e3_dist = ps.e3_distribution()
    progress(label, "done e3 dist")
    one_shot_dist = ps.one_shot_distribution()
    progress(label, "done one shot dist")

    f = tail_probability(one_shot_dist, ps.threshold())
    p(label, "oneshot", float(-log2(f)))
    p(label, "max_abs_e3", max(dist_absolute(e3_dist)))

    with open(jar(label), "wb") as brine:
        pickle.dump(ps, brine)

    progress(label, "done setup")
def volume_estimate(ps, query_l2_dist, alpha, optimize_e3=False):
    dim = ps.inner_product_dimension()
    threshold = ps.threshold()
    abs_e3_dist = dist_absolute(ps.e3_distribution())

    def crunch(cutoff):
        e3_cutoff = sorted(abs_e3_dist)[cutoff]
        t = threshold - e3_cutoff
        f = partial(queries, dim=dim, t=t, alpha=alpha)
        result = expectation(query_l2_dist, f)
        return result

    cutoff = sorted(abs_e3_dist).index(min(top_quantile(abs_e3_dist, 100)))
    result = crunch(cutoff)
    e3_cutoff = sorted(abs_e3_dist)[cutoff]
    if optimize_e3:  # Optimize e3_cutoff
        step = 16  #XXX: arbitrary
        while step > 0:
            decreasing = True
            while (len(abs_e3_dist) + cutoff - step > 0) and decreasing:
                candidate = crunch(cutoff - step)
                decreasing = candidate < result
                if decreasing:
                    result = candidate
                    e3_cutoff = sorted(abs_e3_dist)[cutoff - step]
                    cutoff -= step
            step //= 2
    result *= 2  # Account for absolute value bars
    return -log2(result), e3_cutoff
예제 #6
0
    def do(label, lgsq, lgeq):
        with open(jar(label), "rb") as brine:
            ps = pickle.load(brine)

        progress(label, "starting s/{}/e/{}".format(lgsq, lgeq))
        f = tail_probability(ps.one_shot_s_quantile(lgsq), ps.threshold())
        p(label, "s/{}/e/{}/cost".format(lgsq, lgeq), float(-log2(f)))
        progress(label, "done s/{}/e/{}".format(lgsq, lgeq))
예제 #7
0
def calc_gain(lNums, p_iSum=None):
    if p_iSum is None: p_iSum = sum(lNums)
    fSum = float(p_iSum)
    fResult = 0
    for num in lNums:
        value = (num / fSum)
        fResult += -value * log2(value)
    return fResult
예제 #8
0
def DivOp(op, x, y):  # (* x = x op y *)
    global RH
    e = log2(y.a)  # note: shadows math.e
    if op == ORS.div:
        if (x.mode == ORB.Const) and (y.mode == ORB.Const):
            if y.a > 0:
                x.a = x.a / y.a
            else:
                ORS.Mark("bad divisor")
        elif (y.mode == ORB.Const) and (y.a >= 2) and (e == 1):
            load(x)
            Put1(Asr, x.r, x.r, e)
        elif y.mode == ORB.Const:
            if y.a > 0:
                load(x)
                Put1a(Div, x.r, x.r, y.a)
            else:
                ORS.Mark("bad divisor")
        else:
            load(y)
            if check:
                Trap(LE, 6)
            load(x)
            Put0(Div, RH - 2, x.r, y.r)
            RH -= 1
            x.r = RH - 1

    else:  # (*op == ORS.mod*)
        if (x.mode == ORB.Const) and (y.mode == ORB.Const):
            if y.a > 0:
                x.a = x.a % y.a
            else:
                ORS.Mark("bad modulus")
        elif (y.mode == ORB.Const) and (y.a >= 2) and (e == 1):
            load(x)
            if e <= 16:
                Put1(And, x.r, x.r, y.a - 1)
            else:
                Put1(Lsl, x.r, x.r, 32 - e)
                Put1(Ror, x.r, x.r, 32 - e)
        elif y.mode == ORB.Const:
            if y.a > 0:
                load(x)
                Put1a(Div, x.r, x.r, y.a)
                Put0(Mov + U, x.r, 0, 0)
            else:
                ORS.Mark("bad modulus")
        else:
            load(y)
            if check:
                Trap(LE, 6)
            load(x)
            Put0(Div, RH - 2, x.r, y.r)
            Put0(Mov + U, RH - 2, 0, 0)
            RH -= 1
            x.r = RH - 1
예제 #9
0
def DivOp(op, x, y): # (* x = x op y *)
  global RH
  e = log2(y.a) # note: shadows math.e
  if op == ORS.div:
    if (x.mode == ORB.Const) and (y.mode == ORB.Const):
      if y.a > 0:
        x.a = x.a / y.a
      else:
        ORS.Mark("bad divisor")
    elif (y.mode == ORB.Const) and (y.a >= 2) and (e == 1):
      load(x)
      Put1(Asr, x.r, x.r, e)
    elif y.mode == ORB.Const:
      if y.a > 0:
        load(x)
        Put1a(Div, x.r, x.r, y.a)
      else:
        ORS.Mark("bad divisor")
    else:
      load(y);
      if check:
        Trap(LE, 6)
      load(x)
      Put0(Div, RH-2, x.r, y.r)
      RH -= 1
      x.r = RH-1

  else: # (*op == ORS.mod*)
    if (x.mode == ORB.Const) and (y.mode == ORB.Const):
      if y.a > 0:
        x.a = x.a % y.a
      else:
        ORS.Mark("bad modulus")
    elif (y.mode == ORB.Const) and (y.a >= 2) and (e == 1):
      load(x);
      if e <= 16:
        Put1(And, x.r, x.r, y.a-1)
      else:
        Put1(Lsl, x.r, x.r, 32-e)
        Put1(Ror, x.r, x.r, 32-e)
    elif y.mode == ORB.Const:
      if y.a > 0:
        load(x)
        Put1a(Div, x.r, x.r, y.a)
        Put0(Mov+U, x.r, 0, 0)
      else:
        ORS.Mark("bad modulus")
    else:
      load(y);
      if check:
        Trap(LE, 6)
      load(x)
      Put0(Div, RH-2, x.r, y.r)
      Put0(Mov+U, RH-2, 0, 0)
      RH -= 1
      x.r = RH-1
예제 #10
0
    def __init__(self, n, q=None, improper=False):
        self.n = n
        if q is None:
            self.q = int(2**round(0.5 + 3.5 + log2(n)))
        else:
            self.q = q
        self.improper = improper

        self.cache_dist_1 = None
        self.cache_dist_2 = None
        self.cache_dist_3 = None
예제 #11
0
파일: __init__.py 프로젝트: jenix21/ndbg
def run(settings, load_cb=None):
  global _running
  _running = True


  # actual init of the UI is done via this callback,
  # which is posted to the MessageLoop at the end of this function
  def do_init():
    # create them...
    resources = Resources(settings, ndbg.get_basedir())

    global _mc
    mw = MainWindow(settings, resources)

    def run_load_cb():
      log2("UI init: running on_load callback")
      if load_cb:
        load_cb(_mc)
      return False # ensure the timeout is a one-time thing
    def on_ready(*args):
      log2("UI init: window shown, scheduling load in 200ms")
      MessageLoop.add_delayed_message(run_load_cb, 200)
    if load_cb:
      mw.connect('show', on_ready)

    settings.set_delayed_save(True)
    _mc = MainControl(settings, mw)




  # Go! :)
  MessageLoop.add_message(do_init)
  MessageLoop.run()


  # cleanup
  _running = False
  if _mc:
    _mc.destroy()
  log2("ui.run done")
예제 #12
0
def run(settings, load_cb=None):
    global _running
    _running = True

    # actual init of the UI is done via this callback,
    # which is posted to the MessageLoop at the end of this function
    def do_init():
        # create them...
        resources = Resources(settings, ndbg.get_basedir())

        global _mc
        mw = MainWindow(settings, resources)

        def run_load_cb():
            log2("UI init: running on_load callback")
            if load_cb:
                load_cb(_mc)
            return False  # ensure the timeout is a one-time thing

        def on_ready(*args):
            log2("UI init: window shown, scheduling load in 200ms")
            MessageLoop.add_delayed_message(run_load_cb, 200)

        if load_cb:
            mw.connect('show', on_ready)

        settings.set_delayed_save(True)
        _mc = MainControl(settings, mw)

    # Go! :)
    MessageLoop.add_message(do_init)
    MessageLoop.run()

    # cleanup
    _running = False
    if _mc:
        _mc.destroy()
    log2("ui.run done")
예제 #13
0
    def setup(label, ps):
        progress(label, "starting setup")
        try:
            with open(jar(label), "rb") as brine:
                ps = pickle.load(brine)
        except IOError:
            progress(label, "generating distributions")

        secret_l2_dist = ps.secret_l2_distribution()
        query_l2_dist = ps.query_l2_distribution()
        one_shot_dist = ps.one_shot_distribution()

        f = tail_probability(one_shot_dist, ps.threshold())
        p(label, "oneshot", float(-log2(f)))

        with open(jar(label), "wb") as brine:
            pickle.dump(ps, brine)
def log_cap(dim, theta):
    return -0.5 * log2(2 * mp.pi * dim) - log2(
        mp.cos(theta)) + (dim - 1) * log2(mp.sin(theta))
예제 #15
0
 def on_ready(*args):
     log2("UI init: window shown, scheduling load in 200ms")
     MessageLoop.add_delayed_message(run_load_cb, 200)
예제 #16
0
 def run_load_cb():
     log2("UI init: running on_load callback")
     if load_cb:
         load_cb(_mc)
     return False  # ensure the timeout is a one-time thing
예제 #17
0
import util

if __name__ == '__main__':
    print(util.log2(1024))
    util.test_util()
예제 #18
0
파일: __init__.py 프로젝트: jenix21/ndbg
 def run_load_cb():
   log2("UI init: running on_load callback")
   if load_cb:
     load_cb(_mc)
   return False # ensure the timeout is a one-time thing
예제 #19
0
파일: __init__.py 프로젝트: jenix21/ndbg
 def on_ready(*args):
   log2("UI init: window shown, scheduling load in 200ms")
   MessageLoop.add_delayed_message(run_load_cb, 200)