ó
¡¼™\c           @  s‘   d  Z  d d l m Z 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 g Z d e f d „  ƒ  YZ d	 S(
   s"   The commutator: [A,B] = A*B - B*A.iÿÿÿÿ(   t   print_functiont   division(   t   St   Exprt   Mult   Add(   t
   prettyForm(   t   Dagger(   t   Operatort
   Commutatorc           B  sk   e  Z d  Z e Z d „  Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d	 „  Z RS(
   s  The standard commutator, in an unevaluated state.

    Evaluating a commutator is defined [1]_ as: ``[A, B] = A*B - B*A``. This
    class returns the commutator in an unevaluated form. To evaluate the
    commutator, use the ``.doit()`` method.

    Canonical ordering of a commutator is ``[A, B]`` for ``A < B``. The
    arguments of the commutator are put into canonical order using ``__cmp__``.
    If ``B < A``, then ``[B, A]`` is returned as ``-[A, B]``.

    Parameters
    ==========

    A : Expr
        The first argument of the commutator [A,B].
    B : Expr
        The second argument of the commutator [A,B].

    Examples
    ========

    >>> from sympy.physics.quantum import Commutator, Dagger, Operator
    >>> from sympy.abc import x, y
    >>> A = Operator('A')
    >>> B = Operator('B')
    >>> C = Operator('C')

    Create a commutator and use ``.doit()`` to evaluate it:

    >>> comm = Commutator(A, B)
    >>> comm
    [A,B]
    >>> comm.doit()
    A*B - B*A

    The commutator orders it arguments in canonical order:

    >>> comm = Commutator(B, A); comm
    -[A,B]

    Commutative constants are factored out:

    >>> Commutator(3*x*A, x*y*B)
    3*x**2*y*[A,B]

    Using ``.expand(commutator=True)``, the standard commutator expansion rules
    can be applied:

    >>> Commutator(A+B, C).expand(commutator=True)
    [A,C] + [B,C]
    >>> Commutator(A, B+C).expand(commutator=True)
    [A,B] + [A,C]
    >>> Commutator(A*B, C).expand(commutator=True)
    [A,C]*B + A*[B,C]
    >>> Commutator(A, B*C).expand(commutator=True)
    [A,B]*C + B*[A,C]

    Adjoint operations applied to the commutator are properly applied to the
    arguments:

    >>> Dagger(Commutator(A, B))
    -[Dagger(A),Dagger(B)]

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Commutator
    c         C  s;   |  j  | | ƒ } | d  k	 r" | St j |  | | ƒ } | S(   N(   t   evalt   NoneR   t   __new__(   t   clst   At   Bt   rt   obj(    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyR   \   s
    c         C  sÎ   | o	 | s t  j S| | k r& t  j S| j s8 | j r? t  j S| j ƒ  \ } } | j ƒ  \ } } | | } | r¡ t t | Œ  |  t j | ƒ t j | ƒ ƒ ƒ S| j | ƒ d k rÊ t  j |  | | ƒ Sd  S(   Ni   (   R   t   Zerot   is_commutativet   args_cncR   t
   _from_argst   comparet   NegativeOne(   R   t   at   bt   cat   ncat   cbt   ncbt   c_part(    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyR
   c   s    
.c         K  sl  |  j  d } |  j  d } t | t ƒ r‡ g  } xK | j  D]@ } t | | ƒ } t | t ƒ rl | j ƒ  } n  | j | ƒ q9 Wt | Œ  St | t ƒ rô g  } xK | j  D]@ } t | | ƒ } t | t ƒ rÙ | j ƒ  } n  | j | ƒ q¦ Wt | Œ  St | t ƒ r®| j  d } t | j  d Œ  } | }	 t | |	 ƒ }
 t | |	 ƒ } t |
 t ƒ re|
 j ƒ  }
 n  t | t ƒ rƒ| j ƒ  } n  t | |
 ƒ } t | | ƒ } t | | ƒ St | t ƒ rh| } | j  d } t | j  d Œ  }	 t | | ƒ }
 t | |	 ƒ } t |
 t ƒ r|
 j ƒ  }
 n  t | t ƒ r=| j ƒ  } n  t |
 |	 ƒ } t | | ƒ } t | | ƒ S|  S(   Ni    i   (   t   argst
   isinstanceR   R	   t   _eval_expand_commutatort   appendR   (   t   selft   hintsR   R   t   sargst   termt   commR   R   t   ct   comm1t   comm2t   firstt   second(    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyR!   x   sZ    

c         K  sË   |  j  d } |  j  d } t | t ƒ r² t | t ƒ r² y | j | |  } WnE t k
 r• y d | j | |  } Wq– t k
 r‘ d } q– Xn X| d k	 r² | j |   Sn  | | | | j |   S(   s    Evaluate commutator i    i   iÿÿÿÿN(   R   R    R   t   _eval_commutatort   NotImplementedErrorR   t   doit(   R#   R$   R   R   R'   (    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyR/   ®   s    c         C  s'   t  t |  j d ƒ t |  j d ƒ ƒ S(   Ni   i    (   R	   R   R   (   R#   (    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyt   _eval_adjoint¾   s    c         G  s7   d |  j  j | j |  j d ƒ | j |  j d ƒ f S(   Ns	   %s(%s,%s)i    i   (   t	   __class__t   __name__t   _printR   (   R#   t   printerR   (    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyt
   _sympyreprÁ   s    c         G  s   d |  j  d |  j  d f S(   Ns   [%s,%s]i    i   (   R   (   R#   R4   R   (    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyt	   _sympystrÇ   s    c         G  s~   | j  |  j d | Œ } t | j t d ƒ ƒ Œ  } t | j | j  |  j d | Œ ƒ Œ  } t | j d d d d ƒ Œ  } | S(   Ni    u   ,i   t   leftt   [t   rightt   ](   R3   R   R   R9   t   parens(   R#   R4   R   t   pform(    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyt   _prettyÊ   s
    (c         G  s0   d t  g  |  j D] } | j | | Œ ^ q ƒ S(   Ns   \left[%s,%s\right](   t   tupleR   R3   (   R#   R4   R   t   arg(    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyt   _latexÑ   s    (   R2   t
   __module__t   __doc__t   FalseR   R   t   classmethodR
   R!   R/   R0   R5   R6   R=   R@   (    (    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyR	      s   D		6					N(   RB   t
   __future__R    R   t   sympyR   R   R   R   t    sympy.printing.pretty.stringpictR   t   sympy.physics.quantum.daggerR   t   sympy.physics.quantum.operatorR   t   __all__R	   (    (    (    s?   lib/python2.7/site-packages/sympy/physics/quantum/commutator.pyt   <module>   s   "	