"""Verify rank-one entropy minorants at the knots of a shared-sign response.""" from bisect import bisect_right from check_rank import HALF, SCALE, L, entropy from flint import arb class InterpolatedKernel: def __init__(self, rows, responses=None): self.knots = [a for a, _ in rows] self.values = [v for _, v in rows] assert self.knots[0] == 0 and self.knots[-1] == 200 assert all(a < b for a, b in zip(self.knots, self.knots[1:])) assert all(0 <= v <= SCALE for v in self.values) points = [arb(a) / 200 for a in self.knots] if responses is None: responses = [ x.min(1 - x).min((HALF - x * x).nonnegative_part().sqrt()) for x in points ] else: assert len(responses) == len(points) assert all( 0 <= 200 * f <= SCALE * min(a, 200 - a) for a, f in zip(self.knots, responses) ) responses = [arb(f) / SCALE for f in responses] for i, x in enumerate(points): for j in range(i + 1): if self.values[i] * self.values[j] == 0: continue z = x * points[j] + responses[i] * responses[j] if not entropy(z) >= L * self.values[i] * self.values[j] / SCALE**2: raise ArithmeticError(f"entropy minorant failed at knots {i}, {j}") def feature(self, span, x): i = bisect_right(self.knots, span) - 1 t = (200 * x - self.knots[i]) / (self.knots[i + 1] - self.knots[i]) value = self.values[i] + t * (self.values[i + 1] - self.values[i]) return value.nonnegative_part() / SCALE