#NEWLINE# def solve_univariate_inequality( expr , gen , relational = True ) : #NEWLINE# #INDENT# "Solves a real univariate inequality.\n\n    Examples\n    ========\n\n    >>> from sympy.solvers.inequalities import solve_univariate_inequality\n    >>> from sympy.core.symbol import Symbol\n    >>> x = Symbol('x')\n\n    >>> solve_univariate_inequality(x**2 >= 4, x)\n    Or(And(-oo < x, x <= -2), And(2 <= x, x < oo))\n\n    >>> solve_univariate_inequality(x**2 >= 4, x, relational=False)\n    (-oo, -2] U [2, oo)\n\n    " #NEWLINE# from sympy.solvers.solvers import solve , denoms #NEWLINE# d = Dummy ( real = True ) #NEWLINE# expr = expr . subs ( gen , d ) #NEWLINE# _gen = gen #NEWLINE# gen = d #NEWLINE# if ( expr is S . true ) : #NEWLINE# #INDENT# rv = S . Reals #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( expr is S . false ) : #NEWLINE# #INDENT# rv = S . EmptySet #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# e = ( expr . lhs - expr . rhs ) #NEWLINE# parts = ( n , d ) = e . as_numer_denom ( ) #NEWLINE# if all ( ( i . is_polynomial ( gen ) for i in parts ) ) : #NEWLINE# #INDENT# solns = solve ( n , gen , check = False ) #NEWLINE# singularities = solve ( d , gen , check = False ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# solns = solve ( e , gen , check = False ) #NEWLINE# singularities = [ ] #NEWLINE# for d in denoms ( e ) : #NEWLINE# #INDENT# singularities . extend ( solve ( d , gen ) ) #NEWLINE# #UNINDENT# #UNINDENT# include_x = expr . func ( 0 , 0 ) #NEWLINE# def valid( x ) : #NEWLINE# #INDENT# v = e . subs ( gen , x ) #NEWLINE# try : #NEWLINE# #INDENT# r = expr . func ( v , 0 ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# r = S . false #NEWLINE# #UNINDENT# if ( r in ( S . true , S . false ) ) : #NEWLINE# #INDENT# return r #NEWLINE# #UNINDENT# if ( v . is_real is False ) : #NEWLINE# #INDENT# return S . false #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# v = v . n ( 2 ) #NEWLINE# if v . is_comparable : #NEWLINE# #INDENT# return expr . func ( v , 0 ) #NEWLINE# #UNINDENT# return S . false #NEWLINE# #UNINDENT# #UNINDENT# start = S . NegativeInfinity #NEWLINE# sol_sets = [ S . EmptySet ] #NEWLINE# try : #NEWLINE# #INDENT# reals = _nsort ( set ( ( solns + singularities ) ) , separated = True ) [ 0 ] #NEWLINE# #UNINDENT# except NotImplementedError : #NEWLINE# #INDENT# raise NotImplementedError ( 'sorting of these roots is not supported' ) #NEWLINE# #UNINDENT# for x in reals : #NEWLINE# #INDENT# end = x #NEWLINE# if ( end in [ S . NegativeInfinity , S . Infinity ] ) : #NEWLINE# #INDENT# if valid ( S ( 0 ) ) : #NEWLINE# #INDENT# sol_sets . append ( Interval ( start , S . Infinity , True , True ) ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# pt = ( ( ( start + end ) / 2 ) if ( start is not S . NegativeInfinity ) else ( ( end / 2 ) if end . is_positive else ( ( 2 * end ) if end . is_negative else ( end - 1 ) ) ) ) #NEWLINE# if valid ( pt ) : #NEWLINE# #INDENT# sol_sets . append ( Interval ( start , end , True , True ) ) #NEWLINE# #UNINDENT# if ( x in singularities ) : #NEWLINE# #INDENT# singularities . remove ( x ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if include_x : #NEWLINE# #INDENT# sol_sets . append ( FiniteSet ( x ) ) #NEWLINE# #UNINDENT# #UNINDENT# start = end #NEWLINE# #UNINDENT# end = S . Infinity #NEWLINE# pt = ( 0 if ( start is S . NegativeInfinity ) else ( ( start / 2 ) if start . is_negative else ( ( 2 * start ) if start . is_positive else ( start + 1 ) ) ) ) #NEWLINE# if valid ( pt ) : #NEWLINE# #INDENT# sol_sets . append ( Interval ( start , end , True , True ) ) #NEWLINE# #UNINDENT# rv = Union ( * sol_sets ) . subs ( gen , _gen ) #NEWLINE# #UNINDENT# #UNINDENT# return ( rv if ( not relational ) else rv . as_relational ( _gen ) )