I'm trying to solve an inequality using sympy.solve
, here is the code
from sympy import Symbol, solve
x = Symbol('x', positive=True, integer=True)
ineq1 = x - 3 < 0
solution = solve((ineq1), x)
print(solution)
The program above yields x < 3
. The result makes sense though, i'd like to get a set that consists of 2 integers 1 and 2, {1, 2}
.
how do i achieve that?
A univariate inequality can be handled by Sets and Set intersection can limit the result to positive integers:
>>> from sympy import Range, oo, S, And
>>> from sympy.abc import x
>>> (x - 3 < 0).as_set().intersection(Range(1,oo))
{1, 2}
You can also replace Range(1,oo)
with S.Naturals
. You can also use a compound expression and intersect its set with S.Integers
:
>>> And(x>0,x<3).as_set().intersection(S.Integers)
{1, 2}