SymPy: Limit symbol/variable to interval

后端 未结 4 1722
生来不讨喜
生来不讨喜 2021-01-04 08:32

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

4条回答
  •  情话喂你
    2021-01-04 09:13

    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)
    

    Here we search for a solution of equation x**2 == 1 such that x is in the interval [0.5, 3].

提交回复
热议问题