U
    ZÊ¹ja  ã                   @   s~   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
„ d
eƒZdS )z"The commutator: [A,B] = A*B - B*A.é    )ÚAdd)ÚExpr)ÚMul)ÚPow)ÚS)Ú
prettyForm)ÚDagger)ÚOperatorÚ
Commutatorc                   @   sh   e Zd ZdZdZdd„ Zedd„ ƒZdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zd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                 C   s*   |   ||¡}|d k	r|S t | ||¡}|S )N)Úevalr   Ú__new__)ÚclsÚAÚBÚrÚobj© r   ú_/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/physics/quantum/commutator.pyr   a   s
    zCommutator.__new__c                 C   s”   |r|st jS ||krt jS |js(|jr.t jS | ¡ \}}| ¡ \}}|| }|rrtt|Ž | t |¡t |¡ƒƒS | |¡dkr�t j| ||ƒ S d S )Né   )r   ZZeroÚis_commutativeZargs_cncr   Z
_from_argsÚcompareZNegativeOne)r   ÚaÚbÚcaZncaÚcbÚncbZc_partr   r   r   r   h   s     zCommutator.evalc           	      C   sž   |j }|jr | ¡ r t|ƒdkr$| S |j}|jr@|jd }| }t||ƒjdd�}||d  | }td|ƒD ]$}|||d |  | ||  7 }ql|| ¡  S )Nr   éÿÿÿÿT)Z
commutator)	ÚexpÚ
is_integerZis_constantÚabsÚbaseZis_negativer
   ÚexpandÚrange)	Úselfr   r   Úsignr   r    ÚcommÚresultÚir   r   r   Ú_expand_pow}   s    
"zCommutator._expand_powc                 K   sæ  | j d }| j d }t|tƒr\g }|j D ]*}t||ƒ}t|tƒrH| ¡ }| |¡ q(t|Ž S t|tƒr¤g }|j D ]*}t||ƒ}t|tƒr�| ¡ }| |¡ qpt|Ž 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   r(   )r#   Úhintsr   r   ZsargsÚtermr%   r   r   ÚcZcomm1Zcomm2ÚfirstÚsecondr   r   r   r+   �   sb    






















z"Commutator._eval_expand_commutatorc                 K   s¬   | j d }| j d }t|tƒr”t|tƒr”z|j|f|Ž}W nD tk
r~   zd|j|f|Ž }W n tk
rx   d}Y nX Y nX |dk	r”|jf |ŽS || ||  jf |ŽS )z Evaluate commutator r   r   r   N)r)   r*   r	   Z_eval_commutatorÚNotImplementedErrorÚdoit)r#   r-   r   r   r%   r   r   r   r3   É   s    

zCommutator.doitc                 C   s   t t| jd ƒt| jd ƒƒS )Nr   r   )r
   r   r)   )r#   r   r   r   Ú_eval_adjointÙ   s    zCommutator._eval_adjointc                 G   s*   d| j j| | jd ¡| | jd ¡f S )Nz	%s(%s,%s)r   r   )Ú	__class__Ú__name__Ú_printr)   ©r#   Úprinterr)   r   r   r   Ú
_sympyreprÜ   s     ÿþzCommutator._sympyreprc                 G   s$   d|  | jd ¡|  | jd ¡f S )Nz[%s,%s]r   r   )r7   r)   r8   r   r   r   Ú	_sympystrâ   s     ÿzCommutator._sympystrc                 G   s^   |j | jd f|žŽ }t| tdƒ¡Ž }t| |j | jd f|žŽ ¡Ž }t|jddd�Ž }|S )Nr   ú,r   ú[ú])ÚleftÚright)r7   r)   r   r@   Zparens)r#   r9   r)   Zpformr   r   r   Ú_prettyæ   s
     zCommutator._prettyc                    s   dt ‡ ‡fdd„| jD ƒƒ S )Nz\left[%s,%s\right]c                    s   g | ]}ˆj |fˆ žŽ ‘qS r   )r7   )Ú.0Úarg©r)   r9   r   r   Ú
<listcomp>î   s    z%Commutator._latex.<locals>.<listcomp>)Útupler)   r8   r   rD   r   Ú_latexí   s    ÿzCommutator._latexN)r6   Ú
__module__Ú__qualname__Ú__doc__r   r   Úclassmethodr   r(   r+   r3   r4   r:   r;   rA   rG   r   r   r   r   r
      s   G
<N)rJ   Zsympy.core.addr   Zsympy.core.exprr   Zsympy.core.mulr   Zsympy.core.powerr   Zsympy.core.singletonr   Z sympy.printing.pretty.stringpictr   Zsympy.physics.quantum.daggerr   Zsympy.physics.quantum.operatorr	   Ú__all__r
   r   r   r   r   Ú<module>   s   ÿ	