I am trying to define in sympy an inequality inecuacion = "19 < -25*x - 1 <= 37" so that I can print the solution set on a graph. When I print with a single value, i.e. like this: 2*x-4 < 0, it can be evaluated, but when it is 19 < -25*x - 1 <= 37, I get the error:
raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
Fragment of my code:
inecuacion = "19 < -25*x - 1 <= 37"
ineq = parse_expr(inecuacion)
interval = solveset(ineq, domain=S.Reals)
plot_interval(title=latex(ineq, mode="inline"),
start=interval.start, end=interval.end,
start_open=interval.left_open, end_open=interval.right_open,
x_axis=(-10, 10),
color="#073065")
How can I make it so that sympy can interpret this kind of inequality 19 < -25*x - 1 <= 37? Tanks very much. Regards
I posted the original question on StackOverflow in Spanish, it was answered by Abulafia, I leave the link if you want more details. ¿Cómo puedo evaluar una inecuación con dos desigualdades en python?
You can use a regular expression to "split" your double inequality into two separate inequalities, like so (explanations later):
As you can see, the trick is the regular expression
([<>]=?)which means the following:re.split()to not only return the resulting "chunks", but also return the "separators" between the chunks (which are the inequality signs of the inequalities).[<>]which can appear either<or>=?which can optionally appear with an equal sign (so that it admits>=and<=as a separator)The result of
re.split()is going to be a list with five chunks, as long as your input string has a pair of inequality comparers. The first chunk would be in this example the"20 ". The next chunk would be the first separator ("<"in the example) the next chunk would be " 3*x +5"in this case. The next would be the second separator, again"<"in this example, and the fourth and last chunk would be"26"`.The following
.join()joins the appropriate pieces together to form two expressions (inequalities). One with the first three pieces, another with the last three. The result oneq1andeq2is:Once we have it separated in this way, we can use
sympyto solve the two inequalities, for example like this:The solution found by sympy is an And operation between two intervals and it shows it like this:
If you want to reduce it to "a single interval" (which in this example would be
(5,7)) so that you can plot it with the technique explained [in this other answer](https://en.stackoverflow.com/a /510763/7123) you could calculate the intersection of these two intervals with the interval(-infinity, +infinity), for example like this:As you can see, we start by creating the infinite interval, and then we iterate through
intervals.args(which are each of the inequalities previously obtained withsolve()), to resolve the inequalities posed by each of them, and so on. have intervals whose intersection withsolutionwe compute with the&=operator.The result in
solutionis an interval, namely(5, 7)in this case, which you can already plot with the above solution.Note: This solution is not quite general, depending on the input inequality it may be that in the end you will not be able to obtain a single interval-solution, but two if
solve()generates two disjoint inequalities.