Datasets:
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
Tags:
mathematics
formal-verification
symbolic-algebra
theorem-proving
multi-agent-systems
benchmark
Download tests/test_exact.py from tailot/lingologos-math: direct link, hf CLI and curl.
- Browser
- Download file 3.14 kB
-
https://huggingface.co/datasets/tailot/lingologos-math/resolve/main/tests/test_exact.py
- Command line
-
hf download hf://datasets/tailot/lingologos-math/tests/test_exact.py
-
curl -L -o test_exact.py https://huggingface.co/datasets/tailot/lingologos-math/resolve/main/tests/test_exact.py
3.14 kB
| """Exact arithmetic: polynomial tools, Sturm root isolation and cell truth against Z3.""" | |
| import random | |
| from fractions import Fraction as F | |
| import pytest | |
| from lgmath import answers, exact, parse_a, smt | |
| from lgmath.tree import UnsupportedError | |
| def poly(*coeffs): | |
| return exact.p_norm([F(c) for c in coeffs]) | |
| def test_polynomial_gcd_and_squarefree(): | |
| p = exact.p_mul(poly(-1, 1), exact.p_mul(poly(-1, 1), poly(-2, 1))) # (x-1)^2 (x-2) | |
| assert exact.p_squarefree(p) == exact.p_monic(exact.p_mul(poly(-1, 1), poly(-2, 1))) | |
| assert exact.p_gcd(poly(-1, 0, 1), poly(1, 1)) == poly(1, 1) | |
| def test_isolation_counts_every_real_root(): | |
| cases = {(-6, 11, -6, 1): 3, (-2, 0, 1): 2, (1, 0, 1): 0, (1, -3, 0, 1): 3, (1, -3, 0, 0, 0, 1): 3} | |
| for coeffs, expected in cases.items(): | |
| p = exact.p_squarefree(poly(*coeffs)) | |
| roots = exact.isolate_roots(p) if exact.p_deg(p) >= 1 else [] | |
| assert len(roots) == expected, coeffs | |
| for root in roots: | |
| if root.exact is None: | |
| assert exact.sign(exact.p_eval(p, root.lo)) * exact.sign(exact.p_eval(p, root.hi)) < 0 | |
| def test_rational_roots_are_recognised_as_exact(): | |
| roots = exact.isolate_roots(exact.p_squarefree(poly(-6, 11, -6, 1))) | |
| assert [r.exact for r in roots] == [F(1), F(2), F(3)] | |
| def test_evaluation_is_exact_and_division_by_zero_fails_cleanly(): | |
| assert exact.eval_term(parse_a.parse_problem("evaluate 1/3 + 1/6")[1], {}) == F(1, 2) | |
| with pytest.raises(exact.DivisionByZero): | |
| exact.check_fragment(parse_a.parse_problem("evaluate 1/(2-2)")) | |
| with pytest.raises(UnsupportedError): | |
| exact.check_fragment(parse_a.parse_problem("prove x/y > 0")) | |
| with pytest.raises(UnsupportedError): | |
| exact.check_fragment(parse_a.parse_problem("evaluate 2^x")) | |
| def _random_univariate(rng): | |
| def atom(): | |
| coeffs = [rng.randint(-4, 4) for _ in range(rng.randint(2, 5))] | |
| text = " + ".join("%d*x^%d" % (c, i) for i, c in enumerate(coeffs)) | |
| return "(%s) %s %d" % (text, rng.choice(["=", "!=", "<", "<=", ">", ">="]), rng.randint(-3, 3)) | |
| parts = [atom() for _ in range(rng.randint(1, 3))] | |
| joined = parts[0] | |
| for part in parts[1:]: | |
| joined = "(%s) %s (%s)" % (joined, rng.choice(["and", "or", "=>"]), part) | |
| return "solve %s for x" % joined | |
| def test_cell_decomposition_agrees_with_z3_on_random_formulas(): | |
| """sigma built from the exact cells must be provably equivalent to the formula (Z3, all reals).""" | |
| rng = random.Random(99) | |
| checked = 0 | |
| for _ in range(60): | |
| problem = parse_a.parse_problem(_random_univariate(rng)) | |
| table, truth = exact.exact_cell_truth(problem[1], "x") | |
| answer = answers.set_answer("x", table, truth) | |
| view = smt.problem_smt(problem) | |
| sigma = answers.sigma_smt(answer) | |
| sound = "(assert (and %s (not %s)))" % (sigma, view["phi"]) | |
| complete = "(assert (and %s (not %s)))" % (view["phi"], sigma) | |
| assert smt.check(view["decls"], [sound])[0] == "unsat" | |
| assert smt.check(view["decls"], [complete])[0] == "unsat" | |
| checked += 1 | |
| assert checked == 60 | |