
~9\c           @  s  d  d l  m Z m Z d d l m Z m Z m Z d d l 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 d  d
 l m Z m Z d  d l m Z d e e f d     YZ d e e f d     YZ d   Z  d e f d     YZ! d   Z" d   Z# d d l$ 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/ m0 Z0 d d l1 m2 Z2 m3 Z3 d S(   i(   t   print_functiont   divisioni   (   t   sympifyt   _sympifyt   SympifyError(   t   Basict   Atom(   t   S(   t
   EvalfMixint   pure_complex(   t
   _sympifyitt   call_highest_priority(   t   cacheit(   t   reducet   as_intt   default_sort_keyt   ranget   Iterable(   t   mpf_logt   prec_to_dps(   t   defaultdictt   Exprc        
   B  s  e  Z d  Z g  Z e Z e d    Z e e	 d   Z
 d Z d   Z d   Z d   Z e d e  e d  d	     Z e d e  e d
  d     Z e d e  e d  d     Z e d e  e d  d     Z e d e  e d  d     Z e d e  e d  d     Z e d e  e d  d     Z e	 d  Z e d e  e d  d     Z e d e  e d  d     Z e d e  e d  d     Z e Z e Z e d e  e d  d     Z e d e  e d  d      Z  e d e  e d!  d"     Z! e d e  e d#  d$     Z" e d e  e d%  d&     Z# e d e  e d'  d(     Z$ d)   Z% e% Z& d*   Z' d+   Z( d,   Z) d-   Z* d.   Z+ d/   Z, d0   Z- e. d1    Z/ e d2    Z0 e	 d3 d3 d4 d4 d5  Z1 d6   Z2 e3 d7  Z4 d8   Z5 d9   Z6 d:   Z7 d;   Z8 d<   Z9 d=   Z: d>   Z; d?   Z< d@   Z= dA   Z> e? dB    Z@ e	 dC  ZA e	 e3 dD  ZB dE   ZC dF   ZD dG   ZE dH   ZF e	 dI  ZG e3 e e dJ  ZH d4 e3 dK  ZI dL   ZJ dM   ZK dN   ZL e dO  ZM dP   ZN dQ   ZO dR   ZP dS   ZQ dT   ZR dU   ZS e3 e dV  ZT dW   ZU dX   ZV dY   ZW dZ   ZX e d[    ZY d\   ZZ e3 d]  Z[ d^   Z\ d_   Z] d`   Z^ da   Z_ db   Z` dc   Za e	 dd de df e	 dg  Zb dh   Zc e	 dd df e	 di  Zd e	 dj  Ze e	 dd de df e	 dk  Zf dl   Zg df dm  Zh e	 dn  Zi e do    Zj dp   Zk dq   Zl dr   Zm e3 ds  Zn e3 dt  Zo e	 dd d4 e du e e3 dv  Zp e	 dw  Zq dx   Zr dy   Zs e. e dz   Zt e e e	 e e e e e e d{   Zu d|   Zv d} e	 e3 e3 d~  Zw g  e	 e3 d  Zx e3 e3 d  Zy e	 e e3 e d  Zz d   Z{ e	 d  Z| d   Z} d   Z~ d   Z d   Z d   Z d   Z d   Z e d  Z d   Z d   Z dd d  Z d   Z RS(   sZ  
    Base class for algebraic expressions.

    Everything that requires arithmetic operations to be defined
    should subclass this class, instead of Basic (which should be
    used only for argument storage and expression manipulation, i.e.
    pattern matching, substitutions, etc).

    See Also
    ========

    sympy.core.basic.Basic
    c         C  s   t  S(   sU  Return True if one can differentiate with respect to this
        object, else False.

        Subclasses such as Symbol, Function and Derivative return True
        to enable derivatives wrt them. The implementation in Derivative
        separates the Symbol and non-Symbol (_diff_wrt=True) variables and
        temporarily converts the non-Symbols into Symbols when performing
        the differentiation. By default, any object deriving from Expr
        will behave like a scalar with self.diff(self) == 1. If this is
        not desired then the object must also set `is_scalar = False` or
        else define an _eval_derivative routine.

        Note, see the docstring of Derivative for how this should work
        mathematically. In particular, note that expr.subs(yourclass, Symbol)
        should be well-defined on a structural level, or this will lead to
        inconsistent results.

        Examples
        ========

        >>> from sympy import Expr
        >>> e = Expr()
        >>> e._diff_wrt
        False
        >>> class MyScalar(Expr):
        ...     _diff_wrt = True
        ...
        >>> MyScalar().diff(MyScalar())
        1
        >>> class MySymbol(Expr):
        ...     _diff_wrt = True
        ...     is_scalar = False
        ...
        >>> MySymbol().diff(MySymbol())
        Derivative(MySymbol(), MySymbol())
        (   t   False(   t   self(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt	   _diff_wrt!   s    &c         C  s#  |  j    \ } } | j r- | j \ } } n | t j } } | j rX | j   f } n | j rs t |  f } np | j	 r | j
 d |  } n' | j r | j d |  } n	 | j } t g  | D] } t | d | ^ q  } t |  t |  f } | j d |  } | j   | | | f S(   Nt   order(   t   as_coeff_Mult   is_Powt   argsR   t   Onet   is_Dummyt   sort_keyt   is_Atomt   strt   is_Addt   as_ordered_termst   is_Mult   as_ordered_factorst   tupleR   t   lent	   class_key(   R   R   t   coefft   exprt   expR   t   arg(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR   I   s$    						(g      $@c         C  s   |  S(   N(    (   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __pos__v   s    c         C  s   t  t j |   S(   N(   t   MulR   t   NegativeOne(   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __neg__y   s    c         C  s   d d l  m } | |   S(   Ni(   t   Abs(   t   sympyR1   (   R   R1   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __abs__|   s    t   othert   __radd__c         C  s   t  |  |  S(   N(   t   Add(   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __add__   s    R7   c         C  s   t  | |   S(   N(   R6   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR5      s    t   __rsub__c         C  s   t  |  |  S(   N(   R6   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __sub__   s    R9   c         C  s   t  | |   S(   N(   R6   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR8      s    t   __rmul__c         C  s   t  |  |  S(   N(   R.   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __mul__   s    R;   c         C  s   t  | |   S(   N(   R.   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR:      s    t   __rpow__c         C  s   t  |  |  S(   N(   t   Pow(   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _pow   s    c         C  s   | d  k r |  j |  Syp t |   t |  t |  } } } | d k r^ t | | |  Sd d l m } | t | | |  |  SWnA t k
 r |  j |  } y | | SWq t k
 r t SXn Xd  S(   Ni    i(   t   mod_inverse(	   t   NoneR>   R   t   powt   sympy.core.numbersR?   t
   ValueErrort	   TypeErrort   NotImplemented(   R   R4   t   modt   _selfR?   t   power(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __pow__   s    &RI   c         C  s   t  | |   S(   N(   R=   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR<      s    t   __rdiv__c         C  s   t  |  t | t j   S(   N(   R.   R=   R   R/   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __div__   s    RK   c         C  s   t  | t |  t j   S(   N(   R.   R=   R   R/   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRJ      s    t   __rmod__c         C  s   t  |  |  S(   N(   t   Mod(   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __mod__   s    RN   c         C  s   t  | |   S(   N(   RM   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRL      s    t   __rfloordiv__c         C  s   d d l  m } | |  |  S(   Ni(   t   floor(   t#   sympy.functions.elementary.integersRP   (   R   R4   RP   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __floordiv__   s    RR   c         C  s   d d l  m } | | |   S(   Ni(   RP   (   RQ   RP   (   R   R4   RP   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRO      s    t   __rdivmod__c         C  s-   d d l  m } | |  |  t |  |  f S(   Ni(   RP   (   RQ   RP   RM   (   R   R4   RP   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt
   __divmod__   s    RT   c         C  s-   d d l  m } | | |   t | |   f S(   Ni(   RP   (   RQ   RP   RM   (   R   R4   RP   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRS      s    c         C  s)  d d l  m } |  j s( t d   n  |  j d  } | j sO t d   n  | t j t j t j	 f k r t d |   n  t
 |  } | s d S| | k r%|  | j d  r%| d k r d n d } |   } |  | j d d	 i | | 6d k rd n d } | | k r%| | 8} q%n  | S(
   Ni(   t   Dummys   can't convert symbols to inti   s   can't convert complex to ints   can't convert %s to inti    i   t   subs(   R2   RU   t	   is_numberRD   t   roundt	   is_NumberR   t   NaNt   Infinityt   NegativeInfinityt   intt   equalst   evalf(   R   RU   t   rt   it   isignt   xt	   diff_sign(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __int__   s$    		 	2c         C  sW   |  j    } | j r t |  S| j rG | j   d rG t d   n  t d   d  S(   Ni   s   can't convert complex to floats!   can't convert expression to float(   R_   RY   t   floatRW   t   as_real_imagRD   (   R   t   result(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt	   __float__  s    	
c         C  s7   |  j    } | j   \ } } t t |  t |   S(   N(   R_   Rg   t   complexRf   (   R   Rh   t   ret   im(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __complex__  s    c         C  s5  d d l  m } y t |  } Wn' t k
 rI t d |  | f   n Xx] |  | f D]O } | j r | j t k r t d |   n  | t j	 k rW t d   qW qW Wt
 |  |  } | d  k	 r t | d k  S|  j s | j r"|  | } | j d  k	 r"| j | j k	 r"t | j  Sn  | |  | d t S(   Ni(   t   GreaterThans   Invalid comparison %s >= %ss    Invalid comparison of complex %ss   Invalid NaN comparisoni    t   evaluate(   R2   Rn   R   R   RD   t
   is_complext   is_realR   R   RZ   t   _n2R@   t   is_nonnegativet   is_negativeR   (   R   R4   Rn   t   met   n2t   dif(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __ge__  s&    
c         C  s5  d d l  m } y t |  } Wn' t k
 rI t d |  | f   n Xx] |  | f D]O } | j r | j t k r t d |   n  | t j	 k rW t d   qW qW Wt
 |  |  } | d  k	 r t | d k  S|  j s | j r"|  | } | j d  k	 r"| j | j k	 r"t | j  Sn  | |  | d t S(   Ni(   t   LessThans   Invalid comparison %s <= %ss    Invalid comparison of complex %ss   Invalid NaN comparisoni    Ro   (   R2   Ry   R   R   RD   Rp   Rq   R   R   RZ   Rr   R@   t   is_nonpositivet   is_positiveR   (   R   R4   Ry   Ru   Rv   Rw   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __le__4  s&    
c         C  s5  d d l  m } y t |  } Wn' t k
 rI t d |  | f   n Xx] |  | f D]O } | j r | j t k r t d |   n  | t j	 k rW t d   qW qW Wt
 |  |  } | d  k	 r t | d k  S|  j s | j r"|  | } | j d  k	 r"| j | j k	 r"t | j  Sn  | |  | d t S(   Ni(   t   StrictGreaterThans   Invalid comparison %s > %ss    Invalid comparison of complex %ss   Invalid NaN comparisoni    Ro   (   R2   R}   R   R   RD   Rp   Rq   R   R   RZ   Rr   R@   R{   Rz   R   (   R   R4   R}   Ru   Rv   Rw   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __gt__I  s&    
c         C  s5  d d l  m } y t |  } Wn' t k
 rI t d |  | f   n Xx] |  | f D]O } | j r | j t k r t d |   n  | t j	 k rW t d   qW qW Wt
 |  |  } | d  k	 r t | d k   S|  j s | j r"|  | } | j d  k	 r"| j | j k	 r"t | j  Sn  | |  | d t S(   Ni(   t   StrictLessThans   Invalid comparison %s < %ss    Invalid comparison of complex %ss   Invalid NaN comparisoni    Ro   (   R2   R   R   R   RD   Rp   Rq   R   R   RZ   Rr   R@   Rt   Rs   R   (   R   R4   R   Ru   Rv   Rw   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   __lt__^  s&    
c         C  s&   |  j  s t d   n
 t |   Sd  S(   Ns&   can't truncate symbols and expressions(   RW   RD   t   Integer(   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt	   __trunc__s  s    	c         C  s   d d l  m } t |  d  r2 | j |  j |  St |  d  r |  j \ } } | j | |  } | j | |  t j } | | St d   d  S(   Ni(   t   Floatt   _mpf_t   _mpc_s#   expected mpmath number (mpf or mpc)(	   R2   R   t   hasattrt   _newR   R   R   t   ImaginaryUnitRD   (   Rc   t   precR   Rk   Rl   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _from_mpmathy  s    c         C  s   t  d   |  j D  S(   si  Returns True if ``self`` has no free symbols and no
        undefined functions (AppliedUndef, to be precise). It will be
        faster than ``if not self.free_symbols``, however, since
        ``is_number`` will fail as soon as it hits a free symbol
        or undefined function.

        Examples
        ========

        >>> from sympy import log, Integral, cos, sin, pi
        >>> from sympy.core.function import Function
        >>> from sympy.abc import x
        >>> f = Function('f')

        >>> x.is_number
        False
        >>> f(1).is_number
        False
        >>> (2*x).is_number
        False
        >>> (2 + Integral(2, x)).is_number
        False
        >>> (2 + Integral(2, (x, 1, 2))).is_number
        True

        Not all numbers are Numbers in the SymPy sense:

        >>> pi.is_number, pi.is_Number
        (True, False)

        If something is a number it should evaluate to a number with
        real and imaginary parts that are Numbers; the result may not
        be comparable, however, since the real and/or imaginary part
        of the result may not have precision.

        >>> cos(1).is_number and cos(1).is_comparable
        True

        >>> z = cos(1)**2 + sin(1)**2 - 1
        >>> z.is_number
        True
        >>> z.is_comparable
        False

        See Also
        ========
        sympy.core.basic.is_comparable
        c         s  s   |  ] } | j  Vq d  S(   N(   RW   (   t   .0t   obj(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>  s    (   t   allR   (   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRW     s    2ii   c         C  s  |  j  } d } | r d d l m } | | | | f \ }	 }
 } } t t t | g  | D]! } | |	 | |
 | d t ^ qV    } y t |  j d d |  } Wq t	 t
 f k
 r d SXn i  } t |  j d   } t | d  s d S| j d k rkd d l m } d d	 l m } xH | d |  D]4 } t |  j | d |  } | j d k r0Pq0q0Wn  | j d k r| d k rt | d
  } n  |  j | d | Sd S(   s  Return self evaluated, if possible, replacing free symbols with
        random complex values, if necessary.

        The random complex value for each free symbol is generated
        by the random_complex_number routine giving real and imaginary
        parts in the range given by the re_min, re_max, im_min, and im_max
        values. The returned value is evaluated to a precision of n
        (if given) else the maximum of 15 and the precision needed
        to get more than 1 digit of precision. If the expression
        could not be evaluated to a number, or could not be evaluated
        to more than 1 digit of precision, then None is returned.

        Examples
        ========

        >>> from sympy import sqrt
        >>> from sympy.abc import x, y
        >>> x._random()                         # doctest: +SKIP
        0.0392918155679172 + 0.916050214307199*I
        >>> x._random(2)                        # doctest: +SKIP
        -0.77 - 0.87*I
        >>> (x + y/2)._random(2)                # doctest: +SKIP
        -0.57 + 0.16*I
        >>> sqrt(2)._random(2)
        1.4

        See Also
        ========

        sympy.utilities.randtest.random_complex_number
        i   i(   t   random_complex_numbert   rationali   RV   t   _prec(   t   giant_steps(   t   DEFAULT_MAXPRECi   N(   t   free_symbolst   sympy.utilities.randtestR   t   dictt   listt   zipt   Truet   absR_   RC   RD   R@   R   R   t   mpmath.libmp.libintmathR   t   sympy.core.evalfR   t   max(   R   t   nt   re_mint   im_mint   re_maxt   im_maxt   freeR   R   t   at   ct   bt   dt   zit   repst   nmagR   t   target(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _random  s6    !	4c         O  s  | j  d t  } |  j r t S|  j } | s2 t St |  } | rS | | @rS t S| p\ | } |  } | rz | j   } n  | j r t Sd } | | k ryb | j t	 t
 | d g t |    d t } | t j k r | j d d d d d  } n  Wn t k
 rd } n X| d k	 r| t j k	 ryb | j t	 t
 | d g t |    d t } | t j k r| j d d d d d  } n  Wn t k
 rd } n X| d k	 r| t j k	 r| j |  t k rt S| j d d d d d  } | d k	 r/| t j k	 r/| j |  t k r/t S| j   } | d k	 r| t j k	 r| j |  t k rot S| j r~| n | } qqn  xz | D]r }	 | j |	  }
 | r|
 j   }
 n  |
 d k rt |
 d t s| j  d t  r| S|
 j rd Sn  t SqWt S(	   s
  Return True if self is constant, False if not, or None if
        the constancy could not be determined conclusively.

        If an expression has no free symbols then it is a constant. If
        there are free symbols it is possible that the expression is a
        constant, perhaps (but not necessarily) zero. To test such
        expressions, two strategies are tried:

        1) numerical evaluation at two random points. If two such evaluations
        give two different values and the values have a precision greater than
        1 then self is not constant. If the evaluations agree or could not be
        obtained with any precision, no decision is made. The numerical testing
        is done only if ``wrt`` is different than the free symbols.

        2) differentiation with respect to variables in 'wrt' (or all free
        symbols if omitted) to see if the expression is constant or not. This
        will not always lead to an expression that is zero even though an
        expression is constant (see added test in test_expr.py). If
        all derivatives are zero then self is constant with respect to the
        given symbols.

        If neither evaluation nor differentiation can prove the expression is
        constant, None is returned unless two numerical values happened to be
        the same and the flag ``failing_number`` is True -- in that case the
        numerical value will be returned.

        If flag simplify=False is passed, self will not be simplified;
        the default is True since self should be simplified before testing.

        Examples
        ========

        >>> from sympy import cos, sin, Sum, S, pi
        >>> from sympy.abc import a, n, x, y
        >>> x.is_constant()
        False
        >>> S(2).is_constant()
        True
        >>> Sum(x, (x, 1, 10)).is_constant()
        True
        >>> Sum(x, (x, 1, n)).is_constant()
        False
        >>> Sum(x, (x, 1, n)).is_constant(y)
        True
        >>> Sum(x, (x, 1, n)).is_constant(n)
        False
        >>> Sum(x, (x, 1, n)).is_constant(x)
        True
        >>> eq = a*cos(x)**2 + a*sin(x)**2 - a
        >>> eq.is_constant()
        True
        >>> eq.subs({x: pi, a: 2}) == eq.subs({x: pi, a: 3}) == 0
        True

        >>> (0**x).is_constant()
        False
        >>> x.is_constant()
        False
        >>> (x**x).is_constant()
        False
        >>> one = cos(x)**2 + sin(x)**2
        >>> one.is_constant()
        True
        >>> ((one - 1)**(x + 1)).is_constant() in (True, False) # could be 0 or 1
        True
        t   simplifyi    t   simultaneousi   it   or_realt   failing_numberN(   t   getR   RW   R   t   setR   t   is_zeroR@   RV   R   R   R'   R   RZ   R   t   ZeroDivisionErrorR^   R   t   diffR	   (   R   t   wrtt   flagsR   R   R*   R   R   R   t   wt   deriv(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   is_constant  sl    D			(	"
(	"
00	c           s}  d d l  m   m  d d l m } d d l m } d d l m } t	 |  } |  | k rb t
 St  |  |  d t
 } | s t
 S| j t t  s t S| j d t d t
  } | t k r t S| d	 k r | j r d	 S| t
 k r| j   } | rt Sn  | j rM| j   }	 |	 sMg  | j t  D]   j d
 j r0 ^ q0}
 |
 j d d    x |
 D]  yy  j rt | |    } n	  g } | r | k rt
 S j rt     f d   | D  rt
 Sqn  Wqlt k
 rqlXqlWt
 rJy! | |  } | j r%t
 St SWqG| t f k
 rCqGXqJqMn  | t
 d	 f k ro| d
 k rot S| ry| Sd	 S(   s  Return True if self == other, False if it doesn't, or None. If
        failing_expression is True then the expression which did not simplify
        to a 0 will be returned instead of None.

        If ``self`` is a Number (or complex number) that is not zero, then
        the result is False.

        If ``self`` is a number and has not evaluated to zero, evalf will be
        used to test whether the expression evaluates to zero. If it does so
        and the result has significance (i.e. the precision is either -1, for
        a Rational result, or is greater than 1) then the evalf value will be
        used to return True or False.

        i(   t	   nsimplifyR   (   t   solveset(   t   NotAlgebraic(   t   minimal_polynomialt   radicalR   R   Ni    t   keyc         S  s   |  j  d S(   Ni    (   R   (   Rc   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   <lambda>  t    c         3  s9   |  ]/ }   |  g   k o0  |   k Vq d  S(   N(    (   R   t   si(   R   t   sR   (    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>  s   (   t   sympy.simplify.simplifyR   R   t   sympy.solvers.solvesetR   t   sympy.polys.polyerrorsR   t   sympy.polys.numberfieldsR   R   R   t   factor_termst   hasR6   RM   R   R   R@   RW   R   t   atomsR=   R   t
   is_Integert   sortt	   is_SymbolR   Rq   t   anyt   NotImplementedError(   R   R4   t   failing_expressionR   R   R   R   t   constantt   ndifft   approxt   surdst   solt   mp(    (   R   R   R   s.   lib/python2.7/site-packages/sympy/core/expr.pyR^     sj    	2				c         C  s  d d l  m } d d l m } |  j r|  j t k r< t Sy |  j d  } Wn t k
 rc d  SX| d  k rt d  St
 | d d  d k r d  S| t j k r d  S|  j d  j   \ } } | j s | j r t S| j d k r| j d k rt | o| d k  S| j d k r| s0| j d k r|  j r|  j t  ry | |   j r_t SWq}| t f k
 ryq}Xqn  d  S(   Ni(   R   (   R   i   R   i   i    (   R   R   R   R   RW   Rq   R   t   _eval_evalfRC   R@   t   getattrR   RZ   R_   Rg   RY   R   t   boolt   is_algebraicR   t   FunctionR   R   (   R   R   R   Rv   R   Ra   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_is_positive  s6    	%c         C  s  d d l  m } d d l m } |  j r|  j t k r< t Sy |  j d  } Wn t k
 rc d  SX| d  k rt d  St
 | d d  d k r d  S| t j k r d  S|  j d  j   \ } } | j s | j r t S| j d k r| j d k rt | o| d k   S| j d k r| s0| j d k r|  j r|  j t  ry | |   j r_t SWq}| t f k
 ryq}Xqn  d  S(   Ni(   R   (   R   i   R   i   i    (   R   R   R   R   RW   Rq   R   R   RC   R@   R   R   RZ   R_   Rg   RY   R   R   R   R   R   R   R   (   R   R   R   Rv   R   Ra   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_is_negative)  s6    	%c         C  s  d d l  m } m } d d l m } d d l m } d d l m } d d l	 m
 }	 | d k r} | d k r} t d   n  | | k r d S| d k r d }
 n |  j | |  }
 |
 j t j t j t j t j |	  rN| | k  t k r| |  | | d	  }
 n | |  | | d
  }
 |
 t j k r-|
 St |
 |  rNt d   qNn  | d k rcd } n |  j | |  } | j t j t j t j t j |	  r| | k  t k r| |  | | d
  } n | |  | | d	  } t | |  rt d   qn  | o| d k r| |
 S| |
 } | j r| j r| | k  rP| | |  } n | | |  } | |  j   j   d | d | } x7 |  j |  D]& } | | | j d | d | B} qWy x | D] } | t j k rPn  | j sqn  | | k  | | k  k ot k n rH| | |  | | d	  | |  | | d
  7} q| | k  | | k  k okt k n r| | |  | | d	  | |  | | d
  7} qqWWqt k
 rqXn  | S(   s^  
        Returns evaluation over an interval.  For most functions this is:

        self.subs(x, b) - self.subs(x, a),

        possibly using limit() if NaN is returned from subs, or if
        singularities are found between a and b.

        If b or a is None, it only evaluates -self.subs(x, a) or self.subs(b, x),
        respectively.

        i(   t   limitt   Limit(   R   (   t   Interval(   t   log(   t   AccumBoundss"   Both interval ends cannot be None.i    t   +t   -s   Could not compute limiti   t   domainN(   t   sympy.seriesR   R   R   R   t   sympy.sets.setsR   t&   sympy.functions.elementary.exponentialR   t   sympy.calculus.utilR   R@   RC   RV   R   R   RZ   R[   R\   t   ComplexInfinityR   t
   isinstanceR   t   is_comparablet   cancelt   as_numer_denomR   R   R   RD   (   R   Rc   R   R   R   R   R   R   R   R   t   At   Bt   valueR   t   singularitiest   logtermR   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_intervalL  sl    	'	'
		(0(7c         C  s   d  S(   N(   R@   (   R   R4   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_power  s    c         C  s   |  j  r |  S|  j r |  Sd  S(   N(   Rq   t   is_imaginary(   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_conjugate  s    		c         C  s   d d l  m } | |   S(   Ni(   t	   conjugate(   t$   sympy.functions.elementary.complexesR   (   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         C  sH   d d l  m } |  j r |  S|  j r0 | |   S|  j rD | |   Sd  S(   Ni(   R   (   R   R   Rp   t   is_hermitiant   is_antihermitian(   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_transpose  s    		
	c         C  s   d d l  m } | |   S(   Ni(   t	   transpose(   R   R   (   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         C  sy   d d l  m } m } |  j r# |  S|  j r1 |  S|  j   } | d  k	 rS | |  S|  j   } | d  k	 ru | |  Sd  S(   Ni(   R   R   (   R   R   R   R   R   R   R@   R   (   R   R   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_adjoint  s    		
c         C  s   d d l  m } | |   S(   Ni(   t   adjoint(   R   R   (   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c           s   d d l  m } t  d d  } | d k r7 t } n | d  } | rV  d  n  |      f d        f d   } | | f S(	   s+   Parse and configure the ordering of terms. i(   t   monomial_keyt
   startswiths   rev-i   c           sT   g  } xA |  D]9 } t  | t  r8 | j   |   q | j |  q Wt |  S(   N(   R   R&   t   append(   t   monomRh   t   m(   t   neg(    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c           s   |  \ } \ \ } } } }    |   } t  g  | D] } | j d   ^ q7  } t |  | f | | f f } | | | f S(   NR   (   R&   R   R   (   t   termt   _Rk   Rl   R  t   ncpartt   eR)   (   t	   monom_keyR  R   (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s
    +N(   t   sympy.polys.orderingsR   R   R@   R   (   t   clsR   R   R  t   reverseR   (    (   R
  R  R   s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _parse_order  s    		c         C  s   |  g S(   s4   Return list of ordered factors (if Mul) else [self].(    (   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR%     s    c           s  d d l  m   m  | d	 k r |  j r    f d   } t t j |   d | } t |  d k r t	 | d    f  r t	 | d t
  r t t
 j | d  d | } t |  d k r t	 | d    r | d j r | d j r | Sq n  |  j |  \ } } |  j   \ } } t d   | D  sVt | d | d | }	 n g  g  }
 } xF | D]> \ } } | j s|
 j | | f  qj| j | | f  qjWt |
 d | d t t | d | d t }	 | r|	 | f Sg  |	 D] \ } } | ^ qSd	 S(
   s"  
        Transform an expression to an ordered list of terms.

        Examples
        ========

        >>> from sympy import sin, cos
        >>> from sympy.abc import x

        >>> (sin(x)**2*cos(x) + sin(x)**2 + 1).as_ordered_terms()
        [sin(x)**2*cos(x), sin(x)**2, 1]

        i   (   t   Numbert   NumberSymbolc           s   t  |     f  S(   N(   R   (   Rc   (   R  R  (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     R   R   i   i    c         s  s   |  ] \ } } | j  Vq d  S(   N(   t   is_Order(   R   R  R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>  s    R  N(   t   numbersR  R  R@   R"   t   sortedR6   t	   make_argsR'   R   R.   R{   Rt   R  t   as_termsR   R  R  R   (   R   R   t   dataR   t   add_argst   mul_argsR  t   termst   genst   orderedt   _termst   _orderR  t   reprR  (    (   R  R  s.   lib/python2.7/site-packages/sympy/core/expr.pyR#     s6    
	
c         C  sC  d d l  m } d d l m } d d l m } t g   g  } } x'| j |   D]} | j   \ } } t	 |  } i  g  }	 }
 | t
 j k	 r/x | j |  D] } | j r y | t	 |  9} Wq t t f k
 r q Xq n  | j r| |  \ } } | |	 | <| j  |  q |
 j |  q Wn  | j | j f } t |
  }
 | j | | |	 |
 f f  qS Wt | d t } t |  i  } } x$ t |  D] \ } } | | | <qWg  } xw | D]o \ } \ } }	 }
 d g | } x( |	 j   D] \ } } | | | | <qW| j | | t |  |
 f f  qW| | f S(   s,   Transform an expression to a list of terms. i   (   R6   (   R.   (   t   decompose_powerR   i    (   t   addR6   t   mulR.   t	   exprtoolsR  R   R  R   Rj   R   R   RW   RD   RC   t   is_commutativeR  t   realt   imagR&   R  R   R'   t	   enumeratet   items(   R   R6   R.   R  R  R  R  R)   t   _termt   cpartR  t   factort   baseR+   t   kt   indicesRa   t   gRh   R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR  .  sF    		
 &c         C  s   |  S(   s1   Removes the additive O(..) symbol if there is one(    (   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   removeOf  s    c         C  s   d S(   s=   Returns the additive O(..) symbol if there is one, else None.N(   R@   (   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   getOj  s    c         C  sD  d d l  m } m } |  j   } | d k r2 d S| j r0| j } | t j k rZ t j	 S| j
 rj t j S| j r~ | j d S| j r0x | j D] } | j
 r t j S| j r | j |  } t |  d k r&| j   } | j | | d d t  } | j j
 r#| j j r#t | j  Sq&q q Wq0n  t d |   d S(   s\  
        Returns the order of the expression.

        The order is determined either from the O(...) term. If there
        is no O(...) term, it returns None.

        Examples
        ========

        >>> from sympy import O
        >>> from sympy.abc import x
        >>> (1 + x + O(x**2)).getn()
        2
        >>> (1 + x).getn()

        i(   RU   t   Symboli   Rc   t   positives   not sure of order of %sN(   R2   RU   R1  R0  R@   R  R*   R   R   t   ZeroR   R   R   R$   R   R'   t   popRV   R   R+  R+   t   is_RationalR   R   (   R   RU   R1  t   ot   oit   symsRc   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   getnn  s0    							c         C  s   d d l  m } | |  |  S(   s7   wrapper for count_ops that returns the operation count.i   (   t	   count_ops(   t   functionR:  (   R   t   visualR:  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR:    s    c         C  sO  |  j  r t |  j  } n	 |  g } xG t |  D]- \ } } | j s1 | |  } | | } Pq1 q1 W| } g  } | r | r | d j r | d j r | d t j k	 r t j | d g | d *n  | rEt	 |  }	 t
 |  } |	 rE| rEt	 |  |	 k rEt d g  | D]* }
 t |  j  j |
  d k r|
 ^ q  qEn  | | g S(   sr  Return [commutative factors, non-commutative factors] of self.

        self is treated as a Mul and the ordering of the factors is maintained.
        If ``cset`` is True the commutative factors will be returned in a set.
        If there were repeated factors (as may happen with an unevaluated Mul)
        then an error will be raised unless it is explicitly suppressed by
        setting ``warn`` to False.

        Note: -1 is always separated from a Number unless split_1 is False.

        >>> from sympy import symbols, oo
        >>> A, B = symbols('A B', commutative=0)
        >>> x, y = symbols('x y')
        >>> (-2*x*y).args_cnc()
        [[-1, 2, x, y], []]
        >>> (-2.5*x).args_cnc()
        [[-1, 2.5, x], []]
        >>> (-2*x*A*B*y).args_cnc()
        [[-1, 2, x, y], [A, B]]
        >>> (-2*x*A*B*y).args_cnc(split_1=False)
        [[-2, x, y], [A, B]]
        >>> (-2*x*y).args_cnc(cset=True)
        [{-1, 2, x, y}, []]

        The arg is always treated as a Mul:

        >>> (-2 + x + A).args_cnc()
        [[], [x - 2 + A]]
        >>> (-oo).args_cnc() # -oo is a singleton
        [[-1, oo], []]
        i    i   s"   repeated commutative arguments: %s(   R$   R   R   R&  R#  RY   Rt   R   R/   R'   R   RC   t   count(   R   t   csett   warnt   split_1R   Ra   t   miR   t   nct   clent   ci(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   args_cnc  s,    !			

Ac           sV  t  |  } t | t  s" t j St |  } | s; t j S| |  k ra | d k rZ t j St j S| t j k r g  t j |   D]% } | j	   d t j k r | ^ q     s t j St     S| d k r{| j
 rd|  j
 rd|  j | d | } | st j S| s6|  t g  t j |  D] } | | ^ q  S|  t g  t j |  D] } | | ^ qL  S|  j | d t d S| | } d   } t d  } g    t j |   } |  j }	 | j }
 |	 r|
 rt j S|	 r| j d t d t  d } x | D] } | j d t d t  d } t |  t |  k r>qn  | j |  } t |  t |  t |  k r  j t |    qqW  g  k rt j S  rRt     Sn|
 r| j d t d t  d } x | D] } | j d t  \ } } t |  t |  k rqn  | j |  } t |  t |  t |  k r  j t t |  |    qqW  g  k r|t j S  rRt     Sn| j d t  \ } } x | D] } | j d t  \ } } t |  t |  k rqn  | j |  } t |  t |  t |  k r  j | | f  qqW  sBt j St   f d	     D  r|   d d | |  } | d k	 r| st t g    D] \ } } t |   ^ q  t   d d |     St   d d | t |    Sqn  t | d
     D  } | r| | | |  } | d k	 r| s  d d } x> t d t     D]' } | j   | d  } | s[Pq[q[Wt t |  | |    S| t |  } t g    D]& \ } } t t |  | |   ^ q  Sqn  t t t | d     D    } | r| | | |  } | d k	 r| s|t g    D]1 \ } } t t |  | t |  |    ^ qD  St | | t |    Sqn  d } x t    D]L \ } \ } } | | | |  } | d k	 r| s| | | f } qPqqW| rK| \ } } } | s0t t |  | |    St | | t |    Sn  t j Sd S(   s  
        Returns the coefficient from the term(s) containing ``x**n``. If ``n``
        is zero then all terms independent of ``x`` will be returned.

        When ``x`` is noncommutative, the coefficient to the left (default) or
        right of ``x`` can be returned. The keyword 'right' is ignored when
        ``x`` is commutative.

        See Also
        ========

        as_coefficient: separate the expression into a coefficient and factor
        as_coeff_Add: separate the additive constant from an expression
        as_coeff_Mul: separate the multiplicative constant from an expression
        as_independent: separate x-dependent terms/factors from others
        sympy.polys.polytools.coeff_monomial: efficiently find the single coefficient of a monomial in Poly
        sympy.polys.polytools.nth: like coeff_monomial but powers of monomial terms are used

        Examples
        ========

        >>> from sympy import symbols
        >>> from sympy.abc import x, y, z

        You can select terms that have an explicit negative in front of them:

        >>> (-x + 2*y).coeff(-1)
        x
        >>> (x - 2*y).coeff(-1)
        2*y

        You can select terms with no Rational coefficient:

        >>> (x + 2*y).coeff(1)
        x
        >>> (3 + 2*x + 4*x**2).coeff(1)
        0

        You can select terms independent of x by making n=0; in this case
        expr.as_independent(x)[0] is returned (and 0 will be returned instead
        of None):

        >>> (3 + 2*x + 4*x**2).coeff(x, 0)
        3
        >>> eq = ((x + 1)**3).expand() + 1
        >>> eq
        x**3 + 3*x**2 + 3*x + 2
        >>> [eq.coeff(x, i) for i in reversed(range(4))]
        [1, 3, 3, 2]
        >>> eq -= 2
        >>> [eq.coeff(x, i) for i in reversed(range(4))]
        [1, 3, 3, 0]

        You can select terms that have a numerical term in front of them:

        >>> (-x - 2*y).coeff(2)
        -y
        >>> from sympy import sqrt
        >>> (x + sqrt(2)*x).coeff(sqrt(2))
        x

        The matching is exact:

        >>> (3 + 2*x + 4*x**2).coeff(x)
        2
        >>> (3 + 2*x + 4*x**2).coeff(x**2)
        4
        >>> (3 + 2*x + 4*x**2).coeff(x**3)
        0
        >>> (z*(x + y)**2).coeff((x + y)**2)
        z
        >>> (z*(x + y)**2).coeff(x + y)
        0

        In addition, no factoring is done, so 1 + z*(1 + y) is not obtained
        from the following:

        >>> (x + z*(x + x*y)).coeff(x)
        1

        If such factoring is desired, factor_terms can be used first:

        >>> from sympy import factor_terms
        >>> factor_terms(x + z*(x + x*y)).coeff(x)
        z*(y + 1) + 1

        >>> n, m, o = symbols('n m o', commutative=False)
        >>> n.coeff(n)
        1
        >>> (3*n).coeff(n)
        3
        >>> (n*m + m*n*m).coeff(n) # = (1 + m)*n*m
        1 + m
        >>> (n*m + m*n*m).coeff(n, right=True) # = (1 + m)*n*m
        m

        If there is more than one possible coefficient 0 is returned:

        >>> (n*m + m*n).coeff(n)
        0

        If there is only one possible coefficient, it is returned:

        >>> (n*m + x*m*n).coeff(m*n)
        x
        >>> (n*m + x*m*n).coeff(m*n, right=1)
        1

        i   i    t   rightt   as_Addc         S  se   |  s | r g  St  t |   t |   } x0 t |  D]" } |  | | | k r: |  |  Sq: W|  S(   N(   t   minR'   R   (   t   l1t   l2R   Ra   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   incommonj  s    c           s    s&  s& t    t    k r* d St    } | sS  j    j   n  xW t d t    | d  D]2   t     f d   t |  D  rq Pqq qq Wd   | s  j    j   n    d k	 r | r t      |   n    S(   s   Find where list sub appears in list l. When ``first`` is True
            the first occurrence from the left is returned, else the last
            occurrence is returned. Return None if sub is not in l.

            >> l = range(5)*2
            >> find(l, [2, 3])
            2
            >> find(l, [2, 3], first=0)
            7
            >> find(l, [2, 4])
            None

            i    i   c         3  s'   |  ] }    |  | k Vq d  S(   N(    (   R   t   j(   Ra   t   lt   sub(    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>  s    N(   R'   R@   R  R   R   (   RM  RN  t   firstR   (    (   Ra   RM  RN  s.   lib/python2.7/site-packages/sympy/core/expr.pyt   finds  s     &
$(
R>  R?  c         3  s)   |  ] \ } } |   d  d k Vq d S(   i    i   N(    (   R   R`   R   (   t   co(    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>  s    c         s  s   |  ] } | d  Vq d S(   i   N(    (   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>  s    c         s  s%   |  ] } t  t | d    Vq d S(   i   N(   R   t   reversed(   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>  s    N(   R   R   R   R   R3  R   R   R6   R  R   R"   R)   t   as_independentR   R#  RE  R   R'   t
   differenceR  R.   R   R   R@   R   R   t   intersectionRR  R&  (   R   Rc   R   RF  R   R   RK  RP  R   t   self_ct   x_ct   xargst   margst   residRB  t   nxt   iiR`   t   begt   gcdcRa   R  t   endt   hit(    (   RQ  s.   lib/python2.7/site-packages/sympy/core/expr.pyR)     s    n%
..
		 		""$"D&=Bc         G  s   |  S(   s/  
        Convert a polynomial to a SymPy expression.

        Examples
        ========

        >>> from sympy import sin
        >>> from sympy.abc import x, y

        >>> f = (x**2 + x*y).as_poly(x, y)
        >>> f.as_expr()
        x**2 + x*y

        >>> sin(x).as_expr()
        sin(x)

        (    (   R   R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   as_expr  s    c         C  s-   |  j  |  } | r) | j |  r) | Sd S(   s"  
        Extracts symbolic coefficient at the given expression. In
        other words, this functions separates 'self' into the product
        of 'expr' and 'expr'-free coefficient. If such separation
        is not possible it will return None.

        Examples
        ========

        >>> from sympy import E, pi, sin, I, Poly
        >>> from sympy.abc import x

        >>> E.as_coefficient(E)
        1
        >>> (2*E).as_coefficient(E)
        2
        >>> (2*sin(E)*E).as_coefficient(E)

        Two terms have E in them so a sum is returned. (If one were
        desiring the coefficient of the term exactly matching E then
        the constant from the returned expression could be selected.
        Or, for greater precision, a method of Poly can be used to
        indicate the desired term from which the coefficient is
        desired.)

        >>> (2*E + x*E).as_coefficient(E)
        x + 2
        >>> _.args[0]  # just want the exact match
        2
        >>> p = Poly(2*E + x*E); p
        Poly(x*E + 2*E, x, E, domain='ZZ')
        >>> p.coeff_monomial(E)
        2
        >>> p.nth(0, 1)
        2

        Since the following cannot be written as a product containing
        E as a factor, None is returned. (If the coefficient ``2*x`` is
        desired then the ``coeff`` method should be used.)

        >>> (2*E*x + x).as_coefficient(E)
        >>> (2*E*x + x).coeff(E)
        2*x

        >>> (E*(x + 1) + x).as_coefficient(E)

        >>> (2*pi*I).as_coefficient(pi*I)
        2
        >>> (2*I).as_coefficient(pi*I)

        See Also
        ========

        coeff: return sum of terms have a given factor
        as_coeff_Add: separate the additive constant from an expression
        as_coeff_Mul: separate the multiplicative constant from an expression
        as_independent: separate x-dependent terms/factors from others
        sympy.polys.polytools.coeff_monomial: efficiently find the single coefficient of a monomial in Poly
        sympy.polys.polytools.nth: like coeff_monomial but powers of monomial terms are used


        N(   t   extract_multiplicativelyR   (   R   R*   R`   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   as_coefficient  s    @c           s2  d d l  m } d d l m } d d l m } d d l m } |  j rY t	 j
 t	 j
 f S|  j } | j d t |  t   r t } n t } t    g   x: | D]2 }	 t |	 |  r  j |	  q  j |	  q W  f d     | | k	 s| t k	 r7| t k	 r7  |   r'| j |  f S|  | j f Sn0 | t k rUt |  j  }
 n |  j   \ }
 } | |
   f d	    }	 |	 t } |	 t } | t k rt |   | |   f SxH t |  D]: \ } }   |  r| j | |  Pn  | j |  qWt |   | r!t d
 t |  n	 | |   f Sd S(   s  
        A mostly naive separation of a Mul or Add into arguments that are not
        are dependent on deps. To obtain as complete a separation of variables
        as possible, use a separation method first, e.g.:

        * separatevars() to change Mul, Add and Pow (including exp) into Mul
        * .expand(mul=True) to change Add or Mul into Add
        * .expand(log=True) to change log expr into an Add

        The only non-naive thing that is done here is to respect noncommutative
        ordering of variables and to always return (0, 0) for `self` of zero
        regardless of hints.

        For nonzero `self`, the returned tuple (i, d) has the
        following interpretation:

        * i will has no variable that appears in deps
        * d will either have terms that contain variables that are in deps, or
          be equal to 0 (when self is an Add) or 1 (when self is a Mul)
        * if self is an Add then self = i + d
        * if self is a Mul then self = i*d
        * otherwise (self, S.One) or (S.One, self) is returned.

        To force the expression to be treated as an Add, use the hint as_Add=True

        Examples
        ========

        -- self is an Add

        >>> from sympy import sin, cos, exp
        >>> from sympy.abc import x, y, z

        >>> (x + x*y).as_independent(x)
        (0, x*y + x)
        >>> (x + x*y).as_independent(y)
        (x, x*y)
        >>> (2*x*sin(x) + y + x + z).as_independent(x)
        (y + z, 2*x*sin(x) + x)
        >>> (2*x*sin(x) + y + x + z).as_independent(x, y)
        (z, 2*x*sin(x) + x + y)

        -- self is a Mul

        >>> (x*sin(x)*cos(y)).as_independent(x)
        (cos(y), x*sin(x))

        non-commutative terms cannot always be separated out when self is a Mul

        >>> from sympy import symbols
        >>> n1, n2, n3 = symbols('n1 n2 n3', commutative=False)
        >>> (n1 + n1*n2).as_independent(n2)
        (n1, n1*n2)
        >>> (n2*n1 + n1*n2).as_independent(n2)
        (0, n1*n2 + n2*n1)
        >>> (n1*n2*n3).as_independent(n1)
        (1, n1*n2*n3)
        >>> (n1*n2*n3).as_independent(n2)
        (n1, n2*n3)
        >>> ((x-n1)*(x-y)).as_independent(x)
        (1, (x - y)*(x - n1))

        -- self is anything else:

        >>> (sin(x)).as_independent(x)
        (1, sin(x))
        >>> (sin(x)).as_independent(y)
        (sin(x), 1)
        >>> exp(x+y).as_independent(x)
        (1, exp(x + y))

        -- force self to be treated as an Add:

        >>> (3*x).as_independent(x, as_Add=True)
        (0, 3*x)

        -- force self to be treated as a Mul:

        >>> (3+x).as_independent(x, as_Add=False)
        (1, x + 3)
        >>> (-3+x).as_independent(x, as_Add=False)
        (1, x - 3)

        Note how the below differs from the above in making the
        constant on the dep term positive.

        >>> (y*(-3+x)).as_independent(x)
        (y, x - 3)

        -- use .as_independent() for true independence testing instead
           of .has(). The former considers only symbols in the free
           symbols while the latter considers all symbols

        >>> from sympy import Integral
        >>> I = Integral(x, (x, 1, 2))
        >>> I.has(x)
        True
        >>> x in I.free_symbols
        False
        >>> I.as_independent(x) == (I, 1)
        True
        >>> (I + x).as_independent(x) == (I, x)
        True

        Note: when trying to get independent terms, a separation method
        might need to be used first. In this case, it is important to keep
        track of what you send to this routine so you know how to interpret
        the returned values

        >>> from sympy import separatevars, log
        >>> separatevars(exp(x+y)).as_independent(x)
        (exp(y), exp(x))
        >>> (x + x*y).as_independent(y)
        (x, x*y)
        >>> separatevars(x + x*y).as_independent(y)
        (x, y + 1)
        >>> (x*(1 + y)).as_independent(y)
        (x, y + 1)
        >>> (x*(1 + y)).expand(mul=True).as_independent(y)
        (x, x*y)
        >>> a, b=symbols('a b', positive=True)
        >>> (log(a*b).expand(log=True)).as_independent(b)
        (log(a), log(b))

        See Also
        ========
        .separatevars(), .expand(log=True), Add.as_two_terms(),
        Mul.as_two_terms(), .as_coeff_add(), .as_coeff_mul()
        i   (   R1  (   t   _unevaluated_Add(   t   _unevaluated_Muli(   t   siftRG  c           s3   |  j      }  s | S| p2 |  j  |  j  @  S(   s   return the standard has() if there are no literal symbols, else
            check to see that symbol-deps are in the free symbols.(   R   R   (   R	  t	   has_other(   R4   t   sym(    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c           s
     |   S(   N(    (   Rc   (   R   (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     R   Ro   N(   t   symbolR1  R   Rd  R!  Re  t   sympy.utilities.iterablesRf  R   R   R3  t   funcR   R   R6   R.   R   R  t   identityR   R   RE  R   R   R&  t   extend(   R   t   depst   hintR1  Rd  Re  Rf  Rk  t   wantR   R   RB  t   dependt   indepRa   R   (    (   R   R4   Rh  s.   lib/python2.7/site-packages/sympy/core/expr.pyRS  J  sL    				

	c         K  sI   d d l  m } m } | j d  |  k r/ d S| |   | |   f Sd S(   s  Performs complex expansion on 'self' and returns a tuple
           containing collected both real and imaginary parts. This
           method can't be confused with re() and im() functions,
           which does not perform complex expansion at evaluation.

           However it is possible to expand both re() and im()
           functions and get exactly the same results as with
           a single call to this function.

           >>> from sympy import symbols, I

           >>> x, y = symbols('x,y', real=True)

           >>> (x + y*I).as_real_imag()
           (x, y)

           >>> from sympy.abc import z, w

           >>> (z + w*I).as_real_imag()
           (re(z) - im(w), re(w) + im(z))

        i(   Rl   Rk   t   ignoreN(   R2   Rl   Rk   R   R@   (   R   t   deept   hintsRl   Rk   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRg     s    c         C  s,   t  t  } | j t |  j   g   | S(   s  Return self as a dictionary of factors with each factor being
        treated as a power. The keys are the bases of the factors and the
        values, the corresponding exponents. The resulting dictionary should
        be used with caution if the expression is a Mul and contains non-
        commutative factors since the order that they appeared will be lost in
        the dictionary.

        See Also
        ========
        as_ordered_factors: An alternative for noncommutative applications,
                            returning an ordered list of factors.
        args_cnc: Similar to as_ordered_factors, but guarantees separation
                  of commutative and noncommutative factors.
        (   R   R]   t   updateR   t   as_base_exp(   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   as_powers_dict$  s    c         C  sQ   |  j    \ } } | j s- t j } |  } n  t t  } | j i | | 6 | S(   s  Return a dictionary mapping terms to their Rational coefficient.
        Since the dictionary is a defaultdict, inquiries about terms which
        were not present will return a coefficient of 0. If an expression is
        not an Add it is considered to have a single term.

        Examples
        ========

        >>> from sympy.abc import a, x
        >>> (3*x + a*x + 4).as_coefficients_dict()
        {1: 4, x: 3, a*x: 1}
        >>> _[a]
        0
        >>> (3*a*x).as_coefficients_dict()
        {a*x: 3}

        (   R   R5  R   R   R   R]   Rv  (   R   R   R  R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   as_coefficients_dict7  s    			c         C  s   |  t  j f S(   N(   R   R   (   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRw  Q  s    c         O  s5   | r% |  j  |   s% |  t   f Sn  t j |  f f S(   s  Return the tuple (c, args) where self is written as a Mul, ``m``.

        c should be a Rational multiplied by any factors of the Mul that are
        independent of deps.

        args should be a tuple of all other factors of m; args is empty
        if self is a Number or if self is independent of deps (when given).

        This should be used when you don't know if self is a Mul or not but
        you want to treat self as a Mul or if you want to process the
        individual arguments of the tail of self as a Mul.

        - if you know self is a Mul and want only the head, use self.args[0];
        - if you don't want to process the arguments of the tail but need the
          tail then use self.as_two_terms() which gives the head and tail;
        - if you want to split self into an independent and dependent parts
          use ``self.as_independent(*deps)``

        >>> from sympy import S
        >>> from sympy.abc import x, y
        >>> (S(3)).as_coeff_mul()
        (3, ())
        >>> (3*x*y).as_coeff_mul()
        (3, (x, y))
        >>> (3*x*y).as_coeff_mul(x)
        (3*y, (x,))
        >>> (3*y).as_coeff_mul(x)
        (3*y, ())
        (   R   R&   R   R   (   R   Rn  t   kwargs(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   as_coeff_mulU  s    c         G  s5   | r% |  j  |   s% |  t   f Sn  t j |  f f S(   s  Return the tuple (c, args) where self is written as an Add, ``a``.

        c should be a Rational added to any terms of the Add that are
        independent of deps.

        args should be a tuple of all other terms of ``a``; args is empty
        if self is a Number or if self is independent of deps (when given).

        This should be used when you don't know if self is an Add or not but
        you want to treat self as an Add or if you want to process the
        individual arguments of the tail of self as an Add.

        - if you know self is an Add and want only the head, use self.args[0];
        - if you don't want to process the arguments of the tail but need the
          tail then use self.as_two_terms() which gives the head and tail.
        - if you want to split self into an independent and dependent parts
          use ``self.as_independent(*deps)``

        >>> from sympy import S
        >>> from sympy.abc import x, y
        >>> (S(3)).as_coeff_add()
        (3, ())
        >>> (3 + x).as_coeff_add()
        (3, (x,))
        >>> (3 + x + y).as_coeff_add(x)
        (y + 3, (x,))
        >>> (3 + y).as_coeff_add(x)
        (y + 3, ())

        (   R   R&   R   R3  (   R   Rn  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   as_coeff_addx  s    c         C  sS   |  s t  j t  j f S|  j d t  \ } } | j rI | | } } n  | | f S(   sR  Return the positive Rational that can be extracted non-recursively
        from every term of self (i.e., self is treated like an Add). This is
        like the as_coeff_Mul() method but primitive always extracts a positive
        Rational (never a negative or a Float).

        Examples
        ========

        >>> from sympy.abc import x
        >>> (3*(x + 1)**2).primitive()
        (3, (x + 1)**2)
        >>> a = (6*x + 2); a.primitive()
        (2, 3*x + 1)
        >>> b = (x/2 + 3); b.primitive()
        (1/2, x + 6)
        >>> (a*b).primitive() == (1, a*b)
        True
        R   (   R   R   R3  R   R   Rt   (   R   R   R`   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt	   primitive  s    	c         C  s   t  j |  f S(   s  This method should recursively remove a Rational from all arguments
        and return that (content) and the new self (primitive). The content
        should always be positive and ``Mul(*foo.as_content_primitive()) == foo``.
        The primitive need not be in canonical form and should try to preserve
        the underlying structure if possible (i.e. expand_mul should not be
        applied to self).

        Examples
        ========

        >>> from sympy import sqrt
        >>> from sympy.abc import x, y, z

        >>> eq = 2 + 2*x + 2*y*(3 + 3*y)

        The as_content_primitive function is recursive and retains structure:

        >>> eq.as_content_primitive()
        (2, x + 3*y*(y + 1) + 1)

        Integer powers will have Rationals extracted from the base:

        >>> ((2 + 6*x)**2).as_content_primitive()
        (4, (3*x + 1)**2)
        >>> ((2 + 6*x)**(2*y)).as_content_primitive()
        (1, (2*(3*x + 1))**(2*y))

        Terms may end up joining once their as_content_primitives are added:

        >>> ((5*(x*(1 + y)) + 2*x*(3 + 3*y))).as_content_primitive()
        (11, x*(y + 1))
        >>> ((3*(x*(1 + y)) + 2*x*(3 + 3*y))).as_content_primitive()
        (9, x*(y + 1))
        >>> ((3*(z*(1 + y)) + 2.0*x*(3 + 3*y))).as_content_primitive()
        (1, 6.0*x*(y + 1) + 3*z*(y + 1))
        >>> ((5*(x*(1 + y)) + 2*x*(3 + 3*y))**2).as_content_primitive()
        (121, x**2*(y + 1)**2)
        >>> ((5*(x*(1 + y)) + 2.0*x*(3 + 3*y))**2).as_content_primitive()
        (1, 121.0*x**2*(y + 1)**2)

        Radical content can also be factored out of the primitive:

        >>> (2*sqrt(2) + 4*sqrt(10)).as_content_primitive(radical=True)
        (2, sqrt(2)*(1 + 2*sqrt(5)))

        If clear=False (default is True) then content will not be removed
        from an Add if it can be distributed to leave one or more
        terms with integer coefficients.

        >>> (x/2 + y).as_content_primitive()
        (1/2, x + 2*y)
        >>> (x/2 + y).as_content_primitive(clear=False)
        (1, x/2 + y)
        (   R   R   (   R   R   t   clear(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   as_content_primitive  s    7c         C  s   |  t  j f S(   s    expression -> a/b -> a, b

        This is just a stub that should be defined by
        an object's class methods to get anything else.

        See Also
        ========
        normal: return a/b instead of a, b
        (   R   R   (   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         C  s[   d d l  m } |  j   \ } } | t j k r5 | S| j rO | | d |  S| | Sd  S(   Ni   (   Re  (   R!  Re  R   R   R   RY   (   R   Re  R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   normal  s    	c         C  s  t  |  } |  t j k r d S| t j k r2 |  S| |  k rE t j S| j r | j   \ } } | t j k	 r t | | d t } q n  | j	 r | j
   \ } } |  j |  } | d k	 r | j |  Sn  |  | } |  j r|  t j k r| j rt j Sq|  t j k r7| j r$t j S| j rt j Sq|  t j k rY| j st j Sq|  j r| j sod S|  j r| j rd S| Sq|  j r| j sd S|  j r| j rd S| Sq|  j r| j sd S|  j r| j rd S| Sqn|  j s|  j s|  t j k r| j	 rnt | j  d k rn| j d j r| j d j r| j d |  k r| Sq| j r| j r| Sn|  j r|  j   \ } }	 | j r| t j k	 r| t j k	 r| j r| j |  }
 n | j |  }
 |
 d k	 r|
 |	 Sn  d S| |	 k r | Sg  } x= |	 j D]2 } | j |  } | d k rUd S| j |  q0W| t j k	 rt j g  | D] } | | ^ q St j |  Sn |  j	 rt |  j  } x t  |  D]; \ } } | j |  } | d k	 r| | | <t |   SqWn |  j! r| j! rc| j" |  j" k rc|  j# j$ | j#  } | d k	 r|  j" | Sq| |  j" k r|  j# j$ d  } | d k	 r|  j" | Sqn  d S(   s  Return None if it's not possible to make self in the form
           c * something in a nice way, i.e. preserving the properties
           of arguments of self.

           Examples
           ========

           >>> from sympy import symbols, Rational

           >>> x, y = symbols('x,y', real=True)

           >>> ((x*y)**3).extract_multiplicatively(x**2 * y)
           x*y**2

           >>> ((x*y)**3).extract_multiplicatively(x**4 * y)

           >>> (2*x).extract_multiplicatively(2)
           x

           >>> (2*x).extract_multiplicatively(3)

           >>> (Rational(1, 2)*x).extract_multiplicatively(3)
           x/6

        Ro   i   i    i   N(%   R   R   RZ   R@   R   R"   R}  R.   R   R$   t   as_two_termsRb  RY   R[   R{   R\   Rt   R   R   R   R5  t   is_Floatt   is_NumberSymbolR   R   R'   R   R/   R  R6   t
   _from_argsR   R&  R   R+  R+   t   extract_additively(   R   R   t   cct   pcR   R   Rc   t   quotientt   cst   pst   xct   newargsR,   t   newargt   tR   Ra   t   new_exp(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRb    s    		
		
		
	
						
!3		$	
	c         C  s  t  |  } |  t j k r d S| t j k r2 |  S| |  k rE t j S|  t j k rX d S|  j r | j sn d S|  } | | } | d k r | d k r | | k  s | d k  r | d k  r | | k r | Sd S| j r|  j   \ } } | j |  } | d k rd S| | S| j r| j	 d j r|  j
 |  } | j d  pJd | } | r|  | | } | | j |  pz| pd S| j   \ } } |  j   \ } }	 | j |  } | d k rd S|	 j |  }
 |
 d k rd S| |
 S|  j
 |  } | j d  pd | } | rE|  | | } | | j |  p=| pDd Sg  } x t j |  D] } | j   \ } } |  j
 |  } | sd S| j   \ } } | j |  } | d k rd S|  | | 8}  | j | | |  q[W| j |   t |   S(   s  Return self - c if it's possible to subtract c from self and
        make all matching coefficients move towards zero, else return None.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> e = 2*x + 3
        >>> e.extract_additively(x + 1)
        x + 2
        >>> e.extract_additively(3*x)
        >>> e.extract_additively(4)
        >>> (y*(x + 1)).extract_additively(x + 1)
        >>> ((x + 1)*(x + 2*y + 1) + 3).extract_additively(x + 1)
        (x + 1)*(x + 2*y) + 3

        Sometimes auto-expansion will return a less simplified result
        than desired; gcd_terms might be used in such cases:

        >>> from sympy import gcd_terms
        >>> (4*x*(y + 1) + y).extract_additively(x)
        4*x*(y + 1) + x*(4*y + 3) - x*(4*y + 4) + y
        >>> gcd_terms(_)
        x*(4*y + 3) + y

        See Also
        ========
        extract_multiplicatively
        coeff
        as_coefficient

        i    i   N(   R   R   RZ   R@   R3  RY   t   as_coeff_AddR  R"   R   R)   R6   R  R   R  (   R   R   RQ  R   R  t   xat   xa0t   ht   sht   stt   xa2t   coeffsR   t   act   att   coct   cot(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    st    "		
$$	c         C  s   d   |  j  D S(   s  
        Like ``free_symbols``, but returns the free symbols only if they are contained in an expression node.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> (x + y).expr_free_symbols
        {x, y}

        If the expression is contained in a non-expression object, don't return
        the free symbols. Compare:

        >>> from sympy import Tuple
        >>> t = Tuple(x + y)
        >>> t.expr_free_symbols
        set()
        >>> t.free_symbols
        {x, y}
        c         S  s&   h  |  ] } | j  D] } |  q q S(    (   t   expr_free_symbols(   R   Ra   RL  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <setcomp>	  s   	 (   R   (   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         C  sc  |  } |  | k r t  S|  j d  d k	 } | j d  d k	 } | | k rQ | S|  j r t |  j  } t g  |  j D] } | j   rv t  ^ qv  } | | } | | k r t  S| | k  rCt Sn |  j rC|  j	   \ } }	 t
 j |  t
 j |	  }
 g  |
 D] } | j   ^ q} t t d |   } t |  d d k St |  j   | j   k   Sd S(   s  Return True if self is not in a canonical form with respect
        to its sign.

        For most expressions, e, there will be a difference in e and -e.
        When there is, True will be returned for one and False for the
        other; False will be returned if there is no difference.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> e = x - y
        >>> {i.could_extract_minus_sign() for i in (e, -e)}
        {False, True}

        ii   i   N(   R   Rb  R@   R"   R'   R   t   could_extract_minus_signR   R$   R   R.   R  R   t   filterR   R   (   R   t   negative_selft   self_has_minust   negative_self_has_minust   all_argsR,   t   negative_argst   positive_argst   numt   denR   t	   arg_signs(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR  	  s,    	.
	c         C  s  d d l  m } m } m } m } m } t d  } t d  } t j |   }	 g  }
 x: |	 D]2 } t	 | |  r |
 | j
 g 7}
 q\ | | 9} q\ Wt d  } g  } xz |
 r |
 j   } | j r |
 | j 7}
 q n  | j r| j | |  } | d k	 r| | 7} q qn  | | g 7} q W| j r9| } d } n | j | j   \ } } | | d t d  d  d } | | d 7} | | } | r| j d  } | d k	 r| t d  d 7} | } qn  | | | | f |   | |   } | d k r| | |  9} n  | | f S(   sc  
        Try to write self as ``exp_polar(2*pi*I*n)*z`` in a nice way.
        Return (z, n).

        >>> from sympy import exp_polar, I, pi
        >>> from sympy.abc import x, y
        >>> exp_polar(I*pi).extract_branch_factor()
        (exp_polar(I*pi), 0)
        >>> exp_polar(2*I*pi).extract_branch_factor()
        (1, 1)
        >>> exp_polar(-pi*I).extract_branch_factor()
        (exp_polar(I*pi), -1)
        >>> exp_polar(3*pi*I + x).extract_branch_factor()
        (exp_polar(x + I*pi), 1)
        >>> (y*exp_polar(-5*pi*I)*exp_polar(3*pi*I + 2*pi*x)).extract_branch_factor()
        (y*exp_polar(2*pi*x), -1)
        >>> exp_polar(-I*pi/2).extract_branch_factor()
        (exp_polar(-I*pi/2), 0)

        If allow_half is True, also extract exp_polar(I*pi):

        >>> exp_polar(I*pi).extract_branch_factor(allow_half=True)
        (1, 1/2)
        >>> exp_polar(2*I*pi).extract_branch_factor(allow_half=True)
        (1, 1)
        >>> exp_polar(3*I*pi).extract_branch_factor(allow_half=True)
        (1, 3/2)
        >>> exp_polar(-I*pi).extract_branch_factor(allow_half=True)
        (1, -1/2)
        i(   t	   exp_polart   pit   It   ceilingR6   i    i   i   N(    (   R2   R  R  R  R  R6   R   R.   R  R   R+   R4  R"   R   R$   Rc  R@   RW   R|  R   R  (   R   t
   allow_halfR  R  R  R  R6   R   t   resR   t   expsR,   t   piimultt   extrasR+   R)   t   tailt
   branchfactR   RB  t   newexp(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   extract_branch_factor6	  sL    (			
			"
%c         C  s&   |  j  j |  t g   k r" t St S(   N(   R   RU  R   R   R   (   R   R8  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_is_polynomial	  s    c         G  sZ   | r t  t t |   } n	 |  j } | j |  j  t  g   k rI t S|  j |  Sd S(   s  
        Return True if self is a polynomial in syms and False otherwise.

        This checks if self is an exact polynomial in syms.  This function
        returns False for expressions that are "polynomials" with symbolic
        exponents.  Thus, you should be able to apply polynomial algorithms to
        expressions for which this returns True, and Poly(expr, \*syms) should
        work if and only if expr.is_polynomial(\*syms) returns True. The
        polynomial does not have to be in expanded form.  If no symbols are
        given, all free symbols in the expression will be used.

        This is not part of the assumptions system.  You cannot do
        Symbol('z', polynomial=True).

        Examples
        ========

        >>> from sympy import Symbol
        >>> x = Symbol('x')
        >>> ((x**2 + 1)**4).is_polynomial(x)
        True
        >>> ((x**2 + 1)**4).is_polynomial()
        True
        >>> (2**x + 1).is_polynomial(x)
        False


        >>> n = Symbol('n', nonnegative=True, integer=True)
        >>> (x**n + 1).is_polynomial(x)
        False

        This function does not attempt any nontrivial simplifications that may
        result in an expression that does not appear to be a polynomial to
        become one.

        >>> from sympy import sqrt, factor, cancel
        >>> y = Symbol('y', positive=True)
        >>> a = sqrt(y**2 + 2*y + 1)
        >>> a.is_polynomial(y)
        False
        >>> factor(a)
        y + 1
        >>> factor(a).is_polynomial(y)
        True

        >>> b = (y**2 + 2*y + 1)/(y + 1)
        >>> b.is_polynomial(y)
        False
        >>> cancel(b)
        y + 1
        >>> cancel(b).is_polynomial(y)
        True

        See also .is_rational_function()

        N(   R   t   mapR   R   RU  R   R  (   R   R8  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   is_polynomial	  s    9	c         C  s&   |  j  j |  t g   k r" t St S(   N(   R   RU  R   R   R   (   R   R8  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_is_rational_function	  s    c         G  s   |  t  j t  j t  j t  j g k r) t S| rG t t t |   } n	 |  j } | j	 |  j  t g   k rr t
 S|  j |  Sd S(   s	  
        Test whether function is a ratio of two polynomials in the given
        symbols, syms. When syms is not given, all free symbols will be used.
        The rational function does not have to be in expanded or in any kind of
        canonical form.

        This function returns False for expressions that are "rational
        functions" with symbolic exponents.  Thus, you should be able to call
        .as_numer_denom() and apply polynomial algorithms to the result for
        expressions for which this returns True.

        This is not part of the assumptions system.  You cannot do
        Symbol('z', rational_function=True).

        Examples
        ========

        >>> from sympy import Symbol, sin
        >>> from sympy.abc import x, y

        >>> (x/y).is_rational_function()
        True

        >>> (x**2).is_rational_function()
        True

        >>> (x/sin(y)).is_rational_function(y)
        False

        >>> n = Symbol('n', integer=True)
        >>> (x**n + 1).is_rational_function(x)
        False

        This function does not attempt any nontrivial simplifications that may
        result in an expression that does not appear to be a rational function
        to become one.

        >>> from sympy import sqrt, factor
        >>> y = Symbol('y', positive=True)
        >>> a = sqrt(y**2 + 2*y + 1)/y
        >>> a.is_rational_function(y)
        False
        >>> factor(a)
        (y + 1)/y
        >>> factor(a).is_rational_function(y)
        True

        See also is_algebraic_expr().

        N(   R   RZ   R[   R   R   R   R  R   R   RU  R   R  (   R   R8  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   is_rational_function	  s    3%	c         C  s&   |  j  j |  t g   k r" t St S(   N(   R   RU  R   R   R   (   R   R8  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_is_algebraic_expr
  s    c         G  sZ   | r t  t t |   } n	 |  j } | j |  j  t  g   k rI t S|  j |  Sd S(   s  
        This tests whether a given expression is algebraic or not, in the
        given symbols, syms. When syms is not given, all free symbols
        will be used. The rational function does not have to be in expanded
        or in any kind of canonical form.

        This function returns False for expressions that are "algebraic
        expressions" with symbolic exponents. This is a simple extension to the
        is_rational_function, including rational exponentiation.

        Examples
        ========

        >>> from sympy import Symbol, sqrt
        >>> x = Symbol('x', real=True)
        >>> sqrt(1 + x).is_rational_function()
        False
        >>> sqrt(1 + x).is_algebraic_expr()
        True

        This function does not attempt any nontrivial simplifications that may
        result in an expression that does not appear to be an algebraic
        expression to become one.

        >>> from sympy import exp, factor
        >>> a = sqrt(exp(x)**2 + 2*exp(x) + 1)/(exp(x) + 1)
        >>> a.is_algebraic_expr(x)
        False
        >>> factor(a).is_algebraic_expr()
        True

        See Also
        ========
        is_rational_function()

        References
        ==========

        - https://en.wikipedia.org/wiki/Algebraic_expression

        N(   R   R  R   R   RU  R   R  (   R   R8  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   is_algebraic_expr
  s    *	i    i   R   c      
     s  d d l  m } m } m   m } m }	 m }
  d k r} |  j } | sM |  St	 |  d k rn t
 d   n  | j    n  t  |	  r  |  j k } n( |   } | |  j i |  6 j k } | s | d k r d   |  g D S|  Sn  t	 |  d k s| d k rt
 d   n  | t j t j g k r| t j k rJd n d  |  j     j  d | d	 d
 } | d k r  f d   | D S| j     S| s| d k ro| d k r | }   |  n  | }   |  |  j  |  j  d d d | d	 d
 d | } | d k r[   f d   | D S| j     S j  j k od k n s j t k	 r| d d t d t  |  j    j  | | | d | } | d k r  f d   | D S| j    Sn  | d k	 rW|  j  d | d | } | j   pQt j } | r| j   } | | k r| d k r| | j   | | |   7} q|   d   7} n| | k  rx t d d  D] } |  j  d | | d | } | j   } | | k r| |
 | | | | |  } |  j  d | d | } x; | j   | k  r|  j  d | d | } | d 7} qQWPqqWt
 d t |  |  f   |    |   7} n  | j   } | j   } nA    |   } | j   } | | j   | k r&t j } n  y | |   | SWqt k
 rS| | SXn1    f d   } | |  j   j   d |  Sd S(   s  
        Series expansion of "self" around ``x = x0`` yielding either terms of
        the series one by one (the lazy series given when n=None), else
        all the terms at once when n != None.

        Returns the series expansion of "self" around the point ``x = x0``
        with respect to ``x`` up to ``O((x - x0)**n, x, x0)`` (default n is 6).

        If ``x=None`` and ``self`` is univariate, the univariate symbol will
        be supplied, otherwise an error will be raised.

        >>> from sympy import cos, exp
        >>> from sympy.abc import x, y
        >>> cos(x).series()
        1 - x**2/2 + x**4/24 + O(x**6)
        >>> cos(x).series(n=4)
        1 - x**2/2 + O(x**4)
        >>> cos(x).series(x, x0=1, n=2)
        cos(1) - (x - 1)*sin(1) + O((x - 1)**2, (x, 1))
        >>> e = cos(x + exp(y))
        >>> e.series(y, n=2)
        cos(x + 1) - y*sin(x + 1) + O(y**2)
        >>> e.series(x, n=2)
        cos(exp(y)) - x*sin(exp(y)) + O(x**2)

        If ``n=None`` then a generator of the series terms will be returned.

        >>> term=cos(x).series(n=None)
        >>> [next(term) for i in range(2)]
        [1, -x**2/2]

        For ``dir=+`` (default) the series is calculated from the right and
        for ``dir=-`` the series from the left. For smooth functions this
        flag will not alter the results.

        >>> abs(x).series(dir="+")
        x
        >>> abs(x).series(dir="-")
        -x

        i(   t   collectRU   t   Ordert   RationalR1  R  i   s+   x must be given for multivariate functions.c         s  s   |  ] } | Vq d  S(   N(    (   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>
  s    s   +-s   Dir must be '+' or '-'R   t   dirR   c         3  s%   |  ] } | j       Vq d  S(   N(   RV   (   R   R   (   t   sgnRc   (    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>
  s    R   t   x0i    t   logxc         3  s%   |  ] } | j       Vq d  S(   N(   RV   (   R   R   (   t   rep2t   rep2bRc   (    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>
  s    Rc   R2  t   finitec         3  s!   |  ] } | j      Vq d  S(   N(   RV   (   R   R   (   Rc   t   xpos(    s.   lib/python2.7/site-packages/sympy/core/expr.pys	   <genexpr>
  s    i	   s#   Could not calculate %s terms for %sc         3  s   x |  D] } | j  s! | Vq n  d }   |    } d } t | j  } x | | | j   } |  9} | sR | j r qR n  | j  r | t | j  7} n
 | d 7} | V| | k r Pn  | | 7} qR Wq Wd S(   s&   Return terms of lseries one at a time.i    i   N(   R"   R'   R   R/  R  (   R   R   t   yieldedR6  t   ndidt   ndot   do(   R  Rc   (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   yield_lseries
  s(    	
	
N(!   R2   R  RU   R  R  R1  R  R@   R   R'   RC   R4  R   t   xreplaceR   R[   R\   RV   t   seriesR{   Rt   R   R   t   _eval_nseriesR0  R3  R9  R   R!   R/  t   doitR   t   _eval_lseries(   R   Rc   R  R   R  R  R  RU   R  R1  R  R8  t   depR   R   t   rept   rvt   s1R6  t   ngott   moret   newnR  t   s1doneR  (    (   R  R  R  R  Rc   R  s.   lib/python2.7/site-packages/sympy/core/expr.pyR  L
  s    *.		+	
31*& c         G  st   d d l  m } m } t |  } | d  } |  j | |  j | |  j | |  j | d  | | | |  S(   s   General method for the taylor term.

        This method is slow, because it differentiates n-times. Subclasses can
        redefine it to make it faster by using the "previous_terms".
        i(   RU   t	   factorialRc   i    (   R2   RU   R  R   RV   R   (   R   R   Rc   t   previous_termsRU   R  t   _x(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   taylor_term
  s    c      	   C  s"   |  j  | | d d d | d | S(   s  
        Wrapper for series yielding an iterator of the terms of the series.

        Note: an infinite series will yield an infinite iterator. The following,
        for exaxmple, will never terminate. It will just keep printing terms
        of the sin(x) series::

          for term in sin(x).lseries(x):
              print term

        The advantage of lseries() over nseries() is that many times you are
        just interested in the next term in the series (i.e. the first term for
        example), but you don't know how many you should ask for in nseries()
        using the "n" parameter.

        See also nseries().
        R   R  R  N(   R  R@   (   R   Rc   R  R  R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   lseries  s    c         c  s   d } |  j  | d | d | } | j sJ | j rA | j   Vn | Vd  Sx2 | j r~ | d 7} |  j  | d | d | } qM W| j   } | VxU x? | d 7} |  j  | d | d | j   } | | k r Pq q W| | V| } q Wd  S(   Ni    R   R  i   (   R  R  R"   R/  (   R   Rc   R  R   R  R	  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s(    		

!	c         C  sj   | r | |  j  k r |  S| d k s7 | s7 | d k rM |  j | | | |  S|  j | d | d | Sd S(   s-  
        Wrapper to _eval_nseries if assumptions allow, else to series.

        If x is given, x0 is 0, dir='+', and self has x, then _eval_nseries is
        called. This calculates "n" terms in the innermost expressions and
        then builds up the final series just by "cross-multiplying" everything
        out.

        The optional ``logx`` parameter can be used to replace any log(x) in the
        returned series with a symbolic value to avoid evaluating log(x) at 0. A
        symbol to use in place of log(x) should be provided.

        Advantage -- it's fast, because we don't have to determine how many
        terms we need to calculate in advance.

        Disadvantage -- you may end up with less terms than you may have
        expected, but the O(x**n) term appended will always be correct and
        so the result, though perhaps shorter, will also be correct.

        If any of those assumptions is not met, this is treated like a
        wrapper to series which will try harder to return the correct
        number of terms.

        See also lseries().

        Examples
        ========

        >>> from sympy import sin, log, Symbol
        >>> from sympy.abc import x, y
        >>> sin(x).nseries(x, 0, 6)
        x - x**3/6 + x**5/120 + O(x**6)
        >>> log(x+1).nseries(x, 0, 5)
        x - x**2/2 + x**3/3 - x**4/4 + O(x**5)

        Handling of the ``logx`` parameter --- in the following example the
        expansion fails since ``sin`` does not have an asymptotic expansion
        at -oo (the limit of log(x) as x approaches 0):

        >>> e = sin(log(x))
        >>> e.nseries(x, 0, 6)
        Traceback (most recent call last):
        ...
        PoleError: ...
        ...
        >>> logx = Symbol('logx')
        >>> e.nseries(x, 0, 6, logx=logx)
        sin(logx)

        In the following example, the expansion works but gives only an Order term
        unless the ``logx`` parameter is used:

        >>> e = x**y
        >>> e.nseries(x, 0, 2)
        O(log(x)**2)
        >>> e.nseries(x, 0, 2, logx=logx)
        exp(logx*y)

        R   R   R  N(   R   R@   R  R  (   R   Rc   R  R   R  R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   nseries7  s
    <c         C  s-   d d l  m } t | d |  j    d S(   s@  
        Return terms of series for self up to O(x**n) at x=0
        from the positive direction.

        This is a method that should be overridden in subclasses. Users should
        never call this method directly (use .nseries() instead), so you don't
        have to write docstrings for _eval_nseries().
        i(   t
   filldedents   
                     The _eval_nseries method should be added to
                     %s to give terms up to O(x**n) at x=0
                     from the positive direction so it is available when
                     nseries calls it.N(   t   sympy.utilities.miscR  R   Rk  (   R   Rc   R   R  R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR  z  s    	c         C  s#   d d l  m } | |  | | |  S(   s     Compute limit x->xlim.
        i(   R   (   t   sympy.series.limitsR   (   R   Rc   t   xlimR  R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         C  s   d d l  m } m } d d l m } |  j   d k r< |  S| d k r{ | d  } | |  | |  j | | |   } n | |  | |  } | j |  S(   s   
        as_leading_term is only allowed for results of .series()
        This is a wrapper to compute a series first.
        i(   RU   R   (   t   calculate_seriesi    R  N(	   R2   RU   R   t   sympy.series.gruntzR  R/  R@   RV   t   as_leading_term(   R   Rc   R  RU   R   R  R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   compute_leading_term  s    'c         G  s   d d l  m } t |  d k rL |  } x | D] } | j |  } q/ W| S| sV |  St | d  } | j s t d |   n  | |  j k r |  S|  j |  } | d
 k	 r | | d t
 d d St d	 |  | f   d
 S(   s  
        Returns the leading (nonzero) term of the series expansion of self.

        The _eval_as_leading_term routines are used to do this, and they must
        always return a non-zero value.

        Examples
        ========

        >>> from sympy.abc import x
        >>> (1 + x + x**2).as_leading_term(x)
        1
        >>> (1/x**2 + x + x**2).as_leading_term(x)
        x**(-2)

        i(   t   powsimpi   i    s   expecting a Symbol but got %sRt  t   combineR+   s   as_leading_term(%s, %s)N(   R2   R  R'   R  R   t	   is_symbolRC   R   t   _eval_as_leading_termR@   R   R   (   R   t   symbolsR  R   Rc   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s"    	c         C  s   |  S(   N(    (   R   Rc   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         C  s   d d l  m } | |  |  } | j |  \ } } t |  d k ru | d j   \ } } | | k ru | | f Sn  | t j f S(   sC    ``c*x**e -> c,e`` where x can be any symbolic expression.
        i(   R  i   i    (   R2   R  R{  R'   Rw  R   R3  (   R   Rc   R  R   R   t   pR   R	  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   as_coeff_exponent  s    c   	      C  s   d d l  m } m } |  j |  } | d  } | j | |   ra | j | |  |  } n  | j |  \ } } | | j k r d d l m	 } t
 | d |  | | f    n  | j | | |   } | | f S(   s   
        Returns the leading term a*x**b as a tuple (a, b).

        Examples
        ========

        >>> from sympy.abc import x
        >>> (1+x+x**2).leadterm(x)
        (1, 0)
        >>> (1/x**2+x+x**2).leadterm(x)
        (1, -2)

        i(   RU   R   R  (   R  sw   
                cannot compute leadterm(%s, %s). The coefficient
                should have been free of x but got %s(   R2   RU   R   R  R   RV   R  R   R  R  RC   (	   R   Rc   RU   R   RM  R   R   R	  R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   leadterm  s    c         C  s   t  j |  f S(   s2   Efficiently extract the coefficient of a product. (   R   R   (   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         C  s   t  j |  f S(   s4   Efficiently extract the coefficient of a summation. (   R   R3  (   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    i   c   	   	   C  s/   d d l  m } | |  | | | | | | |  S(   s   
        Compute formal power power series of self.

        See the docstring of the :func:`fps` function in sympy.series.formal for
        more information.
        i(   t   fps(   t   sympy.series.formalR  (	   R   Rc   R  R  t   hyperR   R   t   fullR  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         C  s   d d l  m } | |  |  S(   s   Compute fourier sine/cosine series of self.

        See the docstring of the :func:`fourier_series` in sympy.series.fourier
        for more information.
        i(   t   fourier_series(   t   sympy.series.fourierR  (   R   t   limitsR  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         O  s    | j  d t  t |  | |  S(   NRo   (   t
   setdefaultR   t
   Derivative(   R   R  t   assumptions(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         K  s$   |  j  |   \ } } | t j | S(   N(   Rg   R   R   (   R   Ru  R$  R%  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_expand_complex   s    c   	      K  s   t  } | r t |  d d  r |  j r g  } xC |  j D]8 } t j | | |  \ } } | | O} | j |  q8 W| r |  j |   }  q n  t |  |  r t |  |  |   } | |  k r | t	 f Sn  |  | f S(   s  
        Helper for ``expand()``.  Recursively calls ``expr._eval_expand_hint()``.

        Returns ``(expr, hit)``, where expr is the (possibly) expanded
        ``expr`` and ``hit`` is ``True`` if ``expr`` was truly expanded and
        ``False`` otherwise.
        R   (    (
   R   R   R    R   R   t   _expand_hintR  Rk  R   R   (	   R*   Ro  Rt  Ru  R`  t   sargsR,   t   arghitt   newexpr(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR  $  s    	"
c	         K  s  d d l  m }
 |	 j d | d | d | d | d | d |  |  } |	 j d	 t  r g  |
 |   D]! } | j d
 | d | |	  ^ qc \ } } | | S|	 j d t  r |
 |   \ } } | | j d
 | d | |	  S|	 j d t  r|
 |   \ } } | j d
 | d | |	  | Sd   } x^ t |	 j   d | D]D } |	 | } | r<d | } t j	 | | d
 | |	 \ } } q<q<Wx t
 rH| } |	 j d t  rt j	 | d d
 | |	 \ } } n  |	 j d t  rt j	 | d d
 | |	 \ } } n  |	 j d t  r5t j	 | d d
 | |	 \ } } n  | | k rPqqW| d k	 rt |  } | j sw| d k rt d |   n  g  } xS t j |  D]B } | j d t
  \ } } | | ;} | r| j | |  qqWt |   } n  | S(   s   
        Expand an expression using hints.

        See the docstring of the expand() function in sympy.core.function for
        more information.

        i(   t   fractiont
   power_baset	   power_expR!  R   t   multinomialt   basict   fracRt  t   modulust   denomt   numerc         S  s   |  d k r d S|  S(   s    Make multinomial come before mulR!  t   mulz(    (   Ro  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _expand_hint_keym  s    R   t   _eval_expand_t   _eval_expand_multinomialt   _eval_expand_mult   _eval_expand_logi    s*   modulus must be a positive integer, got %sR   N(   t   sympy.simplify.radsimpR  Rv  R4  R   t   expandR  t   keysR   R  R   R   R@   R   R   RC   R6   R  R   R  (   R   Rt  R  R  R   R!  R   R  R  Ru  R  R*   R   R   R   R  Ro  t   use_hintR`  t   wasR  R  R  R)   R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR  B  s\    
7	

(	
c         O  s    d d l  m } | |  | |  S(   s-   See the integrate function in sympy.integralsi(   t	   integrate(   t   sympy.integralsR  (   R   R   Rz  R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    g333333?c         C  s<   d d l  m } d d l m } | p) | } | |  | |  S(   s+   See the simplify function in sympy.simplifyi(   R   (   R:  (   t   sympy.simplifyR   t   sympy.core.functionR:  (   R   t   ratiot   measureR   t   inverseR   R:  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         C  s#   d d l  m } | |  | | |  S(   s,   See the nsimplify function in sympy.simplifyi(   R   (   R  R   (   R   t	   constantst	   toleranceR  R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         C  s&   d d l  m } | |  d | d | S(   s+   See the separate function in sympy.simplifyi(   t   expand_power_baseRt  t   force(   R  R  (   R   Rt  R  R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   separate  s    c         C  s)   d d l  m } | |  | | | | |  S(   s*   See the collect function in sympy.simplifyi(   R  (   R  R  (   R   R8  Rk  Ro   t   exactt   distribute_order_termR  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         O  s    d d l  m } | |  | |  S(   s(   See the together function in sympy.polysi(   t   together(   t   sympy.polysR   (   R   R   Rz  R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         K  s    d d l  m } | |  | |  S(   s%   See the apart function in sympy.polysi(   t   apart(   R!  R"  (   R   Rc   R   R"  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR"    s    c         C  s   d d l  m } | |   S(   s*   See the ratsimp function in sympy.simplifyi(   t   ratsimp(   R  R#  (   R   R#  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR#    s    c         K  s   d d l  m } | |  |  S(   s+   See the trigsimp function in sympy.simplifyi(   t   trigsimp(   R  R$  (   R   R   R$  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR$    s    c         K  s   d d l  m } | |  |  S(   s*   See the radsimp function in sympy.simplifyi(   t   radsimp(   R  R%  (   R   Rz  R%  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR%    s    c         O  s    d d l  m } | |  | |  S(   s*   See the powsimp function in sympy.simplifyi(   R  (   R  R  (   R   R   Rz  R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         C  s   d d l  m } | |   S(   s+   See the combsimp function in sympy.simplifyi(   t   combsimp(   R  R&  (   R   R&  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR&    s    c         C  s   d d l  m } | |   S(   s,   See the gammasimp function in sympy.simplifyi(   t	   gammasimp(   R  R'  (   R   R'  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR'    s    c         O  s    d d l  m } | |  | |  S(   s2   See the factor() function in sympy.polys.polytoolsi(   R*  (   R!  R*  (   R   R  R   R*  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR*    s    c         C  s   d d l  m } | |  |  S(   s,   See the refine function in sympy.assumptionsi(   t   refine(   t   sympy.assumptionsR(  (   R   t
   assumptionR(  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR(    s    c         O  s    d d l  m } | |  | |  S(   s&   See the cancel function in sympy.polysi(   R   (   R!  R   (   R   R  R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR     s    c         O  s[   d d l  m } d d l m } |  j rH t | d t  rH | |  |  S| |  | | |  S(   s   Return the multiplicative inverse of ``self`` mod ``g``
        where ``self`` (and ``g``) may be symbolic expressions).

        See Also
        ========
        sympy.core.numbers.mod_inverse, sympy.polys.polytools.invert
        i(   t   invert(   R?   RW   (   t   sympy.polys.polytoolsR+  RB   R?   RW   R   R   (   R   R.  R  R   R+  R?   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR+    s
    c         C  s  d d l  m } |  } | j s. t d   n  | j s | j d  } t | d t s t d t t | d |  d t	 |     q n( | t
 j t
 j t
 j t
 j f k r | S| j s | j   \ } } | j |  t
 j | j |  S| s | St |  } g  | j |  D] } | j ^ q} | rCt t |   n d }	 t |  }
 |
 | } } |	 d k	 r| |	 k r|	 } n  t d	 |  } | } | d
 d | 7} d	 | | j |	 d k	 r|	 n | d
  } | j r5| d
 d | } d	 | | j |	 d k	 r|	 n | d
  } t |  d	 } n t |  d	 } d
 } | d k r`| } n | d k  ry| | } n  t | |  } | j r| t |  |  S| r| |  k r| d
 7} n  | | |  Sd S(   s  Return x rounded to the given decimal place.

        If a complex number would results, apply round to the real
        and imaginary components of the number.

        Examples
        ========

        >>> from sympy import pi, E, I, S, Add, Mul, Number
        >>> S(10.5).round()
        11.
        >>> pi.round()
        3.
        >>> pi.round(2)
        3.14
        >>> (2*pi + E*I).round()
        6. + 3.*I

        The round method has a chopping effect:

        >>> (2*pi + I/10).round()
        6.
        >>> (pi/10 + 2*I).round()
        2.*I
        >>> (pi/10 + E*I).round(2)
        0.31 + 2.72*I

        Notes
        =====

        Do not confuse the Python builtin function, round, with the
        SymPy method of the same name. The former always returns a float
        (or raises an error if applied to a complex value) while the
        latter returns either a Number or a complex number:

        >>> isinstance(round(S(123), -2), Number)
        False
        >>> isinstance(S(123).round(-2), Number)
        True
        >>> isinstance((3*I).round(), Mul)
        True
        >>> isinstance((1 + 3*I).round(), Add)
        True

        i(   R   s   can't round symbolic expressioni   R   s   Expected a number but got %s:Rk  t   __name__i
   i   i    N(    R2   R   RW   RD   R    R   R	   R   R   t   typeR   RZ   R[   R\   R   Rq   Rg   RX   R   R]   R   R   R   R   R@   t   _magR=   Rt   R   R  R   R!   (   R   R  R   Rc   t   xnRa   R`   t   ft   precst   dpst   mag_first_digt   allowt   digits_neededt   magt   xwast   i10R  t   q(    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRX      sV    .		.$	!%	-	-		c         C  s5   d d l  m } | t j t j d |  j |  g S(   Ni(   t   _LeftRightArgst   higher(   t"   sympy.matrices.expressions.matexprR;  R   R   t   _eval_derivative(   R   Rc   R;  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   _eval_derivative_matrix_lines_  s    (   R-  t
   __module__t   __doc__t	   __slots__R   t	   is_scalart   propertyR   R   R@   R   t   _op_priorityR-   R0   R3   R
   RE   R   R7   R5   R9   R8   R;   R:   R>   RI   R<   RK   RJ   t   __truediv__t   __rtruediv__RN   RL   RR   RO   RT   RS   Re   t   __long__Ri   Rm   Rx   R|   R~   R   R   t   staticmethodR   RW   R   R   R   R^   R   R   R   R   R   R   R   R   R   R   t   classmethodR  R%   R#   R  R/  R0  R9  R:  RE  R)   Ra  Rc  RS  Rg   Rx  Ry  Rw  R{  R|  R}  R  R   R  Rb  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R   R  R  R  R  R  R   R  R  R  R   R  R  R  R  R   R   R  R  R   R"  R#  R$  R%  R  R&  R'  R*  R(  R   R+  RX   R?  (    (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR      s&  (*				$							4N	l	#	#	V								%6	8			+< 		D					#	$	9		
		i	.I		D		A		9	C	$					\											_t
   AtomicExprc           B  se   e  Z d  Z e Z e Z g  Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z e d    Z RS(   s   
    A parent class for object which are both atoms and Exprs.

    For example: Symbol, Number, Rational, Integer, ...
    But not: Add, Mul, Pow, ...
    c         C  s   |  | k r t  j St  j S(   N(   R   R   R3  (   R   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR>  p  s    c         C  s   d d l  m } m } d d l  m } m } d d l m } t | | | t | f  rp t	 t
 |   j | |  S|  | k r | |  | | d  f d | | d  f d t f  S| |  | | d  f d t f  Sd  S(   Ni(   t	   Piecewiset   Eq(   t   Tuplet
   MatrixExpr(   t   MatrixCommoni    i   (   R2   RL  RM  RN  RO  t   sympy.matrices.commonRP  R   R   t   superRK  t   _eval_derivative_n_timesR   (   R   R   R   RL  RM  RN  RO  RP  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRS  u  s    4c         C  s   t  S(   N(   R   (   R   R8  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         C  s   t  S(   N(   R   (   R   R8  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         C  s   t  S(   N(   R   (   R   R8  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         C  s   |  S(   N(    (   R   Rc   R   R  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    c         C  s   |  h S(   N(    (   R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    (   R-  R@  RA  R   RW   R   R    RB  R>  RS  R  R  R  R  RD  R  (    (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRK  d  s   						c         C  s   d d l  m } m } m } d d l m } t |  j    } | sK t j	 Sy t
 | | |    } WnE t t f k
 r t
 | | t | j d   | d    } n X| d | d k r d | d | k o d k  n s t  | d 7} n  | S(   s   Return integer ``i`` such that .1 <= x/10**i < 1

    Examples
    ========

    >>> from sympy.core.expr import _mag
    >>> from sympy import Float
    >>> _mag(Float(.1))
    0
    >>> _mag(Float(.01))
    -1
    >>> _mag(Float(1234))
    4
    i(   t   log10t   ceilR   (   R   i5   i
   i   (   t   mathRT  RU  R   R2   R   R   R   R   R3  R]   RC   t   OverflowErrorR   R   t   AssertionError(   Rc   RT  RU  R   R   R  R4  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR/    s    2*t   UnevaluatedExprc           B  s    e  Z d  Z d   Z d   Z RS(   s   
    Expression that is not evaluated unless released.

    Examples
    ========

    >>> from sympy import UnevaluatedExpr
    >>> from sympy.abc import a, b, x, y
    >>> x*(1/x)
    1
    >>> x*UnevaluatedExpr(1/x)
    x*1/x

    c         K  s%   t  |  } t j |  | |  } | S(   N(   R   R   t   __new__(   R  R,   Rz  R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRZ    s    c         K  s5   | j  d t  r& |  j d j |   S|  j d Sd  S(   NRt  i    (   R   R   R   R  (   R   Rz  (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyR    s    (   R-  R@  RA  RZ  R  (    (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRY    s   	c         C  s9   |  j  r5 | j  r5 |  | j d  } | j  r5 | Sn  d S(   s   Return (a - b).evalf(2) if a and b are comparable, else None.
    This should only be used when a and b are already sympified.
    i   N(   R   R_   (   R   R   Rw   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyRr     s    	c         G  sG   |  |   } | j  |  k oF | j t g  | D] } t |  ^ q+  k S(   s  Return True if `func` applied to the `args` is unchanged.
    Can be used instead of `assert foo == foo`.

    Examples
    ========

    >>> from sympy.core.expr import unchanged
    >>> from sympy.functions.elementary.trigonometric import cos
    >>> from sympy.core.numbers import pi

    >>> unchanged(cos, 1)  # instead of assert cos(1) == cos(1)
    True

    >>> unchanged(cos, pi)
    False
    (   Rk  R   R&   R   (   Rk  R   R1  R   (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt	   unchanged  s    (   R.   (   R6   (   R=   (   R  R   (   RM   (   R   (   R   R  N(4   t
   __future__R    R   R   R   R   R  R   R   t	   singletonR   R_   R   R	   t
   decoratorsR
   R   t   cacheR   t   compatibilityR   R   R   R   R   t   mpmath.libmpR   R   t   collectionsR   R   RK  R/  RY  Rr   R[  R!  R.   R   R6   RH   R=   R;  R  R   RF   RM   R"  R   R  R   R  (    (    (    s.   lib/python2.7/site-packages/sympy/core/expr.pyt   <module>   sF   (             c-			