o
    8Va                     @   s  d dl 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 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mZmZ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*m+Z+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 dd Z6dd Z7dd Z8d2ddZ9dd Z:dd Z;ej<ddd Z=ej<fd!d"Z>ej<fd#d$Z?ej<fd%d&Z@G d'd( d(eZAe5eAeAd)d* ZBe5eAed+d* ZBe5eAeAd,d- ZCe5eAed.d- ZCe5eeAd/d- ZCe5eAeAd0d- ZCeAZDd1S )3    )	OrderSloglimitlcm_listimreDummy	Piecewise)AddMulPow)Basic)iterable)
AtomicExprExpr)
expand_mul)
_sympifyitoozoo)is_leis_ltis_geis_gt)_sympify)MinMax)And)IntervalIntersection	FiniteSetUnion
ComplementEmptySet)ImageSetsolve_univariate_inequality)
filldedent)dispatchc           	      C   s   ddl m} ddlm} |tjrQ|}| tD ]}|j	
 d }|jr7|jr7||jdk| }t||}q| tD ]}||jd dk| }t||}q=||| || S )a#  
    Returns the intervals in the given domain for which the function
    is continuous.
    This method is limited by the ability to determine the various
    singularities and discontinuities of the given function.

    Parameters
    ==========

    f : Expr
        The concerned function.
    symbol : Symbol
        The variable for which the intervals are to be determined.
    domain : Interval
        The domain over which the continuity of the symbol has to be checked.

    Examples
    ========

    >>> from sympy import Symbol, S, tan, log, pi, sqrt
    >>> from sympy.sets import Interval
    >>> from sympy.calculus.util import continuous_domain
    >>> x = Symbol('x')
    >>> continuous_domain(1/x, x, S.Reals)
    Union(Interval.open(-oo, 0), Interval.open(0, oo))
    >>> continuous_domain(tan(x), x, Interval(0, pi))
    Union(Interval.Ropen(0, pi/2), Interval.Lopen(pi/2, pi))
    >>> continuous_domain(sqrt(x - 2), x, Interval(-5, 5))
    Interval(2, 5)
    >>> continuous_domain(log(2*x - 1), x, S.Reals)
    Interval.open(1/2, oo)

    Returns
    =======

    Interval
        Union of all intervals where the function is continuous.

    Raises
    ======

    NotImplementedError
        If the method to determine continuity of such a function
        has not yet been developed.

    r   r%   )singularities   )sympy.solvers.inequalitiesr&   Zsympy.calculus.singularitiesr)   Z	is_subsetr   RealsZatomsr   expas_numer_denomZis_evenZ
is_nonzerobaseZas_setr   r   args)	fsymboldomainr&   r)   Zconstrained_intervalZatomdenZ
constraint r5   5/usr/lib/python3/dist-packages/sympy/calculus/util.pycontinuous_domain   s4   /
r7   c              	   C   sh  ddl m} t|trtjS t| |}|tjkrt|  S |durPt|t	r4|j
|j jr3t	d|}nt|trP|jD ]}t|t	rO|j
|j jrOt	d|}q<t| ||}tj}t|t	tfrd|f}nt|trm|j}nttd|D ]}	t|	tr|	D ]}
|
|v r|t| ||
7 }q~qut|	t	r,tj}tj}tj}|	j|	j
df|	j|	jdff}|D ]!\}}}|r|tt| |||7 }||7 }q|t| ||7 }q|| |||	}t|std| t|trtd| ||7 }|D ]}|t| ||7 }qd	\}}|tjur |j
|j
krd
}|j|jkr d
}|t	|j
|j||7 }quttd|S )a  
    Finds the range of a function in a given domain.
    This method is limited by the ability to determine the singularities and
    determine limits.

    Parameters
    ==========

    f : Expr
        The concerned function.
    symbol : Symbol
        The variable for which the range of function is to be determined.
    domain : Interval
        The domain under which the range of the function has to be found.

    Examples
    ========

    >>> from sympy import Symbol, S, exp, log, pi, sqrt, sin, tan
    >>> from sympy.sets import Interval
    >>> from sympy.calculus.util import function_range
    >>> x = Symbol('x')
    >>> function_range(sin(x), x, Interval(0, 2*pi))
    Interval(-1, 1)
    >>> function_range(tan(x), x, Interval(-pi/2, pi/2))
    Interval(-oo, oo)
    >>> function_range(1/x, x, S.Reals)
    Union(Interval.open(-oo, 0), Interval.open(0, oo))
    >>> function_range(exp(x), x, S.Reals)
    Interval.open(0, oo)
    >>> function_range(log(x), x, S.Reals)
    Interval(-oo, oo)
    >>> function_range(sqrt(x), x , Interval(-5, 9))
    Interval(0, 3)

    Returns
    =======

    Interval
        Union of all ranges for all intervals under domain where function is
        continuous.

    Raises
    ======

    NotImplementedError
        If any of the intervals, in the given domain, for which function
        is continuous are not finite or real,
        OR if the critical points of the function on the domain can't be found.
    r   solvesetNzL
                Unable to find range for the given domain.
                +-z%Unable to find critical points for {}z)Infinite number of critical points for {})FFT)sympy.solvers.solvesetr9   
isinstancer#   r   periodicityZeror    expandr   infsupZis_infiniter!   r0   r7   NotImplementedErrorr'   subs	left_open
right_openr   diffr   formatr$   )r1   r2   r3   r9   periodZsub_domZ	intervalsZ	range_intZinterval_iterintervalZ	singletonZvalsZcritical_pointsZcritical_valuesZboundsZis_openZlimit_pointZ	directionZsolutionZcritical_pointrE   rF   r5   r5   r6   function_rangeY   s   3












rK   c                    s4  t |dkr
td| tju rtjS t| tr*| jd }tt| jd g|R  |S t| tr5| }tj	 n
| jd }| jd  t|tsNtdt
||f t |dkrY|d ntd|f fddt trwt fdd	|D  S t trtj} jD ]tfd
d	|D  }t||}q|S dS )ay  
    Finds the domain of the functions in `finite_set` in which the
    `finite_set` is not-empty

    Parameters
    ==========

    finset_intersection : The unevaluated intersection of FiniteSet containing
                        real-valued functions with Union of Sets
    syms : Tuple of symbols
            Symbol for which domain is to be found

    Raises
    ======

    NotImplementedError
        The algorithms to find the non-emptiness of the given FiniteSet are
        not yet implemented.
    ValueError
        The input is not valid.
    RuntimeError
        It is a bug, please report it to the github issue tracker
        (https://github.com/sympy/sympy/issues).

    Examples
    ========

    >>> from sympy import FiniteSet, Interval, not_empty_in, oo
    >>> from sympy.abc import x
    >>> not_empty_in(FiniteSet(x/2).intersect(Interval(0, 1)), x)
    Interval(0, 2)
    >>> not_empty_in(FiniteSet(x, x**2).intersect(Interval(1, 2)), x)
    Union(Interval(1, 2), Interval(-sqrt(2), -1))
    >>> not_empty_in(FiniteSet(x**2/(x + 2)).intersect(Interval(1, oo)), x)
    Union(Interval.Lopen(-2, -1), Interval(2, oo))
    r   z*One or more symbols must be given in syms.r*   z%A FiniteSet must be given, not %s: %sz&more than one variables %s not handledc           
         s   ddl m} |j}|j}||  d  tjd}|jr/|tju r$tj}n|| |k  tjd}n
|| |k tjd}|j	rP|tj
u rEtj}n|| |k tjd}n
|| |k tjd}t||}t||}	|	S )z9 Finds the domain of an expression in any given interval r   r8   r*   r3   )r<   r9   startendr.   r   r,   rF   InfinityrE   NegativeInfinityr   r"   )
exprintrvlr9   Z_startZ_endZ_singularitiesZ_domain1Z_domain2Zexpr_with_singZexpr_domain)symbr5   r6   
elm_domain&  s&   



z not_empty_in.<locals>.elm_domainc                    s   g | ]}| qS r5   r5   .0element)_setsrT   r5   r6   
<listcomp>E  s    z not_empty_in.<locals>.<listcomp>c                    s   g | ]} |qS r5   r5   rU   )rT   rR   r5   r6   rY   J  s    N)len
ValueErrorr   r#   r=   r!   r0   not_empty_inr    r,   typerC   r   )Zfinset_intersectionsymsZelm_in_setsZ
finite_setZ_domainZ_domain_elementr5   )rX   rT   rR   rS   r6   r\      sF   )











r\   Fc           %   
      sD  ddl m} ddlm} ddlm} ddlm} ddlm	}m
}m}	m}
m} ddlm} ddlm} dd	lm} td
dd}|  |} |  fdd}| }d}t| |r[| j| j } || }  | jvrgtjS t| |r}z|  }W n	 ty|   Y nw t| |r| jd }t|||
|	fr||jd }t| }|durt||r||}z|||d W S  ty } z|rt|W Y d}~nd}~ww t| |s| j r| j!tj"krt#tj"t$| j} t%| dkrtt&|  }tt%|  }|dur|durt'||g}| j r@| j!tj"kr@| j\}}|( }|( }|r-|s-t| }n|r9|s9t| }nt)| j }n| j*rf| j+ dd\}}t||sY|tj,ur_t| }nt)|j }n| j-r| + \}}|tjur|t| S t)|j }nt| |r| j\}}| kr|}n{t||rt| }no|. r|| dkr |jvr|||/  }nQt| t0rnJ|du rddlm1}  ||  }!t2|!}"|"dkrt3t4|!D ]*\}#}|"d |# }$| |!|$d  }||kr|| krt| }|dur nq|dur |r|||S |S dS )ab  
    Tests the given function for periodicity in the given symbol.

    Parameters
    ==========

    f : Expr.
        The concerned function.
    symbol : Symbol
        The variable for which the period is to be determined.
    check : Boolean, optional
        The flag to verify whether the value being returned is a period or not.

    Returns
    =======

    period
        The period of the function is returned.
        `None` is returned when the function is aperiodic or has a complex period.
        The value of `0` is returned as the period of a constant function.

    Raises
    ======

    NotImplementedError
        The value of the period computed cannot be verified.


    Notes
    =====

    Currently, we do not support functions with a complex period.
    The period of functions having complex periodic values such
    as `exp`, `sinh` is evaluated to `None`.

    The value returned might not be the "fundamental" period of the given
    function i.e. it may not be the smallest periodic value of the function.

    The verification of the period through the `check` flag is not reliable
    due to internal simplification of the given expression. Hence, it is set
    to `False` by default.

    Examples
    ========
    >>> from sympy import Symbol, sin, cos, tan, exp
    >>> from sympy.calculus.util import periodicity
    >>> x = Symbol('x')
    >>> f = sin(x) + sin(2*x) + sin(3*x)
    >>> periodicity(f, x)
    2*pi
    >>> periodicity(sin(x)*cos(x), x)
    pi
    >>> periodicity(exp(tan(2*x) - 1), x)
    pi/2
    >>> periodicity(sin(4*x)**cos(2*x), x)
    pi
    >>> periodicity(exp(x), x)
    r   )Mod)
Relational)r-   )Abs)TrigonometricFunctionsincoscscsec)simplify)
decompogen)degreexT)realc                    s8   |    | }|| r|S ttd  || |f )z,Return the checked period or raise an error.a  
                The period of the given function cannot be verified.
                When `%s` was replaced with `%s + %s` in `%s`, the result
                was `%s` which was not recognized as being the same as
                the original function.
                So either the period was wrong or the two forms were
                not recognized as being equal.
                Set check=False to obtain the value.)rD   ZequalsrC   r'   )orig_frI   new_fr2   r5   r6   _check  s   
zperiodicity.<locals>._checkN   F)Zas_Addr*   )compogen)5Zsympy.core.modr_   sympy.core.relationalr`   Z&sympy.functions.elementary.exponentialr-   Z$sympy.functions.elementary.complexesra   Z(sympy.functions.elementary.trigonometricrb   rc   rd   re   rf   Zsympy.simplify.simplifyrg   Zsympy.solvers.decompogenrh   Zsympy.polys.polytoolsri   r	   rD   r=   lhsrhsZfree_symbolsr   r?   rI   rC   r0   r>   is_Powr/   ZExp1r   r   r   r   lcimZhas_periodicityZis_MulZas_independentOneZis_AddZis_polynomialrG   r
   rq   rZ   	enumeratereversed)%r1   r2   checkr_   r`   r-   ra   rb   rc   rd   re   rf   rg   rh   ri   Ztempro   rl   rI   argerrZperiod_realZperiod_imagr/   ZexpoZbase_has_symZexpo_has_symcoeffgkanrq   Zg_sZ	num_of_gsindexstart_indexr5   rn   r6   r>   P  s   ;





 












r>   c                 C   s^   g }| D ]}t ||}|du r dS |tjur|| qt|dkr't|S |r-|d S dS )a<  
    Helper for `periodicity` to find the period of a list of simpler
    functions.
    It uses the `lcim` method to find the least common period of
    all the functions.

    Parameters
    ==========

    args : Tuple of Symbol
        All the symbols present in a function.

    symbol : Symbol
        The symbol over which the function is to be evaluated.

    Returns
    =======

    period
        The least common period of the function for all the symbols
        of the function.
        None if for at least one of the symbols the function is aperiodic

    Nr*   r   )r>   r   r?   appendrZ   rv   )r0   r2   Zperiodsr1   rI   r5   r5   r6   rw     s   


rw   c                    s   d}t dd | D r?ttdd | }ttdd |}|d d  t  fd	d|D r= }d
d |D }t|| }|S t dd | D rNt| }|S 	 |S )a  Returns the least common integral multiple of a list of numbers.

    The numbers can be rational or irrational or a mixture of both.
    `None` is returned for incommensurable numbers.

    Parameters
    ==========

    numbers : list
        Numbers (rational and/or irrational) for which lcim is to be found.

    Returns
    =======

    number
        lcim if it exists, otherwise `None` for incommensurable numbers.

    Examples
    ========

    >>> from sympy import S, pi
    >>> from sympy.calculus.util import lcim
    >>> lcim([S(1)/2, S(3)/4, S(5)/6])
    15/2
    >>> lcim([2*pi, 3*pi, pi, pi/2])
    6*pi
    >>> lcim([S(1), 2*pi])
    Nc                 s       | ]}|j V  qd S N)Zis_irrationalrV   numr5   r5   r6   	<genexpr>`      zlcim.<locals>.<genexpr>c                 S      |   S r   )factorr   r5   r5   r6   <lambda>a      zlcim.<locals>.<lambda>c                 S   r   r   )Zas_coeff_Mulr   r5   r5   r6   r   c  r   r   r*   c                 3   s    | ]	\}}| kV  qd S r   r5   rV   r~   r   Ztermr5   r6   r   f  s    c                 S   s   g | ]\}}|qS r5   r5   r   r5   r5   r6   rY   h      zlcim.<locals>.<listcomp>c                 s   r   r   )is_rationalr   r5   r5   r6   r   k  r   )alllistmapr   )ZnumbersresultZfactorized_numsZfactors_numZcommon_termZcoeffsr5   r   r6   rv   B  s&   rv   rL   c                G   sJ   t |dkr
tdt| } |d }| |ddk }t||d|r#dS dS )a  Determines the  convexity of the function passed in the argument.

    Parameters
    ==========

    f : Expr
        The concerned function.
    syms : Tuple of symbols
        The variables with respect to which the convexity is to be determined.
    domain : Interval, optional
        The domain over which the convexity of the function has to be checked.
        If unspecified, S.Reals will be the default domain.

    Returns
    =======

    Boolean
        The method returns `True` if the function is convex otherwise it
        returns `False`.

    Raises
    ======

    NotImplementedError
        The check for the convexity of multivariate functions is not implemented yet.

    Notes
    =====

    To determine concavity of a function pass `-f` as the concerned function.
    To determine logarithmic convexity of a function pass log(f) as
    concerned function.
    To determine logartihmic concavity of a function pass -log(f) as
    concerned function.

    Currently, convexity check of multivariate functions is not handled.

    Examples
    ========

    >>> from sympy import symbols, exp, oo, Interval
    >>> from sympy.calculus.util import is_convex
    >>> x = symbols('x')
    >>> is_convex(exp(x), x)
    True
    >>> is_convex(x**3, x, domain = Interval(-1, oo))
    False

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Convex_function
    .. [2] http://www.ifp.illinois.edu/~angelia/L3_convfunc.pdf
    .. [3] https://en.wikipedia.org/wiki/Logarithmically_convex_function
    .. [4] https://en.wikipedia.org/wiki/Logarithmically_concave_function
    .. [5] https://en.wikipedia.org/wiki/Concave_function

    r*   zMThe check for the convexity of multivariate functions is not implemented yet.r   rp   FT)rZ   rC   r   rG   r&   )r1   r3   r^   varZ	conditionr5   r5   r6   	is_convexs  s   <r   c                 C   sB   ddl m}m} t|trtjS t| ||}||| |||}|S )ax  
    Returns the stationary points of a function (where derivative of the
    function is 0) in the given domain.

    Parameters
    ==========

    f : Expr
        The concerned function.
    symbol : Symbol
        The variable for which the stationary points are to be determined.
    domain : Interval
        The domain over which the stationary points have to be checked.
        If unspecified, S.Reals will be the default domain.

    Returns
    =======

    Set
        A set of stationary points for the function. If there are no
        stationary point, an EmptySet is returned.

    Examples
    ========

    >>> from sympy import Symbol, S, sin, pi, pprint, stationary_points
    >>> from sympy.sets import Interval
    >>> x = Symbol('x')

    >>> stationary_points(1/x, x, S.Reals)
    EmptySet

    >>> pprint(stationary_points(sin(x), x), use_unicode=False)
              pi                              3*pi
    {2*n*pi + -- | n in Integers} U {2*n*pi + ---- | n in Integers}
              2                                2

    >>> stationary_points(sin(x),x, Interval(0, 4*pi))
    {pi/2, 3*pi/2, 5*pi/2, 7*pi/2}

    r   )r9   rG   )sympyr9   rG   r=   r#   r   r7   )r1   r2   r3   r9   rG   setr5   r5   r6   stationary_points  s   *
r   c                 C   B   ddl m} t||rt|trtdt| ||jS td| )a  
    Returns the maximum value of a function in the given domain.

    Parameters
    ==========

    f : Expr
        The concerned function.
    symbol : Symbol
        The variable for maximum value needs to be determined.
    domain : Interval
        The domain over which the maximum have to be checked.
        If unspecified, then Global maximum is returned.

    Returns
    =======

    number
        Maximum value of the function in given domain.

    Examples
    ========

    >>> from sympy import Symbol, S, sin, cos, pi, maximum
    >>> from sympy.sets import Interval
    >>> x = Symbol('x')

    >>> f = -x**2 + 2*x + 5
    >>> maximum(f, x, S.Reals)
    6

    >>> maximum(sin(x), x, Interval(-pi, pi/4))
    sqrt(2)/2

    >>> maximum(sin(x)*cos(x), x)
    1/2

    r   Symbolz+Maximum value not defined for empty domain.%s is not a valid symbol.)r   r   r=   r#   r[   rK   rB   r1   r2   r3   r   r5   r5   r6   maximum     '

r   c                 C   r   )a  
    Returns the minimum value of a function in the given domain.

    Parameters
    ==========

    f : Expr
        The concerned function.
    symbol : Symbol
        The variable for minimum value needs to be determined.
    domain : Interval
        The domain over which the minimum have to be checked.
        If unspecified, then Global minimum is returned.

    Returns
    =======

    number
        Minimum value of the function in the given domain.

    Examples
    ========

    >>> from sympy import Symbol, S, sin, cos, minimum
    >>> from sympy.sets import Interval
    >>> x = Symbol('x')

    >>> f = x**2 + 2*x + 5
    >>> minimum(f, x, S.Reals)
    4

    >>> minimum(sin(x), x, Interval(2, 3))
    sin(3)

    >>> minimum(sin(x)*cos(x), x)
    -1/2

    r   r   z+Minimum value not defined for empty domain.r   )r   r   r=   r#   r[   rK   rA   r   r5   r5   r6   minimum"  r   r   c                   @   s*  e Zd ZdZdZdd ZdZdd Zedd	 Z	ed
d Z
edd Zedd Zededd Zededd ZeZdd Zededd Zededd Zededd ZeZededd Zededd  Zeded!d" Zeded#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-S ).AccumulationBoundsa\  
    # Note AccumulationBounds has an alias: AccumBounds

    AccumulationBounds represent an interval `[a, b]`, which is always closed
    at the ends. Here `a` and `b` can be any value from extended real numbers.

    The intended meaning of AccummulationBounds is to give an approximate
    location of the accumulation points of a real function at a limit point.

    Let `a` and `b` be reals such that a <= b.

    `\left\langle a, b\right\rangle = \{x \in \mathbb{R} \mid a \le x \le b\}`

    `\left\langle -\infty, b\right\rangle = \{x \in \mathbb{R} \mid x \le b\} \cup \{-\infty, \infty\}`

    `\left\langle a, \infty \right\rangle = \{x \in \mathbb{R} \mid a \le x\} \cup \{-\infty, \infty\}`

    `\left\langle -\infty, \infty \right\rangle = \mathbb{R} \cup \{-\infty, \infty\}`

    `oo` and `-oo` are added to the second and third definition respectively,
    since if either `-oo` or `oo` is an argument, then the other one should
    be included (though not as an end point). This is forced, since we have,
    for example, `1/AccumBounds(0, 1) = AccumBounds(1, oo)`, and the limit at
    `0` is not one-sided. As x tends to `0-`, then `1/x -> -oo`, so `-oo`
    should be interpreted as belonging to `AccumBounds(1, oo)` though it need
    not appear explicitly.

    In many cases it suffices to know that the limit set is bounded.
    However, in some other cases more exact information could be useful.
    For example, all accumulation values of cos(x) + 1 are non-negative.
    (AccumBounds(-1, 1) + 1 = AccumBounds(0, 2))

    A AccumulationBounds object is defined to be real AccumulationBounds,
    if its end points are finite reals.

    Let `X`, `Y` be real AccumulationBounds, then their sum, difference,
    product are defined to be the following sets:

    `X + Y = \{ x+y \mid x \in X \cap y \in Y\}`

    `X - Y = \{ x-y \mid x \in X \cap y \in Y\}`

    `X * Y = \{ x*y \mid x \in X \cap y \in Y\}`

    When an AccumBounds is raised to a negative power, if 0 is contained
    between the bounds then an infinite range is returned, otherwise if an
    endpoint is 0 then a semi-infinite range with consistent sign will be returned.

    AccumBounds in expressions behave a lot like Intervals but the
    semantics are not necessarily the same. Division (or exponentiation
    to a negative integer power) could be handled with *intervals* by
    returning a union of the results obtained after splitting the
    bounds between negatives and positives, but that is not done with
    AccumBounds. In addition, bounds are assumed to be independent of
    each other; if the same bound is used in more than one place in an
    expression, the result may not be the supremum or infimum of the
    expression (see below). Finally, when a boundary is ``1``,
    exponentiation to the power of ``oo`` yields ``oo``, neither
    ``1`` nor ``nan``.

    Examples
    ========

    >>> from sympy import AccumBounds, sin, exp, log, pi, E, S, oo
    >>> from sympy.abc import x

    >>> AccumBounds(0, 1) + AccumBounds(1, 2)
    AccumBounds(1, 3)

    >>> AccumBounds(0, 1) - AccumBounds(0, 2)
    AccumBounds(-2, 1)

    >>> AccumBounds(-2, 3)*AccumBounds(-1, 1)
    AccumBounds(-3, 3)

    >>> AccumBounds(1, 2)*AccumBounds(3, 5)
    AccumBounds(3, 10)

    The exponentiation of AccumulationBounds is defined
    as follows:

    If 0 does not belong to `X` or `n > 0` then

    `X^n = \{ x^n \mid x \in X\}`

    >>> AccumBounds(1, 4)**(S(1)/2)
    AccumBounds(1, 2)

    otherwise, an infinite or semi-infinite result is obtained:

    >>> 1/AccumBounds(-1, 1)
    AccumBounds(-oo, oo)
    >>> 1/AccumBounds(0, 2)
    AccumBounds(1/2, oo)
    >>> 1/AccumBounds(-oo, 0)
    AccumBounds(-oo, 0)

    A boundary of 1 will always generate all nonnegatives:

    >>> AccumBounds(1, 2)**oo
    AccumBounds(0, oo)
    >>> AccumBounds(0, 1)**oo
    AccumBounds(0, oo)

    If the exponent is itself an AccumulationBounds or is not an
    integer then unevaluated results will be returned unless the base
    values are positive:

    >>> AccumBounds(2, 3)**AccumBounds(-1, 2)
    AccumBounds(1/3, 9)
    >>> AccumBounds(-2, 3)**AccumBounds(-1, 2)
    AccumBounds(-2, 3)**AccumBounds(-1, 2)

    >>> AccumBounds(-2, -1)**(S(1)/2)
    sqrt(AccumBounds(-2, -1))

    Note: `<a, b>^2` is not same as `<a, b>*<a, b>`

    >>> AccumBounds(-1, 1)**2
    AccumBounds(0, 1)

    >>> AccumBounds(1, 3) < 4
    True

    >>> AccumBounds(1, 3) < -1
    False

    Some elementary functions can also take AccumulationBounds as input.
    A function `f` evaluated for some real AccumulationBounds `<a, b>`
    is defined as `f(\left\langle a, b\right\rangle) = \{ f(x) \mid a \le x \le b \}`

    >>> sin(AccumBounds(pi/6, pi/3))
    AccumBounds(1/2, sqrt(3)/2)

    >>> exp(AccumBounds(0, 1))
    AccumBounds(1, E)

    >>> log(AccumBounds(1, E))
    AccumBounds(0, 1)

    Some symbol in an expression can be substituted for a AccumulationBounds
    object. But it doesn't necessarily evaluate the AccumulationBounds for
    that expression.

    The same expression can be evaluated to different values depending upon
    the form it is used for substitution since each instance of an
    AccumulationBounds is considered independent. For example:

    >>> (x**2 + 2*x + 1).subs(x, AccumBounds(-1, 1))
    AccumBounds(-1, 4)

    >>> ((x + 1)**2).subs(x, AccumBounds(-1, 1))
    AccumBounds(0, 4)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Interval_arithmetic

    .. [2] http://fab.cba.mit.edu/classes/S62.12/docs/Hickey_interval.pdf

    Notes
    =====

    Do not use ``AccumulationBounds`` for floating point interval arithmetic
    calculations, use ``mpmath.iv`` instead.
    Tc                 C   sv   t |}t |}|jr|jstd||kr|S |jr)|jr)|jo'|jo'||k }n|| j}|r4tdt| ||S )Nz*Only real AccumulationBounds are supportedz.Lower limit should be smaller than upper limit)r   is_extended_realr[   	is_numberis_comparableis_extended_negativer   __new__)clsminmaxZbadr5   r5   r6   r     s   
zAccumulationBounds.__new__g      &@c                 C   s   | j jr
| jjrdS d S d S )NT)r   is_realr   selfr5   r5   r6   _eval_is_real  s   z AccumulationBounds._eval_is_realc                 C   
   | j d S )z
        Returns the minimum possible value attained by AccumulationBounds
        object.

        Examples
        ========

        >>> from sympy import AccumBounds
        >>> AccumBounds(1, 3).min
        1

        r   r0   r   r5   r5   r6   r        
zAccumulationBounds.minc                 C   r   )z
        Returns the maximum possible value attained by AccumulationBounds
        object.

        Examples
        ========

        >>> from sympy import AccumBounds
        >>> AccumBounds(1, 3).max
        3

        r*   r   r   r5   r5   r6   r   -  r   zAccumulationBounds.maxc                 C   s   | j | j S )a7  
        Returns the difference of maximum possible value attained by
        AccumulationBounds object and minimum possible value attained
        by AccumulationBounds object.

        Examples
        ========

        >>> from sympy import AccumBounds
        >>> AccumBounds(1, 3).delta
        2

        )r   r   r   r5   r5   r6   delta=  s   zAccumulationBounds.deltac                 C   s   | j | j d S )a/  
        Returns the mean of maximum possible value attained by
        AccumulationBounds object and minimum possible value
        attained by AccumulationBounds object.

        Examples
        ========

        >>> from sympy import AccumBounds
        >>> AccumBounds(1, 3).mid
        2

        rp   )r   r   r   r5   r5   r6   midN  s   zAccumulationBounds.midotherc                 C   s
   |  |S r   )__pow__r   r   r5   r5   r6   _eval_power_  s   
zAccumulationBounds._eval_powerc                 C   s   t |tr{t |trtt| j|jt| j|jS |tju r$| jtju s/|tju r5| jtju r5tt	 t	S |j
rt| jtju rJ| jtju rJtt	 t	S | jtju rYtt	 | j| S | jtju rgt| j| t	S tt| j|t| j|S t| |ddS tS NFZevaluate)r=   r   AccumBoundsr   r   r   r   rO   rP   r   r   NotImplementedr   r5   r5   r6   __add__c  s,   


zAccumulationBounds.__add__c                 C   s   t | j | j S r   )r   r   r   r   r5   r5   r6   __neg__{  s   zAccumulationBounds.__neg__c                 C   s  t |trt |trtt| j|j t| j|j S |tju r&| jtju s1|tju r7| jtju r7tt	 t	S |j
rx| jtju rL| jtju rLtt	 t	S | jtju r[tt	 | j| S | jtju rit| j| t	S tt| j| t| j| S t| | ddS tS r   )r=   r   r   r   r   r   r   rP   rO   r   r   r   r   r5   r5   r6   __sub__~  s2   


zAccumulationBounds.__sub__c                 C   s   |   | S r   )r   r   r5   r5   r6   __rsub__  s   zAccumulationBounds.__rsub__c                 C   s  | j t tfkr
| S t|trt|trB|j t tfkr|S t }| j D ]}|| }|j p/|fD ]}|| q0q$tt| t| S |t	j
u rZ| jjrPtdtS | jjrZtt dS |t	ju rr| jjritt dS | jjrrtdtS |jr|jr| jt	j
u rtdtS | jt	ju rtt dS t	jS |jrtt| j|t| j|S |jrtt| j|t| j|S t|tr|S t| |ddS tS )Nr   Fr   )r0   r   r=   r   r   r   addr   r   r   rO   r   is_zeror   rP   r   r?   is_extended_positiver   r   r   r   )r   r   vivir5   r5   r6   __mul__  sX   












zAccumulationBounds.__mul__c                 C   sX  t |tr*t |tr|jjs|jjr | td|j d|j  S | jjrQ| jjrQ|jjrQ|jjrQ| jj	r=|jj	r=tdt
S | jj	rK|jj	rKtt
 dS tt
 t
S | jjr|jjrp|jj	rft| j|j t
S |jjrptt
 t
S |jj	r|jjrtt
 | j|j S | jjr|jjr|jj	rtt
 | j|j S |jjrtt
 t
S |jj	r|jjrt| j|j t
S n^|jr|tju s|tju r| tt
 t
krtt
 t
S | jtju rttd|td|S | jtju rttd| td| S |jrt| j| | j| S |jrt| j| | j| S d| tju r#t| d| ddS t| d| S tS )Nr*   r   Fr   )r=   r   r   r   Zis_positiver   Zis_negativeis_extended_nonpositiveis_extended_nonnegativer   r   r   r   r   r   rO   rP   r   r   ZComplexInfinityr   r   r   r5   r5   r6   __truediv__  s^   

zAccumulationBounds.__truediv__c                 C   s   t |tr~|jru|jrtjS | jjr^| jj	r^| jjr7|j
r(tt|d| j tS |jr7tt t|d| j S | jjrX|j
rJtt t|d| j S |jrXtt|d| j tS tt tS tt|| j || j t|| j || j S t|d|  ddS tS )Nr*   Fr   )r=   r   r   r   r   r?   r   r   r   r   r   r   r   r   r   r   r   r   r   r5   r5   r6   __rtruediv__  s*   
zAccumulationBounds.__rtruediv__c                    sb  t |tr|tju rRjjr$jdk rtjS jdkrtjS tdt	S jj
r:jdkr0tjS jdk r7tS tjS jdkrLjdk rGtjS tdt	S tt	 t	S |tju r]d t	 S jj jrwjjrw|jrwj| j| S |jr}tjS |js|jr&jjrttj| j| tj| j| S jj
rttj| j| tj| j| S |d dkr|j
rjjrtj| t	S jjrtj| t	S tdt	S ttjtj| j| S |d dkr&|j
rjjrtj| t	S jjrtt	 j| S tt	 t	S tj| j| S |js.|jrbjjs<|jrbjjrb| \} |tju rTt fddjD  S  tjurb| d   S t |trjjswjjr|jjrfdd|jD }tdd	 |D sd
d |D }zt|t|W S  ty   Y nw t|ddS tS )Nr*   r   rp   c                    s   g | ]}|d    qS )r*   r5   rV   r   )r4   r5   r6   rY   g  s    z.AccumulationBounds.__pow__.<locals>.<listcomp>c                       g | ]} | qS r5   r5   r   r   r5   r6   rY   p  r   c                 s   r   r   )ru   r   r5   r5   r6   r   q  r   z-AccumulationBounds.__pow__.<locals>.<genexpr>c                 S   s"   g | ]}|j p	|fD ]}|q
qS r5   r   )rV   r   jr5   r5   r6   rY   r  s   " Fr   )r=   r   r   rO   r   r   r   r?   r   r   r   r   ZNaNrP   Zis_nonnegativefuncr   rx   Z
is_Integer
is_integerr   r   r   r   r   r.   r0   any	TypeErrorr   r   )r   r   r   pr   r5   )r4   r   r6   r     s   













zAccumulationBounds.__pow__c                    s    j rJ jrJ| j| j jrJ tju rtjS  jr6 fdd| jD \}}t|||kr0||}}| ||S  j	rJ| jj	rC| ddS | jjrJtj
S t | ddS )Nc                    r   r5   r5   r   r   r5   r6   rY     r   z/AccumulationBounds.__rpow__.<locals>.<listcomp>r   r*   Fr   )r   r   r   r   r   r   rx   r0   r   r   r?   r   )r   r   r   br5   r   r6   __rpow__|  s    

zAccumulationBounds.__rpow__c                 C   s6   | j jr|  S | jjrttjtt| j| j S | S r   )	r   r   r   r   r   r   r?   r   absr   r5   r5   r6   __abs__  s
   zAccumulationBounds.__abs__c                 C   sf   t |}|tju s|tju r| jtju s| jtju rdS dS t| j|k| j|k}|dvr1td|S )a  
        Returns True if other is contained in self, where other
        belongs to extended real numbers, False if not contained,
        otherwise TypeError is raised.

        Examples
        ========

        >>> from sympy import AccumBounds, oo
        >>> 1 in AccumBounds(-1, 3)
        True

        -oo and oo go together as limits (in AccumulationBounds).

        >>> -oo in AccumBounds(1, oo)
        True

        >>> oo in AccumBounds(-oo, 0)
        True

        TF)TFzinput failed to evaluate)r   r   rO   rP   r   r   r   r   )r   r   Zrvr5   r5   r6   __contains__  s   zAccumulationBounds.__contains__c                 C   s   t |ttfstdt |tr$tj}|D ]}|| v r!|t| }q|S | j|jk s0| j|jkr3tjS | j|jkrN| j|jkrFt|j| jS | j|jkrN|S |j| jkri|j| jk rat| j|jS |j| jkrk| S dS dS )a  
        Returns the intersection of 'self' and 'other'.
        Here other can be an instance of FiniteSet or AccumulationBounds.

        Parameters
        ==========

        other: AccumulationBounds
             Another AccumulationBounds object with which the intersection
             has to be computed.

        Returns
        =======

        AccumulationBounds
            Intersection of 'self' and 'other'.

        Examples
        ========

        >>> from sympy import AccumBounds, FiniteSet
        >>> AccumBounds(1, 3).intersection(AccumBounds(2, 4))
        AccumBounds(2, 3)

        >>> AccumBounds(1, 3).intersection(AccumBounds(4, 6))
        EmptySet

        >>> AccumBounds(1, 4).intersection(FiniteSet(1, 2, 5))
        {1, 2}

        4Input must be AccumulationBounds or FiniteSet objectN)r=   r   r    r   r   r#   r   r   )r   r   Zfin_setr   r5   r5   r6   intersection  s2    
zAccumulationBounds.intersectionc                 C   sv   t |ts	td| j|jkr | j|jkr t| jt| j|jS |j| jkr7|j| jkr9t|jt| j|jS d S d S )Nr   )r=   r   r   r   r   r   r   r5   r5   r6   union  s   
zAccumulationBounds.unionN)__name__
__module____qualname____doc__r   r   Z_op_priorityr   propertyr   r   r   r   r   r   r   r   __radd__r   r   r   r   __rmul__r   r   r   r   r   r   r   r   r5   r5   r5   r6   r   T  sP     )








+
;

^
	":r   c                 C   (   t | j|jr	dS t| j|jrdS d S NTF)r   r   r   r   rs   rt   r5   r5   r6   _eval_is_le  
   r   c                 C   H   |j stdt||f |jr t| j|rdS t| j|r"dS dS dS )aL  
    Returns True if range of values attained by `self` AccumulationBounds
    object is greater than the range of values attained by `other`,
    where other may be any value of type AccumulationBounds object or
    extended real number value, False if `other` satisfies
    the same property, else an unevaluated Relational.

    Examples
    ========

    >>> from sympy import AccumBounds, oo
    >>> AccumBounds(1, 3) > AccumBounds(4, oo)
    False
    >>> AccumBounds(1, 4) > AccumBounds(3, 4)
    AccumBounds(1, 4) > AccumBounds(3, 4)
    >>> AccumBounds(1, oo) > -1
    True

    Invalid comparison of %s %sTFNr   r   r]   r   r   r   r   r   r   r5   r5   r6   r     s   
c                 C   r   r   r   r   r   r   r   r5   r5   r6   _eval_is_ge+  r   r   c                 C   r   )aF  
    Returns True if range of values attained by `lhs` AccumulationBounds
    object is less that the range of values attained by `rhs`, where
    other may be any value of type AccumulationBounds object or extended
    real number value, False if `rhs` satisfies the same
    property, else an unevaluated Relational.

    Examples
    ========

    >>> from sympy import AccumBounds, oo
    >>> AccumBounds(1, 3) >= AccumBounds(4, oo)
    False
    >>> AccumBounds(1, 4) >= AccumBounds(3, 4)
    AccumBounds(1, 4) >= AccumBounds(3, 4)
    >>> AccumBounds(1, oo) >= 1
    True
    r   TFN)r   r   r]   r   r   r   r   r   r   r5   r5   r6   r   2  s   
c                 C   sH   | j stdt| | f | jr t|j| rdS t|j| r"dS d S d S )Nr   TFr   r   r5   r5   r6   r   R  s   
c                 C   r   r   r   r   r5   r5   r6   r   _  r   N)F)Er   r   r   r   r   r   r   r   r	   r
   Z
sympy.corer   r   r   Zsympy.core.basicr   Zsympy.core.compatibilityr   Zsympy.core.exprr   r   Zsympy.core.functionr   Zsympy.core.numbersr   r   r   rr   r   r   r   r   Zsympy.core.sympifyr   Z(sympy.functions.elementary.miscellaneousr   r   Zsympy.logic.boolalgr   Zsympy.sets.setsr   r   r    r!   r"   r#   Zsympy.sets.fancysetsr$   r+   r&   Zsympy.utilitiesr'   Zsympy.multipledispatchr(   r7   rK   r\   r>   rw   rv   r,   r   r   r   r   r   r   r   r   r5   r5   r5   r6   <module>   s^   ,  F 	
o J)1H522     4





