Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SymPy: Limit symbol/variable to interval

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.

like image 990
Se Norm Avatar asked Oct 23 '13 22:10

Se Norm


1 Answers

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

like image 89
vitaut Avatar answered Sep 20 '22 13:09

vitaut