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
--
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 [email protected].
To post to this group, send email to [email protected].
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.