o
    oh                     @   sb  d Z ddlZddlmZmZmZ ddl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 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 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d Z-dd Z.dd Z/d*ddZ0dd Z1dd Z2dej3dfd d!Z4d"d# Z5d+d$d%Z6d&d' Z7g fd(d)Z8dS ),z<Tools for solving inequalities and systems of inequalities.     N)continuous_domainperiodicityfunction_rangesympify)factor_terms)
RelationalLtGeEq)SymbolDummy)Interval	FiniteSetUnionIntersection)S)
expand_mul)AbsAnd)PolyPolynomialErrorparallel_poly_from_expr)_nsort)solvifysolveset)siftiterable)
filldedentc              	   C   s  t | ts	td|  jr.t|  d|}|tju rtjgS |tj	u r(tj
gS td| | jddg }}|dkrN|D ]\}}t||}|| q=|S |dkrrtj}|tjdfg D ]\}	}t||	d	d	}|| |	}q]|S |  dkr{d}
nd
}
d\}}|dkrd}n|dkrd
}n|dkrd\}}n|dkrd\}}ntd| tjd	}	}t|D ]I\}}|d r|
|kr|dt||	| | |
 || }
}	}q|
|kr|s|dt||	d	| |d	}	}q|
|kr|r|dt|| q|
|kr|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multiplicity rJ   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/solvers/inequalities.pysolve_poly_inequality   sx   




0
)


rL   c                 C   s   t d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                 S   s   g | ]}t | D ]}|qqS rJ   )rL   ).0psrJ   rJ   rK   
<listcomp>   s    z+solve_poly_inequalities.<locals>.<listcomp>)r   )polysrJ   rJ   rK   solve_poly_inequalitiesq   s   rR   c                 C   s   t j}| D ]j}|s
qtt jt jg}|D ]P\\}}}t|| |}t|d}g }	t||D ]\}
}|
|}|t jurA|		| q.|	}g }	|D ]}|D ]}||8 }qL|t jur]|		| qH|	}|sd nq|D ]}|
|}qgq|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   r2   r   r6   r7   rL   	itertoolsproduct	intersectr5   union)eqsresult_eqsglobal_intervalsnumerdenomr<   numer_intervalsdenom_intervalsr?   numer_intervalglobal_intervalrB   denom_intervalrJ   rJ   rK   solve_rational_inequalities   s@   






rb   Tc              
      s  d}g }t j}| D ]}|sq	g }t j}|D ]}	t|	tr!|	\}	}
n|	jr/|	j|	j |	j}	}
n|	d}	}
|	t j	u rDt j
t jd}}}
n|	t ju rTt jt jd}}}
n|	  \}}zt||f \\}}}W n tyv   ttdw |jjs| | d}}}|j }|js|js|| }	t|	d|
}	|t|	 ddM }q|||f|
f q|r|t|gM }t fdd|D g}||8 }||O }q	|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                    s6   g | ]}|D ]\\}}}|  r||jfd fqqS )r"   )hasone)rM   indrA   genrJ   rK   rP     s
    z0reduce_rational_inequalities.<locals>.<listcomp>)r   r2   r0   r+   tupleis_Relationallhsrhsrel_opr/   ZeroOner1   togetheras_numer_denomr   r   r   domainis_Exactto_exact	get_exactis_ZZis_QQr   solve_univariate_inequalityr5   rb   evalfas_relational)exprsrj   rc   exactrW   solution_exprsrY   _solexprr<   r[   r\   optrt   excluderJ   ri   rK   reduce_rational_inequalities   s\   








r   c                    s   |j du rttd fdd ddd}g } | D ]"\} }|| vr-t| d|} n	t|  d|| } || g|  q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                    s   g }| j s| jr)| j| jD ]}|}|s|}qfddt||D }q|S | jrG| j  js6t	d|
 fdd| jD  |S t| trw| jd }|D ]\} }|| |t| dg f ||  |t| dg f qU|S | g fg}|S )Nc                    s*   g | ]\\}}\}} |||| fqS rJ   rJ   )rM   r   conds_expr_conds)oprJ   rK   rP   E  s   * zBreduce_abs_inequality.<locals>._bottom_up_scan.<locals>.<listcomp>z'Only Integer Powers are allowed on Abs.c                 3   s     | ]\}}|  |fV  qd S NrJ   )rM   r   r   )rg   rJ   rK   	<genexpr>L  s    zAreduce_abs_inequality.<locals>._bottom_up_scan.<locals>.<genexpr>r   )is_Addis_MulfuncargsrS   rT   is_Powexp
is_Integerr,   extendbaser+   r   r5   r
   r	   )r   r}   argr   r   _bottom_up_scan)rg   r   rK   r   9  s2   





z.reduce_abs_inequality.<locals>._bottom_up_scanr&   r(   r'   r)   r   )is_extended_real	TypeErrorr   keysr   r5   r   )r   r<   rj   mappinginequalitiesr   rJ   r   rK   reduce_abs_inequality  s   


r   c                    s   t  fd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                    s   g | ]
\}}t || qS rJ   )r   )rM   r   r<   ri   rJ   rK   rP   {  s    z+reduce_abs_inequalities.<locals>.<listcomp>r   )r}   rj   rJ   ri   rK   reduce_abs_inequalitiesf  s   r   Fc           )         s  ddl m} |tjdu rttd|tjur-td|d|}|r+|	}|S 	 }|}j
du rCtj}|s>|S |	|S j
du retddd	z	|iW n tyd   ttd
w d}tju rp|}ntju rztj}njj }	t|	}
|
tjkrt|	}	|	d}|tju r|}na|tju rtj}nX|
durt|	|}j}|dv rˈ|jdr|}n#|jdstj}n|dv r|jdr|}n
|jdstj}|j|j}}|| tju rtd|
dd|}|}|du r]|	 \}}z|jvrt |	jdkrt!t"|	|}|du r&t!W n t!tfy?   ttd#t$d w t|	  fdd}g }|D ]}|%t"|| qS|sit& |}djv osjdk}zet'|j(t)|j|j }t)|| t*|  t|j|j|j|v|j|v}t+dd |D rt,|ddd }n*t-|dd }|d rtz|d }t |dkrt.|}W n
 ty   tw W n ty   tdw tj} /tj0 }tjkrd}t) }zt1||}t2|ts"|D ]}||vr||r|j
r|t)|7 }q	no|j|j} }!t,|t)|! D ]S}|| }"| |!kr||}#t3| |}$|$|vr|$j
r||$r|"ra|#ra|t| |7 }n |"rm|t4| |7 }n|#ry|t5| |7 }n|t6| |7 }|} q1|D ]	}%|t)|%8 }qW n ty   tj}d}Y nw |tju rt!td#||f ||}tjg}&|j} | |v r|| r| j7r|&8t)|  |D ]?}'|'}!|t3| |!r|&8t| |!dd |'|v r|9|' n|'|v r	|9|' ||'}(n|}(|(r|&8t)|' |!} q|j}!|!|v r1||!r1|!j7r1|&8t)|! |t3| |!rB|&8t6| |! |tjkrQ|rQ||}nt:t;|& ||#|}|sb|S |	|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)rc   
continuousNrj   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                    s     t| }z|d}W n ty   tj}Y nw |tjtjfv r'|S |jdu r/tjS |d}|j	r=|dS t
d| )Nr   Fr*   z!relationship did not evaluate: %s)subsr   r   r   r   r1   r/   r   rg   is_comparabler3   )r   vr
expanded_er   rj   rJ   rK   valid  s    



z*solve_univariate_inequality.<locals>.valid=r#   c                 s   s    | ]}|j V  qd S r   )r.   )rM   r   rJ   rJ   rK   r   @  s    z.solve_univariate_inequality.<locals>.<genexpr>)	separatedc                 S   s   | j S r   r   )r   rJ   rJ   rK   <lambda>C  s    z-solve_univariate_inequality.<locals>.<lambda>z'sorting of these roots is not supportedz
                        %s contains imaginary parts which cannot be
                        made 0 for any value of %s satisfying the
                        inequality, leading to relations like I < 0.
                        )<sympy.solvers.solversr   	is_subsetr   r0   r3   r   rz   intersectionr|   r   r2   r   xreplacer   r/   r1   rm   rn   r   rp   r   r   r   ro   supinfr7   r   rU   rs   free_symbolslenr,   r   r   r   r   r   setboundaryr   listallr   r   sortedcoeffImaginaryUnitr   r+   _ptRopenLopenopen	is_finiter5   remover   r   ))r   rj   rc   rt   r   r   rv_gen_domaineperiodconstfranger<   r   r   rg   rh   solnsr   singularities	include_xdiscontinuitiescritical_pointsr>   sifted	make_realcoeffIcheckim_solazstartendvalid_startvalid_zptrO   sol_setsr   _validrJ   r   rK   rz     sf  9























rz   c                 C   s   | j s|j s| | d }|S | j r|j rtj}|S | j r!| jdu s)|j r-|jdu r-td|j r3|js9| j r>| jr>|| } }|j rZ| jrJ| d }|S | jrT| tj }|S | d }|S | j rt|jrg|tj }|S |jrp|d }|S |d }|S )z$Return a point between start and endr*   Nz,cannot proceed with unsigned infinite valuesr$   )is_infiniter   rp   is_extended_positiver,   is_extended_negativeHalf)r   r   r   rJ   rJ   rK   r     sF   




r   c                 C   sh  ddl m} || jvr| S | j|kr| j} | j|kr"|| jjvr"| S dd }d}tj}| j| j }z t||}|	 dkrF| 
| d}n
|sP|	 dkrPtW n ttfy   |sz	t| gg|}W n tys   t| |}Y nw || ||}	|	tju r||||tju r|||k d}|| || }
|
tju r|||| tju r|| |k d}||| kd}|tju r|	tju r||kn||k }|
tjurt| |k |}nt|}Y nw g }|du r| }d}|j|dd\}}||8 }||8 }t|}|j|d	d\}}|jd	ks&|j|j  krdu r+n n| jd
vr+|}tj}|| }|jr:| 
||}n| j
||}|| j|| jB }||}|| D ]*}tt|d||d}t|tr||j|kr|||||jtju r|||  qS| |fD ]'}||||tju r|| ||tjur|||u r||k n||k  q|| 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                 S   sJ   z|  ||}|tju r|W S |dvrW d S |W S  ty$   tj Y S w )NTF)r   r   NaNr   )ierO   rf   r   rJ   rJ   rK   classify  s   

z#_solve_inequality.<locals>.classifyNr$   T)as_AddF)r#   r"   )linear)r   r   r   rn   r9   rm   r   r7   r   degreer   r-   r3   r   r   rz   r/   r1   r   r   as_independentr   is_zerois_negativeis_positivero   rq   _solve_inequalityr   r+   r5   )r   rO   r   r   r   r   oor   rN   okoooknoor   r   rn   baxefr   beginning_denomscurrent_denomsrh   crf   rJ   rJ   rK   r     s   J







 
r   c                    s8  i i }}g }| D ]x}|j |j}}|t}t|dkr"|  n"|j|@ }	t|	dkr>|	  |tt	|d|  q	t
td| rU| g ||f q	| fdd}
|
rutdd |
D ru| g ||f q	|tt	|d|  q	dd	 | D }d
d	 | D }t|| |  S )Nr$   r   zZ
                    inequality has more than one symbol of interest.
                    c                    s    |   o| jp| jo| jj S r   )rd   is_Functionr   r   r   )uri   rJ   rK   r     s   
z&_reduce_inequalities.<locals>.<lambda>c                 s   s    | ]}t |tV  qd S r   )r+   r   rM   rf   rJ   rJ   rK   r         z'_reduce_inequalities.<locals>.<genexpr>c                 S   s   g | ]
\}}t |g|qS rJ   )r   rM   rj   r}   rJ   rJ   rK   rP     s    z(_reduce_inequalities.<locals>.<listcomp>c                 S   s   g | ]	\}}t ||qS rJ   )r   r   rJ   rJ   rK   rP     s    )rm   ro   atomsr   r   popr   r5   r   r   r3   r   is_polynomial
setdefaultfindr   itemsr   )r   symbols	poly_partabs_partother
inequalityr   r<   genscommon
componentspoly_reducedabs_reducedrJ   ri   rK   _reduce_inequalitiest  s,   




r  c                    sJ  t | s| g} dd | D } t jdd | D  }t |s |g}t|p%||@ }tdd |D r7ttddd |D   fd	d| D }  fd
d|D }g }| D ]<}t|trj||j	
 |j
  d}n	|dvrst|d}|dkrxqT|dkrtj  S |j	jrtd| || qT|} ~t| |}|d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                 S   s   g | ]}t |qS rJ   r   r   rJ   rJ   rK   rP     s    z'reduce_inequalities.<locals>.<listcomp>c                 S   s   g | ]}|j qS rJ   )r   r   rJ   rJ   rK   rP     s    c                 s   s    | ]}|j d u V  qdS )FNr   r   rJ   rJ   rK   r     r   z&reduce_inequalities.<locals>.<genexpr>zP
            inequalities cannot contain symbols that are not real.
            c                 S   s&   i | ]}|j d u r|t|jddqS )NTr   )r   r   namer   rJ   rJ   rK   
<dictcomp>  s    z'reduce_inequalities.<locals>.<dictcomp>c                    s   g | ]}|  qS rJ   r   r   recastrJ   rK   rP         c                    s   h | ]}|  qS rJ   r  r   r  rJ   rK   	<setcomp>  r  z&reduce_inequalities.<locals>.<setcomp>r   r   TFr    c                 S   s   i | ]\}}||qS rJ   rJ   )rM   kr   rJ   rJ   rK   r    r  )r   r   rV   anyr   r   r+   r   r   rm   r-   rn   r   r   r1   r.   r3   r5   r  r   r  )r   r  r	  keeprf   r   rJ   r  rK   reduce_inequalities  sB   



r  )T)F)9__doc__rS   sympy.calculus.utilr   r   r   
sympy.corer   sympy.core.exprtoolsr   sympy.core.relationalr   r	   r
   r   sympy.core.symbolr   r   sympy.sets.setsr   r   r   r   sympy.core.singletonr   sympy.core.functionr   $sympy.functions.elementary.complexesr   sympy.logicr   sympy.polysr   r   r   sympy.polys.polyutilsr   sympy.solvers.solvesetr   r   sympy.utilities.iterablesr   r   sympy.utilities.miscr   rL   rR   rb   r   r   r   r0   rz   r   r   r  r  rJ   rJ   rJ   rK   <module>   s>    [
B[G  )
! .-