ó
¡¼™\c           @  sØ   d  d l  m Z m 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 d  d l m Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   print_functiont   division(   t   _sympifyt   sympify(   t   Basic(   t   cacheit(   t   orderedt   range(   t	   fuzzy_and(   t   global_evaluate(   t   siftt   AssocOpc           B  s€   e  Z d  Z d g Z e d „  ƒ Z e d
 d „ ƒ Z d „  Z	 e d „  ƒ Z
 i  e d „ Z d „  Z d „  Z e d	 „  ƒ Z RS(   s   Associative operations, can separate noncommutative and
    commutative parts.

    (a op b) op c == a op (b op c) == a op b op c.

    Base class for Add and Mul.

    This is an abstract base class, concrete derived classes must define
    the attribute `identity`.
    t   is_commutativec         O  s1  d d l  m } t t t | ƒ ƒ } g  | D] } | |  j k	 r, | ^ q, } | j d ƒ } | d  k ru t d } n  | s |  j	 | ƒ } |  j
 | ƒ } | St | ƒ d k r¶ |  j St | ƒ d k rÐ | d S|  j | ƒ \ } } }	 | }
 |  j	 | | |
 ƒ } |  j
 | ƒ } |	 d  k	 r-| | |	 Œ S| S(   Niÿÿÿÿ(   t   Ordert   evaluatei    i   (   t   sympyR   t   listt   mapR   t   identityt   gett   NoneR	   t
   _from_argst    _exec_constructor_postprocessorst   lent   flatten(   t   clst   argst   optionsR   t   aR   t   objt   c_partt   nc_partt   order_symbolsR   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyt   __new__   s*    (c         C  s€   t  | ƒ d k r |  j St  | ƒ d k r3 | d St t |  ƒ j |  | Œ } | d k rs t d „  | Dƒ ƒ } n  | | _ | S(   s/   Create new instance with already-processed argsi    i   c         s  s   |  ] } | j  Vq d  S(   N(   R   (   t   .0R   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pys	   <genexpr>B   s    N(   R   R   t   superR   R!   R   R   R   (   R   R   R   R   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR   8   s    	c         O  sC   | j  d t ƒ r* |  j t k r* d } n	 |  j } |  j | | ƒ S(   s  Create new instance of own class with args exactly as provided by
        caller but returning the self class identity if args is empty.

           This is handy when we want to optimize things, e.g.

               >>> from sympy import Mul, S
               >>> from sympy.abc import x, y
               >>> e = Mul(3, x, y)
               >>> e.args
               (3, x, y)
               >>> Mul(*e.args[1:])
               x*y
               >>> e._new_rawargs(*e.args[1:])  # the same as above, but faster
               x*y

           Note: use this with caution. There is no checking of arguments at
           all. This is best used when you are rebuilding an Add or Mul after
           simply removing one or more args. If, for example, modifications,
           result in extra 1s being inserted (as when collecting an
           expression's numerators and denominators) they will not show up in
           the result but a Mul will be returned nonetheless:

               >>> m = (x*y)._new_rawargs(S.One, x); m
               x
               >>> m == x
               False
               >>> m.is_Mul
               True

           Another issue to be aware of is that the commutativity of the result
           is based on the commutativity of self. If you are rebuilding the
           terms that came from a commutative object then there will be no
           problem, but if self was non-commutative then what you are
           rebuilding may now be commutative.

           Although this routine tries to do as little as possible with the
           input, getting the commutativity right is important, so this level
           of safety is enforced: commutativity will always be recomputed if
           self is non-commutative and kwarg `reeval=False` has not been
           passed.
        t   reevalN(   t   popt   TrueR   t   FalseR   R   (   t   selfR   t   kwargsR   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyt   _new_rawargsF   s    *!		c         C  s[   g  } xE | rM | j  ƒ  } | j |  k r= | j | j ƒ q	 | j | ƒ q	 Wg  | d f S(   sÁ   Return seq so that none of the elements are of type `cls`. This is
        the vanilla routine that will be used if a class derived from AssocOp
        does not define its own flatten routine.N(   R%   t	   __class__t   extendR   t   appendR   (   R   t   seqt   new_seqt   o(    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR   v   s    	c           s  d d l  m } m } d d l m } t |  | ƒ rI t ˆ | ƒ rI d S|  ˆ k rY | S|  j ˆ | ƒ } | d k	 r{ | Sd d l m	 ‰ d d l
 m ‰  t |  j ‡  ‡ ‡ f d †  d t ƒ\ } }	 |	 sã t t | ƒ ƒ } n˜ |  j |	 Œ  }
 ˆ j } | r|
 j | rd S|  j ˆ |
 ƒ } | r\ˆ j s=ˆ j r\| j ƒ  ˆ j ƒ  k r\d Sn  |  j | Œ  } | j | | ƒ Sd	 } t ƒ  } xŽˆ | k r| j ˆ ƒ |  j f t t |  j ˆ ƒ ƒ ƒ } xw t | ƒ D]i } x` t | ƒ D]R } | j | | ƒ } | d k	 rë|  j | ƒ j ˆ | ƒ } | d k	 r=| SqëqëWqØW| d	 k r|  j rêˆ j rˆ j  j! rˆ j  d	 k rª| d
 t" ˆ j# ˆ j# ˆ j  d g Œ ‰ n- | d
 t" d ˆ j# ˆ j# ˆ j  d g Œ ‰ | d 7} qqn)|  j rˆ j$ ƒ  \ } } t% | ƒ d k rw| d	 k rF| d
 t" | | d | g Œ ‰ n! | d
 t" | | d | g Œ ‰ | d 7} qn  d d l& m' } ˆ } t ƒ  } x[ t | ƒ D]M } | j( ˆ  ƒ \ } } | j | } | r£| j) | ƒ | ˆ | ƒ ‰ q£q£Wˆ | k r| d	 7} qqn  PqqWd S(   sR  
        Matches Add/Mul "pattern" to an expression "expr".

        repl_dict ... a dictionary of (wild: expression) pairs, that get
                      returned with the results

        This function is the main workhorse for Add/Mul.

        For instance:

        >>> from sympy import symbols, Wild, sin
        >>> a = Wild("a")
        >>> b = Wild("b")
        >>> c = Wild("c")
        >>> x, y, z = symbols("x y z")
        >>> (a+sin(b)*c)._matches_commutative(x+sin(y)*z)
        {a_: x, b_: y, c_: z}

        In the example above, "a+sin(b)*c" is the pattern, and "x+sin(y)*z" is
        the expression.

        The repl_dict contains parts that were already matched. For example
        here:

        >>> (x+sin(b)*c)._matches_commutative(x+sin(y)*z, repl_dict={a: x})
        {a_: x, b_: y, c_: z}

        the only function of the repl_dict is to return it in the
        result, e.g. if you omit it:

        >>> (x+sin(b)*c)._matches_commutative(x+sin(y)*z)
        {b_: y, c_: z}

        the "a: x" is not returned in the result, but otherwise it is
        equivalent.

        i   (   t   Addt   Expriÿÿÿÿ(   t   Mul(   t   WildFunction(   t   Wildc           s    |  j  ˆ  ˆ ƒ o ˆ j  |  ƒ S(   N(   t   has(   t   p(   R5   R4   t   expr(    s4   lib/python2.7/site-packages/sympy/core/operations.pyt   <lambda>½   s    t   binaryi    R   (   t   collectN(*   R8   R1   R2   R   R3   t
   isinstanceR   t   _matches_simplet   functionR4   t   symbolR5   R
   R   R&   R   R   R*   t   free_symbolst   _combine_inverset   is_Addt   is_Mult	   count_opst   matchest   sett   addR   t   tuplet	   make_argst   reversedt   xreplacet   is_Powt   expt
   is_IntegerR'   t   baset   as_coeff_Mult   abst   sympy.simplify.radsimpR;   t   as_coeff_mult   update(   R(   R8   t	   repl_dictt   oldR1   R2   R3   t   dt	   wild_partt
   exact_partt   exactt   freet   newexprt
   newpatternt   it   sawt	   expr_listt   last_opt   wt   d1t   d2t   ct   eR;   t   wast   did(    (   R5   R4   R8   s4   lib/python2.7/site-packages/sympy/core/operations.pyt   _matches_commutative†   s†    '		%	,-
		#!
	
	c           sC   d „  ‰  ˆ  ˆ ƒ \ ‰ ‰ ˆ j  ‰ ‡  ‡ ‡ ‡ ‡ f d †  } | S(   s   Helper for .has()c         S  s1   t  |  j d „  d t ƒ\ } } t | ƒ | f S(   Nc         S  s   |  j  t k S(   N(   R   R&   (   t   arg(    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR9     t    R:   (   R
   R   R&   RF   (   R8   t   cpartt   ncpart(    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyt   _ncsplit	  s    	c           sÏ   |  ˆ k r t  St |  t ƒ s# t St |  ˆ ƒ rË ˆ  |  ƒ \ } } ˆ | @ˆ k rË ˆ s^ t  St ˆ ƒ t | ƒ k rÈ xL t t | ƒ t ˆ ƒ d ƒ D]' } | | | t ˆ ƒ !ˆ k r— t  Sq— WqÈ qË n  t S(   Ni   (   R&   R<   R   R'   R   R   (   R8   t   _ct   _ncR^   (   Rn   Re   R   t   ncR(   (    s4   lib/python2.7/site-packages/sympy/core/operations.pyt   is_in  s    '(   R+   (   R(   Rr   (    (   Rn   Re   R   Rq   R(   s4   lib/python2.7/site-packages/sympy/core/operations.pyt   _has_matcher  s
    		c         C  s²  d d l  m } d d l m } d d l m } d d l m } t |  | | f ƒ rS|  j	 | | ƒ \ } } | |  j
 k p¯ t | t ƒ r” | j p¯ | |  j
 k o¯ t | t ƒ sS| |  j
 k	 rÐ | j | ƒ n |  j
 } g  } t |  j j | ƒ ƒ }	 xF |	 D]> }
 |
 j | ƒ } | d k r/| j |
 ƒ qþ | j | ƒ qþ W|  j | | Œ Sn  g  } xI |  j D]> }
 |
 j | ƒ } | d k r”| j |
 ƒ qc| j | ƒ qcW|  j | Œ  S(   sc  
        Evaluate the parts of self that are numbers; if the whole thing
        was a number with no functions it would have been evaluated, but
        it wasn't so we must judiciously extract the numbers and reconstruct
        the object. This is *not* simply replacing numbers with evaluated
        numbers. Nunmbers should be handled in the largest pure-number
        expression as possible. So the code below separates ``self`` into
        number and non-number parts and evaluates the number parts and
        walks the args of the non-number part recursively (doing the same
        thing).
        i   (   R1   (   R3   (   t   Symbol(   t   AppliedUndefN(   RG   R1   t   mulR3   R?   Rt   R>   Ru   R<   t   as_independentR   R   t   is_Functiont   _evalfRH   t   funcRI   t   _eval_evalfR   R-   R   (   R(   t   precR1   R3   Rt   Ru   t   xt   tailR   t	   tail_argsR   t   newa(    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR{   %  s2    'c         C  s'   t  | |  ƒ r | j St | ƒ f Sd S(   sL  
        Return a sequence of elements `args` such that cls(*args) == expr

        >>> from sympy import Symbol, Mul, Add
        >>> x, y = map(Symbol, 'xy')

        >>> Mul.make_args(x*y)
        (x, y)
        >>> Add.make_args(x*y)
        (x*y,)
        >>> set(Add.make_args(x*y + y)) == set([y, x*y])
        True

        N(   R<   R   R   (   R   R8   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyRI   Y  s    N(   t   __name__t
   __module__t   __doc__t	   __slots__R   R!   t   classmethodR   R   R*   R   R'   Ri   Rs   R{   RI   (    (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR      s   
		0		4t   ShortCircuitc           B  s   e  Z RS(    (   R   R‚   (    (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR†   o  s   t	   LatticeOpc           B  sb   e  Z d  Z e Z d „  Z e d d „ ƒ Z e d „  ƒ Z	 e
 e d „  ƒ ƒ Z e d „  ƒ Z RS(   sô  
    Join/meet operations of an algebraic lattice[1].

    These binary operations are associative (op(op(a, b), c) = op(a, op(b, c))),
    commutative (op(a, b) = op(b, a)) and idempotent (op(a, a) = op(a) = a).
    Common examples are AND, OR, Union, Intersection, max or min. They have an
    identity element (op(identity, a) = a) and an absorbing element
    conventionally called zero (op(zero, a) = zero).

    This is an abstract base class, concrete derived classes must declare
    attributes zero and identity. All defining properties are then respected.

    >>> from sympy import Integer
    >>> from sympy.core.operations import LatticeOp
    >>> class my_join(LatticeOp):
    ...     zero = Integer(0)
    ...     identity = Integer(1)
    >>> my_join(2, 3) == my_join(3, 2)
    True
    >>> my_join(2, my_join(3, 4)) == my_join(2, 3, 4)
    True
    >>> my_join(0, 1, 4, 2, 3, 4)
    0
    >>> my_join(1, 2)
    2

    References:

    [1] - https://en.wikipedia.org/wiki/Lattice_%28order%29
    c         O  s¨   d „  | Dƒ } y t  |  j | ƒ ƒ } Wn t k
 rF t |  j ƒ SX| sZ t |  j ƒ St | ƒ d k r| t | ƒ j ƒ  St	 t
 |  ƒ j |  | ƒ } | | _ | Sd  S(   Nc         s  s   |  ] } t  | ƒ Vq d  S(   N(   R   (   R"   Rj   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pys	   <genexpr>–  s    i   (   t	   frozensett   _new_args_filterR†   R   t   zeroR   R   RF   R%   R#   R   R!   t   _argset(   R   R   R   t   _argsR   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR!   •  s    	c         c  s„   | p	 |  } xq | D]i } | | j  k r7 t | ƒ ‚ q | | j k rL q q | j | k rw x | j D] } | Vqe Wq | Vq Wd S(   s   Generator filtering argsN(   RŠ   R†   R   Rz   R   (   R   t   arg_sequencet   call_clst   nclsRj   R}   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR‰   «  s    c         C  s-   t  | |  ƒ r | j St t | ƒ g ƒ Sd S(   sG   
        Return a set of args such that cls(*arg_set) == expr.
        N(   R<   R‹   Rˆ   R   (   R   R8   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyRI   º  s    c         C  s   t  t |  j ƒ ƒ S(   N(   RH   R   R‹   (   R(   (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR   Ä  s    c         C  s,   t  |  ƒ t  | ƒ k t  |  ƒ t  | ƒ k  S(   N(   t   str(   R   t   b(    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyt   _compare_prettyÉ  s    N(   R   R‚   Rƒ   R&   R   R!   R…   R   R‰   RI   t   propertyR   R   t   staticmethodR’   (    (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyR‡   s  s   	
N(   t
   __future__R    R   t   sympy.core.sympifyR   R   t   sympy.core.basicR   t   sympy.core.cacheR   t   sympy.core.compatibilityR   R   t   sympy.core.logicR   t   sympy.core.evaluateR	   t   sympy.utilities.iterablesR
   R   t	   ExceptionR†   R‡   (    (    (    s4   lib/python2.7/site-packages/sympy/core/operations.pyt   <module>   s   ÿ d