Reputation: 339
At some point in my code, I add an upper constraint on a sympy symbol.
sympy.refine(x, sympy.Q.positive(upper_bound - x))
I would liketo retrieve programmaticaly from the symbol "x" the value of upper_bound (and other constraints). Any idea as how to achieve this ?
cheers
Upvotes: 3
Views: 1846
Reputation: 1264
The refine function does not work that way - it just simplifies an expression given a constraint. For example:
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)
Unfortunately, the use of inequalities does not appear to do anything useful at the moment:
In [62]: sympy.refine(expr, sympy.Q.is_true(x>0))
Out[62]: sqrt(x**2)
You could probably do something useful with 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])
Or maybe a more useful example:
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]
Upvotes: 3