ó
¡¼™\c           @  s¦  d  Z  d d l 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 m Z m Z m Z d d l m Z d d l m Z m Z d d l m Z d d l m Z m Z m Z m Z d d	 d
 d d g Z d	 e f d „  ƒ  YZ d e f d „  ƒ  YZ d
 e f d „  ƒ  YZ  d e f d „  ƒ  YZ! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d d „ Z) d „  Z* d „  Z+ d „  Z, d „  Z- d „  Z. d S(   s   Clebsch-Gordon Coefficients.iÿÿÿÿ(   t   print_functiont   division(   t   Addt   expandt   Eqt   Exprt   Mult	   Piecewiset   Powt   sqrtt   Sumt   symbolst   sympifyt   Wild(   t   range(   t
   prettyFormt
   stringPict(   t   KroneckerDelta(   t   clebsch_gordant	   wigner_3jt	   wigner_6jt	   wigner_9jt   CGt   Wigner3jt   Wigner6jt   Wigner9jt   cg_simpc           B  s¡   e  Z d  Z e Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d	 „  Z d
 „  Z d „  Z RS(   s‚  Class for the Wigner-3j symbols

    Wigner 3j-symbols are coefficients determined by the coupling of
    two angular momenta. When created, they are expressed as symbolic
    quantities that, for numerical parameters, can be evaluated using the
    ``.doit()`` method [1]_.

    Parameters
    ==========

    j1, m1, j2, m2, j3, m3 : Number, Symbol
        Terms determining the angular momentum of coupled angular momentum
        systems.

    Examples
    ========

    Declare a Wigner-3j coefficient and calculate its value

        >>> from sympy.physics.quantum.cg import Wigner3j
        >>> w3j = Wigner3j(6,0,4,0,2,0)
        >>> w3j
        Wigner3j(6, 0, 4, 0, 2, 0)
        >>> w3j.doit()
        sqrt(715)/143

    See Also
    ========

    CG: Clebsch-Gordan coefficients

    References
    ==========

    .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988.
    c         C  s1   t  t | | | | | | f ƒ } t j |  | Œ S(   N(   t   mapR   R   t   __new__(   t   clst   j1t   m1t   j2t   m2t   j3t   m3t   args(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   F   s    !c         C  s   |  j  d S(   Ni    (   R$   (   t   self(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   J   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   N   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR    R   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR!   V   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR"   Z   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR#   ^   s    c         C  s$   t  g  |  j D] } | j ^ q ƒ S(   N(   t   allR$   t	   is_number(   R%   t   arg(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyt   is_symbolicb   s    c         G  s'  | j  |  j ƒ | j  |  j ƒ f | j  |  j ƒ | j  |  j ƒ f | j  |  j ƒ | j  |  j ƒ f f } d } d } d g d } xK t d ƒ D]= } t g  t d ƒ D] } | | | j	 ƒ  ^ q¥ ƒ | | <q Wd  }	 x8t d ƒ D]*} d  }
 xÂ t d ƒ D]´ } | | | } | | | j	 ƒ  } | d } | | } t | j d | ƒ Œ  } t | j d | ƒ Œ  } |
 d  k r‚| }
 qü n  t |
 j d | ƒ Œ  }
 t |
 j | ƒ Œ  }
 qü W|	 d  k rÌ|
 }	 qã n  x) t | ƒ D] } t |	 j d ƒ Œ  }	 qÙWt |	 j |
 ƒ Œ  }	 qã Wt |	 j ƒ  Œ  }	 |	 S(   Ni   i   iÿÿÿÿi   t    (   t   _printR   R   R    R!   R"   R#   R   t   maxt   widtht   NoneR   t   rightt   leftt   belowt   parens(   R%   t   printerR$   t   mt   hsept   vsept   maxwt   jt   it   Dt   D_rowt   st   wdeltat   wleftt   wrightt   _(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyt   _prettyg   s>    !!';

c         G  sD   t  | j |  j |  j |  j |  j |  j |  j f ƒ } d t | ƒ S(   NsH   \left(\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right)(	   R   R+   R   R    R"   R   R!   R#   t   tuple(   R%   R3   R$   t   label(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyt   _latexŠ   s    c         K  sC   |  j  r t d ƒ ‚ n  t |  j |  j |  j |  j |  j |  j ƒ S(   Ns   Coefficients must be numerical(	   R)   t
   ValueErrorR   R   R    R"   R   R!   R#   (   R%   t   hints(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyt   doit   s    	(   t   __name__t
   __module__t   __doc__t   Truet   is_commutativeR   t   propertyR   R   R    R!   R"   R#   R)   RA   RD   RG   (    (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR      s   $		#	c           B  s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s<  Class for Clebsch-Gordan coefficient

    Clebsch-Gordan coefficients describe the angular momentum coupling between
    two systems. The coefficients give the expansion of a coupled total angular
    momentum state and an uncoupled tensor product state. The Clebsch-Gordan
    coefficients are defined as [1]_:

    .. math ::
        C^{j_1,m_1}_{j_2,m_2,j_3,m_3} = \left\langle j_1,m_1;j_2,m_2 | j_3,m_3\right\rangle

    Parameters
    ==========

    j1, m1, j2, m2, j3, m3 : Number, Symbol
        Terms determining the angular momentum of coupled angular momentum
        systems.

    Examples
    ========

    Define a Clebsch-Gordan coefficient and evaluate its value

        >>> from sympy.physics.quantum.cg import CG
        >>> from sympy import S
        >>> cg = CG(S(3)/2, S(3)/2, S(1)/2, -S(1)/2, 1, 1)
        >>> cg
        CG(3/2, 3/2, 1/2, -1/2, 1, 1)
        >>> cg.doit()
        sqrt(3)/2

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    References
    ==========

    .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988.
    c         K  sC   |  j  r t d ƒ ‚ n  t |  j |  j |  j |  j |  j |  j ƒ S(   Ns   Coefficients must be numerical(	   R)   RE   R   R   R    R"   R   R!   R#   (   R%   RF   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRG   À   s    	c         G  sE  | j  |  j |  j |  j |  j f d d ƒ} | j  |  j |  j f d d ƒ} t | j ƒ  | j ƒ  ƒ } t	 | j
 d ƒ Œ  } t	 | j
 d ƒ Œ  } | | j ƒ  k sË t	 | j d | | j ƒ  ƒ Œ  } n  | | j ƒ  k st	 | j d | | j ƒ  ƒ Œ  } n  t d d | ƒ } t	 | j | ƒ Œ  } t	 | j | ƒ Œ  } | S(   Nt	   delimitert   ,R*   t   C(   t
   _print_seqR   R   R    R!   R"   R#   R,   R-   R   R0   R/   R   R1   t   above(   R%   R3   R$   t   bott   topt   padR<   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRA   Å   s    '!&&c         G  sD   t  | j |  j |  j |  j |  j |  j |  j f ƒ } d t | ƒ S(   Ns   C^{%s,%s}_{%s,%s,%s,%s}(	   R   R+   R"   R#   R   R   R    R!   RB   (   R%   R3   R$   RC   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRD   ×   s    (   RH   RI   RJ   RG   RA   RD   (    (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   –   s   (		c           B  s›   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z d	 „  Z d
 „  Z d „  Z RS(   sa   Class for the Wigner-6j symbols

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    c         C  s1   t  t | | | | | | f ƒ } t j |  | Œ S(   N(   R   R   R   R   (   R   R   R    t   j12R"   R8   t   j23R$   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   æ   s    !c         C  s   |  j  d S(   Ni    (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   ê   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR    î   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRV   ò   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR"   ö   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR8   ú   s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRW   þ   s    c         C  s$   t  g  |  j D] } | j ^ q ƒ S(   N(   R&   R$   R'   (   R%   R(   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR)     s    c         G  s3  | j  |  j ƒ | j  |  j ƒ f | j  |  j ƒ | j  |  j ƒ f | j  |  j ƒ | j  |  j ƒ f f } d } d } d g d } xK t d ƒ D]= } t g  t d ƒ D] } | | | j	 ƒ  ^ q¥ ƒ | | <q Wd  }	 x8t d ƒ D]*} d  }
 xÂ t d ƒ D]´ } | | | } | | | j	 ƒ  } | d } | | } t | j d | ƒ Œ  } t | j d | ƒ Œ  } |
 d  k r‚| }
 qü n  t |
 j d | ƒ Œ  }
 t |
 j | ƒ Œ  }
 qü W|	 d  k rÌ|
 }	 qã n  x) t | ƒ D] } t |	 j d ƒ Œ  }	 qÙWt |	 j |
 ƒ Œ  }	 qã Wt |	 j d d d d	 ƒ Œ  }	 |	 S(
   Ni   i   iÿÿÿÿi   R*   R0   t   {R/   t   }(   R+   R   R"   R    R8   RV   RW   R   R,   R-   R.   R   R/   R0   R1   R2   (   R%   R3   R$   R4   R5   R6   R7   R8   R9   R:   R;   R<   R=   R>   R?   R@   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRA     s>    !!';

c         G  sD   t  | j |  j |  j |  j |  j |  j |  j f ƒ } d t | ƒ S(   NsJ   \left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \end{array}\right\}(	   R   R+   R   R    RV   R"   R8   RW   RB   (   R%   R3   R$   RC   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRD   *  s    c         K  sC   |  j  r t d ƒ ‚ n  t |  j |  j |  j |  j |  j |  j ƒ S(   Ns   Coefficients must be numerical(	   R)   RE   R   R   R    RV   R"   R8   RW   (   R%   RF   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRG   0  s    	(   RH   RI   RJ   R   RM   R   R    RV   R"   R8   RW   R)   RA   RD   RG   (    (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   Ý   s   		#	c           B  sÈ   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z e d	 „  ƒ Z e d
 „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z RS(   sa   Class for the Wigner-9j symbols

    See Also
    ========

    Wigner3j: Wigner-3j symbols

    c
         C  s:   t  t | | | | | | | | |	 f	 ƒ }
 t j |  |
 Œ S(   N(   R   R   R   R   (   R   R   R    RV   R"   t   j4t   j34t   j13t   j24R8   R$   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   ?  s    *c         C  s   |  j  d S(   Ni    (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   C  s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR    G  s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRV   K  s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR"   O  s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRZ   S  s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR[   W  s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR\   [  s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR]   _  s    c         C  s   |  j  d S(   Ni   (   R$   (   R%   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR8   c  s    c         C  s$   t  g  |  j D] } | j ^ q ƒ S(   N(   R&   R$   R'   (   R%   R(   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR)   g  s    c         G  s`  | j  |  j ƒ | j  |  j ƒ | j  |  j ƒ f | j  |  j ƒ | j  |  j ƒ | j  |  j ƒ f | j  |  j ƒ | j  |  j ƒ | j  |  j	 ƒ f f } d } d } d g d } xK t
 d ƒ D]= } t g  t
 d ƒ D] } | | | j ƒ  ^ qÒ ƒ | | <q¼ Wd  }	 x8t
 d ƒ D]*} d  }
 xÂ t
 d ƒ D]´ } | | | } | | | j ƒ  } | d } | | } t | j d | ƒ Œ  } t | j d | ƒ Œ  } |
 d  k r¯| }
 q)n  t |
 j d | ƒ Œ  }
 t |
 j | ƒ Œ  }
 q)W|	 d  k rù|
 }	 qn  x) t
 | ƒ D] } t |	 j d ƒ Œ  }	 qWt |	 j |
 ƒ Œ  }	 qWt |	 j d d d d	 ƒ Œ  }	 |	 S(
   Ni   i   iÿÿÿÿi   R*   R0   RX   R/   RY   (   R+   R   R"   R\   R    RZ   R]   RV   R[   R8   R   R,   R-   R.   R   R/   R0   R1   R2   (   R%   R3   R$   R4   R5   R6   R7   R8   R9   R:   R;   R<   R=   R>   R?   R@   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRA   l  sB    **6;

c         G  sV   t  | j |  j |  j |  j |  j |  j |  j |  j |  j	 |  j
 f	 ƒ } d t | ƒ S(   NsZ   \left\{\begin{array}{ccc} %s & %s & %s \\ %s & %s & %s \\ %s & %s & %s \end{array}\right\}(   R   R+   R   R    RV   R"   RZ   R[   R\   R]   R8   RB   (   R%   R3   R$   RC   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRD   ’  s    !'c      
   K  sU   |  j  r t d ƒ ‚ n  t |  j |  j |  j |  j |  j |  j |  j	 |  j
 |  j ƒ	 S(   Ns   Coefficients must be numerical(   R)   RE   R   R   R    RV   R"   RZ   R[   R\   R]   R8   (   R%   RF   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRG   ˜  s    	(   RH   RI   RJ   R   RM   R   R    RV   R"   RZ   R[   R\   R]   R8   R)   RA   RD   RG   (    (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   6  s   		&	c         C  s—   t  |  t ƒ r t |  ƒ St  |  t ƒ r2 t |  ƒ St  |  t ƒ rg t g  |  j D] } t | ƒ ^ qN Œ  St  |  t ƒ r t t |  j	 ƒ |  j
 ƒ S|  Sd S(   s¥  Simplify and combine CG coefficients

    This function uses various symmetry and properties of sums and
    products of Clebsch-Gordan coefficients to simplify statements
    involving these terms [1]_.

    Examples
    ========

    Simplify the sum over CG(a,alpha,0,0,a,alpha) for all alpha to
    2*a+1

        >>> from sympy.physics.quantum.cg import CG, cg_simp
        >>> a = CG(1,1,0,0,1,1)
        >>> b = CG(1,0,0,0,1,0)
        >>> c = CG(1,-1,0,0,1,-1)
        >>> cg_simp(a+b+c)
        3

    See Also
    ========

    CG: Clebsh-Gordan coefficients

    References
    ==========

    .. [1] Varshalovich, D A, Quantum Theory of Angular Momentum. 1988.
    N(   t
   isinstanceR   t   _cg_simp_addR
   t   _cg_simp_sumR   R$   R   R   t   baset   exp(   t   eR(   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   ž  s    

&c         C  sr  g  } g  } t  |  ƒ }  xæ |  j D]Û } | j t ƒ rð t | t ƒ r\ | j t | ƒ ƒ qý t | t ƒ rà d } x= | j D]2 } t | t ƒ r£ | t | ƒ 9} q{ | | 9} q{ W| j t ƒ rÐ | j | ƒ qí | j | ƒ qý | j | ƒ q" | j | ƒ q" Wt	 | ƒ \ } } | j | ƒ t
 | ƒ \ } } | j | ƒ t | ƒ \ } } | j | ƒ t | Œ  t | Œ  S(   s  Takes a sum of terms involving Clebsch-Gordan coefficients and
    simplifies the terms.

    First, we create two lists, cg_part, which is all the terms involving CG
    coefficients, and other_part, which is all other terms. The cg_part list
    is then passed to the simplification methods, which return the new cg_part
    and any additional terms that are added to other_part
    i   (   R   R$   t   hasR   R^   R
   t   appendR`   R   t   _check_varsh_871_1t   _check_varsh_871_2t   _check_varsh_872_9R   (   Rc   t   cg_partt
   other_partR(   t   termst   termt   other(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR_   È  s2    
c   
   
   C  s±   t  t d ƒ \ } } } } | t | | | d | | ƒ } d | d t | d ƒ } | t | ƒ } d | d } | | }	 t | | | | |  | | | | f | | f | |	 ƒ	 S(	   Nt   at   alphat   bt   lti    i   i   (   Rn   Ro   Rp   Rq   (   R   R   R   R   t   abst   _check_cg_simp(
   t	   term_listRn   Ro   Rp   Rq   t   exprt   simpt   signt
   build_exprt
   index_expr(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRf   ó  s    
c   
   
   C  sÄ   t  t d	 ƒ \ } } } } | t | | | | | d ƒ } t d | d ƒ t | d ƒ } d | | | t | ƒ } d | d } | | }	 t | | | | |  | | | | f | | f | |	 ƒ	 S(
   NRn   Ro   t   cRq   i    i   i   iÿÿÿÿ(   Rn   Ro   Rz   Rq   (   R   R   R   R	   R   Rr   Rs   (
   Rt   Rn   Ro   Rz   Rq   Ru   Rv   Rw   Rx   Ry   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRg   þ  s     !
c         C  s  t  t d ƒ \	 } } } } } } } } }	 |	 t | | | | | | ƒ d
 }
 d } |	 t |	 ƒ } t | | ƒ } t | | ƒ } | | d t | | | k f d t | | ƒ f | | | k f ƒ } | | | } t |
 | | |	 |  | | | | | | |	 f | | | | f | | ƒ	 \ }  } t | | ƒ } | | } | d | | | d } | | | | | | } t |
 | | |	 |  | | | | | | |	 f | | | | f | | ƒ	 \ }  } t | | | | | | ƒ t | | | | | | ƒ }
 t | | ƒ t | | ƒ } t d ƒ } t | | ƒ } t | | ƒ } | | d t | | | k f d t | | ƒ f | | | k f ƒ } | | | } t |
 | | t d ƒ |  | | | | | | | | f | | | | | | f | | ƒ	 \ }  } t | | ƒ } | | } | d | | | d } | | | | | | } t |
 | | t d ƒ |  | | | | | | | | f | | | | | | f | | ƒ	 \ }  } |  | | | f S(   NRn   Ro   t   alphapRp   t   betat   betapRz   t   gammaRq   i   i   i    (	   Rn   Ro   R{   Rp   R|   R}   Rz   R~   Rq   (	   R   R   R   Rr   R   R   Rs   R   R   (   Rt   Rn   Ro   R{   Rp   R|   R}   Rz   R~   Rq   Ru   Rv   Rw   t   xt   yRx   Ry   t   other1t   other2t   other3t   other4(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRh   	  s:    $#EK
K4EZ
Zc	      	   C  sÖ  d }	 d }
 x½|
 t  | ƒ k  rËt | |
 |  t  | ƒ ƒ } | d k rY |
 d 7}
 q n  t | j | ƒ ƒ j s |
 d 7}
 q n  g  | D] } | | | f ^ qˆ } d g | j | ƒ } xt |
 t  | ƒ ƒ D]í } t | | |  j | ƒ t  | ƒ t  | ƒ d | j | ƒ | j | ƒ f ƒ} | d k r5qÐ n  t | j | ƒ j | ƒ ƒ j s\qÐ n  | |  j | d ƒ j | ƒ j | ƒ | j | ƒ | j | ƒ j | ƒ f | | j | ƒ j | ƒ <qÐ Wt d „  | Dƒ ƒ r¾t g  | D] } t	 | d ƒ ^ qáŒ  } g  | D] } | d ^ q} | j
 ƒ  | j ƒ  g  | D] } | j | ƒ ^ q8xL | D]D } t	 | d ƒ | k rX| j | d | | d | d ƒ qXqXW|	 | | | j | ƒ 7}	 q |
 d 7}
 q W| |	 f S(   s½   Checks for simplifications that can be made, returning a tuple of the
    simplified list of terms and any terms generated by simplification.

    Parameters
    ==========

    expr: expression
        The expression with Wild terms that will be matched to the terms in
        the sum

    simp: expression
        The expression with Wild terms that is substituted in place of the CG
        terms in the case of simplification

    sign: expression
        The expression with Wild terms denoting the sign that is on expr that
        must match

    lt: expression
        The expression with Wild terms that gives the leading term of the
        matched expr

    term_list: list
        A list of all of the terms is the sum to be simplified

    variables: list
        A list of all the variables that appears in expr

    dep_variables: list
        A list of the variables that must match for all the terms in the sum,
        i.e. the dependent variables

    build_index_expr: expression
        Expression with Wild terms giving the number of elements in cg_index

    index_expr: expression
        Expression with Wild terms giving the index terms have when storing
        them to cg_index

    i    i   Rw   c         s  s   |  ] } | d  k	 Vq d  S(   N(   R.   (   t   .0R9   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pys	   <genexpr>r  s    i   i   N(   t   lent	   _check_cgR.   R   t   subsR'   R   R&   t   minRr   t   sortt   reverset   popRe   (   Ru   Rv   Rw   Rq   Rt   t	   variablest   dep_variablest   build_index_exprRy   Rj   R9   t   sub_1R   t   sub_dept   cg_indexR8   t   sub_2Rl   t   min_ltt   indices(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRs   6  s>    )

#M!e)

 ,c         C  s‡   |  j  | ƒ } | d k r d S| d k	 rm t | t ƒ sI t d ƒ ‚ n  | d | d j | ƒ k sm d Sn  t | ƒ | k rƒ | Sd S(   s2   Checks whether a term matches the given expressionNs   sign must be a tuplei    i   (   t   matchR.   R^   RB   t	   TypeErrorRˆ   R†   (   t   cg_termRu   t   lengthRw   t   matches(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR‡     s    c         C  s(   t  |  ƒ }  t |  ƒ }  t |  ƒ }  |  S(   N(   t   _check_varsh_sum_871_1t   _check_varsh_sum_871_2t   _check_varsh_sum_872_4(   Rc   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR`     s    c      	   C  sŸ   t  d ƒ } t d ƒ } t  d ƒ } |  j t t | | | d | | ƒ | | | f ƒ ƒ } | d  k	 r› t | ƒ d k r› d | d t | d ƒ j | ƒ S|  S(   NRn   Ro   Rp   i    i   i   (	   R   R   R–   R
   R   R.   R†   R   Rˆ   (   Rc   Rn   Ro   Rp   R–   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR›   —  s    7"c      
   C  s²   t  d ƒ } t d ƒ } t  d ƒ } |  j t d | | t | | | | | d ƒ | | | f ƒ ƒ } | d  k	 r® t | ƒ d k r® t d | d ƒ t | d ƒ j	 | ƒ S|  S(   NRn   Ro   Rz   iÿÿÿÿi    i   i   (
   R   R   R–   R
   R   R.   R†   R	   R   Rˆ   (   Rc   Rn   Ro   Rz   R–   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyRœ   ¡  s    >(c      
   C  sl  t  d ƒ } t  d ƒ } t  d ƒ } t  d ƒ } t  d ƒ } t  d ƒ } t  d ƒ } t  d ƒ } |  j t t | | | | | | ƒ t | | | | | | ƒ | | | f | | | f ƒ ƒ }	 |	 d  k	 rþ t |	 ƒ d	 k rþ t | | ƒ t | | ƒ j |	 ƒ S|  j t t | | | | | | ƒ d
 | | | f | | | f ƒ ƒ }
 |
 d  k	 rht |
 ƒ d k rhd S|  S(   NRn   Ro   Rp   R|   Rz   t   cpR~   t   gammapi   i   i   i   (   R   R–   R
   R   R.   R†   R   Rˆ   (   Rc   Rn   Ro   Rp   R|   Rz   Rž   R~   RŸ   t   match1t   match2(    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyR   ¬  s"    $9#	?c         C  s4  t  |  t ƒ r |  f d d f Sg  } d } t  |  t ƒ pF t  |  t ƒ sX t d ƒ ‚ n  t  |  t ƒ rÍ t |  j ƒ j rÍ t |  j ƒ j rº g  t |  j ƒ D] } | j	 |  j
 ƒ ^ q› qÍ |  f d d f Sn  t  |  t ƒ r0x: |  j D]/ } t  | t ƒ r| j	 | ƒ qæ | | 9} qæ W| | | t | ƒ f Sd  S(   Ni   s    term must be CG, Add, Mul or Pow(   R^   R   R   R   t   NotImplementedErrorR   Rb   R'   R   Re   Ra   R$   Rr   (   Rl   t   cgt   coeffR@   R(   (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyt   _cg_listÀ  s     !/N(/   RJ   t
   __future__R    R   t   sympyR   R   R   R   R   R   R   R	   R
   R   R   R   t   sympy.core.compatibilityR   t    sympy.printing.pretty.stringpictR   R   t(   sympy.functions.special.tensor_functionsR   t   sympy.physics.wignerR   R   R   R   t   __all__R   R   R   R   R   R_   Rf   Rg   Rh   Rs   R.   R‡   R`   R›   Rœ   R   R¥   (    (    (    s7   lib/python2.7/site-packages/sympy/physics/quantum/cg.pyt   <module>   s6   R"	xGYh	*	+			-	K		
		