"""Constants and outward interval bounds for the binary entropy calculations.""" from fractions import Fraction from flint import arb, ctx ctx.prec = 100 HALF = arb(1) / 2 L = arb(2).log() R = arb("0.69090773872540775").union(arb("0.69090773872540776")) M = HALF + (1 - R) * (1 + R).log() / (2 * L) Q = 2 * M - 1 TAU = (HALF - R**2) / (R**2 * (1 - R) ** 2) SCALE = 10**7 def entropy(x): if x.is_zero() or (1 - x).is_zero(): return arb(0) return -x * x.log() - (1 - x) * (1 - x).log() def entropy_range(a, b): values = entropy(a).union(entropy(b)) if not (b < HALF or a > HALF): values = values.union(L) return values def diagonal_input(x): return x**2 * (1 + TAU * (1 - x) ** 2) def floor_lower(x): return int(x.lower().floor().unique_fmpz()) def ceil_upper(x): return int(x.upper().ceil().unique_fmpz()) def exact(value): v = Fraction(value) return arb(v.numerator) / v.denominator