U
    9%e                     @   sp  d dl mZmZmZmZmZmZ d dl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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m Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( d d	l)m*Z*m+Z+m,Z, d d
l-m.Z.m/Z/ d dl0m1Z1 ej2Z3G dd deZ4G dd deZ5d&ddZ6dd Z7dd Z8dd Z9dd Z:dd Z;dd Z<dd  Z=d!dd"d#d$Z>d%S )'    )SFunctiondiffTupleDummyMul)Basicas_Basic)RationalNumberSymbol_illegal)global_parameters)LtGtEqNe
Relational
_canonical_canonical_coeff)ordered)MaxMin)AndBooleandistribute_and_over_orNottruefalseOrITEsimplify_logicto_cnfdistribute_or_over_and)uniqsiftcommon_prefix)
filldedent	func_name)productc                   @   sL   e Zd ZdZdd Zedd Zedd Zedd	 Zd
d Z	dd Z
dS )ExprCondPairz)Represents an expression, condition pair.c                 C   s   t |}|dkrt| |tS |dkr4t| |tS t|trd|trdt	|}t|trd|
t}t|tsttdt| t| ||S )NTFzL
                Second argument must be a Boolean,
                not `%s`)r	   r   __new__r   r   
isinstancer   has	Piecewisepiecewise_foldrewriter   r   	TypeErrorr&   r'   )clsexprcond r4   c/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/functions/elementary/piecewise.pyr*      s    


zExprCondPair.__new__c                 C   s
   | j d S )z6
        Returns the expression of this pair.
        r   argsselfr4   r4   r5   r2   &   s    zExprCondPair.exprc                 C   s
   | j d S )z5
        Returns the condition of this pair.
           r6   r8   r4   r4   r5   r3   -   s    zExprCondPair.condc                 C   s   | j jS N)r2   is_commutativer8   r4   r4   r5   r<   4   s    zExprCondPair.is_commutativec                 c   s   | j V  | jV  d S r;   )r2   r3   r8   r4   r4   r5   __iter__8   s    zExprCondPair.__iter__c                    s   | j  fdd| jD  S )Nc                    s   g | ]}|j f  qS r4   simplify.0akwargsr4   r5   
<listcomp>=   s     z/ExprCondPair._eval_simplify.<locals>.<listcomp>funcr7   r9   rD   r4   rC   r5   _eval_simplify<   s    zExprCondPair._eval_simplifyN)__name__
__module____qualname____doc__r*   propertyr2   r3   r<   r=   rI   r4   r4   r4   r5   r)      s   


r)   c                       s  e Zd ZdZdZdZdd Zedd Zdd	 Z	d
d Z
dLddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZdMddZdN fdd 	ZdOd"d#ZdPd$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d/ Zd1d/ Zd2d/ Zd3d/ Zd4d/ Z d5d/ Z!d6d/ Z"d7d/ Z#d8d/ Z$d9d/ Z%d:d/ Z&d;d/ Z'd<d/ Z(d=d/ Z)d>d/ Z*d?d/ Z+d@d/ Z,dAd/ Z-dBd/ Z.dCd/ Z/edDdE Z0dQdFdGZ1dHdI Z2dJdK Z3  Z4S )Rr-   a  
    Represents a piecewise function.

    Usage:

      Piecewise( (expr,cond), (expr,cond), ... )
        - Each argument is a 2-tuple defining an expression and condition
        - The conds are evaluated in turn returning the first that is True.
          If any of the evaluated conds are not explicitly False,
          e.g. ``x < 1``, the function is returned in symbolic form.
        - If the function is evaluated at a place where all conditions are False,
          nan will be returned.
        - Pairs where the cond is explicitly False, will be removed and no pair
          appearing after a True condition will ever be retained. If a single
          pair with a True condition remains, it will be returned, even when
          evaluation is False.

    Examples
    ========

    >>> from sympy import Piecewise, log, piecewise_fold
    >>> from sympy.abc import x, y
    >>> f = x**2
    >>> g = log(x)
    >>> p = Piecewise((0, x < -1), (f, x <= 1), (g, True))
    >>> p.subs(x,1)
    1
    >>> p.subs(x,5)
    log(5)

    Booleans can contain Piecewise elements:

    >>> cond = (x < y).subs(x, Piecewise((2, x < 0), (3, True))); cond
    Piecewise((2, x < 0), (3, True)) < y

    The folded version of this results in a Piecewise whose
    expressions are Booleans:

    >>> folded_cond = piecewise_fold(cond); folded_cond
    Piecewise((2 < y, x < 0), (3 < y, True))

    When a Boolean containing Piecewise (like cond) or a Piecewise
    with Boolean expressions (like folded_cond) is used as a condition,
    it is converted to an equivalent :class:`~.ITE` object:

    >>> Piecewise((1, folded_cond))
    Piecewise((1, ITE(x < 0, y > 2, y > 3)))

    When a condition is an ``ITE``, it will be converted to a simplified
    Boolean expression:

    >>> piecewise_fold(_)
    Piecewise((1, ((x >= 0) | (y > 2)) & ((y > 3) | (x < 0))))

    See Also
    ========

    piecewise_fold
    piecewise_exclusive
    ITE
    NTc           	      O   s   t |dkrtdg }|D ]:}tt|d| }|j}|tkr@q|| |tkr qXq|dt	j
}|r| j| }|d k	r|S n$t |dkr|d jdkr|d jS tj| f||S )Nr   z(At least one (expr, cond) pair expected.r7   evaluater:   T)lenr0   r)   getattrr3   r   appendr   popr   rO   evalr2   r   r*   )	r1   r7   optionsnewargsecpairr3   rT   rr4   r4   r5   r*      s&    


zPiecewise.__new__c                 G   s   |st S t|dkr0|d d dkr0|d d S t|}t|t|k}tdd t||D }|spttd|sx|s| | S dS )	a  Either return a modified version of the args or, if no
        modifications were made, return None.

        Modifications that are made here:

        1. relationals are made canonical
        2. any False conditions are dropped
        3. any repeat of a previous condition is ignored
        4. any args past one with a true condition are dropped

        If there are no args left, nan will be returned.
        If there is a single arg with a True condition, its
        corresponding expression will be returned.

        EXAMPLES
        ========

        >>> from sympy import Piecewise
        >>> from sympy.abc import x
        >>> cond = -x < -1
        >>> args = [(1, cond), (4, cond), (3, False), (2, True), (5, x < 1)]
        >>> Piecewise(*args, evaluate=False)
        Piecewise((1, -x < -1), (4, -x < -1), (2, True))
        >>> Piecewise(*args)
        Piecewise((1, x > 1), (2, True))
        r:   r   Tc                 s   s   | ]\}}||kV  qd S r;   r4   )rA   rB   br4   r4   r5   	<genexpr>   s     z!Piecewise.eval.<locals>.<genexpr>z
                There are no conditions (or none that
                are not trivially false) to define an
                expression.N)	UndefinedrP   _piecewise_collapse_argumentsallzip
ValueErrorr&   )r1   _argsrV   missingZsamer4   r4   r5   rT      s    zPiecewise.evalc                 K   sp   g }| j D ]Z\}}|ddrVt|tr@|jf |}|| kr@|}t|trV|jf |}|||f q
| j| S )z3
        Evaluate this piecewise function.
        deepT)r7   getr+   r   doitrR   rG   )r9   hintsrV   ecnewer4   r4   r5   rf      s    

zPiecewise.doitc                 K   s   t | f|S r;   )piecewise_simplifyrH   r4   r4   r5   rI      s    zPiecewise._eval_simplifyr   c                 C   s:   | j D ].\}}|dks&||ddkr||  S qd S )NTr   )r7   subsZas_leading_term)r9   xlogxcdirrh   ri   r4   r4   r5   _eval_as_leading_term   s    zPiecewise._eval_as_leading_termc                 C   s   | j dd | jD  S )Nc                 S   s   g | ]\}}|  |fqS r4   )ZadjointrA   rh   ri   r4   r4   r5   rE      s     z+Piecewise._eval_adjoint.<locals>.<listcomp>rF   r8   r4   r4   r5   _eval_adjoint   s    zPiecewise._eval_adjointc                 C   s   | j dd | jD  S )Nc                 S   s   g | ]\}}|  |fqS r4   )	conjugaterq   r4   r4   r5   rE      s     z-Piecewise._eval_conjugate.<locals>.<listcomp>rF   r8   r4   r4   r5   _eval_conjugate   s    zPiecewise._eval_conjugatec                    s   | j  fdd| jD  S )Nc                    s   g | ]\}}t | |fqS r4   )r   rq   rm   r4   r5   rE      s     z.Piecewise._eval_derivative.<locals>.<listcomp>rF   )r9   rm   r4   ru   r5   _eval_derivative   s    zPiecewise._eval_derivativec                    s   | j  fdd| jD  S )Nc                    s   g | ]\}}|  |fqS r4   )Z_evalfrq   precr4   r5   rE      s     z)Piecewise._eval_evalf.<locals>.<listcomp>rF   )r9   rx   r4   rw   r5   _eval_evalf   s    zPiecewise._eval_evalfc                 C   s^   |j s
d S | jD ]H\}}|||}|jr0 d S || jkrD d S |r|||  S qd S r;   )is_realr7   rl   is_Relationalas_setboundary_eval_is_meromorphic)r9   rm   rB   rh   ri   r3   r4   r4   r5   r~      s    zPiecewise._eval_is_meromorphicc                    s*   ddl m  | j fdd| jD  S )a  Return the Piecewise with each expression being
        replaced with its antiderivative. To obtain a continuous
        antiderivative, use the :func:`~.integrate` function or method.

        Examples
        ========

        >>> from sympy import Piecewise
        >>> from sympy.abc import x
        >>> p = Piecewise((0, x < 0), (1, x < 1), (2, True))
        >>> p.piecewise_integrate(x)
        Piecewise((0, x < 0), (x, x < 1), (2*x, True))

        Note that this does not give a continuous function, e.g.
        at x = 1 the 3rd condition applies and the antiderivative
        there is 2*x so the value of the antiderivative is 2:

        >>> anti = _
        >>> anti.subs(x, 1)
        2

        The continuous derivative accounts for the integral *up to*
        the point of interest, however:

        >>> p.integrate(x)
        Piecewise((0, x < 0), (x, x < 1), (2*x - 1, True))
        >>> _.subs(x, 1)
        1

        See Also
        ========
        Piecewise._eval_integral
        r   	integratec                    s"   g | ]\}} |f|fqS r4   r4   rq   r   rD   rm   r4   r5   rE   '  s     z1Piecewise.piecewise_integrate.<locals>.<listcomp>)Zsympy.integralsr   rG   r7   )r9   rm   rD   r4   r   r5   piecewise_integrate  s    "zPiecewise.piecewise_integratec              
      s  |  t}ttfdd|D }|ri g }tdt|dD ]0}tt||d|krdd}ntfddD  }t|j	tdkrdd	l
m}	m  z|	|d }
W q ttfk
r   t fd
d|D  }
Y qX nt| }
|
tjkrqB|
}|| }t|| jrPt|jdkrP|jd \}}t||dkrJdn|}|dk	rB|g | || qBD ]}t|  |< qzfddt|D |df t S dS )ao  Return either None (if the conditions of self depend only on x) else
        a Piecewise expression whose expressions (handled by the handler that
        was passed) are paired with the governing x-independent relationals,
        e.g. Piecewise((A, a(x) & b(y)), (B, c(x) | c(y)) ->
        Piecewise(
            (handler(Piecewise((A, a(x) & True), (B, c(x) | True)), b(y) & c(y)),
            (handler(Piecewise((A, a(x) & True), (B, c(x) | False)), b(y)),
            (handler(Piecewise((A, a(x) & False), (B, c(x) | True)), c(y)),
            (handler(Piecewise((A, a(x) & False), (B, c(x) | False)), True))
        c                    s*   g | ]"} |j kr|tjtjfkr|qS r4   )free_symbolsr   r   r   )rA   rY   ru   r4   r5   rE   6  s     
z*Piecewise._handle_irel.<locals>.<listcomp>)r:   r   )repeatr:   Nc                    s   g | ]} | r|qS r4   r4   rA   irepsr4   r5   rE   B  s      r   )reduce_inequalities_solve_inequalityc                    s   g | ]} |d  ddqS )r   T)Zlinearr4   r@   )r   freer4   r5   rE   M  s     Tc                    s   g | ]}| | fqS r4   r4   )rA   rh   r6   r4   r5   rE   g  s     )atomsr   listr   r(   rP   dictr`   r   r   sympy.solvers.inequalitiesr   r   ra   NotImplementedErrorr   r   r   xreplacer+   rG   r7   
setdefaultrR   r   r#   r-   )r9   rm   handlerrelZirelZexprinordertruthr3   Zandargsr   tr2   Zecondkr4   )r   r7   r   r   rm   r5   _handle_irel)  sF    


zPiecewise._handle_irelc                    s<  ddl m} |r8 fdd}|}|dk	r8|S \}}|s`ddl m}	 |	S dd |D }
tj}| |d	fg}t|
D ]\}}|| |fkrt|D ]$\}\}}}|d	kr|||f||< q q*t|d
 }tt	|D ]:\}\}}}|d	kr|| }t
|||f||||d
 < qqdd |D }tdd |D r`|| |td	f g }d}|D ]\}}}||| d f }|dkr|}n@||}||}|jt s|jt r|}n||7 }|tjkrd}n4j|| d	  j|dkr|k }n|k}|||f qlt| S )a  Return the indefinite integral of the
        Piecewise such that subsequent substitution of x with a
        value will give the value of the integral (not including
        the constant of integration) up to that point. To only
        integrate the individual parts of Piecewise, use the
        ``piecewise_integrate`` method.

        Examples
        ========

        >>> from sympy import Piecewise
        >>> from sympy.abc import x
        >>> p = Piecewise((0, x < 0), (1, x < 1), (2, True))
        >>> p.integrate(x)
        Piecewise((0, x < 0), (x, x < 1), (2*x - 1, True))
        >>> p.piecewise_integrate(x)
        Piecewise((0, x < 0), (x, x < 1), (2*x, True))

        See Also
        ========
        Piecewise.piecewise_integrate
        r   r   c                    s4   t | jr"| jfddi S | jf S d S )N_firstF)r+   rG   _eval_integralr   ZipwrD   r9   rm   r4   r5   r     s    z)Piecewise._eval_integral.<locals>.handlerNIntegralc                 S   s   g | ]\}}}}||fqS r4   r4   rA   rB   r[   _r4   r4   r5   rE     s    
 z,Piecewise._eval_integral.<locals>.<listcomp>rZ   r:   c                 S   s$   g | ]\}}}||kr|||fqS r4   r4   rA   rB   r[   r   r4   r4   r5   rE     s      c                 s   s   | ]\}}}|d kV  qdS )rZ   Nr4   r   r4   r4   r5   r\     s     z+Piecewise._eval_integral.<locals>.<genexpr>TF)sympy.integrals.integralsr   r   
_intervalsr   r   Infinity	enumeraterP   reversed_clipanyrR   r]   rl   _eval_intervalr,   r   r7   r3   r-   )r9   rm   r   rD   r   r   irvokabeir   piecesoodoner   pjrB   r[   r   Nr7   sumZantirh   r3   r4   r   r5   r   l  sZ    
 
"
zPiecewise._eval_integralc                    sJ  |dks|dkr t  |||S tt|||f\ |r fdd}|}|dk	rf|S  k tjkstjks tjkrĈj dd}t	|t
rt
dd |jD  }n| }|S  k tjks tjkstjkrntd}td	}	jrn|} jr n|	}j||dd}
||krj||	krj|
| |	i |
||	 i }}
n(j dd |
||	 i }}
td
dd}jr|
 | i|  i}
| | i|  i}nH jr0|
 | i|  i}
| | i|  i}dd }||}||
}
||krjt
|
 k f|df}nt
| k f|
df}|tkrtdtdd |
|fD rt|}|S \}}|sddlm} | fS dd |D } dfg}tj}t|D ]\}}|dd | |fkrlt|D ](\}\}}}|dkr<|||f||< q< qt|d }tt|D ]>\}\}}}|dkr|| }t|||f||||d < qqdd |D }tj}d}|D ]`\}}}|dkr$|dkr
t  S t
| |kftdf  S ||| d ||7 }|}q|S )z?Evaluates the function along the sym in a given interval [a, b]Nc                    s0   t | jr| j d dS |  S d S )Nr   )r+   rG   r   r   hilor9   rm   r4   r5   r     s    z)Piecewise._eval_interval.<locals>.handlerFr   c                 S   s   g | ]\}}| |fqS r4   r4   rq   r4   r4   r5   rE     s     z,Piecewise._eval_interval.<locals>.<listcomp>r   r    T)Zpositivec                 S   s   |  dd dd S )Nc                 S   s   t | ttfS r;   )r+   r   r   ru   r4   r4   r5   <lambda>      z<Piecewise._eval_interval.<locals>.<lambda>.<locals>.<lambda>c                 S   s   | j | j S r;   rF   ru   r4   r4   r5   r     r   )replacer   r4   r4   r5   r     s   z*Piecewise._eval_interval.<locals>.<lambda>z(Can't integrate across undefined region.c                 s   s   | ]}t |tV  qd S r;   )r+   r-   r   r4   r4   r5   r\     s     z+Piecewise._eval_interval.<locals>.<genexpr>r   r   c                 S   s   g | ]\}}}}||fqS r4   r4   r   r4   r4   r5   rE   )  s    
 rZ      r:   c                 S   s$   g | ]\}}}||kr|||fqS r4   r4   r   r4   r4   r5   rE   8  s      r   )superr   mapr	   r   r   r   r   NegativeInfinityr+   r-   r7   r   r   Zis_comparabler   Z	is_Symbolr]   ra   r   r.   r   r   r   r   r   rP   r   r   ZZero)r9   symrB   r[   r   r   r   rvZ_a_bposnegr   touchr   r   r   r   r   r   r   r   r   r   r   Zupto	__class__r   r5   r     s    
 "  



$

zPiecewise._eval_intervalFc                    sB  ddl m  t| tstfdd fdd}t| j}| t}i |D ],}||\}}|dkrvd|f  S ||< qRfd	d
| jD }g }	d }
}t	|D ]\\}|t
jkrq|t
jkr؈}
} qvt|tr|rdd| f  S qt|}t|trt|}t|tr>|	fdd
|jD  q|t
jk	r\|	|f q|t
jkr}
} qvqg }|	D ]\}t|trZt
j}t
j}g }|jD ]}t|ts  dS t|tr|}|r  dS  qnt|trB|j\}}|kr|| n&|kr,|| n|    S qnD|jkr\t|j|}n*|jkrvt|j|}n|    S q|rtt|}g }t	|D ]n\}||  k rdksn ||  krdkrn nq|s|d|f ||d d |f q||d d |f |d |	fdd
|D  q~nlt|tr|jdkr|j|j }}|jkrt
j}n |jkrt
j}n|  S ndd| f  S |t|| }}|r||kr dS ||kt
jk	r~|||f q~|
dk	r2|t
jt
j|
|f dtt|fS )a_  Return a bool and a message (when bool is False), else a
        list of unique tuples, (a, b, e, i), where a and b
        are the lower and upper bounds in which the expression e of
        argument i in self is defined and $a < b$ (when involving
        numbers) or $a \le b$ when involving symbols.

        If there are any relationals not involving sym, or any
        relational cannot be solved for sym, the bool will be False
        a message be given as the second return value. The calling
        routine should have removed such relationals before calling
        this routine.

        The evaluated conditions will be returned as ranges.
        Discontinuous ranges will be returned separately with
        identical expressions. The first condition that evaluates to
        True will be returned as the last tuple with a, b = -oo, oo.
        r   )r   c                    s   dt d | f fS )NFzJ
                A condition not involving
                %s appeared: %s)r&   r3   )r   r4   r5   
nonsymfail]  s    z(Piecewise._intervals.<locals>.nonsymfailc                    s   | j kr| S z | }W n$ tk
rD   dd| f f Y S X t|tr|jd j }|jd ksr|krdd| f fS |jdkrtj}q|jdkrzt|j	k |j	k}W q t
k
r   tj}Y qX n|tjk tjk @ krtj}d|fS )NFz%Unable to solve relational %s for %s.r:   r   z==!=T)r   r   r+   r   r7   rel_opr   r   r   rhsr0   r   r   r   )rY   r   r   )r   r   r   r4   r5   _solve_relationalb  s(    



z/Piecewise._intervals.<locals>._solve_relationalTFc                    s   g | ]}|  qS r4   )r   r   r   r4   r5   rE     s     z(Piecewise._intervals.<locals>.<listcomp>Nzencountered Eq condition: %sc                    s    g | ]}t |ts |fqS r4   r+   r   )rA   o)r2   r   r4   r5   rE     s    
)Fzexpecting only Relationals)Fz"encountered secondary Eq conditionrZ   r:   c              	      s,   g | ]$} t |d  k |d k fqS )r   r:   )r   r   )r2   iargr   r4   r5   rE     s     r   zunrecognized condition: %s)Fzencountered Eq condition)r   r   r+   r-   AssertionErrorr   r7   r   r   r   r   r   r   r   r!   r   r"   r   extendrR   r   r   r   Zltsr   Zgtsr   r   rS   r   r#   )r9   r   	err_on_Eqr   r7   keysrY   r   sZ	expr_conddefaultZidefaultr3   int_exprlowerupperexcludeZcond2lZnewcondrh   r4   )r   r2   r   r   r   r   r   r5   r   G  s    










6

zPiecewise._intervalsc                    s"    fdd| j D }| j| S )Nc                    s"   g | ]}|j  |jfqS r4   )r2   _eval_nseriesr3   )rA   rW   rn   nrm   r4   r5   rE     s     z+Piecewise._eval_nseries.<locals>.<listcomp>)r7   rG   )r9   rm   r   rn   ro   r7   r4   r   r5   r     s    zPiecewise._eval_nseriesc                    s   | j  fdd| jD  S )Nc                    s   g | ]\}}|  |fqS r4   r4   rq   r   r4   r5   rE     s     z)Piecewise._eval_power.<locals>.<listcomp>rF   )r9   r   r4   r   r5   _eval_power  s    zPiecewise._eval_powerc                 C   sx   t | j}d}t|D ]H\}\}}|||}|dkrFd}|||}||f||< |dkr q`q|sntdff}| j| S )NFT)r   r7   r   _subsr]   rG   )r9   oldnewr7   Z
args_existr   rh   ri   r4   r4   r5   
_eval_subs  s    

zPiecewise._eval_subsc                 C   s   | j dd | jD  S )Nc                 S   s   g | ]\}}|  |fqS r4   )Z	transposerq   r4   r4   r5   rE   	  s     z-Piecewise._eval_transpose.<locals>.<listcomp>rF   r8   r4   r4   r5   _eval_transpose  s    zPiecewise._eval_transposec                 C   sL   d }| j D ]<\}}t||}|d kr* d S |d kr8|}q
||k	r
 d S q
|S r;   )r7   rQ   )r9   is_attrr[   r2   r   rB   r4   r4   r5   _eval_template_is_attr  s    
z Piecewise._eval_template_is_attrc                 C   s
   |  dS )N	is_finiter   r8   r4   r4   r5   r     s   zPiecewise.<lambda>c                 C   s
   |  dS )NZ
is_complexr   r8   r4   r4   r5   r     r   c                 C   s
   |  dS )NZis_evenr   r8   r4   r4   r5   r     r   c                 C   s
   |  dS )NZis_imaginaryr   r8   r4   r4   r5   r     s   c                 C   s
   |  dS )N
is_integerr   r8   r4   r4   r5   r     r   c                 C   s
   |  dS )NZis_irrationalr   r8   r4   r4   r5   r     s   c                 C   s
   |  dS )NZis_negativer   r8   r4   r4   r5   r      r   c                 C   s
   |  dS )NZis_nonnegativer   r8   r4   r4   r5   r   !  s   c                 C   s
   |  dS )NZis_nonpositiver   r8   r4   r4   r5   r   #  s   c                 C   s
   |  dS )NZ
is_nonzeror   r8   r4   r4   r5   r   %  s   c                 C   s
   |  dS )NZis_oddr   r8   r4   r4   r5   r   '  r   c                 C   s
   |  dS )NZis_polarr   r8   r4   r4   r5   r   (  r   c                 C   s
   |  dS )NZis_positiver   r8   r4   r4   r5   r   )  r   c                 C   s
   |  dS )NZis_extended_realr   r8   r4   r4   r5   r   *  s   c                 C   s
   |  dS )NZis_extended_positiver   r8   r4   r4   r5   r   ,  s   c                 C   s
   |  dS )NZis_extended_negativer   r8   r4   r4   r5   r   .  s   c                 C   s
   |  dS )NZis_extended_nonzeror   r8   r4   r4   r5   r   0  s   c                 C   s
   |  dS )NZis_extended_nonpositiver   r8   r4   r4   r5   r   2  s   c                 C   s
   |  dS )NZis_extended_nonnegativer   r8   r4   r4   r5   r   4  s   c                 C   s
   |  dS )Nrz   r   r8   r4   r4   r5   r   6  r   c                 C   s
   |  dS )Nis_zeror   r8   r4   r4   r5   r   7  s   c                 C   sN   |dkrdS t |trJz|j|j }|jr2|jW S W n tk
rH   Y nX dS )z(Return the truth value of the condition.TN)r+   r   lhsr   r<   r   r0   )r1   r3   r   r4   r4   r5   Z__eval_cond:  s    
zPiecewise.__eval_condc           
      C   s   |dkrt j}g }|}|t j }t }| jD ]\}}||jO }t|dkrZttd|r|	t
D ]}t|ttfshttdqh|| }	||	 }|	t jkr0|||	f q0|S )aP  Return tuples for each argument of self that give
        the expression and the interval in which it is valid
        which is contained within the given domain.
        If a condition cannot be converted to a set, an error
        will be raised. The variable of the conditions is
        assumed to be real; sets of real values are returned.

        Examples
        ========

        >>> from sympy import Piecewise, Interval
        >>> from sympy.abc import x
        >>> p = Piecewise(
        ...     (1, x < 2),
        ...     (2,(x > 0) & (x < 4)),
        ...     (3, True))
        >>> p.as_expr_set_pairs()
        [(1, Interval.open(-oo, 2)),
         (2, Interval.Ropen(2, 4)),
         (3, Interval(4, oo))]
        >>> p.as_expr_set_pairs(Interval(0, 3))
        [(1, Interval.Ropen(0, 2)),
         (2, Interval(2, 3))]
        Nr:   z=
                    multivariate conditions are not handled.z
                            Inequalities in the complex domain are
                            not supported. Try the real domain by
                            setting domain=S.Reals)r   Reals	is_subsetsetr7   r   rP   r   r&   r   r   r+   r   r   ra   Z	intersectr|   EmptySetrR   )
r9   domainZexp_setsUcomplexZ	cond_freer2   r3   r   Zcond_intr4   r4   r5   as_expr_set_pairsG  s&    

zPiecewise.as_expr_set_pairsc              
   O   sx  i }t |}tdd |D }t|D  ]\}\}}t|tsZ|dkrZttdt| |dkrh q&t|trx|j	n|gD ]}|j
}| }	z ||	tj| ||	< W n( tk
r   |sttd| Y nX ||	 tjtjfkr~t || ||< d }|| d<  qq~|dkr& q&q&|dkr<ttd|| \}
}t|d | D ]\}}t|||
}
qXt|
S )Nc                 s   s   | ]\}}|d kV  qdS )TNr4   )rA   r[   ri   r4   r4   r5   r\   {  s     z1Piecewise._eval_rewrite_as_ITE.<locals>.<genexpr>TzP
                    Expecting Boolean or bool but got `%s`
                    a  
                            A method to determine whether a multivariate
                            conditional is consistent with a complete coverage
                            of all variables has not been implemented so the
                            rewrite is being stopped after encountering `%s`.
                            This error would not occur if a default expression
                            like `(foo, True)` were given.
                            r:   z
                Conditions must cover all reals or a final default
                condition `(foo, True)` must be given.
                )r   r   r   r+   r   r0   r&   r'   r   r7   r   rS   r   r   r   unionr|   r   ZUniversalSetr   ra   r   r   r   )r9   r7   rD   Zbyfreer   r   r[   ri   r   rm   lastr   rB   r4   r4   r5   _eval_rewrite_as_ITEx  sJ     
zPiecewise._eval_rewrite_as_ITEc                    s  ddl m  tddgtddgtddgtd d gtd d giG dd dt fddg }d }|D ]B\}}t|kr|	||f qf|t
jkr|d kr|}qf d S qf|d k	r|}|d d d	 D ]D\}}z |}|| d
| |  }W q k
r   Y  d S X q|S d S )Nr   )KroneckerDeltaFTc                   @   s   e Zd ZdS )zHPiecewise._eval_rewrite_as_KroneckerDelta.<locals>.UnrecognizedConditionN)rJ   rK   rL   r4   r4   r4   r5   UnrecognizedCondition  s   r   c                    s   t | tr | j S t | tr,d | j  S t| | j }}|krL|| \}}|rrtfdd|D  ntfdd|D  }|rd| S |S )Nr:   c                    s   g | ]}d  | qS )r:   r4   rA   ri   r/   r4   r5   rE     s     zNPiecewise._eval_rewrite_as_KroneckerDelta.<locals>.rewrite.<locals>.<listcomp>c                    s   g | ]} |qS r4   r4   r   r   r4   r5   rE     s     )r+   r   r7   r   typer   )r3   r1   r7   b1b2r   r   r   r/   rulesr4   r5   r/     s    


0z:Piecewise._eval_rewrite_as_KroneckerDelta.<locals>.rewriterZ   r:   )Z(sympy.functions.special.tensor_functionsr   r   r   r   r   r   	Exceptionr   rR   r   r   )r9   r7   Z
conditionsZ
true_valuevaluer3   resultr   r4   r  r5   _eval_rewrite_as_KroneckerDelta  s@         

z)Piecewise._eval_rewrite_as_KroneckerDelta)Nr   )T)T)F)r   )N)5rJ   rK   rL   rM   nargsis_Piecewiser*   classmethodrT   rf   rI   rp   rr   rt   rv   ry   r~   r   r   r   r   r   r   r   r   r   r   Z_eval_is_finiteZ_eval_is_complexZ_eval_is_evenZ_eval_is_imaginaryZ_eval_is_integerZ_eval_is_irrationalZ_eval_is_negativeZ_eval_is_nonnegativeZ_eval_is_nonpositiveZ_eval_is_nonzeroZ_eval_is_oddZ_eval_is_polarZ_eval_is_positiveZ_eval_is_extended_realZ_eval_is_extended_positiveZ_eval_is_extended_negativeZ_eval_is_extended_nonzeroZ_eval_is_extended_nonpositiveZ_eval_is_extended_nonnegativeZ_eval_is_realZ_eval_is_zeroZ_Piecewise__eval_condr   r   r  __classcell__r4   r4   r   r5   r-   @   sh   >
1
%C
Y 
 '


1-r-   Tc                    s  t | tr| ts| S g }t | ttfr| jD ]z\} t |tsJt|} trXtt  trv 	  t
 dd t |tr| fdd|jD  q0|| f q0n| js| jr| jrt| jdd dd\}}t|d	d }tt|D ] t|  d
krdd |  D }tdd |D  }t|}	g }
t|	D ],|
| jfdd|D  | f qHg }|D ]V}|	t|krq~||	 jdkr|||	 j n|t||	d ddi q~|r|
| j| df |t|
ddi q||   qn| j}ttt|}tdd |D  D ]*}t| \} || j| t  f q>|dkrttdd dd t|D  D }t|d|i}|dkrt|jd
kr|jd jdkr|jd jS tdd | tD rt|S |S )a  
    Takes an expression containing a piecewise function and returns the
    expression in piecewise form. In addition, any ITE conditions are
    rewritten in negation normal form and simplified.

    The final Piecewise is evaluated (default) but if the raw form
    is desired, send ``evaluate=False``; if trivial evaluation is
    desired, send ``evaluate=None`` and duplicate conditions and
    processing of True and False will be handled.

    Examples
    ========

    >>> from sympy import Piecewise, piecewise_fold, S
    >>> from sympy.abc import x
    >>> p = Piecewise((x, x < 1), (1, S(1) <= x))
    >>> piecewise_fold(x*p)
    Piecewise((x**2, x < 1), (x, True))

    See Also
    ========

    Piecewise
    piecewise_exclusive
    Zcnf)formc                    s"   g | ]\}}t |t| fqS r4   )r.   r   )rA   eici)ri   r4   r5   rE     s   z"piecewise_fold.<locals>.<listcomp>c                 S   s   | j S r;   )r  ru   r4   r4   r5   r     r   z piecewise_fold.<locals>.<lambda>Tbinaryc                 S   s   t dd | jD S )Nc                 S   s   g | ]\}}|qS r4   r4   rq   r4   r4   r5   rE     s     z4piecewise_fold.<locals>.<lambda>.<locals>.<listcomp>)tupler7   ru   r4   r4   r5   r     r   r:   c                 S   s   g | ]}t |jqS r4   )r   r7   r   r4   r4   r5   rE     s     c                 S   s   g | ]}d d |D qS )c                 S   s   g | ]
}|j qS r4   r   r   r4   r4   r5   rE      s     z-piecewise_fold.<locals>.<listcomp>.<listcomp>r4   )rA   r   r4   r4   r5   rE     s    c                    s   g | ]}|  j qS r4   )r2   )rA   Zair   r4   r5   rE   %  s     NrO   Fc                 S   s&   g | ]}t |tr|jn|tfgqS r4   )r+   r-   r7   r   r   r4   r4   r5   rE   9  s   c                 S   s   g | ]\}}||fqS r4   r4   )rA   ri   rh   r4   r4   r5   rE   A  s     c                 S   s   i | ]\}}||qS r4   r4   rq   r4   r4   r5   
<dictcomp>A  s     z"piecewise_fold.<locals>.<dictcomp>r   c                 s   s&   | ]}|j D ]}|jtV  qqd S r;   )r7   r2   r,   r-   )rA   r   r   r4   r4   r5   r\   F  s       z!piecewise_fold.<locals>.<genexpr>)!r+   r   r,   r-   r)   r7   r.   r   r   Zto_nnfr    r   rR   Zis_AddZis_Mulr<   r$   r   r   rP   r%   rangerG   r3   r2   r   r(   r`   r   r   itemsr   r   )r2   rO   new_argsrh   r   r7   ZpcZpargscomr   	collectedZremainsrB   foldedrW   r   r4   )ri   r   r5   r.     s~    





,r.   c                 C   s   |\}}| \}}t t|||t t||| }}t ||| }}g }||kr`|||df n ||krz||||f n ||kr||kr|r|d d dkr|d d |df|d< q|||df n |S )a  Return interval B as intervals that are covered by A (keyed
    to k) and all other intervals of B not covered by A keyed to -1.

    The reference point of each interval is the rhs; if the lhs is
    greater than the rhs then an interval of zero width interval will
    result, e.g. (4, 1) is treated like (1, 1).

    Examples
    ========

    >>> from sympy.functions.elementary.piecewise import _clip
    >>> from sympy import Tuple
    >>> A = Tuple(1, 3)
    >>> B = Tuple(2, 4)
    >>> _clip(A, B, 0)
    [(2, 3, 0), (3, 4, -1)]

    Interpretation: interval portion (2, 3) of interval (2, 4) is
    covered by interval (1, 3) and is keyed to 0 as requested;
    interval (3, 4) was not covered by (1, 3) and is keyed to -1.
    rZ   r   )r   r   rR   )ABr   rB   r[   ri   dr   r4   r4   r5   r   K  s    "r   c                 K   s  ddl m} | jd jj}d }t|dkr| ts| }| j	|dd\}}dd }|rg }t
j}	ddlm}
 |D ]\}}}}| j| j}||||}||||}|
||| | }||	 }|sqz|r,|r,|jr|jrt
j}n>|jr||k}n.||	ks|jr||k}nt||k||k}n~|rb||	ksD|jrN||k }nt||k||k }nH|r|jrz||k}n||k}n&||	kr||k }nt||k ||k }|	|O }	|t
jkr|r|	t
jhO }	|t
jkr|r|	t
jhO }	|||f qzt
j|	s|tdf |d krrt| j}tt|D ]6}|| \}}t|trb||f|}||f||< q:|d	d }tt|D ]L}|| \}}t|tr||fd	d
i|}||kr|}||f||< q|d k	r||d	< t| S )Nr   r>   r:   T)r   c                 S   s.   z|  ||dkW S  tk
r(   Y dS X dS )z/return True if c.subs(x, a) is True, else FalseTFN)rl   r0   )ri   rm   rB   r4   r4   r5   include  s    z-piecewise_simplify_arguments.<locals>.include)Intervalrf   F)Zsympy.simplify.simplifyr?   r7   r3   r   rP   r   r   rS   r   r   r   Zsympy.sets.setsr  is_infiniter   r   r   r   rR   r   r   r]   r   r  r+   r   r-   )r2   rD   r?   f1r7   rm   r   Zabe_r  Zcoveredr  rB   r[   rh   r   ri   Zincl_aZincl_bZivZcsetrf   rj   r4   r4   r5   piecewise_simplify_argumentsy  s    










r   c                 C   s  g }t  }| D ]\}}|dd t}t|trg }t|jD ]V\}\}}||krVq@||kr|dkr|r~t|||fg  }n|} qq@|||f q@d}	|gt|trt	|jng  D ]}||krd}	 qq|	rqt|trJg }
|jD ]P}t|t
r2|jj|krqt|ttfr2|j|kr2d} qj|
| q|j|
 }n t|t
rj|jj|krjtj}|| |r|d j|krt||d j}t|ttfrt|}t|||d< qn|d j|krq|t|| q|S )Nc                 S   s   | j S r;   )r{   r   r4   r4   r5   r     r   z/_piecewise_collapse_arguments.<locals>.<lambda>TFrZ   )r   r   r   r+   r-   r   r7   rR   r   r   r   Znegated	canonicalr   r   ZweakrG   r   r   addr2   r   r3   r   r)   )rb   rV   Zcurrent_condr2   r3   Z
unmatchingr   rh   ri   gotZnonredundantZorcondr4   r4   r5   r^     sv     



r^   c                 C   s,   t | jddo*t | jdd p*t| jttfS )NZ	_diff_wrtF)rQ   r   r   r+   r
   r   rh   r4   r4   r5   r   0  s   r   c                 K   s<   t | f|} t| ts| S t| j}t|}t|}t| S r;   )r   r+   r-   r   r7   _piecewise_simplify_eq_and)_piecewise_simplify_equal_to_next_segment)r2   rD   r7   r4   r4   r5   rk   5  s    

rk   c           
      C   s   d}t tt| D ]\}\}}|dk	rt|trLt|jdd dd\}}n t|trd|gg  }}ng  }}|}|}|r|stt|}|D ]0}	t	| dkst
|	r|j|	j }|j|	j }q||kr| | | |d  d || |< q|}q|}q| S )	z
    See if expressions valid for an Equal expression happens to evaluate
    to the same function as in the next piecewise segment, see:
    https://github.com/sympy/sympy/issues/8458
    Nc                 S   s
   t | tS r;   r   r  r4   r4   r5   r   K  r   z;_piecewise_simplify_equal_to_next_segment.<locals>.<lambda>Tr  r   r:   r   )r   r   r   r+   r   r$   r7   r   r   rP   _blessedrl   rG   )
r7   Zprevexprr   r2   r3   eqsotherZ	_prevexprZ_exprrh   r4   r4   r5   r&  @  s0    
 
"r&  c                    s   t | D ]\}\}}t|tr8t|jdd dd\}}n t|trP|gg  }}ng  }}|rtt|}t |D ]X\} t rp|j	 j } fdd||d d D ||d d<  fd	d|D }qpt||  }| | 
||| |< q| S )
z
    Try to simplify conditions and the expression for
    equalities that are part of the condition, e.g.
    Piecewise((n, And(Eq(n,0), Eq(n + m, 0))), (1, True))
    -> Piecewise((0, And(Eq(n, 0), Eq(m, 0))), (1, True))
    c                 S   s
   t | tS r;   r   r  r4   r4   r5   r   s  r   z,_piecewise_simplify_eq_and.<locals>.<lambda>Tr  c                    s   g | ]}|j  j qS r4   rl   r7   rA   r  r$  r4   r5   rE     s     z._piecewise_simplify_eq_and.<locals>.<listcomp>r:   Nc                    s   g | ]}|j  j qS r4   r*  r+  r$  r4   r5   rE     s     )r   r+   r   r$   r7   r   r   r   r'  rl   rG   )r7   r   r2   r3   r(  r)  r   r4   r$  r5   r%  i  s&    
 
*r%  F)skip_nanrd   c                   s8    fdd}|r|  t|S t| tr0|| j S | S dS )ac	  
    Rewrite :class:`Piecewise` with mutually exclusive conditions.

    Explanation
    ===========

    SymPy represents the conditions of a :class:`Piecewise` in an
    "if-elif"-fashion, allowing more than one condition to be simultaneously
    True. The interpretation is that the first condition that is True is the
    case that holds. While this is a useful representation computationally it
    is not how a piecewise formula is typically shown in a mathematical text.
    The :func:`piecewise_exclusive` function can be used to rewrite any
    :class:`Piecewise` with more typical mutually exclusive conditions.

    Note that further manipulation of the resulting :class:`Piecewise`, e.g.
    simplifying it, will most likely make it non-exclusive. Hence, this is
    primarily a function to be used in conjunction with printing the Piecewise
    or if one would like to reorder the expression-condition pairs.

    If it is not possible to determine that all possibilities are covered by
    the different cases of the :class:`Piecewise` then a final
    :class:`~sympy.core.numbers.NaN` case will be included explicitly. This
    can be prevented by passing ``skip_nan=True``.

    Examples
    ========

    >>> from sympy import piecewise_exclusive, Symbol, Piecewise, S
    >>> x = Symbol('x', real=True)
    >>> p = Piecewise((0, x < 0), (S.Half, x <= 0), (1, True))
    >>> piecewise_exclusive(p)
    Piecewise((0, x < 0), (1/2, Eq(x, 0)), (1, x > 0))
    >>> piecewise_exclusive(Piecewise((2, x > 1)))
    Piecewise((2, x > 1), (nan, x <= 1))
    >>> piecewise_exclusive(Piecewise((2, x > 1)), skip_nan=True)
    Piecewise((2, x > 1))

    Parameters
    ==========

    expr: a SymPy expression.
        Any :class:`Piecewise` in the expression will be rewritten.
    skip_nan: ``bool`` (default ``False``)
        If ``skip_nan`` is set to ``True`` then a final
        :class:`~sympy.core.numbers.NaN` case will not be included.
    deep:  ``bool`` (default ``True``)
        If ``deep`` is ``True`` then :func:`piecewise_exclusive` will rewrite
        any :class:`Piecewise` subexpressions in ``expr`` rather than just
        rewriting ``expr`` itself.

    Returns
    =======

    An expression equivalent to ``expr`` but where all :class:`Piecewise` have
    been rewritten with mutually exclusive conditions.

    See Also
    ========

    Piecewise
    piecewise_fold
    c            	         s   t }g }| d d D ]6\}}t|t| }t|| }|||f q| d \}}t|t| }|||f  st|| }|tk	r|tt| f t|ddiS )NrZ   rO   F)	r   r   r   r?   r   rR   r   r]   r-   )	ZpwargsZcumcondrV   Zexpr_iZcond_iZcancondZexpr_nZcond_nZ	cancond_nr,  r4   r5   make_exclusive  s    z+piecewise_exclusive.<locals>.make_exclusiveN)r   r-   r+   r7   )r2   r,  rd   r.  r4   r-  r5   piecewise_exclusive  s    @

r/  N)T)?Z
sympy.corer   r   r   r   r   r   Zsympy.core.basicr   r	   Zsympy.core.numbersr
   r   r   Zsympy.core.parametersr   Zsympy.core.relationalr   r   r   r   r   r   r   Zsympy.core.sortingr   Z(sympy.functions.elementary.miscellaneousr   r   Zsympy.logic.boolalgr   r   r   r   r   r   r   r   r    r!   r"   Zsympy.utilities.iterablesr#   r$   r%   Zsympy.utilities.miscr&   r'   	itertoolsr(   NaNr]   r)   r-   r.   r   r   r^   r'  rk   r&  r%  r/  r4   r4   r4   r5   <module>   s:    $4.       #
o.Y^)