
    sg^                     x    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	gZ G d
 d	e      Zy)z+The anti-commutator: ``{A,B} = A*B + B*A``.    )Expr)Mul)Integer)S)
prettyForm)Operator)DaggerAntiCommutatorc                   N    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y)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
    Fc                 `    | j                  ||      }||S t        j                  | ||      }|S )N)evalr   __new__)clsABrobjs        W/var/www/html/venv/lib/python3.12/site-packages/sympy/physics/quantum/anticommutator.pyr   zAntiCommutator.__new__S   s2    HHQN=Hll31%
    c           	         |r|st         j                  S ||k(  rt        d      |dz  z  S |j                  s|j                  rt        d      |z  |z  S |j	                         \  }}|j	                         \  }}||z   }|r?t        t        |  | t        j                  |      t        j                  |                  S |j                  |      dk(  r	 | ||      S y )N      )r   Zeror   is_commutativeargs_cncr   
_from_argscompare)r   abcancacbncbc_parts           r   r   zAntiCommutator.evalZ   s    a66M61:ad?"q//1:a<>! **,C**,CbsF|S)<cnnS>Q%RSS 99Q<1q!9 r   c                 n   | j                   d   }| j                   d   }t        |t              r8t        |t              r(	  |j                  |fi |}| |j
                  di |S  ||z  ||z  z   j
                  di |S # t        $ r) 	  |j                  |fi |}n# t        $ r d}Y nw xY wY `w xY w)z Evaluate anticommutator r   r   N )args
isinstancer   _eval_anticommutatorNotImplementedErrordoit)selfhintsr   r   comms        r   r+   zAntiCommutator.doito   s    IIaLIIaLa"z!X'> -q--a959  tyy)5))!ac	(%(( '   1111!=u=D*  D  s6    B 	B4B B4 B.+B4-B..B43B4c                 r    t        t        | j                  d         t        | j                  d               S )Nr   r   )r
   r	   r'   )r,   s    r   _eval_adjointzAntiCommutator._eval_adjoint   s)    fTYYq\2F499Q<4HIIr   c                     | j                   j                  d|j                  | j                  d         d|j                  | j                  d         dS )N(r   ,r   ))	__class____name___printr'   r,   printerr'   s      r   
_sympyreprzAntiCommutator._sympyrepr   sC    NN##W^^		!&&~~diil;
 	
r   c                     d|j                  | j                  d         d|j                  | j                  d         dS )N{r   r3   r   })r7   r'   r8   s      r   	_sympystrzAntiCommutator._sympystr   s5    NN499Q<('..1*FH 	Hr   c                 "    |j                   | j                  d   g| }t        |j                  t        d             }t        |j                   |j                   | j                  d   g|        }t        |j	                  dd       }|S )Nr   r3   r   r<   r=   )leftright)r7   r'   r   rA   parens)r,   r9   r'   pforms       r   _prettyzAntiCommutator._pretty   s}    tyy|3d3EKK
389EKKtyy|(Kd(KLMELLcL=>r   c           
      z    dt        | j                  D cg c]  } |j                  |g|  c}      z  S c c}w )Nz\left\{%s,%s\right\})tupler'   r7   )r,   r9   r'   args       r   _latexzAntiCommutator._latex   sB    )E26))3=+.NGNN3&&3= -> > 	> 3=s   8
N)r6   
__module____qualname____doc__r   r   classmethodr   r+   r0   r:   r>   rD   rH   r&   r   r   r
   r
      sH    :v N  () J
H>r   N)rK   sympy.core.exprr   sympy.core.mulr   sympy.core.numbersr   sympy.core.singletonr    sympy.printing.pretty.stringpictr   sympy.physics.quantum.operatorr   sympy.physics.quantum.daggerr	   __all__r
   r&   r   r   <module>rU      s7    1    & " 7 3 / @>T @>r   