SymPy:将符号/变量限制为区间 [英] SymPy: Limit symbol/variable to interval
本文介绍了SymPy:将符号/变量限制为区间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 SymPy,是否可以将符号/变量的可能值限制在某个范围内?我现在可以在定义符号时设置一些属性,例如 positive=True
,但我需要更多控制,即我需要将其设置在区间 [0,1] 内.然后应该使用这个假设来求解、简化等.
Using SymPy, is it possible to limit the possible values of a symbol/variable to a certain range? I now I can set some properties while defining symbols, like positive=True
, but I need more control, i.e. I need to set it to be in the interval [0,1]. This assumption should then be used for solving, simplifying etc.
推荐答案
您可以将边界指定为不等式,例如 x >= lb
和 x <= ub
,例如:
You can specify the bounds as inequalities such as x >= lb
and x <= ub
, for example:
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
solve([x >= 0.5, x <= 3, x**2 - 1], x)
这里我们搜索方程 x**2 == 1
的解,使得 x
在区间 [0.5, 3]
.
Here we search for a solution of equation x**2 == 1
such that x
is in the interval [0.5, 3]
.
这篇关于SymPy:将符号/变量限制为区间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文