From c5d1988a7f6462a47b202d04ff2343abf918d300 Mon Sep 17 00:00:00 2001 From: Taksh Date: Sat, 18 Jul 2026 19:47:29 +0300 Subject: [PATCH] fix: allow Theta on nonnegative Abs expressions Sympy leaves Abs(n).is_positive as None, so Theta wrongly returned Undefined. --- src/estimates/order_of_magnitude.py | 8 +++++++- tests/test_all.py | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/estimates/order_of_magnitude.py b/src/estimates/order_of_magnitude.py index e8786df..aa7571a 100644 --- a/src/estimates/order_of_magnitude.py +++ b/src/estimates/order_of_magnitude.py @@ -116,7 +116,13 @@ def __new__(cls, expr): if isinstance(expr, OrderOfMagnitude): return expr - if not expr.is_positive: + # Abs(x) is nonnegative but sympy often leaves .is_positive as None (zero possible). + # Still a valid order of magnitude when not identically zero. + if expr.is_positive: + pass + elif expr.is_nonnegative is True and expr.is_zero is not True: + pass + else: print(f"Warning: a non-positive argument {expr!s} was passed to Theta.") return Undefined() diff --git a/tests/test_all.py b/tests/test_all.py index 862b3ed..cae5c5c 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -110,3 +110,12 @@ def test_subst_all_solution_reversed(self, capsys): def test_sympy_simplify_solution(self, capsys): sympy_simplify_solution() self.proof_complete(capsys) + + def test_theta_abs_nonnegative(self): + """Theta(Abs(n)) must not become Undefined when positivity is unknown.""" + from sympy import Abs, Symbol + from estimates.order_of_magnitude import Theta, Undefined + n = Symbol("n", integer=True) + t = Theta(Abs(n)) + assert not isinstance(t, Undefined) + assert str(t).startswith("Theta")