pythontypeerrorsympypiecewise

Type Error in Sympy Piecwise Python: cannot determine truth value of Relational


i try to create linear function in an interval and the other interval is 0. and add it to an array

so i tried this code

import numpy as np
import sympy as sp
import matplotlib as plt  # This is all the library's i need
import mpmath
x = sp.symbols('x')
n = 10
xx = (np.array([np.linspace(0, 1, n+1)]))
i = 0
N = []
N[0] = sp.Piecewise( ((xx[0, i + 1] - x) / (xx[0, i + 1] - xx[0, i]), xx[0, i] <= x),
                     ((xx[0, i + 1] - x) / (xx[0, i + 1] - xx[0, i]), x <= xx[0, i + 1]),
                     (0, True)
)

and it always returns

 line 511, in __bool__
    raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational

EDIT: after i add x is positive to symbol i try to make a vector of linear function in the intervals in a loop and i don't know why it refuses to determine the the truth value even tough in the line above it succeed:

import sympy as sp
import matplotlib as plt  # This is all the library's i need
import mpmath
x = sp.symbols('x', positive=True)
n = 10
xx = (np.array([np.linspace(0, 1, n+1)]))
i = 0
N = []
a = sp.Piecewise(
              ((xx[0, i + 1] - x) / (xx[0, i + 1] - xx[0, i]), (xx[0, i]) <= x),
              ((xx[0, i + 1] - x) / (xx[0, i + 1] - xx[0, i]), (x <= (xx[0, i + 1]))),
              (0, True)
)
N.append(a)
for i in range(1, n):
    a = sp.Piecewise(
        (0, x < xx[0, i - 1]),
        ((xx[0, i - 1] - x) / (xx[0, i - 1] - xx[0, i]), ((xx[0, i - 1]) <= x)),
        ((xx[0, i - 1] - x) / (xx[0, i - 1] - xx[0, i]), (x <= (xx[0, i]))),
        ((xx[0, i + 1] - x) / (xx[0, i + 1] - xx[0, i]), ((xx[0, i]) <= x)),
        ((xx[0, i + 1] - x) / (xx[0, i + 1] - xx[0, i]), (x <= (xx[0, i + 1]))),
        (0, True)
    )
    N.append(a)

and i get the same Error:

File "", line 23, in File "pythonProjectFEANew\venv\lib\site-packages\sympy\core\relational.py", line 511, in bool raise TypeError("cannot determine truth value of Relational") TypeError: cannot determine truth value of Relational



Solution

  • This is a bug in numpy float64 somehow:

    In [48]: xx[0,1]
    Out[48]: 0.1
    
    In [49]: type(xx[0,1])
    Out[49]: numpy.float64
    
    In [50]: xx[0,1] < x
    ---------------------------------------------------------------------------
    TypeError
    
    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 error comes from sympy but it's because numpy.float64.__lt__ is for some reason calling bool on the inequality that is returned by x.__gt__. If you reverse the direction of the inequality or convert the numpy.float64 to an ordinary Python float then it will work fine:

    In [51]: x > xx[0,1]
    Out[51]: x > 0.1
    
    In [52]: float(xx[0,1]) < x
    Out[52]: x > 0.1
    

    The bug is that numpy.float64.__lt__ should be returning NotImplemented somewhere but instead does something weird.