Generate equations with long left sides

Eq left sides were only y, f, x or a depth-0/1 expression, so x^2 + y^2 = r^2
never appeared in training and the model dropped everything after "=".
Left sides are now a single variable (35%), a flat sum of 2-3 textbook terms
like x^2, 3 x, sin(x)^2 (30%), or a random expression up to depth 3 (35%).
The overall length limit still applies.

The hand-test sentences for this live with the eval changes in #3.

Fixes #2
This commit is contained in:
paultal committed 2026-11-27 01:22:36 +02:00
1 parent 473a9358d6
commit bce9647990
2 files changed
+44 -2

No files matched your search

+34 -1
View File
@@ -132,12 +132,45 @@ 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:
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 e