§
    ^²Åja  ã                   óŒ   — d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ d
gZ G d„ d
e¦  «        ZdS )z"The commutator: [A,B] = A*B - B*A.é    )ÚAdd)ÚExpr)ÚMul)ÚPow)ÚS)Ú
prettyForm)ÚDagger)ÚOperatorÚ
Commutatorc                   ób   — e Zd ZdZdZd„ Zed„ ¦   «         Zd„ Zd„ Z	d„ Z
d„ Zd	„ Zd
„ Zd„ Zd„ ZdS )r   a?  The standard commutator, in an unevaluated state.

    Explanation
    ===========

    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
    Fc                 óf   — |                       ||¦  «        }|�|S t          j        | ||¦  «        }|S )N)Úevalr   Ú__new__)ÚclsÚAÚBÚrÚobjs        úQC:\Projects\Historical\venv\Lib\site-packages\sympy/physics/quantum/commutator.pyr   zCommutator.__new__a   s5   € Ø�HŠH�Q˜‰NŒNˆØˆ=ØˆHÝŒl˜3  1Ñ%Ô%ˆØˆ
ó    c           	      óÖ  — |r|st           j        S ||k    rt           j        S |j        s|j        rt           j        S |                     ¦   «         \  }}|                     ¦   «         \  }}||z   }|rEt	          t	          |Ž  | t	          j        |¦  «        t	          j        |¦  «        ¦  «        ¦  «        S |                     |¦  «        dk    rt           j         | ||¦  «        z  S d S )Né   )r   ÚZeroÚis_commutativeÚargs_cncr   Ú
_from_argsÚcompareÚNegativeOne)r   ÚaÚbÚcaÚncaÚcbÚncbÚc_parts           r   r   zCommutator.evalh   sä   € àð 	�að 	Ý”6ˆMØ�Š6ˆ6Ý”6ˆMØÔð 	˜qÔ/ð 	Ý”6ˆMð —*’*‘,”,‰ˆˆCØ—*’*‘,”,‰ˆˆCØ�b‘ˆØð 	TÝ•s˜F�| S S­¬¸Ñ)<Ô)<½c¼nÈSÑ>QÔ>QÑ%RÔ%RÑSÔSÐSð �9Š9�Q‰<Œ<˜1ÒÐÝ”=   Q¨¡¤Ñ*Ð*ð Ðr   c                 ó†  — |j         }|j        r'|                     ¦   «         rt          |¦  «        dk    r| S |j        }|j        r|j        dz  }| }t          ||¦  «                             d¬¦  «        }||dz
  z  |z  }t          d|¦  «        D ]}|||dz
  |z
  z  |z  ||z  z  z  }Œ||                     ¦   «         z  S )Nr   éÿÿÿÿT)Ú
commutator)	ÚexpÚ
is_integerÚis_constantÚabsÚbaseÚis_negativer   ÚexpandÚrange)	Úselfr   r   Úsignr)   r-   ÚcommÚresultÚis	            r   Ú_expand_powzCommutator._expand_pow}   sÝ   € ØŒeˆØŒ~ð 	 S§_¢_Ñ%6Ô%6ð 	½#¸c¹(¼(Àaº-¸-àˆKØŒvˆØŒ?ð 	Ø”6˜2‘:ˆDØ�$ˆCÝ˜$ Ñ"Ô"×)Ò)°TÐ)Ñ:Ô:ˆà˜˜a™‘ 4Ñ'ˆÝ�q˜#‘”ð 	;ð 	;ˆAØ�d˜S 1™W q™[Ñ)¨DÑ0°4¸±7Ñ:Ñ:ˆFˆFØ�F—M’M‘O”OÑ#Ð#r   c                 ó:  — | j         d         }| j         d         }t          |t          ¦  «        rcg }|j         D ]P}t          ||¦  «        }t          |t          ¦  «        r|                     ¦   «         }|                     |¦  «         ŒQt          |Ž S t          |t          ¦  «        rcg }|j         D ]P}t          ||¦  «        }t          |t          ¦  «        r|                     ¦   «         }|                     |¦  «         ŒQt          |Ž S t          |t          ¦  «        rÇ|j         d         }t          |j         dd …         Ž }|}	t          ||	¦  «        }
t          ||	¦  «        }t          |
t          ¦  «        r|
                     ¦   «         }
t          |t          ¦  «        r|                     ¦   «         }t          ||
¦  «        }t          ||¦  «        }t          ||¦  «        S t          |t          ¦  «        rÇ|}|j         d         }t          |j         dd …         Ž }	t          ||¦  «        }
t          ||	¦  «        }t          |
t          ¦  «        r|
                     ¦   «         }
t          |t          ¦  «        r|                     ¦   «         }t          |
|	¦  «        }t          ||¦  «        }t          ||¦  «        S t          |t          ¦  «        r|                      ||d¦  «        S t          |t          ¦  «        r|                      ||d¦  «        S | S )Nr   r   r'   )	ÚargsÚ
isinstancer   r   Ú_eval_expand_commutatorÚappendr   r   r6   )r1   Úhintsr   r   ÚsargsÚtermr3   r   r    ÚcÚcomm1Úcomm2ÚfirstÚseconds                 r   r:   z"Commutator._eval_expand_commutator�   sí  € ØŒI�aŒLˆØŒI�aŒLˆå�a�ÑÔð 3	.àˆEØœð #ð #�Ý! $¨Ñ*Ô*�Ý˜d¥JÑ/Ô/ð :Ø×7Ò7Ñ9Ô9�DØ—’˜TÑ"Ô"Ð"Ð"Ý˜�;ÐÝ˜�3ÑÔð *	.àˆEØœð #ð #�Ý! ! TÑ*Ô*�Ý˜d¥JÑ/Ô/ð :Ø×7Ò7Ñ9Ô9�DØ—’˜TÑ"Ô"Ð"Ð"Ý˜�;ÐÝ˜�3ÑÔð !	.à”�q”	ˆAÝ�Q”V˜A˜B˜B”ZÐ ˆAØˆAÝ˜q !Ñ$Ô$ˆEÝ˜q !Ñ$Ô$ˆEÝ˜%¥Ñ,Ô,ð 8Ø×5Ò5Ñ7Ô7�Ý˜%¥Ñ,Ô,ð 8Ø×5Ò5Ñ7Ô7�Ý˜˜5‘M”MˆEÝ˜ ‘]”]ˆFÝ�u˜fÑ%Ô%Ð%Ý˜�3ÑÔð 	.àˆAØ”�q”	ˆAÝ�Q”V˜A˜B˜B”ZÐ ˆAÝ˜q !Ñ$Ô$ˆEÝ˜q !Ñ$Ô$ˆEÝ˜%¥Ñ,Ô,ð 8Ø×5Ò5Ñ7Ô7�Ý˜%¥Ñ,Ô,ð 8Ø×5Ò5Ñ7Ô7�Ý˜˜q‘M”MˆEÝ˜˜E‘]”]ˆFÝ�u˜fÑ%Ô%Ð%Ý˜�3ÑÔð 	.à×#Ò# A q¨!Ñ,Ô,Ð,Ý˜�3ÑÔð 	.à×#Ò# A q¨"Ñ-Ô-Ð-ð ˆr   c                 ó^  — | j         d         }| j         d         }t          |t          ¦  «        rit          |t          ¦  «        rT	  |j        |fi |¤Ž}n5# t          $ r( 	 d |j        |fi |¤Žz  }n# t          $ r d}Y nw xY wY nw xY w|� |j        di |¤ŽS  ||z  ||z  z
  j        di |¤ŽS )z Evaluate commutator r   r   r'   N© )r8   r9   r
   Ú_eval_commutatorÚNotImplementedErrorÚdoit)r1   r<   r   r   r3   s        r   rH   zCommutator.doitÉ   s	  € àŒI�aŒLˆØŒI�aŒLˆÝ�a�Ñ"Ô"ð 		*¥z°!µXÑ'>Ô'>ð 		*ð Ø)�qÔ)¨!Ð5Ð5¨uÐ5Ð5��øÝ&ð  ð  ð  ð ØÐ0˜aÔ0°Ð<Ð<°eÐ<Ð<Ñ<�D�DøÝ*ð  ð  ð  Ø�D�D�Dð øøøøøð øøøð
 ÐØ �t”yÐ)Ð) 5Ð)Ð)Ð)Ø��!‘�a˜‘c‘	ÔÐ(Ð( %Ð(Ð(Ð(s6   ÁA Á
BÁ A2Á1BÁ2BÁ>BÂ BÂBÂBc                 ó‚   — t          t          | j        d         ¦  «        t          | j        d         ¦  «        ¦  «        S )Nr   r   )r   r	   r8   )r1   s    r   Ú_eval_adjointzCommutator._eval_adjointÙ   s.   € Ý�& ¤¨1¤Ñ.Ô.µ°t´yÀ´|Ñ0DÔ0DÑEÔEÐEr   c                 ó¤   — | j         j        ›d|                     | j        d         ¦  «        ›d|                     | j        d         ¦  «        ›d�S )Nú(r   ú,r   ú))Ú	__class__Ú__name__Ú_printr8   ©r1   Úprinterr8   s      r   Ú
_sympyreprzCommutator._sympyreprÜ   sX   € àŒNÔ#Ð#Ð# W§^¢^Ø”	˜!”ñ&ô &ð &ð &Ø&Ÿ~š~¨d¬i¸¬lÑ;Ô;Ð;Ð;ð
ð 	
r   c                 óŒ   — d|                      | j        d         ¦  «        ›d|                      | j        d         ¦  «        ›d�S )Nú[r   rM   r   ú])rQ   r8   rR   s      r   Ú	_sympystrzCommutator._sympystrâ   sE   € € à�NŠN˜4œ9 Qœ<Ñ(Ô(Ð(Ð(¨'¯.ª.¸¼À1¼Ñ*FÔ*FÐ*FÐ*FðHð 	Hr   c                 ó,  —  |j         | j        d         g|¢R Ž }t          |                     t          d¦  «        ¦  «        Ž }t          |                      |j         | j        d         g|¢R Ž ¦  «        Ž }t          |                     dd¬¦  «        Ž }|S )Nr   rM   r   rV   rW   )ÚleftÚright)rQ   r8   r   r[   Úparens)r1   rS   r8   Úpforms       r   Ú_prettyzCommutator._prettyæ   sŒ   € Ø�”˜tœy¨œ|Ð3¨dÐ3Ð3Ð3ˆÝ˜EŸKšK­
°3©¬Ñ8Ô8Ð9ˆÝ˜EŸKšK¨¨¬°t´yÀ´|Ð(KÀdÐ(KÐ(KÐ(KÑLÔLÐMˆÝ˜EŸLšL¨c¸˜LÑ=Ô=Ð>ˆØˆr   c                 óN   ‡‡— dt          ˆˆfd„| j        D ¦   «         ¦  «        z  S )Nz\left[%s,%s\right]c                 ó,   •— g | ]} ‰j         |g‰¢R Ž ‘ŒS rE   )rQ   )Ú.0Úargr8   rS   s     €€r   ú
<listcomp>z%Commutator._latex.<locals>.<listcomp>î   s:   ø€ ð /=ð /=ð /=Ø+.ˆNˆGŒN˜3Ð& Ð&Ð&Ð&ð/=ð /=ð /=r   )Útupler8   rR   s    ``r   Ú_latexzCommutator._latexí   sJ   øø€ Ø%­ð /=ð /=ð /=ð /=ð /=Ø26´)ð/=ñ /=ô /=ñ )>ô )>ñ >ð 	>r   N)rP   Ú
__module__Ú__qualname__Ú__doc__r   r   Úclassmethodr   r6   r:   rH   rJ   rT   rX   r^   re   rE   r   r   r   r      sÑ   € € € € € ðFð FðN €Nðð ð ð ð+ð +ñ „[ð+ð($ð $ð $ð :ð :ð :ðx)ð )ð )ð Fð Fð Fð
ð 
ð 
ðHð Hð Hðð ð ð>ð >ð >ð >ð >r   N)rh   Úsympy.core.addr   Úsympy.core.exprr   Úsympy.core.mulr   Úsympy.core.powerr   Úsympy.core.singletonr   Ú sympy.printing.pretty.stringpictr   Úsympy.physics.quantum.daggerr	   Úsympy.physics.quantum.operatorr
   Ú__all__r   rE   r   r   ú<module>rs      så   ðØ (Ð (à Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ø Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ø "Ð "Ð "Ð "Ð "Ð "Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7à /Ð /Ð /Ð /Ð /Ð /Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3ð ð€ðX>ð X>ð X>ð X>ð X>�ñ X>ô X>ð X>ð X>ð X>r   