Skip to content

Commit

Permalink
Trac #32185: Failing weak order assertion on random symbolic expression
Browse files Browse the repository at this point in the history
{{{
File "src/sage/symbolic/random_tests.py", line 430, in
sage.symbolic.random_tests.test_symbolic_expression_order
Failed example:
    test_symbolic_expression_order(10000)  # long time
Exception raised:
    Traceback (most recent call last):
      File "/srv/public/kliem/sage/local/lib/python3.7/site-
packages/sage/doctest/forker.py", line 718, in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/srv/public/kliem/sage/local/lib/python3.7/site-
packages/sage/doctest/forker.py", line 1137, in compile_and_execute
        exec(compiled, globs)
      File "<doctest
sage.symbolic.random_tests.test_symbolic_expression_order[2]>", line 1,
in <module>
        test_symbolic_expression_order(Integer(10000))  # long time
      File "/srv/public/kliem/sage/local/lib/python3.7/site-
packages/sage/symbolic/random_tests.py", line 456, in
test_symbolic_expression_order
        assert_strict_weak_order(a, b, c, mixed_order)
      File "/srv/public/kliem/sage/local/lib/python3.7/site-
packages/sage/symbolic/random_tests.py", line 387, in
assert_strict_weak_order
        cmp_M[i, j] = (cmp_func(x[i], x[j]) == 1)   # or -1, doesn't
matter
      File "sage/symbolic/comparison.pyx", line 228, in
sage.symbolic.comparison.mixed_order
(build/cythonized/sage/symbolic/comparison.cpp:4349)
        cpdef int mixed_order(lhs, rhs) except -2:
      File "sage/symbolic/comparison.pyx", line 268, in
sage.symbolic.comparison.mixed_order
(build/cythonized/sage/symbolic/comparison.cpp:4126)
        less_than = _mixed_key(lhs) < _mixed_key(rhs)
      File "sage/symbolic/comparison.pyx", line 355, in
sage.symbolic.comparison._mixed_key.__lt__
(build/cythonized/sage/symbolic/comparison.cpp:5238)
        return det_ex < 0
    TypeError: '<' not supported between instances of 'Pi' and 'int'
**********************************************************************
1 item had failures:
   2 of   4 in sage.symbolic.random_tests.test_symbolic_expression_order
    [49 tests, 2 failures, 3.59 s]
}}}

The underlying problem is the following:
{{{
sage: pi = sage.symbolic.constants.Pi()
sage: pi < 0
------------------------------------------------------------------------
---
TypeError                                 Traceback (most recent call
last)
<ipython-input-7-f23f08971c70> in <module>
----> 1 pi < Integer(0)

/srv/public/kliem/sage/local/lib/python3.7/site-
packages/sage/rings/integer.pyx in
sage.rings.integer.Integer.__richcmp__
(build/cythonized/sage/rings/integer.c:7888)()
    947             c = mpz_cmp_d((<Integer>left).value, d)
    948         else:
--> 949             return coercion_model.richcmp(left, right, op)
    950
    951         return rich_to_bool_sgn(op, c)

/srv/public/kliem/sage/local/lib/python3.7/site-
packages/sage/structure/coerce.pyx in
sage.structure.coerce.CoercionModel.richcmp
(build/cythonized/sage/structure/coerce.c:20854)()
   2006             raise bin_op_exception('<=', x, y)
   2007         elif op == Py_GT:
-> 2008             raise bin_op_exception('>', x, y)
   2009         else:
   2010             raise bin_op_exception('>=', x, y)

TypeError: unsupported operand parent(s) for >: 'Integer Ring' and
'<class 'sage.symbolic.constants.Pi'>'
}}}

URL: https://trac.sagemath.org/32185
Reported by: gh-kliem
Ticket author(s): Michael Orlitzky
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Jul 23, 2021
2 parents 14d47e3 + 551d3ae commit c4935c8
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/sage/symbolic/comparison.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ cpdef int mixed_order(lhs, rhs) except -2:
sage: mixed_order(SR(oo), sqrt(2))
1
Ensure that :trac:`32185` is fixed::
sage: mixed_order(pi, 0)
1
sage: mixed_order(golden_ratio, 0)
1
sage: mixed_order(log2, 0)
1
"""
if lhs is rhs:
return 0
Expand Down Expand Up @@ -345,7 +355,7 @@ class _mixed_key(object):
return pynac_result == relational_true

det_ex = self.ex - other.ex
if not has_symbol_or_function((<Expression>rel)._gobj):
if not has_symbol_or_function((<Expression>rel)._gobj) and not det_ex.is_constant():
while hasattr(det_ex, 'pyobject') and isinstance(det_ex, Expression):
try:
det_ex = det_ex.pyobject()
Expand Down

0 comments on commit c4935c8

Please sign in to comment.