
\c           @  sQ  d  Z  d d l m Z m Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z m Z m Z d d	 l m Z m Z m Z m Z m Z m Z m Z m Z m Z d d
 l m Z d d l m Z d   Z d e e
  f d     YZ d e f d     YZ  d   Z! e" d  Z# d e f d     YZ$ d   Z% d S(   s'   Base class for all the objects in SymPyi(   t   print_functiont   division(   t   defaultdict(   t   chaini   (   t	   BasicMetat   ManagedProperties(   t   cacheit(   t   _sympifyt   sympifyt   SympifyError(	   t   iterablet   Iteratort   orderedt   string_typest   with_metaclasst   zip_longestt   ranget   PY3t   Mapping(   t   S(   t   getmroc         C  sL   d d l  m } y t |   SWn' t k
 rG t d | |     n Xd S(   s   Return expr as a Basic instance using strict sympify
    or raise a TypeError; this is just a wrapper to _sympify,
    raising a TypeError instead of a SympifyError.i(   t	   func_names)   Argument must be a Basic object, not `%s`N(   t   sympy.utilities.miscR   R   R	   t	   TypeError(   t   exprR   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   as_Basic   s    t   Basicc           B  sY  e  Z d  Z d d d g Z e Z e Z e Z e Z e Z	 e Z
 e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z  e Z! e Z" d   Z# d   Z$ d   Z% d   Z& d   Z' d	   Z( d
   Z) d   Z* e+ d    Z, d   Z- e. d    Z/ e0 d    Z1 e0 d    Z2 e3 d< d   Z5 d   Z6 d   Z7 d< d  Z8 d   Z9 d   Z: d   Z; e; Z< d   Z= e+ d    Z> e+ d    Z? d   Z@ e+ d    ZA d   ZB e. d    ZC d   ZD e+ d     ZE e+ d!    ZF e+ d"    ZG e+ d#    ZH d$   ZI e eJ d%  ZK d&   ZL e3 d'    ZM d(   ZN d)   ZO d*   ZP e3 d+    ZQ d,   ZR d-   ZS e eJ d< d.  ZT e d/  ZU d0   ZV i  e d1  ZW e d2  ZX d< d3  ZY d4   ZZ d5   Z[ d6   Z\ d7   Z] d8   Z^ d9   Z_ d:   Z` i  Za e0 d;    Zb RS(=   s  
    Base class for all objects in SymPy.

    Conventions:

    1) Always use ``.args``, when accessing parameters of some instance:

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

        >>> cot(x).args
        (x,)

        >>> cot(x).args[0]
        x

        >>> (x*y).args
        (x, y)

        >>> (x*y).args[1]
        y


    2) Never use internal methods or variables (the ones prefixed with ``_``):

        >>> cot(x)._args    # do not use this, use cot(x).args instead
        (x,)

    t   _mhasht   _argst   _assumptionsc         G  s1   t  j |   } |  j | _ d  | _ | | _ | S(   N(   t   objectt   __new__t   default_assumptionsR   t   NoneR   R   (   t   clst   argst   obj(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR   `   s
    		c         C  s   |  j  |  j   S(   N(   t   funcR#   (   t   self(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   copyh   s    c         C  s   t  |   |  j   |  j   f S(   s    Pickling support.(   t   typet   __getnewargs__t   __getstate__(   R&   t   proto(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   __reduce_ex__k   s    c         C  s   |  j  S(   N(   R#   (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR)   o   s    c         C  s   i  S(   N(    (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR*   r   s    c         C  s1   x* | j    D] \ } } t |  | |  q Wd  S(   N(   t   itemst   setattr(   R&   t   statet   kt   v(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   __setstate__u   s    c         C  sG   |  j  } | d  k rC t t |   j f |  j    } | |  _  n  | S(   N(   R   R!   t   hashR(   t   __name__t   _hashable_content(   R&   t   h(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   __hash__y   s
    	"c         C  s   |  j  S(   s  Return a tuple of information about self that can be used to
        compute the hash. If a class defines additional attributes,
        like ``name`` in Symbol, then this method should be updated
        accordingly to return such relevant attributes.

        Defining more than _hashable_content is necessary if __eq__ has
        been defined by a class. See note about this in Basic.__eq__.(   R   (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR5      s    c         C  s   i  S(   s  
        Return object `type` assumptions.

        For example:

          Symbol('x', real=True)
          Symbol('x', integer=True)

        are different objects. In other words, besides Python type (Symbol in
        this case), the initial assumptions are also forming their typeinfo.

        Examples
        ========

        >>> from sympy import Symbol
        >>> from sympy.abc import x
        >>> x.assumptions0
        {'commutative': True}
        >>> x = Symbol("x", positive=True)
        >>> x.assumptions0
        {'commutative': True, 'complex': True, 'hermitian': True,
        'imaginary': False, 'negative': False, 'nonnegative': True,
        'nonpositive': False, 'nonzero': True, 'positive': True, 'real': True,
        'zero': False}

        (    (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   assumptions0   s    c   	      C  s9  |  | k r d S|  j  } | j  } | | k | | k  } | rB | S|  j   } | j   } t |  t |  k t |  t |  k  } | r | Sx t | |  D] \ } } t | t  r t |   n | } t | t  r t |   n | } t | t  r| j |  } n | | k | | k  } | r | Sq Wd S(   s  
        Return -1, 0, 1 if the object is smaller, equal, or greater than other.

        Not in the mathematical sense. If the object is of a different type
        from the "other" then their classes are ordered according to
        the sorted_classes list.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> x.compare(y)
        -1
        >>> x.compare(x)
        0
        >>> y.compare(x)
        1

        i    (   t	   __class__R5   t   lent   zipt
   isinstancet	   frozensetR   t   compare(	   R&   t   othert   n1t   n2t   ct   stt   ott   lt   r(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR>      s*    		.!!c         C  sp  d d l  m } t |  |  r3 t | |  r3 d St |  |  rV t | |  rV d S|  j r | j r |  j | j } | j |  j } | | k | | k  Sd d l m } | d  | d  | d  } } } |  j | | |  }	 |	 r`| |	 k r`|	 | }
 | j | | |  } | r`| | k r`| | } t	 j
 |
 |  } | d k r]| Sq`n  t	 j
 |  |  S(	   Ni(   t   Orderi   (   t   Wildt   p1t   p2t   p3i    (   t   sympy.series.orderRG   R<   t   is_Rationalt   pt   qt   sympy.core.symbolRH   t   matchR   R>   (   t   at   bRG   RE   RF   RH   RI   RJ   RK   t   r_at   a3t   r_bt   b3RB   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   _compare_pretty   s*    &


c         K  s   |  t  |  |   S(   sa  
        Create a new object from an iterable.

        This is a convenience function that allows one to create objects from
        any iterable, without having to convert to a list or tuple first.

        Examples
        ========

        >>> from sympy import Tuple
        >>> Tuple.fromiter(i for i in range(5))
        (0, 1, 2, 3, 4)

        (   t   tuple(   R"   R#   t   assumptions(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   fromiter   s    c         C  s   d d |  j  f S(   s   Nice order of classes. i   i    (   R4   (   R"   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt	   class_key  s    c           sk     f d   } |  j  } t |  t g  | D] } | |  ^ q+  f } |  j   | t j j   t j f S(   s  
        Return a sort key.

        Examples
        ========

        >>> from sympy.core import S, I

        >>> sorted([S(1)/2, I, -I], key=lambda x: x.sort_key())
        [1/2, -I, I]

        >>> S("[x, 1/x, 1/x**2, x**2, x**(1/2), x**(1/4), x**(3/2)]")
        [x, 1/x, x**(-2), x**2, sqrt(x), x**(1/4), x**(3/2)]
        >>> sorted(_, key=lambda x: x.sort_key())
        [x**(-2), 1/x, x**(1/4), sqrt(x), x, x**(3/2), x**2]

        c           s$   t  |  t  r |  j    S|  Sd  S(   N(   R<   R   t   sort_key(   t   arg(   t   order(    s/   lib/python2.7/site-packages/sympy/core/basic.pyt	   inner_key  s    (   t   _sorted_argsR:   RY   R\   R   t   OneR]   (   R&   R_   R`   R#   R^   (    (   R_   s/   lib/python2.7/site-packages/sympy/core/basic.pyR]   
  s    	1c         C  s   |  | k r t  St |   } t |  } | | k	 r y t |  } t |  } Wn t k
 rd t SXt s t |  j t j k	 r | | k r t Sq | | k	 r t Sn  |  j   | j   k S(   s  Return a boolean indicating whether a == b on the basis of
        their symbolic trees.

        This is the same as a.compare(b) == 0 but faster.

        Notes
        =====

        If a class that overrides __eq__() needs to retain the
        implementation of __hash__() from a parent class, the
        interpreter must be told this explicitly by setting __hash__ =
        <ParentClass>.__hash__. Otherwise the inheritance of __hash__()
        will be blocked, just as if __hash__ had been explicitly set to
        None.

        References
        ==========

        from http://docs.python.org/dev/reference/datamodel.html#object.__hash__
        (	   t   TrueR(   R   R	   t   NotImplementedR   t   __ne__t   FalseR5   (   R&   R?   t   tselft   tother(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   __eq__)  s     c         C  s   |  | k S(   s   a != b  -> Compare two symbolic trees and see whether they are different

           this is the same as:

             a.compare(b) != 0

           but faster
        (    (   R&   R?   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyRe   W  s    	c   
      C  s   |  j    } t |  } | j    } g  | j D] } | j r. | ^ q. } t |  d k rj | j   } n
 | | k S| d k r | j } t |  d k r | j   } q | | k Sn  | j   }	 | j | |	  | j | |	  k S(   s  
        Compare two expressions and handle dummy symbols.

        Examples
        ========

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

        >>> u = Dummy('u')

        >>> (u**2 + 1).dummy_eq(x**2 + 1)
        True
        >>> (u**2 + 1) == (x**2 + 1)
        False

        >>> (u**2 + y).dummy_eq(x**2 + y, x)
        True
        >>> (u**2 + y).dummy_eq(x**2 + y, y)
        False

        i   N(	   t   as_dummyR   t   free_symbolst   is_DummyR:   t   popR!   R9   t   subs(
   R&   R?   t   symbolt   st   ot   it   dummy_symbolst   dummyt   symbolst   tmp(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   dummy_eqb  s    %
	c         C  s    d d l  m } | |  d d S(   s_   Method to return the string representation.
        Return the expression as a string.
        i(   t   sstrR_   N(   t   sympy.printingRx   R!   (   R&   Rx   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   __repr__  s    c         C  s    d d l  m } | |  d d  S(   Ni(   Rx   R_   (   Ry   Rx   R!   (   R&   Rx   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   __str__  s    c         C  s*   d d l  m } | |  d d } d | S(   s^  
        IPython/Jupyter LaTeX printing

        To change the behavior of this (e.g., pass in some settings to LaTeX),
        use init_printing(). init_printing() will also enable LaTeX printing
        for built in numeric types like ints and container types that contain
        SymPy objects, like lists and dictionaries of expressions.
        i(   t   latext   modet   plains   $\displaystyle %s$(   t   sympy.printing.latexR|   (   R&   R|   Rp   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   _repr_latex_  s    	c         G  s   | rC t  g  | D]' } t | t  r+ | n	 t |  ^ q  } n	 t f } t   } x3 t |   D]% } t | |  rb | j |  qb qb W| S(   s	  Returns the atoms that form the current object.

           By default, only objects that are truly atomic and can't
           be divided into smaller pieces are returned: symbols, numbers,
           and number symbols like I and pi. It is possible to request
           atoms of any type, however, as demonstrated below.

           Examples
           ========

           >>> from sympy import I, pi, sin
           >>> from sympy.abc import x, y
           >>> (1 + x + 2*sin(y + I*pi)).atoms()
           {1, 2, I, pi, x, y}

           If one or more types are given, the results will contain only
           those types of atoms.

           >>> from sympy import Number, NumberSymbol, Symbol
           >>> (1 + x + 2*sin(y + I*pi)).atoms(Symbol)
           {x, y}

           >>> (1 + x + 2*sin(y + I*pi)).atoms(Number)
           {1, 2}

           >>> (1 + x + 2*sin(y + I*pi)).atoms(Number, NumberSymbol)
           {1, 2, pi}

           >>> (1 + x + 2*sin(y + I*pi)).atoms(Number, NumberSymbol, I)
           {1, 2, I, pi}

           Note that I (imaginary unit) and zoo (complex infinity) are special
           types of number symbols and are not part of the NumberSymbol class.

           The type can be given implicitly, too:

           >>> (1 + x + 2*sin(y + I*pi)).atoms(x) # x is a Symbol
           {x, y}

           Be careful to check your assumptions when using the implicit option
           since ``S(1).is_Integer = True`` but ``type(S(1))`` is ``One``, a special type
           of sympy atom, while ``type(S(2))`` is type ``Integer`` and will find all
           integers in an expression:

           >>> from sympy import S
           >>> (1 + x + 2*sin(y + I*pi)).atoms(S(1))
           {1}

           >>> (1 + x + 2*sin(y + I*pi)).atoms(S(2))
           {1, 2}

           Finally, arguments to atoms() can select more than atomic atoms: any
           sympy type (loaded in core/__init__.py) can be listed as an argument
           and those types of "atoms" as found in scanning the arguments of the
           expression recursively:

           >>> from sympy import Function, Mul
           >>> from sympy.core.function import AppliedUndef
           >>> f = Function('f')
           >>> (1 + f(x) + 2*sin(y + I*pi)).atoms(Function)
           {f(x), sin(y + I*pi)}
           >>> (1 + f(x) + 2*sin(y + I*pi)).atoms(AppliedUndef)
           {f(x)}

           >>> (1 + x + 2*sin(y + I*pi)).atoms(Mul)
           {I*pi, 2*sin(y + I*pi)}

        (   RY   R<   R(   t   Atomt   sett   preorder_traversalt   add(   R&   t   typest   tt   resultR   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   atoms  s    E:		c         C  s)   t    j g  |  j D] } | j ^ q   S(   sO  Return from the atoms of self those which are free symbols.

        For most expressions, all symbols are free symbols. For some classes
        this is not true. e.g. Integrals use Symbols for the dummy variables
        which are bound variables, so Integral has a method to return all
        symbols except those. Derivative keeps track of symbols with respect
        to which it will perform a derivative; those are
        bound variables, too, so it has its own free_symbols method.

        Any other method that uses bound variables should implement a
        free_symbols method.(   R   t   unionR#   Rk   (   R&   RR   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyRk      s    c         C  s
   t  g   S(   N(   R   (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   expr_free_symbols  s    c           s%   d     |  j  d     f d    S(   s  Return the expression with any objects having structurally
        bound symbols replaced with unique, canonical symbols within
        the object in which they appear and having only the default
        assumption for commutativity being True.

        Examples
        ========

        >>> from sympy import Integral, Symbol
        >>> from sympy.abc import x, y
        >>> r = Symbol('r', real=True)
        >>> Integral(r, (r, x)).as_dummy()
        Integral(_0, (_0, x))
        >>> _.variables[0].is_real is None
        True

        Notes
        =====

        Any object that has structural dummy variables should have
        a property, `bound_symbols` that returns a list of structural
        dummy symbols of the object itself.

        Lambda and Subs have bound symbols, but because of how they
        are cached, they already compare the same regardless of their
        bound symbols:

        >>> from sympy import Lambda
        >>> Lambda(x, x + 1) == Lambda(y, y + 1)
        True
        c         S  sc   d   |  j  D } |  j |  }  |  j } |  j |  }  |  j t d   | j   D   }  |  S(   Nc         S  s   i  |  ] } | j    |  q S(    (   Rj   (   t   .0Rr   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pys
   <dictcomp>4  s   	 c         s  s!   |  ] \ } } | | f Vq d  S(   N(    (   R   R0   R1   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>;  s    (   t   bound_symbolsRn   t   canonical_variablest   xreplacet   dictR-   (   t   xt   dRB   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   can3  s    	%c         S  s   t  |  d  S(   NR   (   t   hasattr(   R   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   <lambda>>  t    c           s
     |   S(   N(    (   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR   ?  R   (   t   replace(   R&   (    (   R   s/   lib/python2.7/site-packages/sympy/core/basic.pyRj     s     	
c   	      C  s   d d l  m } d d l m } t |  d  s3 i  S| d  } i  } |  j } t g  |  j |  t |  D] } | j ^ qk  } xd | D]\ } t	 |  } | j
 r x4 | j | j k s | j | k r t	 |  } q Wn  | | | <q W| S(   sX  Return a dictionary mapping any variable defined in
        ``self.bound_symbols`` to Symbols that do not clash
        with any existing symbol in the expression.

        Examples
        ========

        >>> from sympy import Lambda
        >>> from sympy.abc import x
        >>> Lambda(x, 2*x).canonical_variables
        {x: _0}
        i(   t   Symbol(   t   numbered_symbolsR   t   _(   RP   R   t   sympy.utilities.iterablesR   R   R   R   R   t   namet   nextt	   is_Symbol(	   R&   R   R   t   dumst   repsR1   Rr   t   freeR   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR   A  s    	5	$c         G  s   t  j |  |  S(   s  Apply on the argument recursively through the expression tree.

        This method is used to simulate a common abuse of notation for
        operators. For instance in SymPy the the following will not work:

        ``(x+Lambda(y, 2*y))(z) == x+2*z``,

        however you can use

        >>> from sympy import Lambda
        >>> from sympy.abc import x, y, z
        >>> (x + Lambda(y, 2*y)).rcall(z)
        x + 2*z
        (   R   t   _recursive_call(   R&   R#   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   rcalla  s    c         C  s   d d l  m } d   } t |   rQ | |   rQ t |  |  rD |  S|  |   SnE |  j r g  |  j D] } t j | |  ^ qd } t |   |   S|  Sd S(   s!   Helper for rcall method.
        i(   R   c         S  s:   x3 t  t |    D] } d | j k r | t k Sq Wd  S(   Nt   __call__(   R   R(   t   __dict__R   (   R   R"   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   the_call_method_is_overriddenw  s    N(   t   sympyR   t   callableR<   R#   R   R   R(   (   t   expr_to_callt   on_argsR   R   t   subR#   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR   r  s    		%c         C  s#   d d l  m } | |  |  d  k	 S(   Ni(   t	   hypersimp(   t   sympy.simplifyR   R!   (   R&   R0   R   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   is_hypergeometric  s    c         C  s   |  j  } | t k r t S|  j s& t Sg  |  j   D]$ } | j sQ | j d  n | ^ q3 \ } } | j or | j sy t S| r t S| j d k Sd S(   s  Return True if self can be computed to a real number
        (or already is a real number) with precision, else False.

        Examples
        ========

        >>> from sympy import exp_polar, pi, I
        >>> (I*exp_polar(I*pi/2)).is_comparable
        True
        >>> (I*exp_polar(I*pi*2)).is_comparable
        False

        A False result does not mean that `self` cannot be rewritten
        into a form that would be comparable. For example, the
        difference computed below is zero but without simplification
        it does not evaluate to a zero with precision:

        >>> e = 2**pi*(1 + 2**pi)
        >>> dif = e - e.expand()
        >>> dif.is_comparable
        False
        >>> dif.n(2)._prec
        1

        i   i   N(   t   is_realRf   t	   is_numbert   as_real_imagt	   is_Numbert   evalft   _prec(   R&   R   RN   t   nRr   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   is_comparable  s    		:c         C  s   |  j  S(   s  
        The top-level function in an expression.

        The following should hold for all objects::

            >> x == x.func(*x.args)

        Examples
        ========

        >>> from sympy.abc import x
        >>> a = 2*x
        >>> a.func
        <class 'sympy.core.mul.Mul'>
        >>> a.args
        (2, x)
        >>> a.func(*a.args)
        2*x
        >>> a == a.func(*a.args)
        True

        (   R9   (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR%     s    c         C  s   |  j  S(   s>  Returns a tuple of arguments of 'self'.

        Examples
        ========

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

        >>> cot(x).args
        (x,)

        >>> cot(x).args[0]
        x

        >>> (x*y).args
        (x, y)

        >>> (x*y).args[1]
        y

        Notes
        =====

        Never use self._args, always use self.args.
        Only use _args in __new__ when creating a new function.
        Don't override .args() from Basic (so that it's easy to
        change the interface in the future if needed).
        (   R   (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR#     s    c         C  s   |  j  S(   s   
        The same as ``args``.  Derived classes which don't fix an
        order on their arguments should override this method to
        produce the sorted representation.
        (   R#   (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyRa     s    c         O  sV   d d l  m } m } y' | |  | |  } | j s8 d S| SWn | k
 rQ d SXd S(   s  Converts ``self`` to a polynomial or returns ``None``.

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

           >>> print((x**2 + x*y).as_poly())
           Poly(x**2 + x*y, x, y, domain='ZZ')

           >>> print((x**2 + x*y).as_poly(x, y))
           Poly(x**2 + x*y, x, y, domain='ZZ')

           >>> print((x**2 + sin(y)).as_poly(x, y))
           None

        i(   t   Polyt   PolynomialErrorN(   t   sympy.polysR   R   t   is_PolyR!   (   R&   t   gensR#   R   R   t   poly(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   as_poly  s    	c         C  s   t  j |  f S(   s   A stub to allow Basic args (like Tuple) to be skipped when computing
        the content and primitive components of an expression.

        See Also
        ========

        sympy.core.expr.Expr.as_content_primitive
        (   R   Rb   (   R&   t   radicalt   clear(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   as_content_primitive  s    	c      	   O  s9  d d l  m } d d l m } d d l m } m } t } t |  d k r | d } t	 | t
  rp t } q t	 | | t f  r t } | j   } q t |  s d d l m }	 t |	 d    q n* t |  d	 k r | g } n t d
   t |  } x t |  D] \ }
 } t	 | d t  rM| | d  | d f } n  y3 g  | D]" } t | d t	 | t  ^ qW} Wn t k
 rd | |
 <qn Xt |   rd n	 t |  | |
 <qWt t d |   } | rBt |  } t d   | D  si  } xr | j   D]d \ } } y | j   t | j  f } Wn t k
 r[d } n X| j  | g   j! | | f  qWg  } xT t" | j#   d t D]: } | j$ t" g  | | D] } | d ^ qd |  qWg  | D] } | | | f ^ q} ~ ~ qBt" g  | j   D] \ } } | | f ^ qd | } n  | j% d t  ri  } |  } t | d <|   } x\ | D]T \ } } | d | j&  } | j' | | | |  } t	 | t(  sPn  | | | <qzWt) j* | | <| j+ |  S|  } x< | D]4 \ } } | j' | | |  } t	 | t(  sPqqW| Sd S(   s  
        Substitutes old for new in an expression after sympifying args.

        `args` is either:
          - two arguments, e.g. foo.subs(old, new)
          - one iterable argument, e.g. foo.subs(iterable). The iterable may be
             o an iterable container with (old, new) pairs. In this case the
               replacements are processed in the order given with successive
               patterns possibly affecting replacements already made.
             o a dict or set whose key/value items correspond to old/new pairs.
               In this case the old/new pairs will be sorted by op count and in
               case of a tie, by number of args and the default_sort_key. The
               resulting sorted list is then processed as an iterable container
               (see previous).

        If the keyword ``simultaneous`` is True, the subexpressions will not be
        evaluated until all the substitutions have been made.

        Examples
        ========

        >>> from sympy import pi, exp, limit, oo
        >>> from sympy.abc import x, y
        >>> (1 + x*y).subs(x, pi)
        pi*y + 1
        >>> (1 + x*y).subs({x:pi, y:2})
        1 + 2*pi
        >>> (1 + x*y).subs([(x, pi), (y, 2)])
        1 + 2*pi
        >>> reps = [(y, x**2), (x, 2)]
        >>> (x + y).subs(reps)
        6
        >>> (x + y).subs(reversed(reps))
        x**2 + 2

        >>> (x**2 + x**4).subs(x**2, y)
        y**2 + y

        To replace only the x**2 but not the x**4, use xreplace:

        >>> (x**2 + x**4).xreplace({x**2: y})
        x**4 + y

        To delay evaluation until all substitutions have been made,
        set the keyword ``simultaneous`` to True:

        >>> (x/y).subs([(x, 0), (y, 0)])
        0
        >>> (x/y).subs([(x, 0), (y, 0)], simultaneous=True)
        nan

        This has the added feature of not allowing subsequent substitutions
        to affect those already made:

        >>> ((x + y)/y).subs({x + y: y, y: x + y})
        1
        >>> ((x + y)/y).subs({x + y: y, y: x + y}, simultaneous=True)
        y/(x + y)

        In order to obtain a canonical result, unordered iterables are
        sorted by count_op length, number of arguments and by the
        default_sort_key to break any ties. All other iterables are left
        unsorted.

        >>> from sympy import sqrt, sin, cos
        >>> from sympy.abc import a, b, c, d, e

        >>> A = (sqrt(sin(2*x)), a)
        >>> B = (sin(2*x), b)
        >>> C = (cos(2*x), c)
        >>> D = (x, d)
        >>> E = (exp(x), e)

        >>> expr = sqrt(sin(2*x))*sin(exp(x)*x)*cos(2*x) + sin(2*x)

        >>> expr.subs(dict([A, B, C, D, E]))
        a*c*sin(d*e) + b

        The resulting expression represents a literal replacement of the
        old arguments with the new arguments. This may not reflect the
        limiting behavior of the expression:

        >>> (x**3 - 3*x).subs({x: oo})
        nan

        >>> limit(x**3 - 3*x, x, oo)
        oo

        If the substitution will be followed by numerical
        evaluation, it is better to pass the substitution to
        evalf as

        >>> (1/x).evalf(subs={x: 3.0}, n=21)
        0.333333333333333333333

        rather than

        >>> (1/x).subs({x: 3.0}).evalf(21)
        0.333333333333333314830

        as the former will ensure that the desired level of precision is
        obtained.

        See Also
        ========
        replace: replacement capable of doing wildcard-like matching,
                 parsing of match, and conditional replacements
        xreplace: exact node replacement in expr tree; also capable of
                  using matching rules
        evalf: calculates the given formula to a desired level of precision

        i(   t   Dict(   t   default_sort_key(   t   DummyR   i   i    (   t
   filldedents   
                   When a single argument is passed to subs
                   it should be a dictionary of old: new pairs or an iterable
                   of (old, new) tuples.i   s$   subs accepts either 1 or 2 argumentst   strictc         s  s   |  ] } | j  Vq d  S(   N(   t   is_Atom(   R   R0   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>  s    t   reverset   keyt   simultaneoust   hack2t   commutativeN(   i    i    (,   t   sympy.core.containersR   t   sympy.utilitiesR   R   R   R   Rf   R:   R<   R   Rc   R   R-   R
   R   R   t
   ValueErrort   listt	   enumerateR   R   R	   R!   t   _aresameRY   t   filterR   t   allt	   count_opsR#   R   t
   setdefaultt   appendt   sortedt   keyst   extendRm   t   is_commutativet   _subsR   R   Rb   R   (   R&   R#   t   kwargsR   R   R   R   t	   unorderedt   sequenceR   Rr   Rp   R   R   Rq   R   t   opst   newseqR0   R1   R   t   rvt   mt   oldt   new(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyRn   &  s    q
	0
&
#2#	.
	c           sY     f d   } t  |  |  r" | S|  j | |  } | d k rU | |  | |  } n  | S(   s  Substitutes an expression old -> new.

        If self is not equal to old then _eval_subs is called.
        If _eval_subs doesn't want to make any special replacement
        then a None is received which indicates that the fallback
        should be applied wherein a search for replacements is made
        amongst the arguments of self.

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

        Examples
        ========

        Add's _eval_subs knows how to target x + y in the following
        so it makes the change:

            >>> (x + y + z).subs(x + y, 1)
            z + 1

        Add's _eval_subs doesn't need to know how to find x + y in
        the following:

            >>> Add._eval_subs(z*(x + y) + 3, x + y, 1) is None
            True

        The returned None will cause the fallback routine to traverse the args and
        pass the z*(x + y) arg to Mul where the change will take place and the
        substitution will succeed:

            >>> (z*(x + y) + 3).subs(x + y, 1)
            z + 3

        ** Developers Notes **

        An _eval_subs routine for a class should be written if:

            1) any arguments are not instances of Basic (e.g. bool, tuple);

            2) some arguments should not be targeted (as in integration
               variables);

            3) if there is something other than a literal replacement
               that should be attempted (as in Piecewise where the condition
               may be updated without doing a replacement).

        If it is overridden, here are some special cases that might arise:

            1) If it turns out that no special change was made and all
               the original sub-arguments should be checked for
               replacements then None should be returned.

            2) If it is necessary to do substitutions on a portion of
               the expression then _subs should be called. _subs will
               handle the case of any sub-expression being equal to old
               (which usually would not be the case) while its fallback
               will handle the recursion into the sub-arguments. For
               example, after Add's _eval_subs removes some matching terms
               it must process the remaining terms so it calls _subs
               on each of the un-matched terms and then adds them
               onto the terms previously obtained.

           3) If the initial expression should remain unchanged then
              the original expression should be returned. (Whenever an
              expression is returned, modified or not, no further
              substitution of old -> new is attempted.) Sum's _eval_subs
              routine uses this strategy when a substitution is attempted
              on any of its summation variables.
        c           sH  t  } t |  j  } xj t |  D]\ \ } } t | d  sC q" n  | j | |    } t | | |  s" t } | | | <q" q" W| rD|  j |   }   j	 d t   } | r@|  j
 r@| j
 r@t j }	 g  }
 x1 | D]) } | j r |	 | 9}	 q |
 j |  q W|  j |
   }
 |	 t j k r'|
 S|  j |	 |
 d t  Sn  | S|  S(   sQ   
            Try to replace old with new in any of self's arguments.
            t
   _eval_subsR   t   evaluate(   Rf   R   R#   R   R   R   R   Rc   R%   t   gett   is_MulR   Rb   R   R   (   R&   R   R   t   hitR#   Rr   R^   R   R   t   coefft	   nonnumber(   t   hints(    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   fallback2  s2    		N(   R   R   R!   (   R&   R   R   R   R   R   (    (   R   s/   lib/python2.7/site-packages/sympy/core/basic.pyR     s    H c         C  s   d S(   s   Override this stub if you want to do anything more than
        attempt a replacement of old with new in the arguments of self.

        See also: _subs
        N(   R!   (   R&   R   R   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR   Z  s    c         C  s   |  j  |  \ } } | S(   s  
        Replace occurrences of objects within the expression.

        Parameters
        ==========
        rule : dict-like
            Expresses a replacement rule

        Returns
        =======
        xreplace : the result of the replacement

        Examples
        ========

        >>> from sympy import symbols, pi, exp
        >>> x, y, z = symbols('x y z')
        >>> (1 + x*y).xreplace({x: pi})
        pi*y + 1
        >>> (1 + x*y).xreplace({x: pi, y: 2})
        1 + 2*pi

        Replacements occur only if an entire node in the expression tree is
        matched:

        >>> (x*y + z).xreplace({x*y: pi})
        z + pi
        >>> (x*y*z).xreplace({x*y: pi})
        x*y*z
        >>> (2*x).xreplace({2*x: y, x: z})
        y
        >>> (2*2*x).xreplace({2*x: y, x: z})
        4*z
        >>> (x + y + 2).xreplace({x + y: 2})
        x + y + 2
        >>> (x + 2 + exp(x + 2)).xreplace({x + 2: y})
        x + exp(y) + 2

        xreplace doesn't differentiate between free and bound symbols. In the
        following, subs(x, y) would not change x since it is a bound symbol,
        but xreplace does:

        >>> from sympy import Integral
        >>> Integral(x, (x, 1, 2*x)).xreplace({x: y})
        Integral(y, (y, 1, 2*y))

        Trying to replace x with an expression raises an error:

        >>> Integral(x, (x, 1, 2*x)).xreplace({x: 2*y}) # doctest: +SKIP
        ValueError: Invalid limits given: ((2*y, 1, 4*y),)

        See Also
        ========
        replace: replacement capable of doing wildcard-like matching,
                 parsing of match, and conditional replacements
        subs: substitution of subexpressions as defined by the objects
              themselves.

        (   t	   _xreplace(   R&   t   rulet   valueR   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR   b  s    <c         C  s   |  | k r | |  t  f S| r g  } t } xj |  j D]_ } t | d d  } | d k	 r | |  } | j | d  | | d O} q6 | j |  q6 Wt |  } | r |  j |   t  f Sn  |  t f S(   sV   
        Helper for xreplace. Tracks whether a replacement actually occurred.
        R   i    i   N(   Rc   Rf   R#   t   getattrR!   R   RY   R%   (   R&   R   R#   t   changedRR   R   t   a_xr(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     s     c           s   t    f d   | D  S(   s  
        Test whether any subexpression matches any of the patterns.

        Examples
        ========

        >>> from sympy import sin
        >>> from sympy.abc import x, y, z
        >>> (x**2 + sin(x*y)).has(z)
        False
        >>> (x**2 + sin(x*y)).has(x, y, z)
        True
        >>> x.has(x)
        True

        Note ``has`` is a structural algorithm with no knowledge of
        mathematics. Consider the following half-open interval:

        >>> from sympy.sets import Interval
        >>> i = Interval.Lopen(0, 5); i
        Interval.Lopen(0, 5)
        >>> i.args
        (0, 5, True, False)
        >>> i.has(4)  # there is no "4" in the arguments
        False
        >>> i.has(0)  # there *is* a "0" in the arguments
        True

        Instead, use ``contains`` to determine whether a number is in the
        interval or not:

        >>> i.contains(4)
        True
        >>> i.contains(0)
        False


        Note that ``expr.has(*patterns)`` is exactly equivalent to
        ``any(expr.has(p) for p in patterns)``. In particular, ``False`` is
        returned when the list of patterns is empty.

        >>> x.has()
        False

        c         3  s   |  ] }   j  |  Vq d  S(   N(   t   _has(   R   t   pattern(   R&   (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>  s    (   t   any(   R&   t   patterns(    (   R&   s/   lib/python2.7/site-packages/sympy/core/basic.pyt   has  s    /c           s   d d l  m } m } t  |  rK t  f d   |  j | |  D  St    t  t  r t  f d   t |   D  St	  d d  } | d k	 r |     t   f d   t |   D  St  f d   t |   D  Sd S(	   s   Helper for .has()i(   t   UndefinedFunctiont   Functionc         3  s*   |  ]  } | j    k p! |   k Vq d  S(   N(   R%   (   R   t   f(   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>  s   c         3  s   |  ] } t  |    Vq d  S(   N(   R<   (   R   R^   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>  s   t   _has_matcherc         3  s   |  ] }   |  Vq d  S(   N(    (   R   R^   (   RQ   (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>  s    c         3  s   |  ] } |   k Vq d  S(   N(    (   R   R^   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>  s    N(   t   sympy.core.functionR   R   R<   R   R   R   R   R   R   R!   (   R&   R   R   R   R   (    (   RQ   R   s/   lib/python2.7/site-packages/sympy/core/basic.pyR     s    	 c           s     f d   S(   s   Helper for .has()c           s
     |  k S(   N(    (   R?   (   R&   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     R   (    (   R&   (    (   R&   s/   lib/python2.7/site-packages/sympy/core/basic.pyR     s    c           s  d d l  m   m } d d l m } y t    Wn t k
 rI n Xy t    Wn t k
 rm n Xt  t  r  f d    t  t  r  f d    qt	   r  f d    qt
 d   nt  t  r f d    | d k rt  j |   d	 k n | } t  t  r^| rL f d
    q f d    qt	   r| r f d    q f d    qt
 d   nK t	   r  t	   r f d    qt
 d   n t
 d   i   g          f d   } | |  | d t }	  r{t t     x3  D]( \ }
 } i | |
 6 |	 j   }	 qLWn  | s|	 S rx@  D]5 \ }
 } i | |
 6  f d    j   D  qWn  |	  f Sd S(   s3  
        Replace matching subexpressions of ``self`` with ``value``.

        If ``map = True`` then also return the mapping {old: new} where ``old``
        was a sub-expression found with query and ``new`` is the replacement
        value for it. If the expression itself doesn't match the query, then
        the returned value will be ``self.xreplace(map)`` otherwise it should
        be ``self.subs(ordered(map.items()))``.

        Traverses an expression tree and performs replacement of matching
        subexpressions from the bottom to the top of the tree. The default
        approach is to do the replacement in a simultaneous fashion so
        changes made are targeted only once. If this is not desired or causes
        problems, ``simultaneous`` can be set to False. In addition, if an
        expression containing more than one Wild symbol is being used to match
        subexpressions and  the ``exact`` flag is True, then the match will only
        succeed if non-zero values are received for each Wild that appears in
        the match pattern.

        The list of possible combinations of queries and replacement values
        is listed below:

        Examples
        ========

        Initial setup

            >>> from sympy import log, sin, cos, tan, Wild, Mul, Add
            >>> from sympy.abc import x, y
            >>> f = log(sin(x)) + tan(sin(x**2))

        1.1. type -> type
            obj.replace(type, newtype)

            When object of type ``type`` is found, replace it with the
            result of passing its argument(s) to ``newtype``.

            >>> f.replace(sin, cos)
            log(cos(x)) + tan(cos(x**2))
            >>> sin(x).replace(sin, cos, map=True)
            (cos(x), {sin(x): cos(x)})
            >>> (x*y).replace(Mul, Add)
            x + y

        1.2. type -> func
            obj.replace(type, func)

            When object of type ``type`` is found, apply ``func`` to its
            argument(s). ``func`` must be written to handle the number
            of arguments of ``type``.

            >>> f.replace(sin, lambda arg: sin(2*arg))
            log(sin(2*x)) + tan(sin(2*x**2))
            >>> (x*y).replace(Mul, lambda *args: sin(2*Mul(*args)))
            sin(2*x*y)

        2.1. pattern -> expr
            obj.replace(pattern(wild), expr(wild))

            Replace subexpressions matching ``pattern`` with the expression
            written in terms of the Wild symbols in ``pattern``.

            >>> a, b = map(Wild, 'ab')
            >>> f.replace(sin(a), tan(a))
            log(tan(x)) + tan(tan(x**2))
            >>> f.replace(sin(a), tan(a/2))
            log(tan(x/2)) + tan(tan(x**2/2))
            >>> f.replace(sin(a), a)
            log(x) + tan(x**2)
            >>> (x*y).replace(a*x, a)
            y

            Matching is exact by default when more than one Wild symbol
            is used: matching fails unless the match gives non-zero
            values for all Wild symbols:

            >>> (2*x + y).replace(a*x + b, b - a)
            y - 2
            >>> (2*x).replace(a*x + b, b - a)
            2*x

            When set to False, the results may be non-intuitive:

            >>> (2*x).replace(a*x + b, b - a, exact=False)
            2/x

        2.2. pattern -> func
            obj.replace(pattern(wild), lambda wild: expr(wild))

            All behavior is the same as in 2.1 but now a function in terms of
            pattern variables is used rather than an expression:

            >>> f.replace(sin(a), lambda a: sin(2*a))
            log(sin(2*x)) + tan(sin(2*x**2))

        3.1. func -> func
            obj.replace(filter, func)

            Replace subexpression ``e`` with ``func(e)`` if ``filter(e)``
            is True.

            >>> g = 2*sin(x**3)
            >>> g.replace(lambda expr: expr.is_Number, lambda expr: expr**2)
            4*sin(x**9)

        The expression itself is also targeted by the query but is done in
        such a fashion that changes are not made twice.

            >>> e = x*(x*y + 1)
            >>> e.replace(lambda x: x.is_Mul, lambda x: 2*x)
            2*x*(2*x*y + 1)

        See Also
        ========
        subs: substitution of subexpressions as defined by the objects
              themselves.
        xreplace: exact node replacement in expr tree; also capable of
                  using matching rules

        i(   R   RH   (   t	   bottom_upc           s   t  |     S(   N(   R<   (   R   (   t   query(    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     R   c           s     |  j    S(   N(   R#   (   R   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     R   c           s     |  j    S(   N(   R#   (   R   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     R   s:   given a type, replace() expects another type or a callablec           s   |  j     S(   N(   RQ   (   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     R   i   c           s#   t  | j    r   j |  S|  S(   N(   R   t   valuesRn   (   R   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     s    c           s     j  |  S(   N(   Rn   (   R   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     R   c           s:   t  d   | j   D  r6   d   | j   D   S|  S(   Nc         s  s   |  ] } | Vq d  S(   N(    (   R   t   val(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>  s    c         S  s)   i  |  ] \ } } | t  |  d    q S(   i(   t   str(   R   R0   R1   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pys
   <dictcomp>  s   	 (   R   R   R-   (   R   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     s    c           s     d   | j    D   S(   Nc         S  s)   i  |  ] \ } } | t  |  d    q S(   i(   R  (   R   R0   R1   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pys
   <dictcomp>  s   	 (   R-   (   R   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     s   sG   given an expression, replace() expects another expression or a callablec           s
     |   S(   N(    (   R   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     R   s4   given a callable, replace() expects another callablesG   first argument to replace() must be a type, an expression or a callablec           s    |   } | s | i  k r  |  |  } | d  k	 r | |  k r |  |  < r t | d t  } | d  k r| t } n    d |  }  j | | f  | }  q | }  q n  |  S(   NR   R   (   R!   R   Rc   R   (   R   R   R   t   comR   (   R   t   _queryt   _valuet   mappingt   maskR   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   rec_replace  s    
		R   c           s1   i  |  ]' \ } } | j     | j      q S(    (   R   (   R   R0   R1   (   RF   (    s/   lib/python2.7/site-packages/sympy/core/basic.pys
   <dictcomp>  s   	N(   RP   R   RH   t   sympy.simplify.simplifyR   R   R	   R<   R(   R   R   R   R!   R:   R   Rc   R   t   reversedR   R-   (   R&   R   R   t   mapR   t   exactRH   R   R  R   Rq   R   (    (	   R   R  R  R  R  R   RF   R   R   s/   lib/python2.7/site-packages/sympy/core/basic.pyR     sp    y-	c         C  s   t  |  } t t | t |     } | s7 t |  Si  } x7 | D]/ } | | k ri | | c d 7<qD d | | <qD W| Sd S(   s*   Find all subexpressions matching a query. i   N(   t   _make_find_queryR   R   R   R   (   R&   R   t   groupt   resultst   groupsR   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   find  s    
c           s,   t       t   f d   t |   D  S(   s-   Count the number of matching subexpressions. c         3  s!   |  ] } t    |   Vq d  S(   N(   t   bool(   R   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>  s    (   R  t   sumR   (   R&   R   (    (   R   s/   lib/python2.7/site-packages/sympy/core/basic.pyt   count  s    c         C  s   t  |  } t | |  j  s" d S|  | k r2 | St |  j  t | j  k rT d S| j   } xf t |  j | j  D]O \ } } | | k r qv n  | j |  j	 | | d | } | d k rv d Sqv W| S(   s  
        Helper method for match() that looks for a match between Wild symbols
        in self and expressions in expr.

        Examples
        ========

        >>> from sympy import symbols, Wild, Basic
        >>> a, b, c = symbols('a b c')
        >>> x = Wild('x')
        >>> Basic(a + x, x).matches(Basic(a + b, c)) is None
        True
        >>> Basic(a + x, x).matches(Basic(a + b + c, b + c))
        {x_: b + c}
        R   N(
   R   R<   R9   R!   R:   R#   R'   R;   R   t   matches(   R&   R   t	   repl_dictR   R   R^   t	   other_arg(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR    s    "!c         C  s   t  |  } | j |  d | S(   s  
        Pattern matching.

        Wild symbols match all.

        Return ``None`` when expression (self) does not match
        with pattern. Otherwise return a dictionary such that::

          pattern.xreplace(self.match(pattern)) == self

        Examples
        ========

        >>> from sympy import Wild
        >>> from sympy.abc import x, y
        >>> p = Wild("p")
        >>> q = Wild("q")
        >>> r = Wild("r")
        >>> e = (x+y)**(x+y)
        >>> e.match(p**p)
        {p_: x + y}
        >>> e.match(p**q)
        {p_: x + y, q_: x + y}
        >>> e = (2*x)**2
        >>> e.match(p*q**r)
        {p_: 4, q_: x, r_: 2}
        >>> (p*q**r).xreplace(e.match(p*q**r))
        4*x**2

        The ``old`` flag will give the old-style pattern matching where
        expressions and patterns are essentially solved to give the
        match. Both of the following give None unless ``old=True``:

        >>> (x - 2).match(p - x, old=True)
        {p_: 2*x - 2}
        >>> (2/x).match(p*x, old=True)
        {p_: 2/x**2}

        R   (   R   R  (   R&   R   R   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyRQ     s    (c         C  s   d d l  m } | |  |  S(   s7   wrapper for count_ops that returns the operation count.i(   R   (   R   R   (   R&   t   visualR   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR   D  s    c         K  sa   | j  d t  rY g  |  j D]* } t | t  r@ | j |   n | ^ q } |  j |   S|  Sd S(   s5  Evaluate objects that are not evaluated by default like limits,
           integrals, sums and products. All objects of this kind will be
           evaluated recursively, unless some species were excluded via 'hints'
           or unless the 'deep' hint was set to 'False'.

           >>> from sympy import Integral
           >>> from sympy.abc import x

           >>> 2*Integral(x, x)
           2*Integral(x, x)

           >>> (2*Integral(x, x)).doit()
           x**2

           >>> (2*Integral(x, x)).doit(deep=False)
           2*Integral(x, x)

        t   deepN(   R   Rc   R#   R<   R   t   doitR%   (   R&   R   t   termt   terms(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR  I  s
    7c         K  s  |  j  r, t |  |  r( t |  |    S|  S| j d t  r g  |  j D]0 } t | t  rr | j | | |  n | ^ qH } n	 |  j } | d  k s t |  |  r t |  |  r t |  |  | |   } | d  k	 r | Sq n  | j d t  r|  j
 |   S|  S(   NR  R   (   R   R   R   R   Rc   R#   R<   R   t   _eval_rewriteR!   R%   (   R&   R   R   R   RR   R#   t	   rewritten(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR  c  s    	@	
c         C  s   | j  |   S(   N(   t   _visit_eval_derivative_scalar(   R&   Rp   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   _accept_eval_derivativex  s    c         C  s   | j  |   S(   N(   t   _eval_derivative(   R&   t   base(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR  |  s    c         C  s   | j  |   S(   N(   R   (   R&   R!  (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   _visit_eval_derivative_array  s    c         C  s   d d l  m } t | t | f  rw |  } xE t |  D]7 } | j |  } | | k se | d  k ri Pn  | } q8 W| Sd  Sd  S(   Ni(   t   Integer(   R   R#  R<   t   intR   R  R!   (   R&   Rp   R   R#  R$   Rr   t   obj2(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   _eval_derivative_n_times  s    
c         O  s   | s
 |  S| d  } t  | d t  r8 d | d } n3 y d | d j } Wn d | d j j } n X| s |  j d | |  St | d  r | d } n  g  | D] } |  j |  r | ^ q } | r |  j t |  | |  S|  Sd S(   s   Rewrite functions in terms of other functions.

        Rewrites expression containing applications of functions
        of one kind in terms of functions of different kind. For
        example you can rewrite trigonometric functions as complex
        exponentials or combinatorial functions as gamma function.

        As a pattern this function accepts a list of functions to
        to rewrite (instances of DefinedFunction class). As rule
        you can use string or a destination function instance (in
        this case rewrite() will use the str() function).

        There is also the possibility to pass hints on how to rewrite
        the given expressions. For now there is only one such hint
        defined called 'deep'. When 'deep' is set to False it will
        forbid functions to rewrite their contents.

        Examples
        ========

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

        Unspecified pattern:

        >>> sin(x).rewrite(exp)
        -I*(exp(I*x) - exp(-I*x))/2

        Pattern as a single function:

        >>> sin(x).rewrite(sin, exp)
        -I*(exp(I*x) - exp(-I*x))/2

        Pattern as a list of functions:

        >>> sin(x).rewrite([sin, ], exp)
        -I*(exp(I*x) - exp(-I*x))/2

        it   _eval_rewrite_as_i    N(	   R<   R   R4   R9   R  R!   R
   R   RY   (   R&   R#   R   R   R   RN   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   rewrite  s"    (
(c   
      C  s   | j  j } t t  } x | j D] } yt d   t |  j   D } xQ t j |  D]@ \ } } | | j	 g  | D] } | | | k rt | ^ qt  qW WWq" t
 k
 r q" Xq" Wx& | j | g   D] }	 |	 |  } q W| S(   Nc         s  s1   |  ]' } | t  j k r t  j | j   Vq d  S(   N(   R   t"   _constructor_postprocessor_mappingR-   (   R   R"   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pys	   <genexpr>  s   (   R9   R4   R   R   R#   R(   t   mroR   t   from_iterableR   R   R   (
   R"   R$   t   clsnamet   postprocessorsRr   t   postprocessor_mappingsR0   R1   t   jR   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt    _exec_constructor_postprocessors  s    
<N(c   R4   t
   __module__t   __doc__t	   __slots__Rf   R   R   R   t	   is_symbolt
   is_IndexedRl   t   is_Wildt   is_Functiont   is_AddR   t   is_PowR   t   is_FloatRM   t
   is_Integert   is_NumberSymbolt   is_Ordert   is_Derivativet   is_PiecewiseR   t   is_AlgebraicNumbert   is_Relationalt   is_Equalityt
   is_Booleant   is_Nott	   is_Matrixt	   is_Vectort   is_Pointt	   is_MatAddt	   is_MatMulR   R'   R,   R)   R*   R2   R7   R5   t   propertyR8   R>   t   staticmethodRX   t   classmethodR[   R\   R   R!   R]   Ri   Re   Rw   Rz   R{   R   t   _repr_latex_origR   Rk   R   Rj   R   R   R   R   R   R%   R#   Ra   R   Rc   R   Rn   R   R   R   R   R   R   R   R   R  R  R  RQ   R   R  R  R  R  R"  R&  R(  R)  R0  (    (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR      s   										
	.	.	0				P	. 		/ 
		p		?	1			#+							AR   c           B  sw   e  Z d  Z e Z g  Z i  e d  Z e d  Z d   Z	 e
 d    Z e d d   Z d   Z e d    Z RS(	   s   
    A parent class for atomic things. An atom is an expression with no subexpressions.

    Examples
    ========

    Symbol, Number, Rational, Integer, ...
    But not: Add, Mul, Pow, ...
    c         C  s   |  | k r | Sd  S(   N(    (   R&   R   R  R   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR  
  s    c         C  s   | j  |  |   S(   N(   R   (   R&   R   R   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     s    c         K  s   |  S(   N(    (   R&   R   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR    s    c         C  s   d d |  j  f S(   Ni   i    (   R4   (   R"   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR\     s    c         C  s1   |  j    d t |   f f t j j   t j f S(   Ni   (   R\   R  R   Rb   R]   (   R&   R_   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR]     s    c         C  s   |  S(   N(    (   R&   t   ratiot   measuret   rationalt   inverse(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   _eval_simplify  s    c         C  s   t  d   d  S(   NsX   Atoms have no args. It might be necessary to make a check for Atoms in the calling code.(   t   AttributeError(   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyRa     s    N(   R4   R1  R2  Rc   R   R3  Rf   R  R   R  RL  R\   R   R!   R]   RR  RJ  Ra   (    (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     s   			c         C  s   d d l  m } m } x t t |   t |   D] \ } } | | k sb t |  t |  k r2 t | |  r t | |  s t | |  r t | |  r | j   | j   k r t Sq t Sq2 q2 Wt	 S(   s{  Return True if a and b are structurally the same, else False.

    Examples
    ========

    To SymPy, 2.0 == 2:

    >>> from sympy import S
    >>> 2.0 == S(2)
    True

    Since a simple 'same or not' result is sometimes useful, this routine was
    written to provide that query:

    >>> from sympy.core.basic import _aresame
    >>> _aresame(S(2.0), S(2))
    False

    i   (   t   AppliedUndefR   (
   t   functionRT  R   R   R   R(   R<   R\   Rf   Rc   (   RR   RS   RT  t	   UndefFuncRr   R/  (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR   )  s    ($c   
      C  s  d d l  m } m } m } t |   } t   } t |  t  rh t |  d d  } | d k ro |  h Sn t   St   } x | D] }	 |	 | k r | j
   q n  | j |	  t |	 |  r |	 | k r | j |	  q t |	 | | f  r | s| j
   n  | j |	  q q W| S(   sL  Return atom-like quantities as far as substitution is
    concerned: Derivatives, Functions and Symbols. Don't
    return any 'atoms' that are inside such quantities unless
    they also appear outside, too, unless `recursive` is True.

    Examples
    ========

    >>> from sympy import Derivative, Function, cos
    >>> from sympy.abc import x, y
    >>> from sympy.core.basic import _atomic
    >>> f = Function('f')
    >>> _atomic(x + y)
    {x, y}
    >>> _atomic(x + f(y))
    {x, f(y)}
    >>> _atomic(Derivative(f(x), x) + cos(x) + y)
    {y, cos(x), Derivative(f(x), x)}

    i(   t
   DerivativeR   R   Rk   N(   R   RW  R   R   R   R   R<   R   R   R!   t   skipR   (
   t   et	   recursiveRW  R   R   t   pott   seenR   R   RN   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   _atomicI  s*    	
	
R   c           B  s>   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z RS(   s  
    Do a pre-order traversal of a tree.

    This iterator recursively yields nodes that it has visited in a pre-order
    fashion. That is, it yields the current node then descends through the
    tree breadth-first to yield all of a node's children's pre-order
    traversal.


    For an expression, the order of the traversal depends on the order of
    .args, which in many cases can be arbitrary.

    Parameters
    ==========
    node : sympy expression
        The expression to traverse.
    keys : (default None) sort key(s)
        The key(s) used to sort args of Basic objects. When None, args of Basic
        objects are processed in arbitrary order. If key is defined, it will
        be passed along to ordered() as the only key(s) to use to sort the
        arguments; if ``key`` is simply True then the default keys of ordered
        will be used.

    Yields
    ======
    subtree : sympy expression
        All of the subtrees in the tree.

    Examples
    ========

    >>> from sympy import symbols
    >>> from sympy.core.basic import preorder_traversal
    >>> x, y, z = symbols('x y z')

    The nodes are returned in the order that they are encountered unless key
    is given; simply passing key=True will guarantee that the traversal is
    unique.

    >>> list(preorder_traversal((x + y)*z, keys=None)) # doctest: +SKIP
    [z*(x + y), z, x + y, y, x]
    >>> list(preorder_traversal((x + y)*z, keys=True))
    [z*(x + y), z, x + y, x, y]

    c         C  s"   t  |  _ |  j | |  |  _ d  S(   N(   Rf   t
   _skip_flagt   _preorder_traversalt   _pt(   R&   t   nodeR   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   __init__  s    	c         c  s
  | V|  j  r t |  _  d  St | t  r | rL t | d  rL | j } n	 | j } | r | t k r t | | d t } q t |  } n  xu | D]( } x |  j	 | |  D] } | Vq Wq WnB t
 |  rx3 | D]( } x |  j	 | |  D] } | Vq Wq Wn  d  S(   Nt   _argsett   default(   R^  Rf   R<   R   R   Rc  R#   Rc   R   R_  R
   (   R&   Ra  R   R#   R^   t   subtreet   item(    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR_    s&    			c         C  s   t  |  _ d S(   s  
        Skip yielding current node's (last yielded node's) subtrees.

        Examples
        ========

        >>> from sympy.core import symbols
        >>> from sympy.core.basic import preorder_traversal
        >>> x, y, z = symbols('x y z')
        >>> pt = preorder_traversal((x+y*z)*z)
        >>> for i in pt:
        ...     print(i)
        ...     if i == x+y*z:
        ...             pt.skip()
        z*(x + y*z)
        z
        x + y*z
        N(   Rc   R^  (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyRX    s    c         C  s   t  |  j  S(   N(   R   R`  (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   __next__  s    c         C  s   |  S(   N(    (   R&   (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   __iter__  s    N(	   R4   R1  R2  R!   Rb  R_  RX  Rg  Rh  (    (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR   v  s   -			c           s`   y t       Wn t k
 r# n Xt   t  r@   f d   St   t  r\   f d   S  S(   s4   Convert the argument of Basic.find() into a callablec           s   t  |     S(   N(   R<   (   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     R   c           s   |  j     d  k	 S(   N(   RQ   R!   (   R   (   R   (    s/   lib/python2.7/site-packages/sympy/core/basic.pyR     R   (   R   R	   R<   R(   R   (   R   (    (   R   s/   lib/python2.7/site-packages/sympy/core/basic.pyR    s    N(&   R2  t
   __future__R    R   t   collectionsR   t	   itertoolsR   RZ   R   R   t   cacheR   R   R   R	   t   compatibilityR
   R   R   R   R   R   R   R   R   t	   singletonR   t   inspectR   R   R   R   R   Rf   R]  R   R  (    (    (    s/   lib/python2.7/site-packages/sympy/core/basic.pyt   <module>   s,   @	      .	 -g