File size: 1,360 Bytes
ae01165 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #!/usr/bin/env python3
"""Process calc kernel. Copy this file. Stdlib only."""
from __future__ import annotations
from fractions import Fraction as F
def as_exact(x):
if isinstance(x, bool):
raise TypeError("theta bool")
if isinstance(x, int):
return F(x)
if isinstance(x, F):
return F(x)
raise TypeError("theta not Q: %s" % type(x).__name__)
def isolate(v, seed):
v, seed = as_exact(v), as_exact(seed)
if seed == 0:
raise ValueError("theta seed 0")
return (v, seed)
def mix(a, b):
(v, r), (w, s) = a, b
return (v * w, r * w + v * s)
def add(a, b):
(v, r), (w, s) = a, b
return (v + w, r + s)
def rate(top, bot):
if bot[1] == 0:
raise ValueError("theta dead isolator")
return top[1] / bot[1]
def certify(fn, values, seeds):
if not values or not seeds:
raise ValueError("theta empty grid")
rows = []
for v in values:
got = []
for s in seeds:
x = isolate(v, s)
got.append(rate(fn(x), x))
if any(g != got[0] for g in got):
raise ValueError("gauge split at %s %s" % (v, got))
rows.append((v, got[0]))
return rows
if __name__ == "__main__":
x = isolate(3, 1)
print(rate(mix(x, x), x))
print(certify(lambda z: mix(z, z), (2, 3, -1), (1, 2, F(1, 2), -1)))
|