a
    <b                     @   sV  d 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d Z,dd Z-dd Z.d)ddZ/dd Z0dd Z1dej2dfddZ3d d! Z4d*d"d#Z5d$d% Z6g fd&d'Z7d(S )+z<Tools for solving inequalities and systems of inequalities.     )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              	   C   s2  t | tstd|  jr\t|  d|}|tju r>tjgS |tj	u rPtj
gS td| | jddg  }}|dkr|D ]\}}t||}|| qzn|dkrtj}|tjdfg D ]$\}	}t||	d	d	}|| |	}qnJ|  dkrd}
nd
}
d\}}|dkrd}nD|dkr"d
}n4|dkr6d\}}n |dkrJd\}}ntd| tjd	 }	}t|D ]\}}|d r|
|kr|dt||	| | |
 ||   }
}	}nT|
|kr|s|dt||	d	| |d	 }	}n"|
|krj|rj|dt|| qj|
|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NotImplementedErrorZ
real_rootsr   appendNegativeInfinityInfinityZLCreversedinsert)Zpolyreltreals	intervalsroot_intervalleftrightsignZeq_signequalZ
right_openZmultiplicity rD   j/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.pysolve_poly_inequality   sr    















rF   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 rD   )rF   ).0psrD   rD   rE   
<listcomp>}       z+solve_poly_inequalities.<locals>.<listcomp>)r   )ZpolysrD   rD   rE   solve_poly_inequalitieso   s    rL   c                 C   s   t j}| D ]}|sq
tt jt jg}|D ]\\}}}t|| |}t|d}g }	|D ],}
|D ]"}|
|}|t jur\|	| q\qT|	}g }	|D ]*}|D ]}||8 }q|t jur|	| q|	}|s( qq(|D ]}||}qq
|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   r5   r6   rF   	intersectr4   union)eqsresult_eqsZglobal_intervalsnumerdenomr9   Znumer_intervalsZdenom_intervalsr<   Znumer_intervalZglobal_intervalr?   Zdenom_intervalrD   rD   rE   solve_rational_inequalities   s6    




rT   Tc              
      s  d}g }| rt jnt j}| D ]\}g }|D ]>}t|trD|\}}	n&|jr`|j|j |j }}	n
|d }}	|t j	u rt j
t jd  }
}}	n0|t ju rt jt jd  }
}}	n|  \}
}zt|
|f \\}
}}W n ty   ttdY n0 |jjs|
 | d  }
}}|j }|jsX|jsX|
| }t|d|	}|t| ddM }q*||
|f|	f q*|r|| q|r|t|M }t fdd|D g}||8 }|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"   )hasZone)rG   indr>   genrD   rE   rJ     s   z0reduce_rational_inequalities.<locals>.<listcomp>)r   r0   r2   r+   tupleZis_Relationallhsrhsrel_opr/   ZeroOner1   Ztogetheras_numer_denomr   r   r   domainZis_ExactZto_exactZ	get_exactZis_ZZZis_QQr	   solve_univariate_inequalityr4   rT   Zevalfas_relational)exprsr[   rU   exactrO   Zsolution_exprsrQ   exprr9   rR   rS   optrc   excluderD   rZ   rE   reduce_rational_inequalities   sV    










rl   c                    s   |j du rttd fdd  | }ddd}g }|D ]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rr| j}| jD ]R} |}|s2|}qg }|D ].\} }|D ] \}}||| ||| f qFq:|}qn| jr| j}	|	jstd | j	}|D ]\} }|| |	 |f qnjt
| tr | jd }|D ]>\} }|| |t| dg f ||  |t| dg f qn
| g fg}|S )Nz'Only Integer Powers are allowed on Abs.r   )Zis_AddZis_Mulfuncargsr4   is_Powexp
is_Integerr,   baser+   r   r   r   )
ri   rf   opargrh   rn   condsZ_exprZ_condsrX   _bottom_up_scanrD   rE   rw   6  s4    

 
z.reduce_abs_inequality.<locals>._bottom_up_scanr&   r(   r'   r)   r   )is_extended_real	TypeErrorr   keysr	   r4   rl   )ri   r9   r[   rf   mappinginequalitiesru   rD   rv   rE   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 rD   )r~   )rG   ri   r9   rZ   rD   rE   rJ     s   z+reduce_abs_inequalities.<locals>.<listcomp>r   )rf   r[   rD   rZ   rE   reduce_abs_inequalitiesm  s    r   Fc           (         s  ddl m} |tjdu r*ttdn2|tjur\td|d|}|rX|	}|S }|}j
du rtj}|s||S |	|S j
du rtddd	z|iW n ty   ttd
Y n0 d}tju r|}ntju rtj}njj }	t|	}
|
tjkrTt|	}	|	d}|tju r@|}n|tju rtj}n|
durt|	|}j}|dv r|jdr|}n|jdstj}n6|dv rވ|jdr|}n|jdstj}|j|j }}|| tju rtd|
dd|}|}|du r|	 \}}z>|jvrLt |	jdkrLt!t"|	|}|du rft!W n4 t!tfy   ttd#t$d Y n0 t|	  fdd}g }|D ]}|%t"|| q|st& |}djv ojdk}zt'|j(t)|j|j }t)|| t*|  t|j|j|j|v|j|v}t+dd |D rzt,|ddd }n\t-|dd }|d rtz&|d }t |dkrt*t.|}W n ty   tY n0 W n ty   tdY n0 tj}t/ tjkrd}t) }z4t0t/ |}t1|tsl|D ].}||vr:||r:|j
r:|t)|7 }q:n|j|j }} t,|t)|  D ]}||}!|| kr*||}"t2||}#|#|vr*|#j
r*||#r*|!r|"r|t||7 }n@|!r|t3||7 }n(|"r|t4||7 }n|t5||7 }|}q|D ]}$|t)|$8 }q6W n tyj   tj}d}Y n0 |tju rt!td#||f ||}tjg}%|j}||v r||r|j6r|%7t)| |D ]~}&|&} |t2|| r|%7t|| dd |&|v r|8|& n6|&|v r:|8|& ||&}'n|}'|'rR|%7t)|& | }q|j} | |v r|| r| j6r|%7t)|  |t2|| r|%7t5||  t/ tjkr|r||}nt9t:|% ||#|}|s|S |	|S )aS  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() doesn't 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)rU   
continuousNr[   TZextended_realz
                When gen is real, the relational has a complex part
                which leads to an invalid comparison like I < 0.
                rx   )r&   r(   r$   z
                    The inequality, %s, cannot be solved using
                    solve_univariate_inequality.
                    xc                    s     t| }z|d}W n ty8   tj}Y n0 |tjtjfv rN|S |jdu r^tjS |d}|j	rz|dS t
d| d S )Nr   Fr*   z!relationship did not evaluate: %s)subsr   rm   rz   r   r1   r/   ry   rX   Zis_comparabler3   )r   vrZ
expanded_eri   r[   rD   rE   valid  s    


z*solve_univariate_inequality.<locals>.valid=r#   c                 s   s   | ]}|j V  qd S N)r.   )rG   r   rD   rD   rE   	<genexpr>G  rK   z.solve_univariate_inequality.<locals>.<genexpr>)	separatedc                 S   s   | j S r   ry   )r   rD   rD   rE   <lambda>J  rK   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   Z	is_subsetr   r0   r3   r   rd   intersectionre   ry   r2   r   xreplacerz   r/   r1   r]   r^   r   r`   r   rm   r   r_   supinfr6   r   rM   rb   free_symbolslenr,   r   r   r   extendr   setboundaryr   listallr   r   sortedr   r   r+   _ptZRopenZLopenopen	is_finiter4   remover   r   )(ri   r[   rU   rc   r   r   rvZ_genZ_domaineZperiodconstZfranger9   r   r   rX   rY   Zsolnsr   ZsingularitiesZ	include_xZdiscontinuitiesZcritical_pointsr;   ZsiftedZ	make_realcheckZim_solazstartendZvalid_startZvalid_zptrI   Zsol_setsr   _validrD   r   rE   rd     sF   9
























rd   c                 C   s   | j s|j s| | d }n| j r.|j r.tj}n| j r>| jdu sN|j rV|jdu rVtd|j rb|jsn| j rx| jrx||  } }|j r| jr| d }q| jr| tj }q| d }n0| j r|jr|tj }n|jr|d }n|d }|S )z$Return a point between start and endr*   Nz,cannot proceed with unsigned infinite valuesr$   )is_infiniter   r`   Zis_extended_positiver,   Zis_extended_negativeZHalf)r   r   r   rD   rD   rE   r     s:    



r   c                 C   s|  ddl m} || jvr| S | j|kr*| j} | j|krD|| jjvrD| S dd }d}tj}| j| j }zBt||}|	 dkr| 
| d}n|s|	 dkrtW n, ttfy   |szt| gg|}W n ty   t| |}Y n0 || ||}	|	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0 g }|du rj| }d}|j|dd\}}||8 }||8 }t|}|j|d	d\}}|jd	ks`|j|j  krPdu rjn n| jd
vrj|}tj}|| }|jr| 
||}n| j
||}|| j|| jB }||}|| D ]T}tt|d||d}t|tr|j|kr||||jtju r||  q| |fD ]N}||||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   sN   z0|  ||}|tju r|W S |dvr,W d S |W S  tyH   tj Y S 0 d S )NTF)r   r   NaNrz   )ierI   rW   r   rD   rD   rE   classify   s    
z#_solve_inequality.<locals>.classifyNr$   T)Zas_AddF)r#   r"   )linear)r   r   r   r^   r7   r]   r   r6   r   Zdegreerm   r-   r3   r   rl   rd   r/   r1   r   r   Zas_independentr   is_zeroZis_negativeZis_positiver_   ra   _solve_inequalityr
   r+   r4   )r   rI   r   r   r   r   Zoori   rH   ZokooZoknooru   r   r^   baxZefr   Zbeginning_denomsZcurrent_denomsrY   crW   rD   rD   rE   r     s    J



 

$
r   c                    sh  i i  }}g }| D ]}|j |j }}|t}t|dkrD|  nF|j|@ }	t|	dkr~|	  |tt	|d|  qnt
td| r| g ||f q| fdd}
|
rtdd |
D r| g ||f q|tt	|d|  qg }g }| D ]\ }|t|g  q| D ]\ }|t|  q<t|| |  S )Nr$   r   zZ
                    inequality has more than one symbol of interest.
                    c                    s    |   o| jp| jo| jj S r   )rV   Zis_Functionro   rp   rq   )urZ   rD   rE   r     s    
z&_reduce_inequalities.<locals>.<lambda>c                 s   s   | ]}t |tV  qd S r   )r+   r   rG   rW   rD   rD   rE   r     rK   z'_reduce_inequalities.<locals>.<genexpr>)r]   r_   Zatomsr   r   popr   r4   r   r	   r3   r   Zis_polynomial
setdefaultfindr   itemsrl   r   r   )r}   symbolsZ	poly_partZabs_partotherZ
inequalityri   r9   genscommon
componentsZpoly_reducedZabs_reducedrf   rD   rZ   rE   _reduce_inequalities{  s4    




r   c                    sP  t | s| g} dd | D } t jdd | D  }t |s@|g}t|pJ||@ }tdd |D rnttddd |D   fd	d| D }  fd
d|D }g }| D ]~}t|tr||j	
 |j
  d}n|dvrt|d}|dkrqn|dkrtj  S |j	jrtd| || q|} ~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 rD   )r   r   rD   rD   rE   rJ     rK   z'reduce_inequalities.<locals>.<listcomp>c                 S   s   g | ]
}|j qS rD   )r   r   rD   rD   rE   rJ     rK   c                 s   s   | ]}|j d u V  qdS )FNr   r   rD   rD   rE   r     rK   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   )ry   r   namer   rD   rD   rE   
<dictcomp>  s   z'reduce_inequalities.<locals>.<dictcomp>c                    s   g | ]}|  qS rD   r   r   ZrecastrD   rE   rJ     rK   c                    s   h | ]}|  qS rD   r   r   r   rD   rE   	<setcomp>  rK   z&reduce_inequalities.<locals>.<setcomp>r   r   TFr    c                 S   s   i | ]\}}||qS rD   rD   )rG   kr   rD   rD   rE   r     rK   )r   r   rN   anyrz   r   r+   r	   rm   r]   r-   r^   r
   r   r1   r.   r3   r4   r   r   r   )r}   r   r   ZkeeprW   r   rD   r   rE   reduce_inequalities  sB    





r   N)T)F)8__doc__Zsympy.calculus.utilr   r   r   Z
sympy.corer   r   r   Zsympy.core.exprtoolsr   Zsympy.core.relationalr	   r
   r   r   Zsympy.sets.setsr   r   r   r   Zsympy.core.singletonr   Zsympy.core.functionr   Z$sympy.functions.elementary.complexesr   r   Zsympy.logicr   Zsympy.polysr   r   r   Zsympy.polys.polyutilsr   Zsympy.solvers.solvesetr   r   Zsympy.utilities.iterablesr   r   Zsympy.utilities.miscr   rF   rL   rT   rl   r~   r   r0   rd   r   r   r   r   rD   rD   rD   rE   <module>   s8   [B
ZQ  )!
 .3