Author SHA1 Message Date
paultal 56dd0b921a Add bracket-balance metric and grow hand test to 102 tagged rows
compiles alone accepts unbalanced parens and stray letters, so isotope-eval
now also reports a balanced column and breaks hand-test exact match down by
category (equation, variables, robustness, bounds, bigop-lhs, long, ...).
The new rows come from the stress-test failures in #2 and #4-#9.

Baseline with the current model: hand exact 41.2%, compiles 99.0%.

Refs #3
2026-11-27 01:21:48 +02:00
4 changed files with 6 additions and 94 deletions

No files matched your search

+1 -8
View File
@@ -106,14 +106,7 @@ class Eq(Node):
right: Node
# lowercase Greek letters usable as variables; pi is a constant, and omicron is left out because it looks like o
GREEK = [
"alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu",
"nu", "xi", "rho", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega",
] # fmt: skip
# e is a constant (e^x), and d is kept free for "dx" and later derivatives (#8)
LATIN = [c for c in "abcdefghijklmnopqrstuvwxyz" if c not in "de"]
UPPER = list("ABCFGHKLMNPQRSTVXY")
GREEK = ["alpha", "beta", "gamma", "delta", "theta", "lambda", "mu", "sigma", "phi", "omega"]
FUNCS = ["sin", "cos", "tan", "ln", "log", "exp"]
BIG_OPS = (Integral, Sum, Lim)
+4 -64
View File
@@ -16,28 +16,7 @@ import pathlib
import random
from typing import ClassVar
from .ast import (
FUNCS,
GREEK,
LATIN,
UPPER,
Add,
Eq,
Frac,
Func,
Integral,
Lim,
Mul,
Neg,
Node,
Num,
Pow,
Sqrt,
Sub,
Sum,
Var,
walk,
)
from .ast import FUNCS, GREEK, Add, Eq, Frac, Func, Integral, Lim, Mul, Neg, Node, Num, Pow, Sqrt, Sub, Sum, Var, walk
from .render import to_english, to_typst
from .validate import compiles_many
@@ -52,14 +31,8 @@ class TreeGen:
def var(self, prefer: str | None = None) -> Var:
if prefer and self.rng.random() < 0.6: # inside "integral ... dx", mostly use x
return Var(prefer)
r = self.rng.random()
if r < 0.45: # x and y stay the most common, as in real maths
return Var("x" if r < 0.33 else "y")
if r < 0.80:
return Var(self.rng.choice(LATIN))
if r < 0.95:
return Var(self.rng.choice(GREEK))
return Var(self.rng.choice(UPPER))
pool = ["x"] * 12 + ["y"] * 4 + ["z", "t", "a", "b", "c", "n", "k", "u", "r", "s", "m", "p"] + GREEK
return Var(self.rng.choice(pool))
def num(self) -> Num:
r = self.rng.random()
@@ -159,45 +132,12 @@ class TreeGen:
to = r.choice([Num(0)] * 4 + [INF] * 4 + [Num(1), Var("a"), Neg(INF)])
return Lim(self.expr(max(depth, 1), v), v, to)
def term(self) -> Node:
"""One textbook-style term: x^2, 3 x, sin(x)^2, y, 7."""
r = self.rng
v = self.var()
match r.choices(["pow", "coef", "trig", "var", "num"], [5, 2, 1, 2, 1])[0]:
case "pow":
return Pow(v, Num(r.choice([2, 2, 2, 3])))
case "coef":
return Mul(Num(r.randint(2, 9)), r.choice([v, Pow(v, Num(2))]))
case "trig":
return Pow(Func(r.choice(["sin", "cos", "tan"]), v), Num(2))
case "var":
return v
return self.num()
def terms(self) -> Node:
"""A flat sum of 2-3 simple terms, like x^2 + y^2 or a x^2 + b x + c.
Random trees almost never produce these shapes, yet they are the most common left sides people say.
"""
r = self.rng
out = self.term()
for _ in range(r.choice([1, 1, 2])):
out = (Add if r.random() < 0.75 else Sub)(out, self.term())
return out
def tree(self) -> Node:
r = self.rng
depth = r.choices([1, 2, 3, 4], [25, 40, 25, 10])[0]
e = self.big(depth - 1) if r.random() < 0.3 else self.expr(depth)
if r.random() < 0.2:
# a long left side too ("x^2 + y^2 = r^2"), not only "y = ...": depth 2+ was never seen before (#2)
x = r.random()
if x < 0.35:
lhs: Node = r.choice([Var("y"), Var("f"), Var("x")])
elif x < 0.65:
lhs = self.terms()
else:
lhs = self.expr(r.choices([0, 1, 2, 3], [2, 3, 3, 2])[0])
lhs = r.choice([Var("y"), Var("f"), Var("x"), self.expr(r.choice([0, 1]))])
return Eq(lhs, e)
return e
-12
View File
@@ -1,12 +0,0 @@
import random
from isotope_typst.ast import GREEK, LATIN, UPPER
from isotope_typst.generate import TreeGen
def test_variable_pool_covers_alphabet():
"""#4: any letter the model never saw gets spelled out letter by letter, so all of them must show up."""
gen = TreeGen(random.Random(0))
seen = {gen.var().name for _ in range(20000)}
assert set(LATIN) | set(GREEK) | set(UPPER) <= seen
assert "d" not in seen and "e" not in seen
+1 -10
View File
@@ -1,7 +1,7 @@
import collections
import random
from isotope_typst.ast import Eq, Frac, Integral, Neg, Num, Pow, Var, walk
from isotope_typst.ast import Frac, Integral, Neg, Num, Pow, Var
from isotope_typst.generate import TreeGen, split_of
from isotope_typst.render import to_english, to_typst
from isotope_typst.validate import compiles_many
@@ -33,12 +33,3 @@ def test_english_is_unambiguous():
def test_split_is_by_structure():
assert split_of("x^2") == split_of("x^2")
def test_equations_have_long_left_sides():
"""#2: "x^2 + y^2 = r^2" needs left sides deeper than a single operator in the training data."""
gen = TreeGen(random.Random(0))
eqs = [t for t in (gen.tree() for _ in range(5000)) if isinstance(t, Eq)]
deep = [t for t in eqs if sum(1 for _ in walk(t.left)) >= 5]
assert len(deep) > 0.15 * len(eqs)
assert any(to_typst(t).startswith("x^2 + y^2 = ") for t in (gen.tree() for _ in range(200_000)))