o
    jga                     @   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 ur|S t| ||}|S )N)evalr   __new__)clsABrobj r   X/var/www/html/zoom/venv/lib/python3.10/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 | \}}| \}}|| }|r9tt| | t|t|S ||dkrHt j| || S d S )N   )r   Zerois_commutativeargs_cncr   
_from_argscompareNegativeOne)r   abcancacbncb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 }q6||  S )Nr   T)
commutator)	exp
is_integeris_constantabsbase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r$| }|| qt| S t|trRg }|j D ]}t||}t|trH| }|| q8t| S t|tr|j d }t|j dd   }|}	t||	}
t||	}t|
trz|
 }
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   r1   )r,   hintsr   r   sargstermr.   r   r   ccomm1comm2firstsecondr   r   r   r4      sb   





























z"Commutator._eval_expand_commutatorc                 K   s   | j d }| j d }t|trNt|trNz|j|fi |}W n" tyA   zd|j|fi | }W n ty>   d}Y nw Y nw |durN|jdi |S || ||  jdi |S )z Evaluate commutator r   r   r"   Nr   )r2   r3   r	   _eval_commutatorNotImplementedErrordoit)r,   r6   r   r   r.   r   r   r   r@      s    

zCommutator.doitc                 C   s   t t| jd t| jd S )Nr   r   )r
   r   r2   )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___printr2   r,   printerr2   r   r   r   
_sympyrepr   s   
zCommutator._sympyreprc                 G   s$   d| | jd | | jd f S )Nz[%s,%s]r   r   )rD   r2   rE   r   r   r   	_sympystr   s   zCommutator._sympystrc                 G   sb   |j | jd g|R  }t|td }t||j | jd g|R   }t|jddd }|S )Nr   ,r   [])leftright)rD   r2   r   rM   parens)r,   rF   r2   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 |g R  qS r   )rD   ).0argr2   rF   r   r   
<listcomp>   s    z%Commutator._latex.<locals>.<listcomp>)tupler2   rE   r   rS   r   _latex   s   
zCommutator._latexN)rC   
__module____qualname____doc__r   r   classmethodr   r1   r4   r@   rA   rG   rH   rP   rV   r   r   r   r   r
      s    G
<N)rY   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
   r   r   r   r   <module>   s    	