从 reduce_rational_inequalities([[-3 < 2*x + 1]], x) 给出的 And 对象中提取值。 example.And(-2 < x, x < oo)

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

我想从 运行 reduce_rational_inequalities([[-3 < 2*x + 1]], x) 得到的结果中提取值。例如,我在 运行 之后的回答是

And(-2 < x, x < oo)

。现在我要

-2

从这个输出。我也在用另一个方程式做同样的操作。 reduce_rational_inequalities([[2*x + 1 < 5]], x)。我得到的答案是

And(-oo < x, x < 2)

。现在我要

2

也来自此输出。并希望合并这两个输出。

如果有人通过特定操作访问过这个 sympy 库。请告诉我。

提前感谢您的帮助。

有人可能会提供更好的答案。但这种方法确实有效。

>>> 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

即使结果可以表示为单边区间的元组,就像这里的情况一样,那么所涉及的常数仍然可以出现在不等式的两边.但是,您可以使用 is_number 方法来确定哪一侧包含数字。

希望有人有更好的方法。

如果您将 x 设置为有限,x < oo 部分将被删除:

>>> from sympy.solvers.inequalities import reduce_rational_inequalities
>>> x = symbols('x', finite=True)
>>> reduce_rational_inequalities([[-3 < 2*x + 1]], x)
-2 < x