process-calc / PROCESS.py
ApplePiesFromScratch's picture
Upload folder using huggingface_hub
ae01165 verified
Raw
History Blame Contribute Delete
1.36 kB
#!/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)))