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mZ d
gZG dd
„ d
eƒZej ee¡dd„ ƒZdS )z+The anti-commutator: ``{A,B} = A*B + B*A``.é    )ÚExpr)ÚKindDispatcher)ÚMul)ÚInteger)ÚS)Ú
prettyForm)ÚDagger)Ú_OperatorKindÚOperatorKindÚAntiCommutatorc                   @   sp   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S )r   a  The standard anticommutator, in an unevaluated state.

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

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

    Canonical ordering of an anticommutator is ``{A, B}`` for ``A < B``. The
    arguments of the anticommutator are put into canonical order using
    ``__cmp__``. If ``B < A``, then ``{A, B}`` is returned as ``{B, A}``.

    Parameters
    ==========

    A : Expr
        The first argument of the anticommutator {A,B}.
    B : Expr
        The second argument of the anticommutator {A,B}.

    Examples
    ========

    >>> from sympy import symbols
    >>> from sympy.physics.quantum import AntiCommutator
    >>> from sympy.physics.quantum import Operator, Dagger
    >>> x, y = symbols('x,y')
    >>> A = Operator('A')
    >>> B = Operator('B')

    Create an anticommutator and use ``doit()`` to multiply them out.

    >>> ac = AntiCommutator(A,B); ac
    {A,B}
    >>> ac.doit()
    A*B + B*A

    The commutator orders it arguments in canonical order:

    >>> ac = AntiCommutator(B,A); ac
    {A,B}

    Commutative constants are factored out:

    >>> AntiCommutator(3*x*A,x*y*B)
    3*x**2*y*{A,B}

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

    >>> Dagger(AntiCommutator(A,B))
    {Dagger(A),Dagger(B)}

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Commutator
    FÚAntiCommutator_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/anticommutator.pyÚ	<genexpr>X   s   € z&AntiCommutator.kind.<locals>.<genexpr>)ÚargsÚ_kind_dispatcher)ÚselfÚ	arg_kindsr   r   r   r   V   s   
zAntiCommutator.kindc                 C   s*   |   ||¡}|d ur|S t | ||¡}|S r   )Úevalr   Ú__new__)ÚclsÚAÚBÚrÚobjr   r   r   r   [   s
   zAntiCommutator.__new__c                 C   s¢   |r|st jS ||krtdƒ|d  S |js|jr!tdƒ| | S | ¡ \}}| ¡ \}}|| }|rCtt|Ž | t |¡t |¡ƒƒS | |¡dkrO| ||ƒS d S )Né   é   )r   ÚZeror   Úis_commutativeÚargs_cncr   Ú
_from_argsÚcompare)r   r   ÚbÚcaÚncaÚcbÚncbÚc_partr   r   r   r   b   s    
ÿzAntiCommutator.evalc                 K   sÀ   ddl m} | jd }| jd }t||ƒrRt||ƒrRz|j|fi |¤Ž}W n  tyE   z|j|fi |¤Ž}W n tyB   d}Y nw Y nw |durR|jdi |¤ŽS || ||  jdi |¤ŽS )z Evaluate anticommutator r   )ÚOperatorr!   Nr   )Úsympy.physics.quantum.operatorr-   r   Ú
isinstanceÚ_eval_anticommutatorÚNotImplementedErrorÚdoit)r   Úhintsr-   r   r   Úcommr   r   r   r2   w   s"   

ÿ€ýzAntiCommutator.doitc                 C   s   t t| jd ƒt| jd ƒƒS )Nr   r!   )r   r   r   )r   r   r   r   Ú_eval_adjointŠ   s   zAntiCommutator._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AntiCommutator._sympyreprc                 G   s$   d|  | jd ¡|  | jd ¡f S )Nz{%s,%s}r   r!   )r8   r   r9   r   r   r   Ú	_sympystr“   s   ÿzAntiCommutator._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)r8   r   r   rA   Úparens)r   r:   r   Úpformr   r   r   Ú_pretty—   s
   "zAntiCommutator._prettyc                    s   dt ‡ ‡fdd„| jD ƒƒ S )Nz\left\{%s,%s\right\}c                    s   g | ]}ˆj |gˆ ¢R Ž ‘qS r   )r8   )r   Úarg©r   r:   r   r   Ú
<listcomp>Ÿ   s    ÿz)AntiCommutator._latex.<locals>.<listcomp>)Útupler   r9   r   rF   r   Ú_latexž   s   
ÿzAntiCommutator._latexN)r7   Ú
__module__Ú__qualname__Ú__doc__r#   r   r   Úpropertyr   r   Úclassmethodr   r2   r5   r;   r<   rD   rI   r   r   r   r   r      s    ;

c                 C   s   t S )z8Find the kind of an anticommutator of two OperatorKinds.)r
   )Úe1Úe2r   r   r   Úfind_op_kind£   s   rQ   N)rL   Úsympy.core.exprr   Úsympy.core.kindr   Úsympy.core.mulr   Úsympy.core.numbersr   Úsympy.core.singletonr   Ú sympy.printing.pretty.stringpictr   Úsympy.physics.quantum.daggerr   Úsympy.physics.quantum.kindr	   r
   Ú__all__r   r   ÚregisterrQ   r   r   r   r   Ú<module>   s    ÿ	 