-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathisolve.py
More file actions
40 lines (34 loc) · 1.16 KB
/
isolve.py
File metadata and controls
40 lines (34 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'''
isolve.py
Single variable inequality solver
'''
from sympy import Symbol, sympify, SympifyError
from sympy import solve_poly_inequality, solve_rational_inequalities
from sympy import solve_univariate_inequality, Poly
from sympy.core.relational import Relational, Equality
def isolve(ineq_obj):
x = Symbol('x')
expr = ineq_obj.lhs
rel = ineq_obj.rel_op
if expr.is_polynomial():
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:
return solve_univariate_inequality(ineq_obj , x, relational=False)
if __name__ == '__main__':
ineq = input('Enter the inequality to solve: ')
try:
ineq_obj = sympify(ineq)
except SympifyError:
print('Invalid inequality')
else:
# We check if the input expression is an inequality here
if isinstance(ineq_obj, Relational) and not isinstance(ineq_obj, Equality):
print(isolve(ineq_obj))
else:
print('Invalid inequality')