Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot determine truth value of Relational while simplifying expression #24045

Closed
clinssen opened this issue Sep 12, 2022 · 1 comment
Closed
Labels

Comments

@clinssen
Copy link

For a moderately simple expression, sympy>=1.11 throws an exception when simplifying. Small (minimal?) reproducer:

import sympy
a, b, c = sympy.symbols("a b c")
sympy.powsimp(sympy.exp(a)/((c*a - c*b)*(sympy.Float(1.0)*c*a - sympy.Float(1.0)*c*b)))
[...snip...]
~/.local/lib/python3.9/site-packages/sympy/core/relational.py in __bool__(self)
    509 
    510     def __bool__(self):
--> 511         raise TypeError("cannot determine truth value of Relational")
    512 
    513     def _eval_as_set(self):

TypeError: cannot determine truth value of Relational

The offending subexpression is:

(Pdb) self
1.0*a*c - 1.0*b*c < a*c - b*c

This works fine on sympy<=1.10. The cause was tracked down props to @pnbabu: earlier sympy used sorted(d.keys()) whereas the current release uses sorted(d.items()) while sorting a dictionary d created internally by powsimp():

0ca399d#diff-18021242f0d8c835ef8fa3190d8379ab5cb7ca45c31a75fffbc65d1f0f36797fL296

@oscarbenjamin
Copy link
Collaborator

I think this is a bug in ordered. It tries to sort this:

In [15]: l = [(((10, 0, 'tuple'), (2, (((3, 1, 'Add'), (2, (((3, 0, 'Mul'), (2, (((2, 0, 'Symbol'), (1, (
    ...: 'a',)), ((1, 0, 'Number'), (0, ()), (), 1), 1), ((2, 0, 'Symbol'), (1, ('c',)), ((1, 0, 'Number'
    ...: ), (0, ()), (), 1), 1))), ((1, 0, 'Number'), (0, ()), (), 1), 1), ((3, 0, 'Mul'), (2, (((2, 0, '
    ...: Symbol'), (1, ('b',)), ((1, 0, 'Number'), (0, ()), (), 1), 1), ((2, 0, 'Symbol'), (1, ('c',)), (
    ...: (1, 0, 'Number'), (0, ()), (), 1), 1))), ((1, 0, 'Number'), (0, ()), (), 1), -1))), ((1, 0, 'Num
    ...: ber'), (0, ()), (), 1), 1), ((10, 0, 'list'), (1, (((1, 0, 'Number'), (0, ()), (), -1),)), ((1,
    ...: 0, 'Number'), (0, ()), (), 1), 1))), ((1, 0, 'Number'), (0, ()), (), 1), 1), [(a*c - b*c, [-1])]
    ...: ), (((10, 0, 'tuple'), (2, (((3, 1, 'Add'), (2, (((3, 0, 'Mul'), (2, (((2, 0, 'Symbol'), (1, ('a
    ...: ',)), ((1, 0, 'Number'), (0, ()), (), 1), 1), ((2, 0, 'Symbol'), (1, ('c',)), ((1, 0, 'Number'),
    ...:  (0, ()), (), 1), 1))), ((1, 0, 'Number'), (0, ()), (), 1), 1.00000000000000), ((3, 0, 'Mul'), (
    ...: 2, (((2, 0, 'Symbol'), (1, ('b',)), ((1, 0, 'Number'), (0, ()), (), 1), 1), ((2, 0, 'Symbol'), (
    ...: 1, ('c',)), ((1, 0, 'Number'), (0, ()), (), 1), 1))), ((1, 0, 'Number'), (0, ()), (), 1), -1.000
    ...: 00000000000))), ((1, 0, 'Number'), (0, ()), (), 1), 1), ((10, 0, 'list'), (1, (((1, 0, 'Number')
    ...: , (0, ()), (), -1),)), ((1, 0, 'Number'), (0, ()), (), 1), 1))), ((1, 0, 'Number'), (0, ()), (),
    ...:  1), 1), [(1.0*a*c - 1.0*b*c, [-1])])]
    ...: 

In [16]: sorted(l)
TypeError: cannot determine truth value of Relational

The two elements of this list should be orderable but the problem is that the floats and integers compare equal:

In [17]: l[0]
Out[17]: 
(((10, 0, 'tuple'),
  (2,
   (((3, 1, 'Add'),
     (2,
      (((3, 0, 'Mul'),
        (2,
         (((2, 0, 'Symbol'),
           (1, ('a',)),
           ((1, 0, 'Number'), (0, ()), (), 1),
           1),
          ((2, 0, 'Symbol'),
           (1, ('c',)),
           ((1, 0, 'Number'), (0, ()), (), 1),
           1))),
        ((1, 0, 'Number'), (0, ()), (), 1),
        1),
       ((3, 0, 'Mul'),
        (2,
         (((2, 0, 'Symbol'),
           (1, ('b',)),
           ((1, 0, 'Number'), (0, ()), (), 1),
           1),
          ((2, 0, 'Symbol'),
           (1, ('c',)),
           ((1, 0, 'Number'), (0, ()), (), 1),
           1))),
        ((1, 0, 'Number'), (0, ()), (), 1),
        -1))),
     ((1, 0, 'Number'), (0, ()), (), 1),
     1),
    ((10, 0, 'list'),
     (1, (((1, 0, 'Number'), (0, ()), (), -1),)),
     ((1, 0, 'Number'), (0, ()), (), 1),
     1))),
  ((1, 0, 'Number'), (0, ()), (), 1),
  1),
 [(a*c - b*c, [-1])])

In [18]: l[1]
Out[18]: 
(((10, 0, 'tuple'),
  (2,
   (((3, 1, 'Add'),
     (2,
      (((3, 0, 'Mul'),
        (2,
         (((2, 0, 'Symbol'),
           (1, ('a',)),
           ((1, 0, 'Number'), (0, ()), (), 1),
           1),
          ((2, 0, 'Symbol'),
           (1, ('c',)),
           ((1, 0, 'Number'), (0, ()), (), 1),
           1))),
        ((1, 0, 'Number'), (0, ()), (), 1),
        1.0),
       ((3, 0, 'Mul'),
        (2,
         (((2, 0, 'Symbol'),
           (1, ('b',)),
           ((1, 0, 'Number'), (0, ()), (), 1),
           1),
          ((2, 0, 'Symbol'),
           (1, ('c',)),
           ((1, 0, 'Number'), (0, ()), (), 1),
           1))),
        ((1, 0, 'Number'), (0, ()), (), 1),
        -1.0))),
     ((1, 0, 'Number'), (0, ()), (), 1),
     1),
    ((10, 0, 'list'),
     (1, (((1, 0, 'Number'), (0, ()), (), -1),)),
     ((1, 0, 'Number'), (0, ()), (), 1),
     1))),
  ((1, 0, 'Number'), (0, ()), (), 1),
  1),
 [(1.0*a*c - 1.0*b*c, [-1])])

Related to this:

In [19]: list(ordered([1, 1.0]))
Out[19]: [1, 1.0]

In [20]: list(ordered([1.0, 1]))
Out[20]: [1.0, 1]

The problem is that there is no tie-breaker rule to distinguish floats from rationals.

smichr added a commit to smichr/sympy that referenced this issue Sep 24, 2023
smichr added a commit to smichr/sympy that referenced this issue Sep 24, 2023
smichr added a commit to smichr/sympy that referenced this issue Sep 24, 2023
smichr added a commit to smichr/sympy that referenced this issue Sep 24, 2023
smichr added a commit to smichr/sympy that referenced this issue Sep 26, 2023
@smichr smichr closed this as completed in 6c29b0d Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants