def isolve(expr): x = symbols('x') ineq_obj = sympify(expr > 0) lhs = ineq_obj.lhs rel = ineq_obj.rel_op sln = 0 if expr.is_polynomial(): p = Poly(lhs, x) sln = solve_poly_inequality(p, rel) return sln elif expr.is_rational_function(): numer, denom = lhs.as_numer_denom() p1 = Poly(numer) p2 = Poly(denom) sln = solve_rational_inequalities([[((p1, p2), rel)]]) else: sln = solve_univariate_inequality(ineq_obj, x, relational=False) return sln
def solve_poly(expr): x = Symbol('x') ineq_obj = expr lhs = ineq_obj.lhs p = Poly(lhs, x) rel = ineq_obj.rel_op return solve_poly_inequality(p, rel)
def solve_poly_inequality_1(expression): print(f"function 1 ") x = Symbol('x') ineq_obj = expression #-x**2 + 4 < 0 lhs = ineq_obj.lhs p = Poly(lhs, x) rel = ineq_obj.rel_op ans_1 = solve_poly_inequality(p, rel) print(f"ans_1 {ans_1}") return ans_1
def resolve(self): """ return value of the solution of the inequation in a String""" results = [] for sym in self._symbols: lfrac = self._getFrac(str(self._left_operand)) rfrac = self._getFrac(str(self._right_operand)) frac = lfrac + ['-'] + rfrac expr = self._unfraction(frac) den = self._getDenom( str(self._left_operand) + '-' + str(self._right_operand)) polyExpr = Poly(expr, sym) if den != '': polyDen = Poly(den, sym) else: polyDen = Poly('1', sym) posiCase = solve_poly_inequality(polyDen, '>') negaCase = solve_poly_inequality(polyDen, '<') posCase = posiCase[0] for cas in posiCase: posCase = posCase.union(cas) negCase = negaCase[0] for cas in negaCase: negCase = negCase.union(cas) posiSol = solve_poly_inequality(polyExpr, self._operator) negaSol = solve_poly_inequality(-polyExpr, self._operator) posSol = posiSol[0] for cas in posiSol: posSol = posSol.union(cas) negSol = negaSol[0] for cas in negaSol: negSol = negSol.union(cas) result = (posCase.intersect(posSol)).union( negCase.intersect(negSol)) results.append(result) return results
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)
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)
def solve_inequality(ineq_obj): x = Symbol('x') lhs = ineq_obj.lhs if lhs.is_polynomial(): p = Poly(lhs, x) rel = ineq_obj.rel_op pprint(solve_poly_inequality(p, rel)) elif lhs.is_rational_function(): lhs = ineq_obj.lhs numer, denom = lhs.as_numer_denom() p1 = Poly(numer) p2 = Poly(denom) rel = ineq_obj.rel_op pprint(solve_rational_inequalities([[((p1, p2), rel)]])) else: pprint(solve_univariate_inequality(ineq_obj, x, relational=False))
def i_solver(expr_raw): x = sympy.Symbol("x") rel = expr_raw.rel_op lhs = expr_raw.lhs if expr.is_polynomial(): poly = sympy.Poly(lhs, x) return sympy.solve_poly_inequality(poly, rel) elif expr.is_rational_function(): numer, denum = lhs.as_numer_denom() p1 = sympy.Poly(numer) p2 = sympy.Poly(denum) return sympy.solve_rational_inequalities([[((p1, p2), rel)]]) else: return sympy.solve_univariate_inequality(expr_raw, x, relational=False)
def isolve(expr): """This function solves the inequality.""" x = sp.Symbol('x') if expr.is_polynomial(): lhs = expr.lhs p = sp.Poly(lhs, x) rel = expr.rel_op ans = sp.solve_poly_inequality(p, rel) elif expr.is_rational_function(): lhs = expr.lhs numer, denom = lhs.as_numer_denom() p1 = sp.Poly(numer) p2 = sp.Poly(denom) rel = expr.rel_op ans = sp.solve_rational_inequalities([[((p1, p2), rel)]]) else: ans = sp.solve_univariate_inequality(expr, x, relational=False) print(ans)
def poly_solver(ineq_obj): #This is a prequisite for every function x=Symbol('x') expr=ineq_obj.lhs rel=ineq_obj.rel_op #First we check if the function is polynomial and solve if it is if expr.is_polynomial(): p=Poly(expr,x) return solve_poly_inequality(p,rel) #If the function is not polynomial we use elif(else if) to check whether it is rational, if so we solve elif expr.is_rational_function(): numer,denom=expr.as_numer_denom() p1=Poly(numer) p2=Poly(denom) return solve_rational_inequalities([[((p1,p2),rel)]]) #If it's none of the above we simply solve for other else: return solve_univariate_inequality(ineq_obj,x,relational=False)
from sympy import Poly, Symbol, solve_poly_inequality x = Symbol('x') ineq_obj = -x**2 + 4 < 0 lhs = ineq_obj.lhs p = Poly(lhs, x) rel = ineq_obj.rel_op solve_poly_inequality(p, rel) from sympy import Symbol, Poly, solve_rational_inequalities x = Symbol('x') ineq_obj = ((x-1)/(x+2)) > 0 lhs = ineq_obj.lhs numer, denom = lhs.as_numer_denom() p1 = Poly(numer) p2 = Poly(denom) rel = ineq_obj.rel_op result= solve_rational_inequalities([[((p1, p2), rel)]]) print(result) from sympy import Symbol, solve, solve_univariate_inequality, sin x = Symbol('x') ineq_obj = sin(x) - 0.6 > 0 result_1= solve_univariate_inequality(ineq_obj, x, relational=False) print(result_1)
def solve_polynomial(expr, x): polynomial = sp.Poly(expr.lhs, x) return sp.solve_poly_inequality(polynomial, expr.rel_op)
def poly_inequality_solve(eqn): lhs = eqn.lhs p = sympy.Poly(lhs, x) relation_operator = eqn.rel_op return sympy.solve_poly_inequality(p, relation_operator)
from sympy import Poly, Symbol, solve_poly_inequality x = Symbol('x') ineq_obj = -x**2 + 4 < 0 lhs = ineq_obj.lhs p = Poly(lhs, x) rel = ineq_obj.rel_op ans_1 = solve_poly_inequality(p, rel) print(ans_1) from sympy import Symbol, Poly, solve_rational_inequalities x = Symbol('x') ineq_obj = ((x - 1) / (x + 2)) > 0 lhs = ineq_obj.lhs numer, denom = lhs.as_numer_denom() p1 = Poly(numer) p2 = Poly(denom) rel = ineq_obj.rel_op ans_2 = solve_rational_inequalities([[((p1, p2), rel)]]) print(ans_2) from sympy import Symbol, solve, solve_univariate_inequality, sin x = Symbol('x') ineq_obj = sin(x) - 0.6 > 0 ans_3 = solve_univariate_inequality(ineq_obj, x, relational=False) print(ans_3) x = Symbol('x') expr = x**2 - 4 > 0