def logLikelihood(x, y, t_1, t_2): psigmoid = [sigmoid(i, t_1, t_2) for i in x] u = [ q * gmpy2.log(p) + (1 - q) * gmpy2.log(1 - p) for (p, q) in zip(psigmoid, y) ] return gmpy2.fsum(u)
def fast_etienne_likelihood(mod, params, kda=None, kda_x=None): ''' same as Abundance inner function, but takes advantage of constant m value when varying only theta. :argument abd: Abundance object :argument params: list containing theta and m :argument kda_x: precomputed list of exp(kda + ind*immig) ''' theta = params[0] immig = float (params[1]) / (1 - params[1]) * (mod.community.J - 1) log_immig = log (immig) theta_s = theta + mod.community.S if not kda_x: kda_x = [exp(mod._kda[val] + val * log_immig) for val in \ xrange (mod.community.J - mod.community.S)] poch1 = exp (mod._factor + log (theta) * mod.community.S - \ lpoch (immig, mod.community.J) + \ log_immig * mod.community.S + lngamma(theta)) gam_theta_s = gamma (theta_s) lik = mpfr(0.0) for val in xrange (mod.community.J - mod.community.S): lik += poch1 * kda_x[val] / gam_theta_s gam_theta_s *= theta_s + val return ((-log (lik)), kda_x)
def likelihood(self, params): ''' log-likelihood function :argument params: a list of 2 parameters: * theta = params[0] * m = params[1] :returns: log likelihood of given theta and I ''' kda = self._kda theta = params[0] immig = float(params[1]) / (1 - params[1]) * (self.community.J - 1) log_immig = log(immig) theta_s = theta + self.community.S poch1 = exp(self._factor + log(theta) * self.community.S - \ lpoch(immig, self.community.J) + \ log_immig * self.community.S + lngamma(theta)) gam_theta_s = gamma(theta_s) lik = mpfr(0.0) for abd in xrange(int(self.community.J - self.community.S)): lik += poch1 * exp(kda[abd] + abd * log_immig) / gam_theta_s gam_theta_s *= theta_s + abd return -log(lik)
def shannon_entropy(abund, inds): ''' computes Shannon entropy (H) for a given abundance table and number of individuals. :argument abund: distribution of abundances as list :returns: Shannon entropy ''' return (sum ([-spe * log(spe) for spe in abund]) + inds * log (inds)) / inds
def N(value, prec=6): pre_prec = gmp.get_context().precision adj_prec = int((prec * (gmp.log(2) + gmp.log(5))) / gmp.log(2)) gmp.get_context().precision = adj_prec try: x, y = getattr(value, 'numerator'), getattr(value, 'denominator') return x / y except AttributeError: return value
def cregulator(x, fullnetsize, synapsec): #the limit of setbase limits this output global cbase if x < synapsec: p = x / ((fullnetsize - 1) * fullnetsize) else: p = (((x - synapsec)**2) + synapsec) / ( (fullnetsize - 1) * fullnetsize) out = mp.log(p) / mp.log(cbase) return out
def logn(x: int, n: int) -> float: """ Base n logarithm of x Args: x (int): antilogarithm n (int): base Returns: float """ return gmpy2.log(x) / gmpy2.log(n)
def process_r(N): if N < 2**threshold_bits: x = gmpy2.mpz_random(random_state, (N + 1) // 2) + N // 2 + 1 return x, factor_N(x) while True: p, alpha = process_f(N) q = p**alpha Nprime = int(N // q) y, yf = process_r(Nprime) x = y * q l = gmpy2.mpfr_random(random_state) if l < gmpy2.log(N // 2) / gmpy2.log(x): return x, [p] * alpha + yf
def __init__(self, processId, primeNumber): super(Process, self).__init__() self.processId = processId self.primeNumber = mpfr(primeNumber) self.logicalTime = 0 self.vectorClock = [0 for index in range(1, number_of_processes + 1)] self.primeClock = mpfr(1) self.logClock = gmpy2.log(1) self.logPrime = gmpy2.log(primeNumber) self.queue = deque() self.receiver_queue = deque() self.receivedPrimes = list() self.receivedPrimes.append(self.primeNumber)
def lslmsr(q_, i, a): assert q_[i] + a >= 0 q = q_[:] alpha = 1 / 16. scale = 12 Bq1 = alpha * scale * sum(q) C1 = Bq1 * gmpy2.log(sum(gmpy2.exp(q_i / Bq1) for q_i in q)) q[i] += a Bq2 = alpha * scale * sum(q) C2 = Bq2 * gmpy2.log(sum(gmpy2.exp(q_i / Bq2) for q_i in q)) if a > 0: return (C2 - C1) * 1.015625 else: return (C1 - C2) * 0.984375
def lslmsr(q_, i, a): assert q_[i] + a >= 0 q = q_[:] alpha = 1/16. scale = 12 Bq1 = alpha*scale*sum(q) C1 = Bq1*gmpy2.log(sum(gmpy2.exp(q_i/Bq1) for q_i in q)) q[i] += a Bq2 = alpha*scale*sum(q) C2 = Bq2*gmpy2.log(sum(gmpy2.exp(q_i/Bq2) for q_i in q)) if a > 0: return (C2 - C1)*1.015625 else: return (C1 - C2)*0.984375
def computeLaplaceMatrix(sqdist, t, logeps=mp.mpfr("-10")): """ Compute heat approximation to Laplacian matrix using logarithms and gmpy2. Use mpfr to gain more precision. This is slow, but more accurate. Cutoff for really small values, and row/column elimination if degenerate. """ # cutoff ufunc cutoff = np.frompyfunc((lambda x: mp.inf(-1) if x < logeps else x), 1, 1) t2 = mp.mpfr(t) lt = mp.log(2 / t2) d = to_mpfr(sqdist) L = d * d L /= -2 * t2 cutoff(L, out=L) logdensity = logsumexp(L) L = exp(L - logdensity[:, None] + lt) L[np.diag_indices(len(L))] -= 2 / t2 L = np.array(to_double(L), dtype=float) # if just one nonzero element, then erase row and column degenerate = np.sum(L != 0.0, axis=1) <= 1 L[:, degenerate] = 0 L[degenerate, :] = 0 return L
def _get_kda (self, verbose=True): ''' compute K(D,A) according to etienne formula ''' specabund = [sorted(list(set(self.community.abund))), table_mpfr(self.community.abund)] sdiff = len(specabund[1]) polyn = [] # compute all stirling numbers taking advantage of recurrence function needed = {0: True} for i in xrange(sdiff): for k in xrange(1, int(specabund[0][i] + 1)): needed[int(specabund[0][i])] = True if verbose: stdout.write(' Getting some stirling numbers...\n') pre_get_stirlings(int(max(specabund[0])), needed, verbose=verbose) for i in xrange(sdiff): if verbose: stdout.write("\r Computing K(D,A) at species %s out of %s" \ % (i+1, sdiff)) stdout.flush () sai = int(specabund[0][i]) polyn1 = [stirling(sai, k) * factorial_div(k, sai) \ for k in xrange(1, sai+1)] polyn1 = power_polyn(polyn1, specabund[1][i]) \ if polyn1 else [mpfr(1.)] polyn = mul_polyn(polyn, polyn1) self._kda = [log(i) for i in polyn] if verbose: stdout.write('\n')
def metric_griffiths_2004(logliks): """ Calculate metric as in [GriffithsSteyvers2004]_. Calculates the harmonic mean of the log-likelihood values `logliks`. Burn-in values should already be removed from `logliks`. .. [GriffithsSteyvers2004] Thomas L. Griffiths and Mark Steyvers. 2004. Finding scientific topics. Proceedings of the National Academy of Sciences 101, suppl 1: 5228–5235. http://doi.org/10.1073/pnas.0307752101 .. note:: Requires `gmpy2 <https://github.com/aleaxit/gmpy>`_ package for multiple-precision arithmetic to avoid numerical underflow. :param logliks: array with log-likelihood values :return: calculated metric """ import gmpy2 # using median trick as in Martin Ponweiser's Diploma Thesis 2012, p.36 ll_med = np.median(logliks) ps = [gmpy2.exp(ll_med - x) for x in logliks] ps_mean = gmpy2.mpfr(0) for p in ps: ps_mean += p / len(ps) return float(ll_med - gmpy2.log(ps_mean) ) # after taking the log() we can use a Python float() again
def process_r_with_multiplicative_group(N): if N < 2**threshold_bits: x = gmpy2.mpz_random(random_state, (N + 1) // 2) + N // 2 + 1 xf = factor_N(x) # No factorisation hints for small factors, we can easily generate them later return x, xf, {} while True: p, alpha, phi_hint = process_f_with_multiplicative_group(N) q = p**alpha Nprime = int(N // q) y, yf, phi_hint2 = process_r_with_multiplicative_group(Nprime) x = y * q l = gmpy2.mpfr_random(random_state) if l < gmpy2.log(N // 2) / gmpy2.log(x): phi_hint.update(phi_hint2) return x, [p] * alpha + yf, phi_hint
def avg_error(coeffs, trials, max): gmpy2.get_context().precision = 256 total_error = 0 for i in range(trials): x = random.random()*max expected = gmpy2.log(x) * 2**64 result = fxp_ln(int(x*2**64), coeffs) total_error += abs(result - expected)/expected return total_error/trials
def binary_attack(encrypt, key1, key2): S = gmpy2.powmod(2, key1[0], key1[1]) encrypt = encrypt * S % key1[1]; lo = D(0) up = D(int(key1[1])) bound = int(gmpy2.log(key1[1])/gmpy2.log(2)) + 1 getcontext().prec = bound for x in range(bound): # each time determine the most significant digit. middle = (lo+up)/2 # oracle function if gmpy2.powmod(encrypt, key2[0], key2[1]) % 2 == 0: up = middle else: lo = middle encrypt = encrypt * S % key1[1] print "The original message is: " # print the upper_bound print i2a(int(up)).encode('utf-8')
def export_divisor_sums(divisorDb: DivisorDb, output_file: TextIO) -> None: # for now, use only 115 primes for columns # prime_subset = list(primes[:115]) # prime_columns = ','.join(['%d' % p for p in prime_subset]) output_file.write('log_n,witness_value\n') # ,' + prime_columns + '\n') for rds in divisorDb.load(): log_n = log(rds.n) # factorization = factorize(rds.n, prime_subset) # factor_columns = ','.join(['%d' % d for (p, d) in factorization]) output_file.write(f'{log_n:.10f},{rds.witness_value:.10f}\n' ) # ,{factor_columns}\n')
def graph_rb(graph,label): eigen_vals = nx.linalg.spectrum.adjacency_spectrum(graph, weight='weight') max_val = 0 ret_val = mpfr('0') for vals in eigen_vals: if vals > max_val: max_val = vals print(max_val) eigen_vals = np.array(eigen_vals, dtype=np.float128) print(eigen_vals) for i in range(len(eigen_vals)): ret_val += gmpy2.exp(mpfr(str(eigen_vals[i]))) ret_val /= 500 ret_val = gmpy2.log(ret_val) print(label,str(np.real(ret_val))) return np.real(ret_val)
def ln(self): return Numeric(gmpy2.log(self.val))
def max_bits_in_digits(d): """ Returns maximum number of bits fitting in given number of digits eg. 2 digits (99) fits in 7 bits :param d: number of digits """ return gmpy2.mpz(gmpy2.ceil(d * (gmpy2.log(10) / gmpy2.log(2))))
def metric_held_out_documents_wallach09(dtm_test, theta_test, phi_train, alpha, n_samples=10000): """ Estimation of the probability of held-out documents according to [Wallach2009]_ using a document-topic estimation `theta_test` that was estimated via held-out documents `dtm_test` on a trained model with a topic-word distribution `phi_train` and a document-topic prior `alpha`. Draw `n_samples` according to `theta_test` for each document in `dtm_test` (memory consumption and run time can be very high for larger `n_samples` and a large amount of big documents in `dtm_test`). A document-topic estimation `theta_test` can be obtained from a trained model from the "lda" package or scikit-learn package with the `transform()` method. Adopted MATLAB code `originally from Ian Murray, 2009 <https://people.cs.umass.edu/~wallach/code/etm/>`_ and downloaded from `umass.edu <https://people.cs.umass.edu/~wallach/code/etm/lda_eval_matlab_code_20120930.tar.gz>`_. .. note:: Requires `gmpy2 <https://github.com/aleaxit/gmpy>`_ package for multiple-precision arithmetic to avoid numerical underflow. .. [Wallach2009] Wallach, H.M., Murray, I., Salakhutdinov, R. and Mimno, D., 2009. Evaluation methods for topic models. :param dtm_test: held-out documents of shape NxM with N documents and vocabulary size M :param theta_test: document-topic estimation of `dtm_test`; shape NxK with K topics :param phi_train: topic-word distribution of a trained topic model that should be evaluated; shape KxM :param alpha: document-topic prior of the trained topic model that should be evaluated; either a scalar or an array of length K :return: estimated probability of held-out documents """ import gmpy2 n_test_docs, n_vocab = dtm_test.shape if n_test_docs != theta_test.shape[0]: raise ValueError( 'shapes of `dtm_test` and `theta_test` do not match (unequal number of documents)' ) _, n_topics = theta_test.shape if n_topics != phi_train.shape[0]: raise ValueError( 'shapes of `theta_test` and `phi_train` do not match (unequal number of topics)' ) if n_vocab != phi_train.shape[1]: raise ValueError( 'shapes of `dtm_test` and `phi_train` do not match (unequal size of vocabulary)' ) if isinstance(alpha, np.ndarray): alpha_sum = np.sum(alpha) else: alpha_sum = alpha * n_topics alpha = np.repeat(alpha, n_topics) if alpha.shape != (n_topics, ): raise ValueError( '`alpha` has invalid shape (should be vector of length n_topics)') # samples: random topic assignments for each document # shape: n_test_docs x n_samples # values in [0, n_topics) ~ theta_test samples = np.array([ np.random.choice(n_topics, n_samples, p=theta_test[d, :]) for d in range(n_test_docs) ]) assert samples.shape == (n_test_docs, n_samples) assert 0 <= samples.min() < n_topics assert 0 <= samples.max() < n_topics # n_k: number of documents per topic and sample # shape: n_topics x n_samples # values in [0, n_test_docs] n_k = np.array([np.sum(samples == t, axis=0) for t in range(n_topics)]) assert n_k.shape == (n_topics, n_samples) assert 0 <= n_k.min() <= n_test_docs assert 0 <= n_k.max() <= n_test_docs # calculate log p(z) for each sample # shape: 1 x n_samples log_p_z = np.sum(gammaln(n_k + alpha[:, np.newaxis]), axis=0) + gammaln(alpha_sum) \ - np.sum(gammaln(alpha)) - gammaln(n_test_docs + alpha_sum) assert log_p_z.shape == (n_samples, ) # calculate log p(w|z) for each sample # shape: 1 x n_samples log_p_w_given_z = np.zeros(n_samples) dtm_is_sparse = issparse(dtm_test) for d in range(n_test_docs): if dtm_is_sparse: word_counts_d = dtm_test[d].toarray().flatten() else: word_counts_d = dtm_test[d] words = np.repeat(np.arange(n_vocab), word_counts_d) assert words.shape == (word_counts_d.sum(), ) phi_topics_d = phi_train[ samples[d]] # phi for topics in samples for document d log_p_w_given_z += np.sum(np.log(phi_topics_d[:, words]), axis=1) log_joint = log_p_z + log_p_w_given_z # calculate log theta_test # shape: 1 x n_samples log_theta_test = np.zeros(n_samples) for d in range(n_test_docs): log_theta_test += np.log(theta_test[d, samples[d]]) # compare log_weights = log_joint - log_theta_test # calculate final log evidence # requires using gmpy2 to avoid numerical underflow exp_sum = gmpy2.mpfr(0) for exp in (gmpy2.exp(x) for x in log_weights): exp_sum += exp return float(gmpy2.log(exp_sum)) - np.log(n_samples)
def log(a): return gmpy2.log(a)
s = t.state() s.block.number = 5555555 # gmpy2 precision initialization BITS = (1 << 10) BYTES = BITS/8 gmpy2.get_context().precision = BITS # a whole lotta bits def random(): seed = int(os.urandom(BYTES).encode('hex'), 16) return gmpy2.mpfr_random(gmpy2.random_state(seed)) # Useful constants as mpfr PI = gmpy2.acos(-1) LOG2E = gmpy2.log2(gmpy2.exp(1)) LN2 = gmpy2.log(2) # Same, as 192.64 fixedpoint FX_BASE = 2 FX_POWER = 64 FX_BASE = 10 FX_POWER = 18 MAX_POWER = int(255 - FX_POWER*gmpy2.log(FX_BASE)/LN2) FX_ONE = FX_BASE**FX_POWER FX_PI = int(PI * FX_ONE) FX_LOG2E = int(LOG2E * FX_ONE) FX_LN2 = int(LN2 * FX_ONE) ## The index of a poly is the power of x, ## the val at the index is the coefficient. ## ## An nth degree poly is a list of len n + 1. ##
def log_likelihood(x,y, t_1, t_2): sigmoidP = [sigmoid(i, t_1, t_2) for i in x] u = [q*gmpy2.log(p) + (1 - q)*gmpy2.log(1 - p) for p,q in zip(sigmoidP,y)] return gmpy2.fsum(u)
class AugurTest(ContractTest): def __init__(self): def test_faucet with timer(): print title('Starting Tests', dim=False) print title('Compiling %s ...' % FILE) with timer(): augur = Contract(FILE, gas=10**7) with timer(): print title('Testing Faucet and Balance') for addr, key in addr2key.items(): print yellow( "Results of calls with address ", pretty_address(addr), ":") result = augur.faucet(sender=key, profiling=True) print yellow("\taugur.faucet: ", cyan("Success")) print yellow("\t\tgas used: ", cyan(result['gas'])) result = augur.balance(addr, profiling=True) print yellow("\taugur.balance: ",cyan(result['output']/float(1<<64))) print yellow("\t\tgas used: ", cyan(result['gas'])) with timer(): print title('Testing Send') for i, (addr, key) in enumerate(addr2key.items()): to = addresses[i-1] augur.send(to, 1 << 64, sender=key) print yellow( "Sent 1 cashcoin from ", pretty_address(addr), " to ", pretty_address(to)) with timer(): print title('Testing CreateSubBranch') addr = addresses[1] key = addr2key[addr] subbranch = augur.createSubbranch('test branch', 100, 1010101, 2**57, sender=key) print yellow( 'Created subbranch ', pretty_address(subbranch), ' with creator ', pretty_address(addr)) with timer(): print title('Testing CreateEvent') addr = addresses[2] key = addr2key[addr] event1 = augur.createEvent( subbranch, #branch '"Will Jack grow at least an inch in March 2015?"', #description 5*5*1000, #expiration block 0, #min value (binary) 1, #max value 2, #number of outcomes sender=key) event2 = augur.createEvent( subbranch, '''\ "What will Jack's height be, from 60 to 72 inches, at noon PST on April 1st, 2015?"''', 5*5*1000, 60, #scalar event! 72, 2, sender=key) print yellow( 'Created events ', pretty_address(event1), ' and ', pretty_address(event2), ' with creator ', pretty_address(addr), ) with timer(): print title('Testing CreateMarket') addr = addresses[3] key = addr2key[addr] market = augur.createMarket( subbranch, #branch '"Market on events %d & %d"' % (event1, event2), #events 1 << 60, #alpha 10 << 70, #fixedpoint initial liquidity 1 << 58, #trading fee [event1, event2], #array of events sender=key) print yellow( 'Created market ', pretty_address(market), ' with creator ', pretty_address(addr)) def lslmsr(q_, i, a): assert q_[i] + a >= 0 q = q_[:] alpha = 1/16. scale = 12 Bq1 = alpha*scale*sum(q) C1 = Bq1*gmpy2.log(sum(gmpy2.exp(q_i/Bq1) for q_i in q)) q[i] += a Bq2 = alpha*scale*sum(q) C2 = Bq2*gmpy2.log(sum(gmpy2.exp(q_i/Bq2) for q_i in q)) if a > 0: return (C2 - C1)*1.015625 else: return (C1 - C2)*0.984375 bought = defaultdict(lambda : [0]*4) shares = [640/(1+12/16.*gmpy2.log(4))]*4 transaction_nonce = defaultdict(int) with timer(): print title('Testing BuyShares') for i in range(50): addr = random.choice(addresses) key = addr2key[addr] outcome = random.randrange(4) amt = random.randrange(1, 3) expected1 = lslmsr(shares, outcome, amt) nonce = trade_pow(subbranch, market, addr, transaction_nonce[addr]) transaction_nonce[addr] += 1 expected2 = augur.price(market, outcome + 1) cost = augur.buyShares(subbranch, market, outcome + 1, amt << 64, nonce, sender=key) shares[outcome] += amt print yellow( pretty_address(addr), " bought ", cyan(amt), " shares of outcome ", cyan(outcome + 1), " for ", dim_red(cost/float(1 << 64))) print yellow("expected cost: ", cyan(float(expected1))) print yellow( "approximate expected cost per share: ", cyan(expected2/float(1 << 64))) bought[addr][outcome] += amt with timer(): print title('Testing SellShares') while bought: addr = random.choice(bought.keys()) key = addr2key[addr] outcome = random.choice([i for i, v in enumerate(bought[addr]) if v]) amt = random.randrange(1, bought[addr][outcome]+1) expected1 = lslmsr(shares, outcome, -amt) expected2 = augur.price(market, outcome + 1) nonce = trade_pow(subbranch, market, addr, transaction_nonce[addr]) transaction_nonce[addr] += 1 paid = augur.sellShares(subbranch, market, outcome + 1, amt << 64, nonce, sender=key) shares[outcome] -= amt print yellow( pretty_address(addr), " sold ", cyan(amt), " shares of outcome ", cyan(outcome + 1), " for ", dim_red(paid/float(1 << 64))) print yellow("expected payment: ", cyan(float(expected1))) print yellow( "approximate expected cost per share: ", cyan(expected2/float(1 << 64))) bought[addr][outcome] -= amt if not any(bought[addr]): bought.pop(addr)
def term(x, y): dot = ß.dot(x) if y: return dot - log(1 + exp(dot)) else: return -log(1 + exp(dot))
q = q_[:] alpha = 1 / 16. scale = 12 Bq1 = alpha * scale * sum(q) C1 = Bq1 * gmpy2.log(sum(gmpy2.exp(q_i / Bq1) for q_i in q)) q[i] += a Bq2 = alpha * scale * sum(q) C2 = Bq2 * gmpy2.log(sum(gmpy2.exp(q_i / Bq2) for q_i in q)) if a > 0: return (C2 - C1) * 1.015625 else: return (C1 - C2) * 0.984375 bought = defaultdict(lambda: [0] * 4) shares = [640 / (1 + 12 / 16. * gmpy2.log(4))] * 4 transaction_nonce = defaultdict(int) with timer(): print title('Testing BuyShares') for i in range(50): addr = random.choice(addresses) key = addr2key[addr] outcome = random.randrange(4) amt = random.randrange(1, 3) expected1 = lslmsr(shares, outcome, amt) nonce = trade_pow(subbranch, market, addr, transaction_nonce[addr]) transaction_nonce[addr] += 1 expected2 = augur.price(market, outcome + 1) cost = augur.buyShares(subbranch, market, outcome + 1,
def check(): with open(ALLGAPS_SQL) as f: lines = f.readlines() LINE_RE = re.compile( r"INSERT INTO gaps VALUES.(\d+),[01],'C','[F?]','[CP?]','[\w.&]*'," r"(-300|[12][890]\d\d)," r"([0-9.]+),([0-9]+),'([^']+)'\);") checked = 0 checked_ends = 0 parse_error = 0 bad_merit = 0 bad_digits = 0 merit_fmt = 0 from tqdm import tqdm for i, line in enumerate(tqdm(lines)): if i < 10 and not line.startswith('INSERT'): continue match = LINE_RE.match(line) if not match and i > 94000: break assert match, (i, line) gap, year, merit, primedigits, start = match.groups() num = parse_num(start) if not num: print("Can't Process: '{}'".format(start)) parse_error += 1 continue checked += 1 gen_digits = len(str(num)) if gen_digits != int(primedigits): print(f"Prime digits {gen_digits} vs {primedigits} @{i}: {line}") bad_digits += 1 end_num = num + int(gap) if gen_digits < 400: # test and verify assert gmpy2.is_prime(num), (i, line) assert gmpy2.is_prime(end_num), (i, line) checked_ends += 1 else: for p in SMALL_PRIMES: assert num % p > 0, (num, p) assert end_num % p > 0, (end_num, p) gen_merit = int(gap) / gmpy2.log(num) # General merit is >10 so .4 is 6 sig figs. fmt_merit = "{:.4f}".format(gen_merit) fmerit = float(merit) if abs(fmerit - gen_merit) / gen_merit > 0.003: print(f"Merit {gen_merit} vs {merit} @{i}: {line}") assert False, ("Bad Merit", line, fmerit, gen_merit) if fmt_merit != merit: #print(f"Merit {fmt_merit} vs {merit} @{i}: {line}") merit_fmt += 1 if UPDATE_MERIT_FMT: lines[i] = line.replace(merit, fmt_merit) print(f"Checked {checked} lines, {checked_ends} pairs of endpoints") print(f"Prime Digits disagreed on {bad_digits} lines") print(f"Merit format disagreed on {merit_fmt} lines") print(f"Failed to parse {parse_error} numbers (41 known parse failures)") if UPDATE_MERIT_FMT and merit_fmt: print() print(f"Updating merit format for {merit_fmt} lines in {GAPS_SQL!r}") with open(ALLGAPS_SQL, "w") as f: for line in lines: assert line.endswith('\n'), line f.write(line)
def _ilog_gmpy(n): return int(gmpy.log(n))
def get_b_smooth(): t = gmpy2.sqrt(gmpy2.mul(gmpy2.log(N), gmpy2.log(gmpy2.log(N)))) b= gmpy2.exp(gmpy2.mul(0.5, t)) print("bsmooth advice: " + str(b))
def delta_n(N, p, alpha): return gmpy2.log(p) / gmpy2.log(N) * hash(N // p**alpha) / N
[apr1_t , apr1_y] = [listf(l) for l in edo1(dy_dt, t_0, y_0, dt, interv)] # for the 1st order Euler EDO method d2y_dt2 = lambda t, y, dy_dt: -2*dy_dt/t + t**-2 + t**-3 t_0 = mp(1) y_0 = mp(2) dy_dt_0 = mp(2) dt = 5*mp(10)**-3 interv = [mp(0.2), mp(1.8)] sol2_t = arange( *interv, dt) sol2_y = list(map(lambda t: (1-1/t)*log(t)-2/t+4, sol2_t)) [apr2_t , apr2_y] = [listf(l) for l in edo2(d2y_dt2, t_0, y_0, dy_dt_0, dt, interv)] # ------------------------------------------------ ### PLOTS ### sns.set_style('darkgrid') mpl.rcParams['text.usetex'] = True (fig, axes) = plt.subplots(1, 2, facecolor='w', figsize=( 16, 9 )) fig.suptitle(r'$\textup{Approximations \& Analytic Solutions for Diff. Eq. using Euler`s Method (singe precision)}$')
def search_next_prime(x, num_workers=31): # search range split step = 2*log(x)//num_workers jobs = [search_range.remote(x + step*i, mpz(x + step*i + step)) for i in range(num_workers)] return [i for e in ray.get(jobs) for i in e]
def lg(x): return gmpy2.log(x) / gmpy2.log(2)
def log(x, b=None): if not b: return gm.log(x) else: return gm.log(x) / gm.log(b)
q = q_[:] alpha = 1/16. scale = 12 Bq1 = alpha*scale*sum(q) C1 = Bq1*gmpy2.log(sum(gmpy2.exp(q_i/Bq1) for q_i in q)) q[i] += a Bq2 = alpha*scale*sum(q) C2 = Bq2*gmpy2.log(sum(gmpy2.exp(q_i/Bq2) for q_i in q)) if a > 0: return (C2 - C1)*1.015625 else: return (C1 - C2)*0.984375 bought = defaultdict(lambda : [0]*4) shares = [640/(1+12/16.*gmpy2.log(4))]*4 transaction_nonce = defaultdict(int) with timer(): print title('Testing BuyShares') for i in range(50): addr = random.choice(addresses) key = addr2key[addr] outcome = random.randrange(4) amt = random.randrange(1, 3) expected1 = lslmsr(shares, outcome, amt) nonce = trade_pow(subbranch, market, addr, transaction_nonce[addr]) transaction_nonce[addr] += 1 expected2 = augur.price(market, outcome + 1) cost = augur.buyShares(subbranch, market, outcome + 1, amt << 64, nonce, sender=key) shares[outcome] += amt print yellow(
import os import bisect from numpy import linspace # gmpy2 precision initialization BITS = (1 << 10) BYTES = BITS/8 gmpy2.get_context().precision = BITS # a whole lotta bits def random(): seed = int(os.urandom(BYTES).encode('hex'), 16) return gmpy2.mpfr_random(gmpy2.random_state(seed)) # Useful constants as mpfr PI = gmpy2.acos(-1) LOG2E = gmpy2.log2(gmpy2.exp(1)) LN2 = gmpy2.log(2) # Same, as 192.64 fixedpoint FX_PI = int(PI * 2**64) FX_LOG2E = int(LOG2E * 2**64) FX_LN2 = int(LN2 * 2**64) FX_ONE = 1 << 64 ## The index of a poly is the power of x, ## the val at the index is the coefficient. ## ## An nth degree poly is a list of len n + 1. ## ## The vals in a poly must all be floating point ## numbers. def poly_add(p1, p2): if p1 == []:
# gmpy2 precision initialization BITS = (1 << 10) BYTES = BITS / 8 gmpy2.get_context().precision = BITS # a whole lotta bits def random(): seed = int(os.urandom(BYTES).encode('hex'), 16) return gmpy2.mpfr_random(gmpy2.random_state(seed)) # Useful constants as mpfr PI = gmpy2.acos(-1) LOG2E = gmpy2.log2(gmpy2.exp(1)) LN2 = gmpy2.log(2) # Same, as 192.64 fixedpoint FX_BASE = 2 FX_POWER = 64 FX_BASE = 10 FX_POWER = 18 MAX_POWER = int(255 - FX_POWER * gmpy2.log(FX_BASE) / LN2) FX_ONE = FX_BASE**FX_POWER FX_PI = int(PI * FX_ONE) FX_LOG2E = int(LOG2E * FX_ONE) FX_LN2 = int(LN2 * FX_ONE) ## The index of a poly is the power of x, ## the val at the index is the coefficient. ## ## An nth degree poly is a list of len n + 1. ##