Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5641d89b27 | ||
|
|
bce9647990 |
No files matched your search
@@ -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
|
||||
|
||||
|
||||
+10
-1
@@ -1,7 +1,7 @@
|
||||
import collections
|
||||
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.render import to_english, to_typst
|
||||
from isotope_typst.validate import compiles_many
|
||||
@@ -33,3 +33,12 @@ 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)))
|
||||
Reference in new issue
Block a user