From 0651c7533e3d945235fa6fd70acbb5a265af7c33 Mon Sep 17 00:00:00 2001 From: Paul Tal <1+paultal@noreply.git.paultal.online> Date: Fri, 27 Nov 2026 01:23:19 +0200 Subject: [PATCH] Use the whole alphabet and Greek alphabet for variables The variable pool was 14 Latin letters plus 10 Greek ones, so w, v, q, g, epsilon, rho, tau, ... were unknown to the model and got spelled out letter by letter. 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 free for dx and future derivatives. Fixes #4 --- src/isotope_typst/ast.py | 9 ++++++++- src/isotope_typst/generate.py | 33 ++++++++++++++++++++++++++++++--- tests/test_generate.py | 12 ++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 tests/test_generate.py diff --git a/src/isotope_typst/ast.py b/src/isotope_typst/ast.py index 1b67178..2c37cc6 100644 --- a/src/isotope_typst/ast.py +++ b/src/isotope_typst/ast.py @@ -106,7 +106,14 @@ class Eq(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"] BIG_OPS = (Integral, Sum, Lim) diff --git a/src/isotope_typst/generate.py b/src/isotope_typst/generate.py index aef8c64..2929cb5 100644 --- a/src/isotope_typst/generate.py +++ b/src/isotope_typst/generate.py @@ -16,7 +16,28 @@ import pathlib import random 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 .validate import compiles_many @@ -31,8 +52,14 @@ 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) - pool = ["x"] * 12 + ["y"] * 4 + ["z", "t", "a", "b", "c", "n", "k", "u", "r", "s", "m", "p"] + GREEK - return Var(self.rng.choice(pool)) + 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)) def num(self) -> Num: r = self.rng.random() diff --git a/tests/test_generate.py b/tests/test_generate.py new file mode 100644 index 0000000..8e2524b --- /dev/null +++ b/tests/test_generate.py @@ -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 -- 2.54.1