"""Independent brute-force polynomial and set-partition evaluations.""" import hashlib import itertools import json import math from functools import lru_cache from pathlib import Path from flint import ctx from certify_dirichlet import construct, prime_model def brute_partition(items, rows): ring = items[0].context() answer = ring.constant(0) for assignment in itertools.permutations(range(len(items)), len(rows)): term = ring.constant(1) for i, r in zip(assignment, rows): term *= items[i] ** r answer += term return answer def multiply(a, b): return (a[0] * b[0], *(a[0] * y + x * b[0] for x, y in zip(a[1:], b[1:]))) def inclusion_partition(items, valuations, rows): """Möbius inversion on set partitions, with exact integer score moments. Distinctness is enforced by inclusion-exclusion, independently of the primary item-processing DP. Coordinate 0 is the value; remaining entries are the prime-variable Euler derivatives. """ dimensions = len(valuations[0]) sums = {} for s in range(1, sum(rows) + 1): powers = [x ** s for x in items] sums[s] = (sum(powers), *(sum(s * v[j] * x for v, x in zip(valuations, powers)) for j in range(dimensions))) @lru_cache(None) def rec(mask): if not mask: return (1,) + (0,) * dimensions first = mask & -mask others = mask ^ first sub = others answer = [0] * (dimensions + 1) while True: block = sub | first count = block.bit_count() degree = sum(r for j, r in enumerate(rows) if block & (1 << j)) coefficient = (-1) ** (count - 1) * math.factorial(count - 1) term = multiply(sums[degree], rec(mask ^ block)) answer = [x + coefficient * y for x, y in zip(answer, term)] if not sub: break sub = (sub - 1) & others return tuple(answer) return rec((1 << len(rows)) - 1) def check_case(n, c, brute=False): primes, z, f, g, ds = construct(n, c) _, ring, monomials = prime_model(n) rows = tuple(range(1, c + 1)) if brute: bz = brute_partition(monomials, rows) bg = brute_partition(monomials, rows + (1,)) bf = bz * sum(monomials, ring.constant(0)) assert z == bz and g == bg and f == bf for j, x in enumerate(ring.gens()): assert ds[j] == bf * x * bg.derivative(j) - bg * x * bf.derivative(j) valuations = [next(m.terms())[0] for m in monomials] for probe in [primes, [p + 1 for p in primes]]: items = [math.prod(p ** k for p, k in zip(probe, v)) for v in valuations] iz = inclusion_partition(items, valuations, rows) ig = inclusion_partition(items, valuations, rows + (1,)) ip = inclusion_partition(items, valuations, (1,)) iff = multiply(iz, ip) assert int(z(*probe)) == iz[0] assert int(g(*probe)) == ig[0] assert int(f(*probe)) == iff[0] for j, d in enumerate(ds): assert int(d(*probe)) == iff[0] * ig[j + 1] - ig[0] * iff[j + 1] return {'N': n, 'C': c, 'full_polynomial_brute_check': brute, 'independent_integer_probes': 2, 'passed': True} def main(): base = Path(__file__).parent certificate_path = base / 'results/dirichlet.json' data = json.loads(certificate_path.read_text()) records = [] for record in data['records']: n, c = record['N'], record['C'] result = check_case(n, c, brute=n <= 6) records.append(result) print(json.dumps(result), flush=True) (base / 'results/independent_checks.json').write_text(json.dumps({ 'certificate_file_sha256': hashlib.sha256(certificate_path.read_bytes()).hexdigest(), 'records': records}, indent=2) + '\n') if __name__ == '__main__': ctx.prec = 160 main()