Author SHA1 Message Date
paultal 4fb7726e4b Use the whole alphabet and Greek alphabet for variables (#14)
Variables are now x/y (45%), any lowercase letter except e and d (35%), any lowercase Greek letter except pi and omicron (15%), or a common uppercase letter like N, F, A (5%). e stays a constant and d is kept for dx and future derivatives.

Hand-test exact 41.2% -> 56.9% (trained without #2), variables category 0% -> 87.5%.

Fixes #4

Co-authored-by: Paul Tal <[email protected]>
2026-11-27 02:40:48 +02:00
paultal c661773c30 Generate equations with long left sides (#13)
Equation left sides are now a single variable (35%), a flat sum of 2-3 simple terms like x^2, 3 x, sin(x)^2 (30%), or a random expression up to depth 3 (35%). Before, x^2 + y^2 = r^2 never appeared in training.

Hand-test exact 41.2% -> 47.1%, equation category 42.9% -> 78.6%.

Fixes #2

Co-authored-by: Paul Tal <[email protected]>
2026-11-27 02:16:29 +02:00
4 changed files with 94 additions and 6 deletions

No files matched your search

+8 -1
View File
@@ -106,7 +106,14 @@ class Eq(Node):
right: Node right: Node
GREEK = ["alpha", "beta", "gamma", "delta", "theta", "lambda", "mu", "sigma", "phi", "omega"] # 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")
FUNCS = ["sin", "cos", "tan", "ln", "log", "exp"] FUNCS = ["sin", "cos", "tan", "ln", "log", "exp"]
BIG_OPS = (Integral, Sum, Lim) BIG_OPS = (Integral, Sum, Lim)
+64 -4
View File
@@ -16,7 +16,28 @@ import pathlib
import random import random
from typing import ClassVar from typing import ClassVar
from .ast import FUNCS, GREEK, Add, Eq, Frac, Func, Integral, Lim, Mul, Neg, Node, Num, Pow, Sqrt, Sub, Sum, Var, walk from .ast import (
FUNCS,
GREEK,
LATIN,
UPPER,
Add,
Eq,
Frac,
Func,
Integral,
Lim,
Mul,
Neg,
Node,
Num,
Pow,
Sqrt,
Sub,
Sum,
Var,
walk,
)
from .render import to_english, to_typst from .render import to_english, to_typst
from .validate import compiles_many from .validate import compiles_many
@@ -31,8 +52,14 @@ class TreeGen:
def var(self, prefer: str | None = None) -> Var: def var(self, prefer: str | None = None) -> Var:
if prefer and self.rng.random() < 0.6: # inside "integral ... dx", mostly use x if prefer and self.rng.random() < 0.6: # inside "integral ... dx", mostly use x
return Var(prefer) return Var(prefer)
pool = ["x"] * 12 + ["y"] * 4 + ["z", "t", "a", "b", "c", "n", "k", "u", "r", "s", "m", "p"] + GREEK r = self.rng.random()
return Var(self.rng.choice(pool)) 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))
def num(self) -> Num: def num(self) -> Num:
r = self.rng.random() r = self.rng.random()
@@ -132,12 +159,45 @@ class TreeGen:
to = r.choice([Num(0)] * 4 + [INF] * 4 + [Num(1), Var("a"), Neg(INF)]) to = r.choice([Num(0)] * 4 + [INF] * 4 + [Num(1), Var("a"), Neg(INF)])
return Lim(self.expr(max(depth, 1), v), v, to) 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: def tree(self) -> Node:
r = self.rng r = self.rng
depth = r.choices([1, 2, 3, 4], [25, 40, 25, 10])[0] 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) e = self.big(depth - 1) if r.random() < 0.3 else self.expr(depth)
if r.random() < 0.2: if r.random() < 0.2:
lhs = r.choice([Var("y"), Var("f"), Var("x"), self.expr(r.choice([0, 1]))]) # 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])
return Eq(lhs, e) return Eq(lhs, e)
return e return e
+12
View File
@@ -0,0 +1,12 @@
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
+10 -1
View File
@@ -1,7 +1,7 @@
import collections import collections
import random import random
from isotope_typst.ast import Frac, Integral, Neg, Num, Pow, Var from isotope_typst.ast import Eq, Frac, Integral, Neg, Num, Pow, Var, walk
from isotope_typst.generate import TreeGen, split_of from isotope_typst.generate import TreeGen, split_of
from isotope_typst.render import to_english, to_typst from isotope_typst.render import to_english, to_typst
from isotope_typst.validate import compiles_many from isotope_typst.validate import compiles_many
@@ -33,3 +33,12 @@ def test_english_is_unambiguous():
def test_split_is_by_structure(): def test_split_is_by_structure():
assert split_of("x^2") == split_of("x^2") 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)))