o
    §ƒðjË  ã                   @   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d
lmZmZ dgZG dd„ deƒZej ee¡dd„ ƒZdS )z"The commutator: [A,B] = A*B - B*A.é    )ÚAdd)ÚExpr)ÚKindDispatcher)ÚMul)ÚPow)ÚS)Ú
prettyForm)ÚDagger)Ú_OperatorKindÚOperatorKindÚ
Commutatorc                   @   s€   e Zd ZdZdZeddd�Zed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
    FÚCommutator_kind_dispatcherT)Úcommutativec                 C   s   dd„ | j D ƒ}| j|Ž S )Nc                 s   s   � | ]}|j V  qd S ©N)Úkind)Ú.0Úa© r   ú¢/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/physics/quantum/commutator.pyÚ	<genexpr>f   s   € z"Commutator.kind.<locals>.<genexpr>)ÚargsÚ_kind_dispatcher)ÚselfÚ	arg_kindsr   r   r   r   d   s   
zCommutator.kindc                 C   s*   |   ||¡}|d ur|S t | ||¡}|S r   )Úevalr   Ú__new__)ÚclsÚAÚBÚrÚobjr   r   r   r   i   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   r   ÚbÚcaÚncaÚcbÚncbÚc_partr   r   r   r   p   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)	r   r   r   Úsignr0   r4   Ú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.   )	r   Ú
isinstancer   r   Ú_eval_expand_commutatorÚappendr   r   r<   )r   Úhintsr   r   ÚsargsÚtermr9   r   r(   ÚcÚcomm1Úcomm2ÚfirstÚsecondr   r   r   r>   •   sb   





























z"Commutator._eval_expand_commutatorc                 K   sÄ   ddl m} | jd }| jd }t||ƒrTt||ƒrTz|j|fi |¤Ž}W n" tyG   zd|j|fi |¤Ž }W n tyD   d}Y nw Y nw |durT|jdi |¤ŽS || ||  jdi |¤ŽS )z Evaluate commutator r   )ÚOperatorr!   r.   Nr   )Úsympy.physics.quantum.operatorrH   r   r=   Ú_eval_commutatorÚNotImplementedErrorÚdoit)r   r@   rH   r   r   r9   r   r   r   rL   Ñ   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!   )rP   r   rQ   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)rP   r   r   rY   Úparens)r   rR   r   Ú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   )rP   )r   Úarg©r   rR   r   r   Ú
<listcomp>ù   s    ÿz%Commutator._latex.<locals>.<listcomp>)Útupler   rQ   r   r^   r   Ú_latexø   s   
ÿzCommutator._latexN)rO   Ú
__module__Ú__qualname__Ú__doc__r#   r   r   Úpropertyr   r   Úclassmethodr   r<   r>   rL   rM   rS   rT   r\   ra   r   r   r   r   r      s"    G

<c                 C   s   t S )z8Find the kind of an anticommutator of two OperatorKinds.)r   )Úe1Úe2r   r   r   Úfind_op_kindý   s   ri   N)rd   Úsympy.core.addr   Úsympy.core.exprr   Úsympy.core.kindr   Úsympy.core.mulr   Úsympy.core.powerr   Úsympy.core.singletonr   Ú sympy.printing.pretty.stringpictr   Úsympy.physics.quantum.daggerr	   Úsympy.physics.quantum.kindr
   r   Ú__all__r   r   Úregisterri   r   r   r   r   Ú<module>   s     ÿ	 f