0

I am trying to solve inequality using sympy symbols but I can't set an interval for my symbols, is there a way to do it???

import sympy as sy
p = sy.Symbol("p") # 0 < p < 1
f = p**2-1
if f < 0:
    print("f is negative")
  • `if` is a python expression that expects a simple True/False value. `f<0` cannot produce that, since `f` is a sympy expression. – hpaulj Dec 04 '21 at 20:47
  • https://docs.sympy.org/latest/modules/core.html#module-sympy.core.relational, `Lt(f,0)` setups of a relational – hpaulj Dec 04 '21 at 21:12
  • There isn't a good way to set inequality assumptions on a symbol (apart from basic things like positive, negative etc). – Oscar Benjamin Dec 04 '21 at 21:41

1 Answers1

0

If you replace x with an expression that has the range you are interested in then you can see that the relationship is true:

>>> from sympy import var, cancel, bottom_up
>>> var('x')
x
>>> var('eps', positive=True)
eps
>>> eq=x**2-1<0
>>> eq.subs(x,1/(1+eps))
-1 + (eps + 1)**(-2) < 0
>>> bottom_up(_, cancel)
True
smichr
  • 16,948
  • 2
  • 27
  • 34
  • still didn't solve my problem, it didn't define the symbol 'P' as a value between to numbers. – Alex Steve Dec 12 '21 at 16:43
  • Isn't the primary question about how to get the inequality to solve with a given assumption for a variable? The secondary question is how to set that assumption on the variable. I showed by example a method of solving a similar inequality which doesn't set an assumption on the symbol (because there is no way to do so). Rather, I replace the symbol with an expression having the desired range and show that it allows the inequality to evaluate: the primary objective is attained by using an auxiliary expression to take the place of the symbol. – smichr Dec 13 '21 at 04:23