
    EdP                     R   d Z ddlZddlmZmZmZ ddlmZmZm	Z	 ddl
mZ ddlmZmZmZmZ ddlmZmZmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z, d Z-d Z.d Z/ddZ0d Z1d Z2dej3        dfdZ4d Z5ddZ6d Z7g fdZ8dS ) z<Tools for solving inequalities and systems of inequalities.     N)continuous_domainperiodicityfunction_range)SymbolDummysympify)factor_terms)
RelationalEqGeLt)Interval	FiniteSetUnionIntersection)S)
expand_mul)imAbsAnd)PolyPolynomialErrorparallel_poly_from_expr)_nsort)solvifysolveset)siftiterable)
filldedentc           
      <   t          | t                    st          d          |                                 j        rkt          |                                 d|          }|t          j        u rt          j        gS |t          j	        u rt          j
        gS t          d|z            |                     d          g }}|dk    r/|D ]*\  }}t          ||          }|                    |           +n|dk    rOt          j        }|t          j        dfgz   D ].\  }	}t          ||	d	d	          }|                    |           |	}/nQ|                                 dk    rd}
nd
}
d\  }}|dk    rd}n3|dk    rd
}n*|dk    rd\  }}n|dk    rd\  }}nt          d|z            t          j        d	}}	t%          |          D ]\  }}|dz  r6|
|k    r'|                    dt          ||	| |                     |
 || }}	}
@|
|k    r-|s+|                    dt          ||	d	|                     |d	}}	s|
|k    r&|r$|                    dt          ||                     |
|k    r0|                    dt          t          j        |	d	|                     |S )a  Solve a polynomial inequality with rational coefficients.

    Examples
    ========

    >>> from sympy import solve_poly_inequality, Poly
    >>> from sympy.abc import x

    >>> solve_poly_inequality(Poly(x, x, domain='ZZ'), '==')
    [{0}]

    >>> solve_poly_inequality(Poly(x**2 - 1, x, domain='ZZ'), '!=')
    [Interval.open(-oo, -1), Interval.open(-1, 1), Interval.open(1, oo)]

    >>> solve_poly_inequality(Poly(x**2 - 1, x, domain='ZZ'), '==')
    [{-1}, {1}]

    See Also
    ========
    solve_poly_inequalities
    z8For efficiency reasons, `poly` should be a Poly instancer   %could not determine truth value of %sF)multiple==!=   T)NF><>=)r&   T<=)r'   Tz'%s' is not a valid relation   )
isinstancer   
ValueErroras_expr	is_numberr
   r   trueRealsfalseEmptySetNotImplementedError
real_rootsr   appendNegativeInfinityInfinityLCreversedinsert)polyreltreals	intervalsroot_intervalleftrightsigneq_signequal
right_openmultiplicitys                  :lib/python3.11/site-packages/sympy/solvers/inequalities.pysolve_poly_inequalityrM      sU   , dD!! HFH H 	H||~~ =t||~~q#..; 	=G9!'\ 	=J<%7!;= = = 669E
d{ 1J 	' 	'GD!d++HX&&&&	' 
 -J!!*a 11 	 	HE1eT488HX&&&DD	
 7799q= 	DDD$#: 		CGGCZ 	CGGD[ 	C%NGUUD[ 	C%NGUU;cABBBJz"*5// 	> 	>D,a >7? I$$8D%UJGGI I I ,0%5yZe7? >5 >$$8D%zBBD D D(,d:EEW_ > >$$Qt(<(<===7? 	J8A.tZHHJ J J     c                 (    t          d | D              S )a  Solve polynomial inequalities with rational coefficients.

    Examples
    ========

    >>> from sympy import Poly
    >>> from sympy.solvers.inequalities import solve_poly_inequalities
    >>> from sympy.abc import x
    >>> solve_poly_inequalities(((
    ... Poly(x**2 - 3), ">"), (
    ... Poly(-x**2 + 1), ">")))
    Union(Interval.open(-oo, -sqrt(3)), Interval.open(-1, 1), Interval.open(sqrt(3), oo))
    c                 *    g | ]}t          | D ]}|S  )rM   ).0pss      rL   
<listcomp>z+solve_poly_inequalities.<locals>.<listcomp>~   s+    GGG-BA-FGG1GGGGrN   )r   )polyss    rL   solve_poly_inequalitiesrW   p   s     GGeGGGHHrN   c                 .   t           j        }| D ]}|st          t           j        t           j                  g}|D ]\  \  }}}t          ||z  |          }t          |d          }g }	t          j        ||          D ]=\  }
}|
                    |          }|t           j        ur|		                    |           >|	}g }	|D ]/}|D ]}||z  }|t           j        ur|		                    |           0|	}|s n|D ]}|
                    |          }|S )a3  Solve a system of rational inequalities with rational coefficients.

    Examples
    ========

    >>> from sympy.abc import x
    >>> from sympy import solve_rational_inequalities, Poly

    >>> solve_rational_inequalities([[
    ... ((Poly(-x + 1), Poly(1, x)), '>='),
    ... ((Poly(-x + 1), Poly(1, x)), '<=')]])
    {1}

    >>> solve_rational_inequalities([[
    ... ((Poly(x), Poly(1, x)), '!='),
    ... ((Poly(-x + 1), Poly(1, x)), '>=')]])
    Union(Interval.open(-oo, 0), Interval.Lopen(0, 1))

    See Also
    ========
    solve_poly_inequality
    r$   )r   r4   r   r8   r9   rM   	itertoolsproduct	intersectr7   union)eqsresult_eqsglobal_intervalsnumerdenomr>   numer_intervalsdenom_intervalsrA   numer_intervalglobal_intervalrD   denom_intervals                 rL   solve_rational_inequalitiesrh      s{   . ZF $, $, 	$Q%7DDE#' 	 	NUEC3E%KEEO3E4@@OI3<3D#%547 47 / //)33ODD1:- /$$X...(I#3 6 6&5 6 6N#~5OO"!*4 6$$_555(#  ) 	, 	,H\\(++FF	, MrN   Tc                    d}g }| rt           j        nt           j        }| D ]}g }|D ]}t          |t                    r|\  }}	n"|j        r|j        |j        z
  |j        }	}n|d}	}|t           j	        u rt           j
        t           j        d}	}}
nR|t           j        u rt           j        t           j        d}	}}
n)|                                                                \  }
}	 t          |
|f          \  \  }
}}n*# t           $ r t!          t#          d                    w xY w|j        j        s*|
                                |                                d}}}
|j                                        }|j        s4|j        s-|
|z  }t1          |d|	          }|t3          |d          z  }|                    |
|f|	f           |r|                    |           |r3|t7          |          z  }t7          fd|D             g          }||z  }|s|r|                                }|r|                              }|S )a8  Reduce a system of rational inequalities with rational coefficients.

    Examples
    ========

    >>> from sympy import Symbol
    >>> from sympy.solvers.inequalities import reduce_rational_inequalities

    >>> x = Symbol('x', real=True)

    >>> reduce_rational_inequalities([[x**2 <= 0]], x)
    Eq(x, 0)

    >>> reduce_rational_inequalities([[x + 2 > 0]], x)
    -2 < x
    >>> reduce_rational_inequalities([[(x + 2, ">")]], x)
    -2 < x
    >>> reduce_rational_inequalities([[x + 2]], x)
    Eq(x, -2)

    This function find the non-infinite solution set so if the unknown symbol
    is declared as extended real rather than real then the result may include
    finiteness conditions:

    >>> y = Symbol('y', extended_real=True)
    >>> reduce_rational_inequalities([[y + 2 > 0]], y)
    (-2 < y) & (y < oo)
    Tr$   z
                    only polynomials and rational functions are
                    supported in this context.
                    Fr   )
relationalc                 f    g | ]-}|D ](\  \  }}}|                               ||j        fd f).S )r$   )hasone)rR   indrC   gens        rL   rU   z0reduce_rational_inequalities.<locals>.<listcomp>  s^     0= 0= 0=A0= 0=(fq!as0=!QUT0B 0= 0= 0= 0=rN   )r   r2   r4   r-   tupleis_Relationallhsrhsrel_opr1   ZeroOner3   togetheras_numer_denomr   r   r    domainis_Exactto_exact	get_exactis_ZZis_QQr
   solve_univariate_inequalityr7   rh   evalfas_relational)exprsrq   rj   exactr]   solution_exprsr_   exprr>   ra   rb   optr{   excludes    `             rL   reduce_rational_inequalitiesr      s   : E
C/qwwQZH ) ) #	3 #	3D$&& + 	cc% + $48 3T[#DD $d#Dqv~ @$%FAE4cu @$%E15$cu#}}==??u&=ENC') ')#"   %j 2 ' '    :& P&+nn&6&68H8H%euZ))++FL 3FL 3U{!$3//7ceTTTTeU^S12222 	JJt
 /444- 0= 0= 0= 0=0= 0= 0= /> ? ?G $X $>>## /))#..Os   %C>>'D%c                 Z   |j         du rt          t          d                    fdddd}g } |           D ]^\  } }||                                vrt	          | d|          } nt	          |  d||                   } |                    | g|z              _t          ||          S )a  Reduce an inequality with nested absolute values.

    Examples
    ========

    >>> from sympy import reduce_abs_inequality, Abs, Symbol
    >>> x = Symbol('x', real=True)

    >>> reduce_abs_inequality(Abs(x - 5) - 3, '<', x)
    (2 < x) & (x < 8)

    >>> reduce_abs_inequality(Abs(x + 2)*3 - 13, '<', x)
    (-19/3 < x) & (x < 7/3)

    See Also
    ========

    reduce_abs_inequalities
    Fzs
            Cannot solve inequalities with absolute values containing
            non-real variables.
            c           	      r   g }| j         s| j        rC| j        | j        D ]3} |          }|s|}fdt	          j        ||          D             }4n| j        rM| j        j        st          d          |
                    fd | j                  D                        nt          | t                    rr | j        d                   }|D ]X\  } }|                    | |t          | d          gz   f           |                    |  |t!          | d          gz   f           Yn| g fg}|S )Nc                 D    g | ]\  \  }}\  }} ||          ||z   fS rQ   rQ   )rR   r   conds_expr_condsops        rL   rU   zBreduce_abs_inequality.<locals>._bottom_up_scan.<locals>.<listcomp>C  sH     > > >Ca=D%RaSXZ`bbuoouv~> > > >rN   z'Only Integer Powers are allowed on Abs.c              3   ,   K   | ]\  }}|z  |fV  d S NrQ   )rR   r   r   ro   s      rL   	<genexpr>zAreduce_abs_inequality.<locals>._bottom_up_scan.<locals>.<genexpr>J  s0      XXkdE$'5)XXXXXXrN   r   )is_Addis_MulfuncargsrY   rZ   is_Powexp
is_Integerr.   extendbaser-   r   r7   r   r   )r   r   argr   r   ro   r   _bottom_up_scans        @@rL   r   z.reduce_abs_inequality.<locals>._bottom_up_scan7  s   ; 	!$+ 	!By > >(-- >"EE> > > >%-eV<<> > >EE> [ 	!A< L !JKKKLLXXXX__TY=W=WXXXXXXXc"" 	!$_TYq\22F% = =etUbqkk]%:;<<<teUbqkk]%:;<<<<= BZLErN   r(   r*   r)   r+   r   )is_extended_real	TypeErrorr    keysr
   r7   r   )r   r>   rq   mappinginequalitiesr   r   s         @rL   reduce_abs_inequalityr     s    ( u$  %     

    > t$$GL&t,, , ,egllnn$ 	6tQ,,DDteQ55DTFUN++++'c:::rN   c                 .    t          fd| D              S )a  Reduce a system of inequalities with nested absolute values.

    Examples
    ========

    >>> from sympy import reduce_abs_inequalities, Abs, Symbol
    >>> x = Symbol('x', extended_real=True)

    >>> reduce_abs_inequalities([(Abs(3*x - 5) - 7, '<'),
    ... (Abs(x + 25) - 13, '>')], x)
    (-2/3 < x) & (x < 4) & (((-oo < x) & (x < -38)) | ((-12 < x) & (x < oo)))

    >>> reduce_abs_inequalities([(Abs(x - 4) + Abs(3*x - 5) - 7, '<')], x)
    (1/2 < x) & (x < 4)

    See Also
    ========

    reduce_abs_inequality
    c                 8    g | ]\  }}t          ||          S rQ   )r   )rR   r   r>   rq   s      rL   rU   z+reduce_abs_inequalities.<locals>.<listcomp>y  s9     ! ! !D# (c377 ! ! !rN   r   )r   rq   s    `rL   reduce_abs_inequalitiesr   d  s8    *  ! ! ! !! ! ! " "rN   Fc                 N   ( ddl m} |                    t          j                  du rt          t          d                    |t          j        ur?t           d|                              |          }|r|	                              }|S 	 }|}j
        du r%t          j        }|s|n|	                    |          S j
        Tt          dd	          	                      |i           n*# t          $ r t          t          d
                    w xY wd} t          j        u r|}n؉ t          j        u rt          j        }n j         j        z
  }	t'          |	          }
|
t          j        k    rRt+          |	          }	                     |	d          }|t          j        u r|}n|t          j        u rt          j        }n|
t/          |	|          } j        }|dv rF                     |j        d          r|}nq                     |j        d          st          j        }nI|dv rE                     |j        d          r|}n'                     |j        d          st          j        }|j        |j        }}||z
  t          j        u r't9          d|
dd                              |          }|}|E|	                                \  }}	 |j        vrtA          |	j                  dk    rtB          tE          |	|          }|tB          nU# tB          t
          f$ rA t          t          d #                    tI          d                    z                      w xY wt+          |	          (( fd}g } |           D ]&}|%                    tE          ||                     '|stM          (|          }d j        v o
 j        dk    }	 tO          |j(        tS          |j        |j                  z
            }tS          ||z   tU          |          z                        t9          |j        |j        |j        |v|j        |v                    }tW          d |D                       rtY          |d          d         }nnt[          |d           }|d         rt
          	 |d         }tA          |          dk    rtU          t]          |                    }n# t          $ r t
          w xY wn# t
          $ r t          d          w xY wt          j        }t_          (          t          j        k    rd}tS                      }	 ta          t_          (          |          }tc          |t8                    s.|D ]*}||vr$ ||          r|j
        r|tS          |          z  }+n|j        |j        } }tY          |tS          |           z             D ]} ||          }!|| k    r ||          }"te          ||          }#|#|vrx|#j
        rq ||#          rf|!r|"r|t9          ||          z  }nN|!r|t9          j3        ||          z  }n3|"r|t9          j4        ||          z  }n|t9          j5        ||          z  }|}|D ]}$|tS          |$          z  }n# t          $ r t          j        }d}Y nw xY w|t          j        u r7tC          t          d #                    |          d|d                    |                    |          }t          j        g}%|j        }||v r4 ||          r)|j6        r"|%7                    tS          |                     |D ]}&|&}  |te          ||                     r%|%7                    t9          || dd                     |&|v r|8                    |&           nK|&|v r!|8                    |&            ||&          }'n|}'|'r"|%7                    tS          |&                     | }|j        } | |v r4 ||           r)| j6        r"|%7                    tS          |                       |te          ||                     r(|%7                    t9          j5        ||                      t_          (          t          j        k    r|r|                    |          }n,ts          tu          |% ||          #                    |          }|s|n|	                    |          S )aT  Solves a real univariate inequality.

    Parameters
    ==========

    expr : Relational
        The target inequality
    gen : Symbol
        The variable for which the inequality is solved
    relational : bool
        A Relational type output is expected or not
    domain : Set
        The domain over which the equation is solved
    continuous: bool
        True if expr is known to be continuous over the given domain
        (and so continuous_domain() does not need to be called on it)

    Raises
    ======

    NotImplementedError
        The solution of the inequality cannot be determined due to limitation
        in :func:`sympy.solvers.solveset.solvify`.

    Notes
    =====

    Currently, we cannot solve all the inequalities due to limitations in
    :func:`sympy.solvers.solveset.solvify`. Also, the solution returned for trigonometric inequalities
    are restricted in its periodic interval.

    See Also
    ========

    sympy.solvers.solveset.solvify: solver returning solveset solutions with solve's output API

    Examples
    ========

    >>> from sympy import solve_univariate_inequality, Symbol, sin, Interval, S
    >>> x = Symbol('x')

    >>> solve_univariate_inequality(x**2 >= 4, x)
    ((2 <= x) & (x < oo)) | ((-oo < x) & (x <= -2))

    >>> solve_univariate_inequality(x**2 >= 4, x, relational=False)
    Union(Interval(-oo, -2), Interval(2, oo))

    >>> domain = Interval(0, S.Infinity)
    >>> solve_univariate_inequality(x**2 >= 4, x, False, domain)
    Interval(2, oo)

    >>> solve_univariate_inequality(sin(x) > 0, x, relational=False)
    Interval.open(0, pi)

    r   denomsFz|
        Inequalities in the complex domain are
        not supported. Try the real domain by
        setting domain=S.Reals)rj   
continuousNrq   Textended_realz
                When gen is real, the relational has a complex part
                which leads to an invalid comparison like I < 0.
                r   )r(   r*   r&   z
                    The inequality, %s, cannot be solved using
                    solve_univariate_inequality.
                    xc                                         t          |                     }	                     |d          }n# t          $ r t          j        }Y nw xY w|t          j        t          j        fv r|S |j        du rt          j        S |                    d          }|j	        r                    |d          S t          d|z            )Nr   Fr,   z!relationship did not evaluate: %s)subsr   r   r   r   r3   r1   r   ro   is_comparabler5   )r   vr
expanded_er   rq   s      rL   validz*solve_univariate_inequality.<locals>.valid  s     OOCA77 		!QAA       AAA )) H%. A7NAA /#yyA.-;a?A A As   = AA=r%   c              3   $   K   | ]}|j         V  d S r   )r0   )rR   r   s     rL   r   z.solve_univariate_inequality.<locals>.<genexpr>>  s$      <<qq{<<<<<<rN   )	separatedc                     | j         S r   r   )r   s    rL   <lambda>z-solve_univariate_inequality.<locals>.<lambda>A  s	    Q=O rN   z'sorting of these roots is not supportedz
                        zZ contains imaginary parts which cannot be
                        made 0 for any value of zm satisfying the
                        inequality, leading to relations like I < 0.
                        );sympy.solvers.solversr   	is_subsetr   r2   r5   r    r   intersectionr   r   r4   r   xreplacer   r1   r3   rt   ru   r   rw   r   r   r   rv   supinfr9   r   r[   rz   free_symbolslenr.   r   r   r   r   r   setboundaryr   listallr   r   sortedr   r   r-   _ptRopenLopenopen	is_finiter7   remover   r   ))r   rq   rj   r{   r   r   rv_gen_domaineperiodconstfranger>   r   r   ro   rp   solnsr   singularities	include_xdiscontinuitiescritical_pointsr@   sifted	make_realcheckim_solazstartendvalid_startvalid_zptrT   sol_setsr   _validr   s)   ``                                      @rL   r   r   }  s
   r -,,,,,  E) !* ." ## ## $ $ 	$ 
qw	 (ce
< < <<HL<P<P 	 	'!!#&&B	
 DG
u$ Z#?rr)9)9$)?)??		 E...	==$--DD 	 	 	J (     	 
Bqv~ EL	 BLZ HtxQ$$QV 	!1AIIaOOE  !'!  Z 	!#AsF33F+Ck! 
$99VZ++ $BB6:q11 $B# $99VZ++ $BB6:q11 $Bz6:CSyAJ& !!!VUD99CCGLL  `	L##%%DAq8an, %Q^1D1Dq1H %$$  3// %$$%  34 8 8 8 ** 6 ))C5566 +7 +7 8 8 88 $AJA A A A A A A6 MVD#&& > >$$WQV%<%<==== D*:sFCCt{*Bt{d/BIU"%fofj&*55'6 #7 #7 #,em.Cd#G% G% /% #''3|VZJf,fj.FH H(I (I   <<O<<<<< 2"?dCCCAFEE!/3O3OPPFd| 2 212 &tu::> 8$($7$7E$ 2 2 2112& U U U)*STTTU
 I*~~' '8"" Jf==A%a22 3!" 7 7A 5 7%%(( 7qGY 7 &)A,, 67 &'UAEs!')C..(H!I!I & &A*/%,,K$| J*/%((%(]]#%]#: !Jr?R !JW\W\]_W`W` !J'2 %Jw %J(.(5!2D2D(D)4 %J(.(.2J2J(J)0 %J(.(.2J2J(J(.(-q2I2I(I$%EE!. 3 3A"ill2FF! " " "WF!EEE" QZ' >$ZZ !%		#t 4 4 4 4ddd	1< &= &= > > > &//77	
|HJE 255<< 2EO 2	% 0 0111  5UC)) FOOHUCt$D$DEEE% 	6!((++++O+ +'..q111!&q!* 6 	!555*Cf} 0s 0 0	#///uS__%% ;eS 9 9:::*~~' LE L**733!H%	7< <<@DdOO   ;22R%5%5d%;%;;sR    C8 8'D!AL$ $AM6,CT 7S< ;T <TT T,*EZ= =[[c                    | j         s|j         s	| |z   dz  }n| j         r|j         rt          j        }n| j         r| j        |j         r|j        t	          d          |j         r|j        s| j         r| j        r|| }} |j         r*| j        r| dz  }nM| j        r| t          j        z  }n6| dz   }n0| j         r)|j        r|t          j        z  }n|j        r|dz  }n|dz
  }|S )z$Return a point between start and endr,   Nz,cannot proceed with unsigned infinite valuesr&   )is_infiniter   rw   is_extended_positiver.   is_extended_negativeHalf)r   r   r   s      rL   r   r     s>    S_ ck1_		 s V 	M%"< 	M	M$'$<	MKLLLO 	$ 8 	$!	$&+&@	$e3E ? 	) 1W+ 16\QY 	' Z) U1WIrN   c                 t   ddl m} || j        vr| S | j        |k    r| j        } | j        |k    r|| j        j        vr| S d }d}t          j        }| j        | j        z
  }	 t          ||          }|	                                dk    r)| 
                    |                                d          }n!|s|	                                dk    rt          nd# t          t          f$ rO |s:	 t          | gg|          }n # t          $ r t          | |          }Y nw xY w || ||          }	|	t          j        u r3 ||||          t          j        u r|                    ||k     d          } || ||           }
|
t          j        u rP ||||           t          j        u r6|                    | |k     d          }|                    || k    d          }|t          j        u r=|	t          j        u r||k    n||k     }|
t          j        urt'          | |k     |          }nt          |          }Y nw xY wg }||                                }d}|                    |d          \  }}||z  }||z  }t+          |          }|                    |d          \  }}|j        dk    s |j        |j        cxk    r	 n n| j        d	vr|}t          j        }||z  }|j        r| 
                    ||          }n| j        
                    ||          } || j                   || j                  z  } ||          }||z
  D ]v}t7          t9          |d          ||
          }t;          |t8                    r?|j        |k    r4 ||||j                  t          j        u r|                    |            w| |fD ]W} ||||          t          j        u r< || ||          t          j        ur#|                    ||u r||k     n||k                X|                    |           t'          | S )a  Return the inequality with s isolated on the left, if possible.
    If the relationship is non-linear, a solution involving And or Or
    may be returned. False or True are returned if the relationship
    is never True or always True, respectively.

    If `linear` is True (default is False) an `s`-dependent expression
    will be isolated on the left, if possible
    but it will not be solved for `s` unless the expression is linear
    in `s`. Furthermore, only "safe" operations which do not change the
    sense of the relationship are applied: no division by an unsigned
    value is attempted unless the relationship involves Eq or Ne and
    no division by a value not known to be nonzero is ever attempted.

    Examples
    ========

    >>> from sympy import Eq, Symbol
    >>> from sympy.solvers.inequalities import _solve_inequality as f
    >>> from sympy.abc import x, y

    For linear expressions, the symbol can be isolated:

    >>> f(x - 2 < 0, x)
    x < 2
    >>> f(-x - 6 < x, x)
    x > -3

    Sometimes nonlinear relationships will be False

    >>> f(x**2 + 4 < 0, x)
    False

    Or they may involve more than one region of values:

    >>> f(x**2 - 4 < 0, x)
    (-2 < x) & (x < 2)

    To restrict the solution to a relational, set linear=True
    and only the x-dependent portion will be isolated on the left:

    >>> f(x**2 - 4 < 0, x, linear=True)
    x**2 < 4

    Division of only nonzero quantities is allowed, so x cannot
    be isolated by dividing by y:

    >>> y.is_nonzero is None  # it is unknown whether it is 0 or not
    True
    >>> f(x*y < 1, x)
    x*y < 1

    And while an equality (or inequality) still holds after dividing by a
    non-zero quantity

    >>> nz = Symbol('nz', nonzero=True)
    >>> f(Eq(x*nz, 1), x)
    Eq(x, 1/nz)

    the sign must be known for other inequalities involving > or <:

    >>> f(x*nz <= 1, x)
    nz*x <= 1
    >>> p = Symbol('p', positive=True)
    >>> f(x*p <= 1, x)
    x <= 1/p

    When there are denominators in the original expression that
    are removed by expansion, conditions for them will be returned
    as part of the result:

    >>> f(x < x*(2/x - 1), x)
    (x < 1) & Ne(x, 0)
    r   r   c                     	 |                      ||          }|t          j        u r|S |dvrd S |S # t          $ r t          j        cY S w xY w)NTF)r   r   NaNr   )ierT   rn   r   s       rL   classifyz#_solve_inequality.<locals>.classify  sh    	1AAEz -' H 	 	 	5LLL	s   %0 0 0 A	A	Nr&   T)as_AddF)r%   r$   )linear)r   r   r   ru   r;   rt   r   r9   r   degreer   r/   r5   r   r   r   r1   r3   r   r   as_independentr	   is_zerois_negativeis_positiverv   rx   _solve_inequalityr   r-   r7   )r   rT   r   r   r   r   oor   rS   okoooknoor   r   ru   baxefr   beginning_denomscurrent_denomsrp   crn   s                          rL   r   r     s   T -,,,,, 		v{ [	v{ q 33 	   
B	
B6BF?DqMM88::? 	&a((BB 	&AHHJJN 	&%%01    	81B4&!<<" 8 8 80Q778 8B2&&Dqv~ +((2q""5"5"@ +WWQVT**HRRC((E ,HRRC((AG3,WWbS1Wd++WWQ"Wd++QV| *"&!&.>a2ggq2v& *bS1Wb))BT

A+. E	 %:IIKK
   4 002	Qq!__  5 111I 		 	 	 	!%	 	 	 	 	-	 AAq= 	*CBB!!!S))B "6"&>>FF26NN:!N2 	% 	%A!"Q((Af===A!R   %QUaZ %8B15))QV3 %LL!$$$#r 	: 	:AQ""af, :HRA&&af4:a2g8QUU1q5999	LL;s8    A2C H5*C=<H5=DH5DDH54H5c           
         i i }}g }| D ]}|j         |j        }}|                    t                    }t	          |          dk    r|                                n|j        |z  }	t	          |	          dk    rG|	                                |                    t          t          |d|                               t          t          d                    |                              r-|                    g                               ||f           |                    fd          }
|
rFt          d |
D                       r-|                    g                               ||f           u|                    t          t          |d|                               d |                                D             }d |                                D             }t#          ||z   |z    S )Nr&   r   zZ
                    inequality has more than one symbol of interest.
                    c                 d    |                                o| j        p| j        o| j        j         S r   )rl   is_Functionr   r   r   )urq   s    rL   r   z&_reduce_inequalities.<locals>.<lambda>  s4    c

 DB!B!%2B.B rN   c              3   @   K   | ]}t          |t                    V  d S r   )r-   r   rR   rn   s     rL   r   z'_reduce_inequalities.<locals>.<genexpr>  s,      !I!I*Q"4"4!I!I!I!I!I!IrN   c                 6    g | ]\  }}t          |g|          S rQ   )r   rR   rq   r   s      rL   rU   z(_reduce_inequalities.<locals>.<listcomp>  s)    ccc:30%#>>cccrN   c                 4    g | ]\  }}t          ||          S rQ   )r   r  s      rL   rU   z(_reduce_inequalities.<locals>.<listcomp>  s'    ZZZ:3*5#66ZZZrN   )rt   rv   atomsr   r   popr   r7   r   r
   r5   r    is_polynomial
setdefaultfindr   itemsr   )r   symbols	poly_partabs_partother
inequalityr   r>   genscommon
componentspoly_reducedabs_reducedrq   s                @rL   _reduce_inequalitiesr   r  s&    bxIE" O O
NJ$5c
 zz&!!t99> 	((**CC&0F6{{a jjll.z$3/G/GMMNNN)* 6 + +    c"" 		O  b))00$==== $D $D $D $D E EJ  Oc!I!Ij!I!I!III O##C,,33T3K@@@@.z$3/G/GMMNNNNccQZQ`Q`QbQbcccLZZIYIYZZZK+e355rN   c                    t          |           s| g} d | D             }  t                      j        d | D              }t          |          s|g}t          |          p||z  }t          d |D                       rt	          t          d                    d |D             fd| D             } fd|D             }g }| D ]}t          |t                    rH|                    |j	        
                                |j        
                                z
  d          }n|d	vrt          |d          }|d
k    rz|dk    rt          j        c S |j	        j        rt!          d|z            |                    |           |} ~t%          | |          }|                    d                                 D                       S )aE  Reduce a system of inequalities with rational coefficients.

    Examples
    ========

    >>> from sympy.abc import x, y
    >>> from sympy import reduce_inequalities

    >>> reduce_inequalities(0 <= x + 3, [])
    (-3 <= x) & (x < oo)

    >>> reduce_inequalities(0 <= x + y*2 - 1, [x])
    (x < oo) & (x >= 1 - 2*y)
    c                 ,    g | ]}t          |          S rQ   )r   r  s     rL   rU   z'reduce_inequalities.<locals>.<listcomp>  s    5551GAJJ555rN   c                     g | ]	}|j         
S rQ   )r   r  s     rL   rU   z'reduce_inequalities.<locals>.<listcomp>  s    >>>A>>>rN   c              3   (   K   | ]}|j         d u V  dS )FNr   r  s     rL   r   z&reduce_inequalities.<locals>.<genexpr>  s*      
8
811&
8
8
8
8
8
8rN   zP
            inequalities cannot contain symbols that are not real.
            c                 J    i | ] }|j         	|t          |j        d          !S )NTr   )r   r   namer  s     rL   
<dictcomp>z'reduce_inequalities.<locals>.<dictcomp>  s@     5 5 5+5aqvT222 5 5 5rN   c                 :    g | ]}|                               S rQ   r   rR   rn   recasts     rL   rU   z'reduce_inequalities.<locals>.<listcomp>  s%    ===1AJJv&&===rN   c                 :    h | ]}|                               S rQ   r)  r*  s     rL   	<setcomp>z&reduce_inequalities.<locals>.<setcomp>  s%    333aqzz&!!333rN   r   r   TFr"   c                     i | ]\  }}||	S rQ   rQ   )rR   kr   s      rL   r'  z'reduce_inequalities.<locals>.<dictcomp>  s    888A1888rN   )r   r   r\   anyr   r    r-   r
   r   rt   r/   ru   r   r   r3   r0   r5   r7   r   r   r  )r   r  r  keeprn   r   r+  s         @rL   reduce_inequalitiesr2    s    L!! &$~55555L355;>>>>>?DG )7||#tt+G

8
8
8
8
888 
 $     	
5 55 5 5F=======L33337333G D  a$$ 	qu}}8!<<AAm# 	1aA9 	%Z 	7NNN5? 	=%7!;= = =AL 
lG	4	4B ;;88888999rN   )T)F)9__doc__rY   sympy.calculus.utilr   r   r   
sympy.corer   r   r   sympy.core.exprtoolsr	   sympy.core.relationalr
   r   r   r   sympy.sets.setsr   r   r   r   sympy.core.singletonr   sympy.core.functionr   $sympy.functions.elementary.complexesr   r   sympy.logicr   sympy.polysr   r   r   sympy.polys.polyutilsr   sympy.solvers.solvesetr   r   sympy.utilities.iterablesr   r   sympy.utilities.miscr    rM   rW   rh   r   r   r   r2   r   r   r   r   r2  rQ   rN   rL   <module>rB     sr   B B              - - - - - - - - - - - - - - - - 8 8 8 8 8 8 8 8 8 8 8 8 D D D D D D D D D D D D " " " " " " * * * * * * 8 8 8 8 8 8 8 8       F F F F F F F F F F ( ( ( ( ( ( 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 + + + + + +X X XvI I I"? ? ?DW W W WtD; D; D;N" " "2 7;17W\ d< d< d< d<N	  Bj j j jZ*6 *6 *6Z /1 9: 9: 9: 9: 9: 9:rN   