Reputation: 25023
If I try to have an expression whose value depends on another expression
from sympy import *
x = symbols('x')
y1 = x/cos(x)
y2 = y2 if y1>0 else nan
but an exception is raised
File /usr/lib/python3.10/site-packages/sympy/core/relational.py:511, in Relational.__bool__(self)
510 def __bool__(self):
--> 511 raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
Is there a chance of having the effect that I'd want?
In case my problem is a X-Y problem, what I ultimately want to do is to plot x/cos(x)
only where the function is positive.
UPDATE
I used the useful suggestion of @Oscar Benjamin (that completely answers my original question) but I have other issues when plotting the Piecewiswe
function, that I'll expose in another question, as well as a wrong plot when I use the plot_implicit
solution they suggested.
Upvotes: 0
Views: 248
Reputation: 14480
What you're looking for is Piecewise
:
In [8]: p = Piecewise((x/cos(x), x/cos(x) > 0), (S.NaN, True))
In [9]: p
Out[9]:
⎧ x x
⎪────── for ────── > 0
⎨cos(x) cos(x)
⎪
⎩ nan otherwise
A more direct solution to your problem though would be something like
plot_implicit(Eq(y, x/cos(x)) & (x/cos(x) > 0))
Upvotes: 1