
~9\c           @  s  d  Z  d d l m Z m Z d d l m Z d d l m Z 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 d d	 l m Z d d
 l m Z m Z m Z m 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' m( Z( m) Z) m* Z* m+ Z+ d d l, m- Z- e d e.  Z/ d   Z0 d   Z1 d   Z2 d   Z3 d e4 f d     YZ5 d e4 f d     YZ6 e7 e. d  Z8 e7 e. e. d  Z9 e7 e7 e7 e. d  Z: e; d  Z< d   Z= d  S(!   s9   Tools for manipulating of large commutative expressions. i(   t   print_functiont   division(   t   Add(   t   iterablet   is_sequencet
   SYMPY_INTSt   range(   t   Mult   _keep_coeff(   t   Pow(   t   Basict   preorder_traversal(   t   Expr(   t   sympify(   t   Rationalt   Integert   Numbert   I(   t   S(   t   Dummy(   t   NonCommutativeExpression(   t   Tuplet   Dict(   t   default_sort_key(   t   common_prefixt   common_suffixt
   variationst   ordered(   t   defaultdictt   positivec         C  s   t  |  t t f  p |  j S(   N(   t
   isinstanceR   t   floatt	   is_Number(   t   i(    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt	   _isnumber   s    c           s	  |  j  s d S|  j r9 t |   } | d k r4 | S| S|  j rW|  j   d j rW|  } | j r | j rx t	 d  St	 d  Sn | j
 r | j r t	 d  St	 d  Sn | j r | j r | j t k r t	 d  St	 d  Sq1| j r t	 j St Sn4 | j r1| j rt	 d  S| j r)t	 j St Sn  | j sL| j sL| j rSt	 j Sd S|  j } t |  d k r|  j   rd d	 l m } d d
 l m } d d l m } | j    } t |      t k s  t k rt	 j   n    d k	 r|  j! |  } | j rg  }	 nU y | |  }	 WnB | t" f k
 rlg  | | |  D] }
 |
 j  rN|
 ^ qN}	 n X|  j# |    } | j r7t$   f d   |	 D  r7| j r| j r| r| j r| St% d d t& St% d d t& Sn  | j r| j r| r!| j r| St% d d t& St% d d t& Sqq| j rt$   f d   |	 D  r| j r| j r| rt% d d t& St% d d t& Sn  | j r| j r| rt% d d t& St% d d t& Sqqqn |  j   \ } } d } | j rt |  } n- | j s8t |  d k	 r8t |  } q8n  | d k	 r| j sV| j r| | } | j ryt% d d t& S| j rt% d d t& S| j rt% d d t& S| j rt% d d t& Sn  d S|  j'   \ } } d } | j   s| j   \ } } | j p| j sd S| j( s)| j) r| j* rt$ d   | j+ t,  D  r| j sc| j rt	 d  } x t- j. |  D]p } | j r| | 9} qn  i  } x5 | j D]* } t |  | | <| | d k rd SqW| | j# |  9} qWqn | rt/ d   | j+ t,  D  r| j s1| j rt0 | j  } i  } xJ | D]B } t |  } | d k rod S| p| j rt n t | | <qMW| j1 |  } qn  | d k	 r| | } | j r| j r| j# t d  S| j r| j r| j# t d  Sn  d S(   s  Return the value closest to 0 that ``self`` may have if all symbols
    are signed and the result is uniformly the same sign for all values of symbols.
    If a symbol is only signed but not known to be an
    integer or the result is 0 then a symbol representative of the sign of self
    will be returned. Otherwise, None is returned if a) the sign could be positive
    or negative or b) self is not in one of the following forms:

    - L(x, y, ...) + A: a function linear in all symbols x, y, ... with an
      additive constant; if A is zero then the function can be a monomial whose
      sign is monotonic over the range of the variables, e.g. (x + 1)**3 if x is
      nonnegative.
    - A/L(x, y, ...) + B: the inverse of a function linear in all symbols x, y, ...
      that does not have a sign change from positive to negative for any set
      of values for the variables.
    - M(x, y, ...) + A: a monomial M whose factors are all signed and a constant, A.
    - A/M(x, y, ...) + B: the inverse of a monomial and constants A and B.
    - P(x): a univariate polynomial

    Examples
    ========

    >>> from sympy.core.exprtools import _monotonic_sign as F
    >>> from sympy import Dummy, S
    >>> nn = Dummy(integer=True, nonnegative=True)
    >>> p = Dummy(integer=True, positive=True)
    >>> p2 = Dummy(integer=True, positive=True)
    >>> F(nn + 1)
    1
    >>> F(p - 1)
    _nneg
    >>> F(nn*p + 1)
    1
    >>> F(p2*p + 1)
    2
    >>> F(nn - 1)  # could be negative, zero or positive
    Ni   i   i   i	   i   ii(   t
   real_roots(   t   roots(   t   PolynomialErrorc         3  s   |  ] } |   k Vq d  S(   N(    (   t   .0t   r(   t   x0(    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys	   <genexpr>   s    t   posR   t   nnegt   nonnegativet   negt   negativet   npost   nonpositivec         3  s   |  ] } |   k Vq d  S(   N(    (   R&   R'   (   R(   (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys	   <genexpr>   s    c         s  s$   |  ] } | j  r | j j Vq d  S(   N(   t   is_Powt   expt
   is_Integer(   R&   t   p(    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys	   <genexpr>   s    c         s  s   |  ] } | j  s | Vq d  S(   N(   t	   is_number(   R&   R3   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys	   <genexpr>   s    i    (2   t   is_realt	   is_Symbolt   _monotonic_signt   Nonet   is_Addt   as_numer_denomR4   t   is_primet   is_oddR   t   is_compositet   is_positivet   is_event   Falset
   is_integert   Onet   _epst   is_negativet   NegativeOnet   is_zerot   is_nonpositivet   is_nonnegativet   Zerot   free_symbolst   lent   is_polynomialt   sympy.polys.polytoolsR#   t   sympy.polys.polyrootsR$   t   sympy.polys.polyerrorsR%   t   popt   difft   NotImplementedErrort   subst   allR   t   Truet   as_coeff_Addt   is_MulR0   t   is_rationalt   atomsR	   R   t	   make_argst   anyt   listt   xreplace(   t   selft   rvt   st   freeR#   R$   R%   t   xt   dt   currentrootsR'   t   yt   nt   dent   vt   ct   at   ait   repsR3   R!   (    (   R(   s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR7      s    %	
		
		
		
			
				/%%		
						
2$
c         C  s  |  j    \ } } | j rg | j rW | j sK t | t d | j   } n  | j } q |  d } } n | j d t	  \ } } | t
 j k r t | |  d } } nS | t
 j k	 r t t d | j  |  } t | |  | j } } n |  d } } | | f S(   s3  
    Decompose power into symbolic base and integer exponent.

    This is strictly only valid if the exponent from which
    the integer is extracted is itself an integer or the
    base is positive. These conditions are assumed and not
    checked here.

    Examples
    ========

    >>> from sympy.core.exprtools import decompose_power
    >>> from sympy.abc import x, y

    >>> decompose_power(x)
    (x, 1)
    >>> decompose_power(x**2)
    (x, 2)
    >>> decompose_power(x**(2*y))
    (x**y, 2)
    >>> decompose_power(x**(2*y/3))
    (x**(y/3), 2)

    i   t   rationali(   t   as_base_expR    t   is_RationalR2   R	   R   t   qR3   t   as_coeff_MulRU   R   RE   RB   R   (   t   exprt   baseR1   t   tail(    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   decompose_power   s    			c         C  s   |  j    \ } } | j r7 | j s |  d } } q n | j d t  \ } } | t j k rw t | |  d } } nS | t j k	 r t	 t
 d | j  |  } t | |  | j } } n |  d } } | | f S(   sD   
    Decompose power into symbolic base and rational exponent.

    i   Rm   i(   Rn   R    Ro   Rq   RU   R   RE   R	   RB   R   R   Rp   R3   (   Rr   Rs   R1   Rt   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   decompose_power_rat  s    		t   Factorsc           B  s   e  Z d  Z d d g Z d d  Z d   Z d   Z e d    Z	 e d    Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e Z d   Z d   Z d   Z d   Z RS(   s0   Efficient representation of ``f_1*f_2*...*f_n``.t   factorst   gensc         C  sL  t  | t t f  r$ t |  } n  t  | t  rE | j j   } n| d	 k s` | t j k ri i  } n| t j	 k s | d k r i t j t j	 6} nft  | t
  r{| } i  } | d k  r t j | t j <| } n  | t j k	 r | j s| j s| t j k rt j | | <qx| j re| j d k rLt j | t | j  <n  t j | t | j  <qxt d |   q nt  | t  r| j ri t j | 6} nYt  | t  rU| j   \ } } | j t  } x! t |  D] } | j t  qWt t j |  j    } | r0t j | | t <n  | r t j | t d t  |  <q n| j   } g  } x6 | D]. } | t k s| d
 k rn| j! |  qnqnW| r t j }	 x; | D]3 } t" | |  sqn  |	 | | j# |  9}	 qW|	 t j k	 r x |	 j$ r|	 j n |	 g D] }
 |
 t j k r=t j | |
 <q|
 t k rYt j | t <q|
 j% rt j | k rt j	 | t j <n  | t j c |
 j& 7<q|
 d k rt j | |
 <q|
 d k rt j | |
 <t j | t j <qt d |
   qWq n  | |  _ t' | d d	  } | d	 k r6t( d   n  t) |    |  _* d	 S(   s  Initialize Factors from dict or expr.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x
        >>> from sympy import I
        >>> e = 2*x**3
        >>> Factors(e)
        Factors({2: 1, x: 3})
        >>> Factors(e.as_powers_dict())
        Factors({2: 1, x: 3})
        >>> f = _
        >>> f.factors  # underlying dictionary
        {2: 1, x: 3}
        >>> f.gens  # base of each factor
        frozenset({2, x})
        >>> Factors(0)
        Factors({0: 1})
        >>> Factors(I)
        Factors({I: 1})

        Notes
        =====

        Although a dictionary can be passed, only minimal checking is
        performed: powers of -1 and I are made canonical.

        i    i   s'   Expected Float|Rational|Integer, not %st   evaluateis   unexpected factor in i1: %st   keyss   expecting Expr or dictionaryN(   ii   (+   R   R   R   R   Rw   Rx   t   copyR8   RB   RI   R   RE   t   is_FloatR2   t   InfinityRo   R3   R   Rp   t
   ValueErrorR
   t   argsR   t   args_cnct   countR   R   t   removet   dictR   t
   _from_argst   as_powers_dictR@   t   appendR"   RP   RW   R0   R1   t   getattrt	   TypeErrort	   frozensetRy   (   R^   Rx   Rf   Ri   t   ncR!   t   _t   handlet   kt   i1Rj   R{   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   __init__$  s    	
!		"		c         C  sK   t  t |  j j     } g  | D] } |  j | ^ q" } t | | f  S(   N(   t   tupleR   Rx   R{   t   hash(   R^   R{   R   t   values(    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   __hash__  s     c         C  sC   d d j  g  t |  j j    D] \ } } d | | f ^ q  S(   Ns   Factors({%s})s   , s   %s: %s(   t   joinR   Rx   t   items(   R^   R   Rh   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   __repr__  s    	c         C  s(   |  j  } t |  d k o' t j | k S(   sj   
        >>> from sympy.core.exprtools import Factors
        >>> Factors(0).is_zero
        True
        i   (   Rx   RK   R   RI   (   R^   t   f(    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyRF     s    	c         C  s   |  j  S(   si   
        >>> from sympy.core.exprtools import Factors
        >>> Factors(1).is_one
        True
        (   Rx   (   R^   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   is_one  s    c         C  s   g  } x |  j  j   D] \ } } | d k r | j   \ } } t | t  rg t t |  |  } n+ t | t  r t | |  } n
 | | 9} | j | |  q | j |  q Wt	 |   S(   s   Return the underlying expression.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x, y
        >>> Factors((x*y**2).as_powers_dict()).as_expr()
        x*y**2

        i   (
   Rx   R   Rn   R   t   intR   R   R   R   R   (   R^   R   t   factorR1   t   bt   e(    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   as_expr  s    
c         C  s   t  | t  s t |  } n  t d   |  | f D  rG t t j  St |  j  } xW | j j   D]F \ } } | | k r | | | } | s | | =qf q n  | | | <qf Wt |  S(   s  Return Factors of ``self * other``.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x, y, z
        >>> a = Factors((x*y**2).as_powers_dict())
        >>> b = Factors((x*y/z).as_powers_dict())
        >>> a.mul(b)
        Factors({x: 2, y: 3, z: -1})
        >>> a*b
        Factors({x: 2, y: 3, z: -1})
        c         s  s   |  ] } | j  Vq d  S(   N(   RF   (   R&   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys	   <genexpr>  s    (   R   Rw   R[   R   RI   R   Rx   R   (   R^   t   otherRx   R   R1   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   mul  s    	c         C  s?  t  | t  s\ t |  } | j r: t   t t j  f S|  j r\ t t j  t   f Sn  t |  j  } t | j  } x|  j j   D]\ } } y | j | } Wn t k
 r q n X| | } | s | | =| | =q t	 |  r | d k r| | | <| | =q%| | =| | | <q | j
 |  } | d k	 rf| rU| | | <| | =q%| | =| | =q | j   \ }	 }
 |	 r| j   \ } } |	 | } | d k r| | c | 8<| } q| d k  r| | c |	 8<| | c |	 8<| | } q|
 | | <| } n  | r| | | <q | | =q Wt |  t |  f S(   sL  Return ``self`` and ``other`` with ``gcd`` removed from each.
        The only differences between this and method ``div`` is that this
        is 1) optimized for the case when there are few factors in common and
        2) this does not raise an error if ``other`` is zero.

        See Also
        ========
        div

        i    N(   R   Rw   RF   R   RI   R   Rx   R   t   KeyErrorR"   t   extract_additivelyR8   RV   (   R^   R   t   self_factorst   other_factorsR   t   self_expt	   other_expR1   R'   t   sct   sat   oct   oaRQ   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   normal  s^    		







	
	c         C  s  t  |  j  i  } } t | t  se t |  } | j rC t  n  |  j re t t j  t   f Sn  x| j j   D]\ } } | | k r| | | } t	 |  r | d k r | | =n  | d k r | ru | | | <qu qu n  | } q| | j
 |  } | d k	 r(| r| | | <q| | =qu | } | | j   \ }	 }
 |	 r| j   \ } } |	 | } | d k r| | c | 8<| } q| d k  r| | c |	 8<| | } q|
 | | <| } n  | r| | | <qu | | k su t  qu n  | | | <qu Wt |  t |  f S(   s  Return ``self`` and ``other`` with ``gcd`` removed from each.
        This is optimized for the case when there are many factors in common.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x, y, z
        >>> from sympy import S

        >>> a = Factors((x*y**2).as_powers_dict())
        >>> a.div(a)
        (Factors({}), Factors({}))
        >>> a.div(x*z)
        (Factors({y: 2}), Factors({z: 1}))

        The ``/`` operator only gives ``quo``:

        >>> a/x
        Factors({y: 2})

        Factors treats its factors as though they are all in the numerator, so
        if you violate this assumption the results will be correct but will
        not strictly correspond to the numerator and denominator of the ratio:

        >>> a.div(x/z)
        (Factors({y: 2}), Factors({z: -1}))

        Factors is also naive about bases: it does not attempt any denesting
        of Rational-base terms, for example the following does not become
        2**(2*x)/2.

        >>> Factors(2**(2*x + 2)).div(S(8))
        (Factors({2: 2*x + 2}), Factors({8: 1}))

        factor_terms can clean up such Rational-bases powers:

        >>> from sympy.core.exprtools import factor_terms
        >>> n, d = Factors(2**(2*x + 2)).div(S(8))
        >>> n.as_expr()/d.as_expr()
        2**(2*x + 2)/8
        >>> factor_terms(_)
        2**(2*x)/2

        i    N(   R   Rx   R   Rw   RF   t   ZeroDivisionErrorR   RI   R   R"   R   R8   RV   t   AssertionError(   R^   R   t   quot   remR   R1   Rc   R'   R   R   R   R   R   RQ   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   div0  sT    .			



	
	c         C  s   |  j  |  d S(   sY  Return numerator Factor of ``self / other``.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x, y, z
        >>> a = Factors((x*y**2).as_powers_dict())
        >>> b = Factors((x*y/z).as_powers_dict())
        >>> a.quo(b)  # same as a/b
        Factors({y: 1})
        i    (   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR     s    c         C  s   |  j  |  d S(   sw  Return denominator Factors of ``self / other``.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x, y, z
        >>> a = Factors((x*y**2).as_powers_dict())
        >>> b = Factors((x*y/z).as_powers_dict())
        >>> a.rem(b)
        Factors({z: -1})
        >>> a.rem(a)
        Factors({})
        i   (   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR     s    c         C  s   t  | t  r6 | j   } | j r6 t |  } q6 n  t  | t  r | d k r i  } | r x. |  j j   D] \ } } | | | | <qm Wn  t |  St d |   d S(   s  Return self raised to a non-negative integer power.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x, y
        >>> a = Factors((x*y**2).as_powers_dict())
        >>> a**2
        Factors({x: 2, y: 4})

        i    s%   expected non-negative integer, got %sN(	   R   Rw   R   R2   R   R   Rx   R   R   (   R^   R   Rx   R   R1   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   pow  s    	
c         C  s   t  | t  s4 t |  } | j r4 t |  j  Sn  i  } x |  j j   D] \ } } t |  t |  } } | | j k rJ | | j | j } | t k r | | | <q | t k r | j | | | <q qJ qJ Wt |  S(   s  Return Factors of ``gcd(self, other)``. The keys are
        the intersection of factors with the minimum exponent for
        each factor.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x, y, z
        >>> a = Factors((x*y**2).as_powers_dict())
        >>> b = Factors((x*y/z).as_powers_dict())
        >>> a.gcd(b)
        Factors({x: 1, y: 1})
        (	   R   Rw   RF   Rx   R   R   RD   RU   R@   (   R^   R   Rx   R   R1   t   lt(    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   gcd  s    	c         C  s   t  | t  sG t |  } t d   |  | f D  rG t t j  Sn  t |  j  } xI | j j   D]8 \ } } | | k r t | | |  } n  | | | <qf Wt |  S(   s  Return Factors of ``lcm(self, other)`` which are
        the union of factors with the maximum exponent for
        each factor.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x, y, z
        >>> a = Factors((x*y**2).as_powers_dict())
        >>> b = Factors((x*y/z).as_powers_dict())
        >>> a.lcm(b)
        Factors({x: 1, y: 2, z: -1})
        c         s  s   |  ] } | j  Vq d  S(   N(   RF   (   R&   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys	   <genexpr>  s    (	   R   Rw   R[   R   RI   R   Rx   R   t   max(   R^   R   Rx   R   R1   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   lcm  s    c         C  s   |  j  |  S(   N(   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   __mul__  s    c         C  s   |  j  |  S(   N(   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt
   __divmod__  s    c         C  s   |  j  |  S(   N(   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   __div__  s    c         C  s   |  j  |  S(   N(   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   __mod__  s    c         C  s   |  j  |  S(   N(   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   __pow__  s    c         C  s.   t  | t  s t |  } n  |  j | j k S(   N(   R   Rw   Rx   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   __eq__"  s    c         C  s   |  | k S(   N(    (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   __ne__'  s    N(   t   __name__t
   __module__t   __doc__t	   __slots__R8   R   R   R   t   propertyRF   R   R   R   R   R   R   R   R   R   R   R   R   R   t   __truediv__R   R   R   R   (    (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyRw     s0   m		
			!	F	f				!							t   Termc           B  s   e  Z d  Z d d d g Z d d d  Z d   Z d   Z d   Z d   Z	 d	   Z
 d
   Z d   Z d   Z d   Z d   Z d   Z e Z d   Z d   Z d   Z RS(   s5   Efficient representation of ``coeff*(numer/denom)``. t   coefft   numert   denomc   
      C  sJ  | d  k r | d  k r | j s0 t d   n  | j   \ } } t t  t t  } } x| | D]t } t |  \ } } | j r | j   \ }	 } | |	 | 9} n  | d k r | | c | 7<qb | | c | 7<qb Wt	 |  } t	 |  } n6 | } | d  k rt	   } n  | d  k r+t	   } n  | |  _
 | |  _ | |  _ d  S(   Ns   commutative expression expectedi    (   R8   t   is_commutativeR   t   as_coeff_mulR   R   Ru   R9   t	   primitiveRw   R   R   R   (
   R^   t   termR   R   R   Rx   R   Rs   R1   t   cont(    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   0  s0    				c         C  s   t  |  j |  j |  j f  S(   N(   R   R   R   R   (   R^   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   T  s    c         C  s   d |  j  |  j |  j f S(   Ns   Term(%s, %s, %s)(   R   R   R   (   R^   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   W  s    c         C  s!   |  j  |  j j   |  j j   S(   N(   R   R   R   R   (   R^   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   Z  s    c         C  s_   |  j  | j  } |  j j | j  } |  j j | j  } | j |  \ } } t | | |  S(   N(   R   R   R   R   R   R   (   R^   R   R   R   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   ]  s
    c         C  s   t  d |  j |  j |  j  S(   Ni   (   R   R   R   R   (   R^   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   invf  s    c         C  s   |  j  | j    S(   N(   R   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   i  s    c         C  sS   | d k  r  |  j    j |  St |  j | |  j j |  |  j j |   Sd  S(   Ni    (   R   R   R   R   R   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   l  s
    c         C  s=   t  |  j j | j  |  j j | j  |  j j | j   S(   N(   R   R   R   R   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   t  s    c         C  s=   t  |  j j | j  |  j j | j  |  j j | j   S(   N(   R   R   R   R   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   y  s    c         C  s$   t  | t  r |  j |  St Sd  S(   N(   R   R   R   t   NotImplemented(   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   ~  s    c         C  s$   t  | t  r |  j |  St Sd  S(   N(   R   R   R   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR     s    c         C  s$   t  | t  r |  j |  St Sd  S(   N(   R   R   R   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR     s    c         C  s4   |  j  | j  k o3 |  j | j k o3 |  j | j k S(   N(   R   R   R   (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR     s    c         C  s   |  | k S(   N(    (   R^   R   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR     s    N(   R   R   R   R   R8   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   +  s$   $														c         C  sM  t  |  t  r1 t  |  t  r1 t j |   }  n  t t t g  |  D] } | rA | ^ qA   }  t |   d k r t	 j
 t	 j
 t	 j f St |   d k r |  d j } |  d j j   } |  d j j   } nB|  d } x! |  d D] } | j |  } q Wx- t |   D] \ } } | j |  |  | <q
W| r|  d j } x$ |  d D] } | j | j  } qKWg  }	 x} |  D]> } | j j | j | j   } |	 j | j | j    qtWn4 g  |  D] } | j   ^ q}	 t t	 d   j } | j   } t |	   } | j   } | r@| j r@| j   \ }
 } | |
 9} n  | | | f S(   sC  Helper function for :func:`gcd_terms`.

    If ``isprimitive`` is True then the call to primitive
    for an Add will be skipped. This is useful when the
    content has already been extrated.

    If ``fraction`` is True then the expression will appear over a common
    denominator, the lcm of all term denominators.
    i    i   (   R   R
   R   R   RZ   R\   t   mapR   RK   R   RI   RB   R   R   R   R   R   t	   enumerateR   R   R   R   R9   R   (   t   termst   isprimitivet   fractiont   tR   R   R   R   R!   t   numerst   _cont(    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt
   _gcd_terms  s>    .
!c      
     s[  d   } t  |  t  } | pM t  |  t  oM t |  d t oM t  |  t  } | rc| rn t |  j  }  n t |   }  | |   \ }  } t	 |     \ } }	 }
 |	 j
 |  }	 | j   \ } }   sH| j   \ } } | j rH  rH|	 j rH| j   \ } } |	 | } t d   | j D  rE| }	 | | } qEqHn  t | | |	 |
 d   St  |  t  sv|  S|  j r|  S|  j r|  j   \ } } t | t g  | D] } t |      ^ q  d   S     f d    t  |  t  r2t g  |  j D] \ } } |  |  f ^ q  S|  j g  |  j D] }  |  ^ qB  S(   s.  Compute the GCD of ``terms`` and put them together.

    ``terms`` can be an expression or a non-Basic sequence of expressions
    which will be handled as though they are terms from a sum.

    If ``isprimitive`` is True the _gcd_terms will not run the primitive
    method on the terms.

    ``clear`` controls the removal of integers from the denominator of an Add
    expression. When True (default), all numerical denominator will be cleared;
    when False the denominators will be cleared only if all terms had numerical
    denominators other than 1.

    ``fraction``, when True (default), will put the expression over a common
    denominator.

    Examples
    ========

    >>> from sympy.core import gcd_terms
    >>> from sympy.abc import x, y

    >>> gcd_terms((x + 1)**2*y + (x + 1)*y**2)
    y*(x + 1)*(x + y + 1)
    >>> gcd_terms(x/2 + 1)
    (x + 2)/2
    >>> gcd_terms(x/2 + 1, clear=False)
    x/2 + 1
    >>> gcd_terms(x/2 + y/2, clear=False)
    (x + y)/2
    >>> gcd_terms(x/2 + 1/x)
    (x**2 + 2)/(2*x)
    >>> gcd_terms(x/2 + 1/x, fraction=False)
    (x + 2/x)/2
    >>> gcd_terms(x/2 + 1/x, fraction=False, clear=False)
    x/2 + 1/x

    >>> gcd_terms(x/2/y + 1/x/y)
    (x**2 + 2)/(2*x*y)
    >>> gcd_terms(x/2/y + 1/x/y, clear=False)
    (x**2/2 + 1)/(x*y)
    >>> gcd_terms(x/2/y + 1/x/y, clear=False, fraction=False)
    (x/2 + 1/x)/y

    The ``clear`` flag was ignored in this case because the returned
    expression was a rational expression, not a simple sum.

    See Also
    ========
    factor_terms, sympy.polys.polytools.terms_gcd

    c         S  s   g  |  D]' } | j  r" | g  f n	 | j   ^ q } g  } xx t |  D]j \ } \ } } | r t |   } t   } | j | | f  | j |  t |   | | <qG | | | <qG W| t |  f S(   sp   replace nc portions of each term with a unique Dummy symbols
        and return the replacements to restore them(   R   R   R   R   R   R   R   (   R   Rj   R   Rl   R!   Ri   R   Rc   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   mask  s    4	t   includec         s  s"   |  ] } | j    d  j Vq d S(   i    N(   Rq   R2   (   R&   Rj   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys	   <genexpr>/  s   t   clearc           s   t  |  t  sp t  |  t  rG |  j g  |  j D] }  |  ^ q.   St |   g  |  D] }  |  ^ qW  St |       S(   N(   R   R   R
   t   funcR   t   typet	   gcd_terms(   Rj   R!   (   R   R   R   R   (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR   @  s
    ))(   R   R   R
   R   t   setR   R\   R   R   R   R]   Rq   R2   R9   R:   R[   R   t   is_AtomRW   R   R   R   R   (   R   R   R   R   R   t   isaddt   addlikeRl   R   R   R   R   Rx   Ri   t   _coeffRf   Rc   t   _numerR   R!   R   Rh   (    (   R   R   R   R   s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR     sF    5	
			/2c           s1         f d    t  |   }   |   S(   sT  Remove common factors from terms in all arguments without
    changing the underlying structure of the expr. No expansion or
    simplification (and no processing of non-commutatives) is performed.

    If radical=True then a radical common to all terms will be factored
    out of any Add sub-expressions of the expr.

    If clear=False (default) then coefficients will not be separated
    from a single Add if they can be distributed to leave one or more
    terms with integer coefficients.

    If fraction=True (default is False) then a common denominator will be
    constructed for the expression.

    If sign=True (default) then even if the only factor in common is a -1,
    it will be factored out of the expression.

    Examples
    ========

    >>> from sympy import factor_terms, Symbol
    >>> from sympy.abc import x, y
    >>> factor_terms(x + x*(2 + 4*y)**3)
    x*(8*(2*y + 1)**3 + 1)
    >>> A = Symbol('A', commutative=False)
    >>> factor_terms(x*A + x*A + x*y*A)
    x*(y*A + 2*A)

    When ``clear`` is False, a rational will only be factored out of an
    Add expression if all terms of the Add have coefficients that are
    fractions:

    >>> factor_terms(x/2 + 1, clear=False)
    x/2 + 1
    >>> factor_terms(x/2 + 1, clear=True)
    (x + 2)/2

    If a -1 is all that can be factored out, to *not* factor it out, the
    flag ``sign`` must be False:

    >>> factor_terms(-x - y)
    -(x + y)
    >>> factor_terms(-x - y, sign=False)
    -x - y
    >>> factor_terms(-2*x - 2*y, sign=False)
    -2*(x + y)

    See Also
    ========
    gcd_terms, sympy.polys.polytools.terms_gcd

    c      
     s  d d l  m } d d l m } t |   } t |  t  sE |  j rx | rt t |   g  |  D] }  |  ^ q[  S|  S|  j	 s |  j
 s | s t |  d  r |  j } t g  | D] }  |  ^ q  } | | k r |  S|  j |   St |  |  r| |  d  d   d  d  S|  j d  d    \ } } | j rOg  t j |  D] }	  |	  ^ qS}
 t d	   |
 D  r| } g  |
 D] }	 |	 ^ q}
 n  i  } xh t |
  D]Z \ } }	 |	 j   \ } } | j r| t | j   k rt   |
 | <|	 | |
 | <qqWt j |
  } t | d
 t d   d  j |  } n7 | j r| j g  | j D] }	  |	  ^ qh  } n  t | | d   d  } | S(   Ni(   t   Sum(   t
   factor_sumR   t   radicalR   R   t   signc         s  s.   |  ]$ } | j    d  j d  d k	 Vq d S(   i    iN(   Rq   t   extract_multiplicativelyR8   (   R&   Rj   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys	   <genexpr>  s   R   (   t   sympy.concrete.summationsR   t   sympy.simplify.simplifyR   R   R   R
   R   R   R0   t   is_Functiont   hasattrR   R   R   t   as_content_primitiveR9   R   RZ   RT   R   Rn   RW   R   R   R   R   RU   R]   R   (   Rr   R   R   t   is_iterableR!   R   t   newargsR   R3   Rj   t	   list_argst   specialR   R   R_   (   R   t   doR   R   R   (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR     sN    )	%"	(			((   R   (   Rr   R   R   R   R   (    (   R   R   R   R   R   s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   factor_termsM  s    5/c           s'   p	 d   f d   } |     f d   } |  } | j  rO |  i  g  f Sg  } t   } t   } t | d t } x t |  D] \ }	   t   f d   | D  r | j   q   j  s   j r | j    | j   q  j	 p   j
 p   j s| j    | j   qq q Wt |  d k r[| r[| j | j   |   f  n8 t |  d k r| r| j | j   |   f  n  t | d t } x= | D]5 }
 | d t  } | j |
 | f  | j |  qW| j |  } t |  } | j d t  | d	   | D | f S(
   s
  
    Return ``eq`` with non-commutative objects replaced with Dummy
    symbols. A dictionary that can be used to restore the original
    values is returned: if it is None, the expression is noncommutative
    and cannot be made commutative. The third value returned is a list
    of any non-commutative symbols that appear in the returned equation.

    ``name``, if given, is the name that will be used with numbered Dummy
    variables that will replace the non-commutative objects and is mainly
    used for doctesting purposes.

    Notes
    =====
    All non-commutative objects other than Symbols are replaced with
    a non-commutative Symbol. Identical objects will be identified
    by identical symbols.

    If there is only 1 non-commutative object in an expression it will
    be replaced with a commutative symbol. Otherwise, the non-commutative
    entities are retained and the calling routine should handle
    replacements in this case since some care must be taken to keep
    track of the ordering of symbols when they occur within Muls.

    Examples
    ========

    >>> from sympy.physics.secondquant import Commutator, NO, F, Fd
    >>> from sympy import symbols, Mul
    >>> from sympy.core.exprtools import _mask_nc
    >>> from sympy.abc import x, y
    >>> A, B, C = symbols('A,B,C', commutative=False)

    One nc-symbol:

    >>> _mask_nc(A**2 - x**2, 'd')
    (_d0**2 - x**2, {_d0: A}, [])

    Multiple nc-symbols:

    >>> _mask_nc(A**2 - B**2, 'd')
    (A**2 - B**2, {}, [A, B])

    An nc-object with nc-symbols but no others outside of it:

    >>> _mask_nc(1 + x*Commutator(A, B), 'd')
    (_d0*x + 1, {_d0: Commutator(A, B)}, [])
    >>> _mask_nc(NO(Fd(x)*F(y)), 'd')
    (_d0, {_d0: NO(CreateFermion(x)*AnnihilateFermion(y))}, [])

    Multiple nc-objects:

    >>> eq = x*Commutator(A, B) + x*Commutator(A, C)*Commutator(A, B)
    >>> _mask_nc(eq, 'd')
    (x*_d0 + x*_d1*_d0, {_d0: Commutator(A, B), _d1: Commutator(A, C)}, [_d0, _d1])

    Multiple nc-objects and nc-symbols:

    >>> eq = A*Commutator(A, B) + B*Commutator(A, C)
    >>> _mask_nc(eq, 'd')
    (A*_d0 + B*_d1, {_d0: Commutator(A, B), _d1: Commutator(A, C)}, [_d0, _d1, A, B])

    If there is an object that:

        - doesn't contain nc-symbols
        - but has arguments which derive from Basic, not Expr
        - and doesn't define an _eval_is_commutative routine

    then it will give False (or None?) for the is_commutative test. Such
    objects are also removed by this routine:

    >>> from sympy import Basic
    >>> eq = (1 + Mul(Basic(), Basic(), evaluate=False))
    >>> eq.is_commutative
    False
    >>> _mask_nc(eq, 'd')
    (_d0**2 + 1, {_d0: Basic()}, [])

    R   c          3  s0   d }  x# t  r+   t |   V|  d 7}  q	 Wd  S(   Ni    i   (   RU   t   str(   R!   (   t   name(    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   numbered_names  s    	c            s&   d d l  m } | t    |  |  S(   Ni(   R   (   t   sympyR   t   next(   R   t   kwargsR   (   t   names(    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR     s    R{   c         3  s   |  ] }   | d  k Vq d S(   i    N(    (   R&   R'   (   Rj   (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys	   <genexpr>  s    i   t   keyt   commutativec         S  s   i  |  ] \ } } | |  q S(    (    (   R&   R   Rh   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pys
   <dictcomp>9  s   	 (   R   R   R   R   R   R[   t   skipt	   is_symbolt   addR9   RW   R0   RK   R   RP   t   sortedR@   RS   R\   t   sort(   t   eqR   R   R   Rr   t   rept   nc_objt   nc_symst   potR!   Rf   R   (    (   Rj   R   R   s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   _mask_nc  sD    O						c   (      C  s  d d l  m } d d l m } m } d   } t |   }  t |  t  sU |  j rY |  S|  j	 s |  j
 g  |  j D] } t |  ^ qr   St |   \ }  } } | r | |   j |  Sg  t j |   D] } | j   ^ q } t j }	 }
 } } t } xn t |  D]` \ } } | d k r7t j | d  }	 q	| d r`| |	 t j | d   }	 q	t j }	 q	W|	 t j k	 rEt } |	 j   \ }	 }
 |
 t j k	 rxY t |  D]H \ } \ } } t t j t j t |   |
   } | | | d <qWn  xC t |  D]2 \ } \ } } | d |	 | d <| | | d <qWn  xt |  D]N\ } } | d k rx| d } n t | | d  } | sR| d d sPn  | d d d j   \ } } t } | j rx | D]V } | d sPn  | d d j   \ } } | j r,| | k r,t | |  } qPqWt } } | | } | | } x< t |  D]. \ } } | | | d d | | d d <q]WPn  | sPqqRqRWt } t |  } t |   } x4 t |  D]& \ } } | | d | | | d <qWxt |  D]N\ } } | d k r,| d } n t | | d  } | s| d d sWPn  | d d d j   \ } } t } | j rGx | D]V } | d sPn  | d d j   \ } } | j r| | k rt | |  } qPqWt } } | | } | | } x< t |  D]. \ } } | | d d | | | d d <qWPn  | sTPqTqqWt } t |  } t |   } x> t |  D]0 \ } } | d t | d  |  | | d <qW| rt g  | D]" \ } } t |   t |   ^ q  } n |  } g  t  | d t! D] } | t"   f ^ q} g  | D] \ } } | | f ^ q0} | j#   t | j |   \ }  }! } | | |    }  |  j |!  j |  }  |  j$ rt% |	 |
 | |  |  S|  j& rg  }" g  }# xi |  j D]^ }$ |$ j' r |" j( |$  q|$ j   \ } } | j r2|# j) | g |  q|# j( |$  qW|
 t |"   | }% | |  |	  }& xS t* |# t |#   D]9 }' |% t |'   | } | |  |& k r}t% |	 |  Sq}Wn  t% |	 |
 | | |  Sd S(	   s  Return the factored form of ``expr`` while handling non-commutative
    expressions.

    Examples
    ========

    >>> from sympy.core.exprtools import factor_nc
    >>> from sympy import Symbol
    >>> from sympy.abc import x
    >>> A = Symbol('A', commutative=False)
    >>> B = Symbol('B', commutative=False)
    >>> factor_nc((x**2 + 2*A*x + A**2).expand())
    (x + A)**2
    >>> factor_nc(((x + A)*(x + B)).expand())
    (x + A)*(x + B)
    i(   t   powsimp(   R   R   c         S  s4   |  j  d t d t d t d t d t d t d t  S(   sC   Expand with the minimal set of hints necessary to check the result.t   deepR   t	   power_expt
   power_baset   basict   multinomialt   log(   t   expandRU   R@   (   Rr   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt
   _pemexpandP  s    i    i   R   N(+   R   R  t   sympy.polysR   R   R   R   R   R   R9   R   t	   factor_ncR  RS   R   RZ   R   R   RB   R@   R   R   R   RU   Rq   R\   R   Rn   R2   t   minRK   R   R   R   R   t   reverseR0   R   RW   R   R   t   extendR   ((   Rr   R  R   R   R  Rj   R  t
   nc_symbolsR   Ri   t   gt   lR'   t   hitR!   t   ccR   Rf   R   R   t   okR   t   btt   ett   ilt   lennR   t   midt   rep1R   Rh   t   unrep1t   new_midt   r2t   cfact   ncfacR   t   pre_midt   targetR`   (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyR  <  s    		)(
(	


&	


&(8.%
				N(>   R   t
   __future__R    R   t   sympy.core.addR   t   sympy.core.compatibilityR   R   R   R   t   sympy.core.mulR   R   t   sympy.core.powerR	   t   sympy.core.basicR
   R   t   sympy.core.exprR   t   sympy.core.sympifyR   t   sympy.core.numbersR   R   R   R   t   sympy.core.singletonR   t   sympy.core.symbolR   t   sympy.core.coreerrorsR   t   sympy.core.containersR   R   t   sympy.utilitiesR   t   sympy.utilities.iterablesR   R   R   R   t   collectionsR   RU   RC   R"   R7   Ru   Rv   t   objectRw   R   R@   R   R   R   R8   R  R  (    (    (    s3   lib/python2.7/site-packages/sympy/core/exprtools.pyt   <module>   s<   """			1	  p<vh