Unifying the inequality solvers

23 views
Skip to first unread message

Amit Saha

unread,
May 10, 2015, 8:24:25 AM5/10/15
to sy...@googlegroups.com
Hi all,

I was exploring the inequality solvers [1] and it seemed to me that
for someone exploring inequality solving with sympy, a helper method
which would just allow the user to feed in the expression and the
relational operator would be useful:

from sympy import Symbol, solve_poly_inequality,
solve_rational_inequalities, solve_univariate_inequality
def isolve(str_expr, rel):
x = Symbol('x')
expr = sympify(str_expr)
if expr.is_polynomial():
# create a Poly() object
p = Poly(expr, x)
return solve_poly_inequality(p, rel)
elif expr.is_rational_function():
p1, p2 = expr.as_numer_denom()
num = Poly(p1)
denom = Poly(p2)
return solve_rational_inequalities([[
((num, denom), rel)
]])
else:
# solve_univariate_uninequality() function expects
# the expressesion in the form "expr" "rel" 0
expr = sympify(str_expr + rel + '0')
return solve_univariate_inequality(expr , x, relational=False)

# polynomial
print(isolve('x+2', '<'))
# rational function
print(isolve('(x-1)/(x+2)', '>'))
# non-polynomial, non-rational
print(isolve('sin(x)-1', '<'))


Output:

[(-oo, -2)]
(-oo, -2) U (1, oo)
(-oo, pi/2) U (pi/2, oo)


[1] http://docs.sympy.org/dev/modules/solvers/inequalities.html

What do you all think of this? Is there a better way to do this? If we
have such a method, the user could just call isolve() function, and if
needed learn more about each individual function.

Best,
Amit.


--
http://echorand.me

Aaron Meurer

unread,
May 10, 2015, 1:22:21 PM5/10/15
to sy...@googlegroups.com
Why wouldn't the input just be the inequality object itself?

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CANODV3kUfWfRUGxJnw%3DJtcy3t6-14SS5e3wV4YvZDnDeEteK_w%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Amit Saha

unread,
May 10, 2015, 6:23:31 PM5/10/15
to sy...@googlegroups.com
Hi Aaron,

On Mon, May 11, 2015 at 3:21 AM, Aaron Meurer <asme...@gmail.com> wrote:
> Why wouldn't the input just be the inequality object itself?

That would be a better way, thanks. Here is what I got now:

from sympy import Symbol, solve_poly_inequality,
solve_rational_inequalities, solve_univariate_inequality
def isolve(ineq_obj):
x = Symbol('x')
expr = ineq_obj.lhs
rel = ineq_obj.rel_op
if expr.is_polynomial():
# create a Poly() object
p = Poly(expr, x)
return solve_poly_inequality(p, rel)
elif expr.is_rational_function():
p1, p2 = expr.as_numer_denom()
num = Poly(p1)
denom = Poly(p2)
return solve_rational_inequalities([[
((num, denom), rel)
]])
else:
# solve_univariate_uninequality() function expects
# the expressesion in the form "expr" "rel" 0
return solve_univariate_inequality(ineq_obj , x, relational=False)

# polynomial
print(isolve(x+2<0))
# rational function
print(isolve((x-1)/(x+2)>0))
# non-polynomial, non-rational
print(isolve(sin(x)-1 < 0))

What do you think of it now?

One limitation is that I am assuming the variable is "x".

-Amit.
Reply all
Reply to author
Forward
0 new messages