Conditional Variables with Sympy Library in Python

Viewed 77

I tried to do this conditional function

import sympy as sp

y1=sp.Symbol('y1')
Xal=4*y1/T
raizl=sp.sqrt(1+Xal**2)
if 0<Xal<=1:
    Pl=T+8/3*(y1**2/T)
else:
    Pl=T/2*(raizl+(1/Xal)*sp.log(Xal+raizl))`

Using sympy but I have an error that is:

"cannot determine truth value of Relational"

but idk the reason why

Pl=sp.Piecewise((T+8/3*(y1**2/T), 0<Xal<=1), (T/2*(raizl+(1/Xal)*sp.log(Xal+raizl)), True))

I'll try to correct the error "cannot determine truth value of Relational"

1 Answers

You cannot follow and if with a boolean expression. This is to avoid the error of thinking that just because x > 1 doesn't evaluate then it must be True like if [x] will be True.

In Piecewise, if is not being used to make a decision; the condition just holds the unevaluated relationship and will return the corresponding expression if it ever becomes True. e.g. Piecewise((1, x > 1), (2, True)).subs(x,3) -> 1

Piecewise is the right object to hold expressions that need a condition to select which one you want.

Related