Compare ranges of numbers SymPy

Viewed 65

I have a problem in SymPy. I solved an inequality, and I need to check if the solution is a certain range.

For example, if I wanted to solve the inequality sympy.solve(x**3 > 0), (which would return (0 < x) & (x < oo)), and I wanted to return true if the solve function returned (0 > x) & (x > -oo), it would return false, as those two ranges are not equal.

1 Answers

Univariate relationships can be converted to Sets and you can check for Set intersections. If the intersection not equivalent to either of the sets then neither is contained in the other:

>>> a, b = (0 < x) & (x < oo), (0 > x) & (x > -oo)
>>> sa = a.as_set(); sb = b.as_set()
>>> sa
Interval.open(0, oo)
>>> sa.intersection(sb)
EmptySet

Since there is no intersection at all, these sets must be different.

Here is a case where there is overlap (containment):

>>> Interval(1, 3).intersection(sa)
Interval(1, 3)
Related