如何查询sympy约束?

2024-04-18 05:52:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的代码中,我在一个sympy符号上添加了一个上约束。 sympy.refine(x, sympy.Q.positive(upper_bound - x))

我想从符号“x”中检索programmaticaly上界值(和其他约束)。有什么办法吗?在

干杯


Tags: 代码符号upperboundsympy办法positiverefine
1条回答
网友
1楼 · 发布于 2024-04-18 05:52:58

refine函数不是这样工作的-它只是简化了给定约束的表达式。例如:

In [54]: import sympy

In [55]: x = sympy.S('x')

In [56]: expr = sympy.S('sqrt(x**2)')

In [57]: sympy.refine(expr, sympy.Q.positive(x))
Out[57]: x

In [58]: sympy.refine(expr, sympy.Q.negative(x))
Out[58]: -x

In [59]: sympy.refine(expr, sympy.Q.real(x))
Out[59]: Abs(x)

不幸的是,目前使用不平等似乎没有任何用处:

^{pr2}$

您可能可以对solveset执行一些有用的操作:

In [68]: expr = sympy.S('A * x**2 + B * x + C')

In [69]: sympy.solveset(expr, x, sympy.Interval(1,10))
Out[69]: ConditionSet(x, Eq(A*x**2 + B*x + C, 0), [1, 10])

或者是一个更有用的例子:

In [19]: a = sympy.S('(x**2)*(sin(x)+x)')

In [20]: x = sympy.S('x')

In [25]: b = sympy.solveset(a,x,sympy.Interval(-2,2))

In [26]: b
Out[26]: ConditionSet(x, Eq(x + sin(x), 0), [-2, 2])

In [34]: b.base_set
Out[34]: [-2, 2]

相关问题 更多 >