
~9\c           @   s  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 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 l 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) d  d l* m+ Z+ d  d l, m- Z- d  d l. m/ Z/ d  d l0 m1 Z1 d   Z2 d   Z3 d   Z4 e5 d  Z6 d   Z7 d   Z8 d   Z9 d e f d     YZ: e: Z; d S(   i(   t   Ordert   St   logt   limitt   lcm_listt   pit   Abst   imt   ret   Symbolt   Dummy(   t   Basic(   t   Addt   Mult   Pow(   t   And(   t
   AtomicExprt   Expr(   t
   _sympifyitt   oo(   t   _sympify(   t   Intervalt   Intersectiont	   FiniteSett   Uniont
   Complementt   EmptySet(   t   ConditionSet(   t   Mint   Max(   t
   filldedent(   t   denom(   t   together(   t   iterable(   t   solve_univariate_inequalityc         C   sA  d d l  m } d d l m } m } | j t j  r| } xn |  j t	  D]] } | | |  \ } }	 | rN |	 d k rN | | j
 d k |  j   }
 t |
 |  } qN qN WxH |  j t  D]7 } | | j d d k |  j   }
 t |
 |  } q W| } n  y |  j t  rJ| d |  | |  | t t |    | |  } n x |  j t	  D]c } | | |  \ } }	 | rZ|	 d k rZ| d |  | |  | t t |    | |  } PqZqZWt | d |  |  |  | t t |    | |  } Wn< t k
 r8d d l } t d  d | j   d f  n X| | S(	   s"  
    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.

    i(   R"   (   t   solvesett   _has_rational_poweri   i    i   NsX   Methods for determining the continuous domains of this function have not been developed.(   t   sympy.solvers.inequalitiesR"   t   sympy.solvers.solvesetR#   R$   t	   is_subsetR   t   Realst   atomsR   t   baset   as_setR   R   t   argst   hasR   R   R    t   NotImplementedErrort   syst   Nonet   exc_info(   t   ft   symbolt   domainR"   R#   R$   t   constrained_intervalt   atomt	   predicatet   denomint
   constraintt   singsR/   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   continuous_domain   sF    .	"#	c      	   C   s{  d d l  m } t | t  r& t j St |  |  } | t j k rT t |  j    S| d k	 r t | t
  r | j | j j r t
 d |  } q q t | t  r xH | j D]: } t | t
  r | j | j j r t
 d |  } q q Wq n  t |  | |  } t j } t | t
 t f  r0| f } n- t | t  rK| j } n t t d    x| D]}	 t |	 t  rx|	 D]1 }
 |
 | k r| t |  j | |
   7} qqWqdt |	 t
  rat j } t j } t j } |	 j |	 j d f |	 j |	 j d f f } xe | D]] \ } } } | rT| t t |  | | |   7} | | 7} q| t |  j | |   7} qW| |  j |  | |	  } t |  st d j |     n  | | 7} x* | D]" } | t |  j | |   7} qWt t } } | t j k	 r?| j | j k r!t } n  | j | j k r?t } q?n  | t
 | j | j | |  7} qdt t d    qdW| S(	   s  
    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.
    i(   R#   i    sL   
                Unable to find range for the given domain.
                t   +t   -s%   Unable to find critical points for {}N(   R&   R#   t
   isinstanceR   R   t   periodicityt   ZeroR   t   expandR0   R   t   inft   supt   is_infiniteR   R,   R;   R.   R   t   subst	   left_opent
   right_openR   t   diffR!   t   formatt   Falset   True(   R2   R3   R4   R#   t   periodt   sub_domt	   intervalst	   range_intt   interval_itert   intervalt	   singletont   valst   critical_pointst   critical_valuest   boundst   is_opent   limit_pointt	   directiont   solutiont   critical_pointRF   RG   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   function_rangen   sn    1	&			 
 	"c   
         s  t  |  d k r! t d   n  |  j r1 t   St |  t  rj |  j d } t t |  j d |  |  St |  t  r |  } t	 j
 } n |  j d } |  j d } t | t  s t d t |  | f   n  t  |  d k r | d   n t d | f     f d   } t | t  rIt g  | D] } | | |  ^ q-  St | t  rt	 j } xH | j D]= } t g  | D] } | | |  ^ q{  }	 t | |	  } qkW| Sd S(   sy  
    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(-sqrt(2), -1), Interval(1, 2))
    >>> not_empty_in(FiniteSet(x**2/(x + 2)).intersect(Interval(1, oo)), x)
    Union(Interval.Lopen(-2, -1), Interval(2, oo))
    i    s*   One or more symbols must be given in syms.i   s%   A FiniteSet must be given, not %s: %ss&   more than one variables %s not handledc   
         s,  d d l  m } | j } | j } | |  j   d   d t j } | j r | t j k rh t j } q | |  | k    d t j } n | |  | k   d t j } | j	 r | t j
 k r t j } q
| |  | k   d t j } n | |  | k   d t j } t | |  } t | |  }	 |	 S(   s9    Finds the domain of an expression in any given interval i(   R#   i   R4   (   R&   R#   t   startt   endt   as_numer_denomR   R(   RG   t   InfinityRF   t   NegativeInfinityR   R   (
   t   exprt   intrvlR#   t   _startt   _endt   _singularitiest   _domain1t   _domain2t   expr_with_singt   expr_domain(   t   symb(    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt
   elm_domain5  s$    			!	!N(   t   lent
   ValueErrort   is_EmptySetR   R>   R   R,   t   not_empty_inR   R   R(   t   typeR.   R   (
   t   finset_intersectiont   symst   elm_in_setst
   finite_sett   _setsRl   t   elementt   _domainRc   t   _domain_element(    (   Rk   s2   lib/python2.7/site-packages/sympy/calculus/util.pyRp      s<    )	&	"c   '         s  d d l  m } 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 } m } t d d t } |  j   |  }  |     f d   } |  } d } t |  |  r|  j |  j }  n  | |   }    |  j k r7t j  St |  |  rpy |  j!    } Wqpt" k
 rlqpXn  t |  |  r6|  j# d } t | | | |
 f  r|	 | j# d  } n  t$ |    } | d k	 r6t | |	  r6| |  } y | | | d  SWq3t" k
 r/} | r0t" |   q0q3Xq6n  t |  |  rt% |   d k rt$ t& |      } t$ t% |      } | d k	 r| d k	 rt' | | g  } qqn  |  j( r=|  j# \ } } | j)    } | j)    } | r	| r	t$ |    } qb| r(| r(t$ |    } qbt* |  j#    } n%|  j+ r|  j,   d t- \ } } t | |  s| t j. k	 rt$ |    } qbt* | j#    } n|  j/ r|  j,    \ } } | t j  k	 rt$ |    St* | j#    } nmt |  |  r|  j# \ }  }! |    k r(|! } qbt |  |  rIt$ |     } qb|  j0    rb| |     d k rb  |! j k rb| |! |  j     } qbn | d k rbd d l m1 }" | |     }# t2 |#  }$ |$ d k rbx~ t3 t4 |#   D]g \ }% } |$ d |% }& |" |# |&    } | | k r| |  k rt$ |    } | d k	 rXPqXqqWqbn  | d k	 r| r| | |  S| Sd S(   sb  
    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)
    i(   RH   (   t   Mod(   t
   Relational(   t   exp(   R   (   t   TrigonometricFunctiont   sint   cost   csct   sec(   t   simplify(   t
   decompogen(   t   degreeR   t   xt   realc            sR   |  j      |  } | j |   r) | St t d     | |  | f    d S(   s,   Return the checked period or raise an error.s  
                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.N(   RE   t   equalsR.   R   (   t   orig_fRL   t   new_f(   R3   (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   _check  s    i    i   t   as_Addi   (   t   compogenN(5   t   sympy.core.functionRH   t   sympy.core.modRz   t   sympy.core.relationalR{   t&   sympy.functions.elementary.exponentialR|   t$   sympy.functions.elementary.complexesR   t(   sympy.functions.elementary.trigonometricR}   R~   R   R   R   t   sympy.simplify.simplifyR   t   sympy.solvers.decompogenR   t   sympy.polys.polytoolsR   R   R
   RK   RE   R0   R>   t   lhst   rhst   free_symbolsR   R@   RL   R.   R,   R?   R   R   t   lcimt   is_PowR-   t   _periodicityt   is_Mult   as_independentRJ   t   Onet   is_Addt   is_polynomialR   Rm   t	   enumeratet   reversed('   R2   R3   t   checkRH   Rz   R{   R|   R   R}   R~   R   R   R   R   R   R   R   t   tempR   R   RL   t   argt   errt   period_realt   period_imagR*   t   expot   base_has_symt   expo_has_symt   coefft   gt   kt   at   nR   t   g_st	   num_of_gst   indext   start_index(    (   R3   s2   lib/python2.7/site-packages/sympy/calculus/util.pyR?   _  s    ;(				$c         C   sy   g  } xL |  D]D } t  | |  } | d k r2 d S| t j k	 r | j |  q q Wt |  d k rq t |  S| d S(   s<  
    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

    i   i    N(   R?   R0   R   R@   t   appendRm   R   (   R,   R3   t   periodsR2   RL   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR   %  s    
c            s   d } t d   |  D  r t t d   |    } t t d   |   } | d d   t   f d   | D  r   } g  | D] \ } } | ^ q } t |  | } q n% t d   |  D  r t |   } n  | S(	   s  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])
    c         s   s   |  ] } | j  Vq d  S(   N(   t   is_irrational(   t   .0t   num(    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pys	   <genexpr>k  s    c         S   s
   |  j    S(   N(   t   factor(   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   <lambda>l  t    c         S   s
   |  j    S(   N(   t   as_coeff_Mul(   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR   n  R   i    i   c         3   s!   |  ] \ } } |   k Vq d  S(   N(    (   R   R   R   (   t   term(    s2   lib/python2.7/site-packages/sympy/calculus/util.pys	   <genexpr>q  s    c         s   s   |  ] } | j  Vq d  S(   N(   t   is_rational(   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pys	   <genexpr>v  s    N(   R0   t   allt   listt   mapR   (   t   numberst   resultt   factorized_numst   factors_numt   common_termR   R   t   coeffs(    (   R   s2   lib/python2.7/site-packages/sympy/calculus/util.pyR   M  s    	c         O   s   t  |  d k r! t d   n  t |   }  | j d t j  } | d } |  j | d  d k  } t | | t |  r} t St	 S(   s  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

    i   sM   The check for the convexity of multivariate functions is not implemented yet.R4   i    i   (
   Rm   R.   R   t   getR   R(   RH   R"   RJ   RK   (   R2   Rs   t   kwargsR4   t   vart	   condition(    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt	   is_convex~  s    <
t   AccumulationBoundsc           B   s  e  Z d  Z e Z d   Z d Z e d    Z e d    Z	 e d    Z
 e d    Z e d e  d    Z e d e  d	    Z e Z d
   Z e d e  d    Z e d e  d    Z e d e  d    Z e Z e d e  d    Z e Z e d e  d    Z e Z e d e  d    Z d   Z d   Z d   Z d   Z d   Z d   Z  d   Z! d   Z" RS(   s  
    # 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\}`

    There is, however, no consensus on Interval division.

    `X / Y = \{ z \mid \exists x \in X, y \in Y \mid y \neq 0, z = x/y\}`

    Note: According to this definition the quotient of two AccumulationBounds
    may not be a AccumulationBounds object but rather a union of
    AccumulationBounds.

    Note
    ====

    The main focus in the interval arithmetic is on the simplest way to
    calculate upper and lower endpoints for the range of values of a
    function in one or more variables. These barriers are not necessarily
    the supremum or infimum, since the precise calculation of those values
    can be difficult or impossible.

    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\}`

    otherwise

    `X^n = \{ x^n \mid x \neq 0, x \in X\} \cup \{-\infty, \infty\}`

    Here for fractional `n`, the part of `X` resulting in a complex
    AccumulationBounds object is neglected.

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

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

    >>> AccumBounds(-1, oo)**(-1)
    AccumBounds(-oo, oo)

    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.

    Same expression can be evaluated to different values depending upon
    the form it is used for substitution. 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.
    c         C   s   t  |  } t  |  } t j t j g } | j p< | | k sV | j pR | | k re t d   n  | j r | j r | | k  r t d   q n  | | k r | St j |  | |  S(   Ns*   Only real AccumulationBounds are supporteds.   Lower limit should be smaller than upper limit(	   R   R   R`   Ra   t   is_realRn   t   is_comparableR   t   __new__(   t   clst   mint   maxt   inftys(    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR   b  s    g      &@c         C   s   |  j  d S(   s   
        Returns the minimum possible value attained by AccumulationBounds
        object.

        Examples
        ========

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

        i    (   R,   (   t   self(    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR   {  s    c         C   s   |  j  d S(   s   
        Returns the maximum possible value attained by AccumulationBounds
        object.

        Examples
        ========

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

        i   (   R,   (   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR     s    c         C   s   |  j  |  j S(   s7  
        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   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   delta  s    c         C   s   |  j  |  j d S(   s/  
        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

        i   (   R   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   mid  s    t   otherc         C   s   |  j  |  S(   N(   t   __pow__(   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   _eval_power  s    c         C   s   t  | t  r t  | t  rI t t |  j | j  t |  j | j   S| t j k rj |  j t j k s | t j k r |  j t j k r t t	 t	  S| j
 r t t |  j |  t |  j |   St |  | d t St S(   Nt   evaluate(   R>   R   t   AccumBoundsR   R   R   R   R`   Ra   R   R   RJ   t   NotImplemented(   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   __add__  s    !!	%c         C   s   t  |  j |  j  S(   N(   R   R   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   __neg__  s    c         C   s   t  | t  r t  | t  rK t t |  j | j  t |  j | j   S| t j k rl |  j t j k s | t j k r |  j t j k r t t	 t	  S| j
 r t t |  j |  t |  j |   St |  | d t St S(   NR   (   R>   R   R   R   R   R   R   Ra   R`   R   R   RJ   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   __sub__  s    !!	c         C   s   |  j    | S(   N(   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   __rsub__  s    c      	   C   sP  t  | t  rLt  | t  r t t t |  j | j  t |  j | j  t |  j | j  t |  j | j   t t |  j | j  t |  j | j  t |  j | j  t |  j | j    S| t j	 k r|  j j
 r t d t  S|  j j
 rt t d  Sn  | t j k rK|  j j
 r/t t d  S|  j j
 rKt d t  Sn  | j r&| j
 r|  t t t  k rt t t  S|  j t j	 k rt d t  S|  j t j k rt t d  St j S| j rt t |  j |  t |  j |   S| j r&t t |  j |  t |  j |   Sn  t  | t  r9| St |  | d t St S(   Ni    R   (   R>   R   R   R   R   R   R   R   R   R`   t   is_zeroR   Ra   R   R@   t   is_positivet   is_negativeR    RJ   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   __mul__  sR    				c         C   s  t  | t  rt  | t  rt j | k rL |  t d | j d | j  St j |  k r t j | k r |  j j r | j j r t d t  S|  j j r | j j r t t d  St t t  S|  j j	 rN| j j	 r| j j r t |  j | j t  S| j j
 rt t t  Sn  | j j rN| j j
 rNt t |  j | j  Sn  |  j j
 r| j j	 r| j j rt t |  j | j  S| j j
 rt t t  Sn  | j j r| j j
 rt |  j | j t  Sqn | j r| t j k s| t j k r|  t t t  k r't t t  S|  j t j k rXt t d |  t d |   S|  j t j k rt t d |  t d |   Sn  | j
 rt |  j | |  j |  S| j	 rt |  j | |  j |  Sn  t |  d | d t St S(   Ni   i    R   (   R>   R   R   R   R@   R   R   R   R   R   R   R   R`   Ra   R   R   R   RJ   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   __div__  sP    	$		c         C   sf  t  | t  r^| j rG| j r( t j St j |  k r	|  j t j k r | j ro t t	 | d |  j
  t  S| j r t t t	 | d |  j
   Sn  |  j
 t j k r | j r t t t	 | d |  j   S| j r t t	 | d |  j  t  Sn  t t t  St t | |  j | |  j
  t | |  j | |  j
   Sn  t	 | d |  d t St Sd  S(   Ni   R   (   R>   R   R   R   R   R@   R   R   R   R   R   R   R   R   R   RJ   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   __rdiv__R  s(    				!		 !c         C   s  d d l  m } t | t  r| t j k r|  j j rs |  j d k  rP t j	 S|  j d k rf t j St
 d t  S|  j j r |  j d k r t j	 S|  j d k  r t t t  St
 t t  S|  j d k r |  j d k  r t j	 St
 d t  St
 t t  Sn  | t j k rd |  t S| j r| j r| j r@t j S| j r|  j j rt
 t |  j | |  j |  t |  j | |  j |   S|  j j rt
 t |  j | |  j |  t |  j | |  j |   S| d d k rd| j r=|  j j rt
 |  j | t  S|  j j r0t
 |  j | t  St
 d t  St
 t j	 t |  j | |  j |   S| j r|  j j rt
 |  j | t  S|  j j rt
 t |  j |  St
 t t  St
 |  j | |  j |  Sn  | j   \ } } | t d  k rm| d d k rHt j	 |  k rH|  j j rEt
 d | |  j |   SqHn  t
 | |  j |  | |  j |   S|  | } | d | St |  | d t St S(   Ni(   t	   real_rooti   i    i   R   (   t(   sympy.functions.elementary.miscellaneousR   R>   R   R   R`   R   t   is_nonnegativeR   R@   R   R   R   R   Ra   R   t	   is_numberR   R   t
   is_IntegerR   R   R   R_   R   RJ   R   (   R   R   R   R   t   dent   num_pow(    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR   m  s|    			$	
c         C   sO   |  j  j r |  j   S|  j j rG t t j t t |  j  |  j    S|  Sd  S(   N(	   R   R   R   R   R   R   R@   R   t   abs(   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   __abs__  s
    
%c         C   s   t  |  } t | t  rJ |  j | j k  r1 t S|  j | j k r t Snx | j pn | t j	 k pn | t j
 k s t d t |  | f   n2 | j r |  j | k  r t S|  j | k r t Sn  t t |   j |  S(   s  
        Returns True if range of values attained by `self` AccumulationBounds
        object is less 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)
        True
        >>> AccumBounds(1, 4) < AccumBounds(3, 4)
        AccumBounds(1, 4) < AccumBounds(3, 4)
        >>> AccumBounds(1, oo) < -1
        False

        s   Invalid comparison of %s %s(   R   R>   R   R   R   RK   RJ   R   R   R`   Ra   t	   TypeErrorRq   R   t   superR   t   __lt__(   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR     s"    	c         C   s   t  |  } t | t  rJ |  j | j k r1 t S|  j | j k r t Snx | j pn | t j	 k pn | t j
 k s t d t |  | f   n2 | j r |  j | k r t S|  j | k r t Sn  t t |   j |  S(   s  
        Returns True if range of values attained by `self` AccumulationBounds
        object is less than or equal to 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)
        True
        >>> AccumBounds(1, 4) <= AccumBounds(3, 4)
        AccumBounds(1, 4) <= AccumBounds(3, 4)
        >>> AccumBounds(1, 3) <= 0
        False

        s   Invalid comparison of %s %s(   R   R>   R   R   R   RK   RJ   R   R   R`   Ra   R   Rq   R   R   R   t   __le__(   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR     s"    	c         C   s   t  |  } t | t  rJ |  j | j k r1 t S|  j | j k r t Snx | j pn | t j	 k pn | t j
 k s t d t |  | f   n2 | j r |  j | k r t S|  j | k r t Sn  t t |   j |  S(   s  
        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

        s   Invalid comparison of %s %s(   R   R>   R   R   R   RK   RJ   R   R   R`   Ra   R   Rq   R   R   R   t   __gt__(   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR   
  s"    	c         C   s   t  |  } t | t  rJ |  j | j k r1 t S|  j | j k  r t Snx | j pn | t j	 k pn | t j
 k s t d t |  | f   n2 | j r |  j | k r t S|  j | k  r t Sn  t t |   j |  S(   s  
        Returns True if range of values attained by `self` AccumulationBounds
        object is less that 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

        s   Invalid comparison of %s %s(   R   R>   R   R   R   RK   RJ   R   R   R`   Ra   R   Rq   R   R   R   t   __ge__(   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR   0  s"    	c         C   s   t  |  } | t j k s* | t j k rV |  j t j k sN |  j t j k rR t St St |  j | k |  j | k  } | t t f k r t	 d   n  | S(   s  
        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

        s   input failed to evaluate(
   R   R   R`   Ra   R   R   RK   RJ   R   R   (   R   R   t   rv(    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   __contains__V  s    $!c         C   s?  t  | t t f  s$ t d   n  t  | t  rp t j } x- | D]% } | |  k rC | t |  } qC qC W| S|  j | j k  s |  j | j k r t j S|  j | j k r |  j | j k r t | j |  j  S|  j | j k r | Sn  | j |  j k r;| j |  j k  r"t |  j | j  S| j |  j k r;|  Sn  d S(   s  
        Returns the intersection of 'self' and 'other'.
        Here other can be an instance of FiniteSet or AccumulationBounds.

        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}

        s4   Input must be AccumulationBounds or FiniteSet objectN(   R>   R   R   R   R   R   R   R   (   R   R   t   fin_sett   i(    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   intersectionx  s*    	$c         C   s   t  | t  s t d   n  |  j | j k ra |  j | j k ra t |  j t |  j | j   S| j |  j k r | j |  j k r t | j t |  j | j   Sd  S(   Ns4   Input must be AccumulationBounds or FiniteSet object(   R>   R   R   R   R   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   union  s    $$(#   t   __name__t
   __module__t   __doc__RK   R   R   t   _op_priorityt   propertyR   R   R   R   R   R   R   R   t   __radd__R   R   R   R   t   __rmul__R   t   __truediv__R   t   __rtruediv__R   R   R   R   R   R   R   R   R   (    (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyR     s:   		,8I		&	&	&	&	"	-N(<   t   sympyR    R   R   R   R   R   R   R   R   R	   R
   t   sympy.core.basicR   t
   sympy.coreR   R   R   t   sympy.logic.boolalgR   t   sympy.core.exprR   R   t   sympy.core.numbersR   R   t   sympy.core.sympifyR   t   sympy.sets.setsR   R   R   R   R   R   t   sympy.sets.conditionsetR   R   R   R   t   sympy.utilitiesR   t   sympy.simplify.radsimpR   t   sympy.polys.rationaltoolsR    t   sympy.core.compatibilityR!   R%   R"   R;   R\   Rp   RJ   R?   R   R   R   R   R   (    (    (    s2   lib/python2.7/site-packages/sympy/calculus/util.pyt   <module>   s2   L.	\		o	(	1	H  