Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract value from And object given by reduce_rational_inequalities([[-3 < 2*x + 1]], x) . example.And(-2 < x, x < oo)

I want to extract value from the result i get by running reduce_rational_inequalities([[-3 < 2*x + 1]], x). For example my answer after running the above function is

And(-2 < x, x < oo)

. Now i want

-2

from this output. I am doing same thing operation with another equation as well. reduce_rational_inequalities([[2*x + 1 < 5]], x). And answer i am getting is

And(-oo < x, x < 2)

. Now i want

2

from this output as well. and want to merge those two outputs.

If anyone went through this sympy library with particular operation. Please let me know.

Thank you for any help in advance.

like image 588
Viraj Avatar asked Dec 10 '25 04:12

Viraj


2 Answers

Someone may well provide a better answer. But this approach does work.

>>> reduce_rational_inequalities([[-3 < 2*x + 1]], x)
And(-2 < x, x < oo)
>>> result = reduce_rational_inequalities([[-3 < 2*x + 1]], x)
>>> result.args
(-2 < x, x < oo)
>>> result.args[0]
-2 < x
>>> result.args[1]
x < oo
>>> result.args[0].args[0]
-2
>>> result.args[0].args[0].is_number
True

Even when the result can be expressed as a tuple of one-sided intervals, as is the case here, then the constants involved could still appear on either side of the inequality. However, you can use the is_number method to determine which side contains the number.

Let's hope someone has a better way.

like image 178
Bill Bell Avatar answered Dec 14 '25 12:12

Bill Bell


If you set x as finite, the x < oo part is removed:

>>> from sympy.solvers.inequalities import reduce_rational_inequalities
>>> x = symbols('x', finite=True)
>>> reduce_rational_inequalities([[-3 < 2*x + 1]], x)
-2 < x
like image 31
asmeurer Avatar answered Dec 14 '25 12:12

asmeurer