def adjust_ret_list(retlist): retlist[0].cdf_low="0.0" retlist[-1].cdf_up="1.0" with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx: min_value=gmpy2.exp10(-digits_for_input_cdf) current=min_value for pbox in retlist: with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx: if gmpy2.is_zero(gmpy2.mpfr(pbox.cdf_low)) or gmpy2.mpfr(pbox.cdf_low)<current: pbox.cdf_low=round_number_down_to_digits(current, digits_for_input_cdf) with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx: current=current+min_value if gmpy2.is_zero(gmpy2.mpfr(pbox.cdf_up)) or gmpy2.mpfr(pbox.cdf_up)<current: pbox.cdf_up=round_number_down_to_digits(current, digits_for_input_cdf) with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx: current=gmpy2.sub(gmpy2.mpfr("1.0"), min_value) for pbox in retlist[::-1]: with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx: if gmpy2.mpfr(pbox.cdf_up)==gmpy2.mpfr("1.0") or gmpy2.mpfr(pbox.cdf_up)>current: pbox.cdf_up=round_number_up_to_digits(current, digits_for_input_cdf) current=gmpy2.sub(current, min_value) if gmpy2.mpfr(pbox.cdf_low)==gmpy2.mpfr("1.0") or gmpy2.mpfr(pbox.cdf_low)>current: pbox.cdf_low=round_number_up_to_digits(current, digits_for_input_cdf) retlist[0].cdf_low = "0.0" retlist[-1].cdf_up = "1.0" return retlist
def check_interval_is_zero(interval): with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx: left = mpfr(interval.lower) with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx: right = mpfr(interval.upper) if gmpy2.is_zero(left) and gmpy2.is_zero(right): return True return False
def check_zero_is_in_interval(interval): with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx: left = mpfr(interval.lower) with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx: right = mpfr(interval.upper) if gmpy2.is_zero(left) and interval.include_lower: return True if gmpy2.is_zero(right) and interval.include_upper: return True if left < mpfr("0.0") < right: return True return False
def ll(n): two = mpz(2) s = two * two div = two**n - 1 for i in prange(2, n): s = powmod(s, two, div) s -= 2 if (not is_zero(s)): raise Exception("Not Prime")
def clean_affine_operation(self): keys = [] for key in self.coefficients: value = self.coefficients[key] remove = [False, False] with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx: if gmpy2.is_zero(mpfr(value.lower)): remove[0] = True with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx: if gmpy2.is_zero(mpfr(value.upper)): remove[1] = True if remove[0] and remove[1]: keys.append(key) for delete in keys: del self.coefficients[delete] self.update_interval() return self
def lucas_lehmer(n): if n == 2: return True if not mp.is_prime(n): return False two = mp.mpz(2) m = two**n - 1 s = two * two for i in range(2, n): sqr = s * s s = (sqr & m) + (sqr >> n) if s >= m: s -= m s -= two return mp.is_zero(s)
def lucas_lehmer(n): if n == 2: return True if not mp.is_prime(n): return False two = mp.mpz(2) m = two**n - 1 s = two*two for i in range(2, n): sqr = s*s s = (sqr & m) + (sqr >> n) if s >= m: s -= m s -= two return mp.is_zero(s)
def mpfr_to_mpf(f): ctx = gmpy2.get_context() p = ctx.precision # emax = ctx.emax - 1 # emax = (2 ** (w - 1)) - 1 # emax + 1 = 2 ** (w - 1) # ln2(emax + 1) = w - 1 # w = ln(emax + 1) + 1 # w = ln(ctx.emax) + 1 w = log2(ctx.emax) + 1 assert w.is_integer() w = int(w) # w = k - p # k = w + p k = w + p eb = k - p sb = p rv = MPF(eb, sb) if gmpy2.is_nan(f): rv.set_nan() elif gmpy2.is_infinite(f): if gmpy2.sign(f) > 0: rv.set_infinite(0) else: rv.set_infinite(1) elif gmpy2.is_zero(f): if str(f) == "-0.0": rv.set_zero(1) elif str(f) == "0.0": rv.set_zero(0) else: assert False else: a, b = f.as_integer_ratio() rv.from_rational(RM_RNE, Rational(int(a), int(b))) assert mpf_to_mpfr(rv) == f return rv
def fp_is_zero(self) -> Bit: return Bit(gmpy2.is_zero(self._value))
def multiplication(self, interval): reset_default_precision() tmp_res_left = [] tmp_res_right = [] zero_is_included = self.check_zero_is_in_interval( ) or interval.check_zero_is_in_interval() with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx: tmp_res_left.append( gmpy2.mul(mpfr(self.lower), mpfr(interval.lower))) tmp_res_left.append( gmpy2.mul(mpfr(self.lower), mpfr(interval.upper))) tmp_res_left.append( gmpy2.mul(mpfr(self.upper), mpfr(interval.lower))) tmp_res_left.append( gmpy2.mul(mpfr(self.upper), mpfr(interval.upper))) min_index = [ i for i, value in enumerate(tmp_res_left) if value == min(tmp_res_left) ] with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx: tmp_res_right.append( gmpy2.mul(mpfr(self.lower), mpfr(interval.lower))) tmp_res_right.append( gmpy2.mul(mpfr(self.lower), mpfr(interval.upper))) tmp_res_right.append( gmpy2.mul(mpfr(self.upper), mpfr(interval.lower))) tmp_res_right.append( gmpy2.mul(mpfr(self.upper), mpfr(interval.upper))) max_index = [ i for i, value in enumerate(tmp_res_right) if value == max(tmp_res_right) ] tmp_bounds = [ self.include_lower * interval.include_lower, self.include_lower * interval.include_upper, self.include_upper * interval.include_lower, self.include_upper * interval.include_upper ] res_inc_left = any([tmp_bounds[index] for index in min_index]) res_inc_right = any([tmp_bounds[index] for index in max_index]) min_value = tmp_res_left[min_index[0]] max_value = tmp_res_right[max_index[0]] if gmpy2.is_zero(min_value) and zero_is_included: res_inc_left = True if gmpy2.is_zero(max_value) and zero_is_included: res_inc_right = True res_left = round_number_down_to_digits(min_value, self.digits) res_right = round_number_up_to_digits(max_value, self.digits) return Interval(res_left, res_right, res_inc_left, res_inc_right, self.digits)