sympyrelational

Sympy Typeerror: cannot determine truth value of Relational (How to make sure x > 0)


I want to check if 6**x1 is bigger than 0 for every positive value of x1. I am using sympy.

I've done the following:

x1 = sm.symbols('x_1',nonnegative=True)
u = 6**x1
def checker(func):
   if u > 0:
      return True
   else:
      return False

However, I get the error:

TypeError: cannot determine truth value of Relational

I think this is because the function does not know that x1 is positive. But how do I make sure it knows this? Apparently, its not enough with the sm.symbols definition.

Zebraboard


Solution

  • SymPy only let's you compare things that can be computed to a number with literal > (and similar). If you want to make a query on a symbolic expression use expr.is_positive or (expr.is_extended_positive if you want oo to be considered, too).

    >>> u.is_positive
    True
    

    This can be None or False, too. None is returned when a definitive determination cannot be made, e.g. symbols('x').is_positive is None -> True.