
    g^                         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dS )z+The anti-commutator: ``{A,B} = A*B + B*A``.    )Expr)Mul)Integer)S)
prettyForm)Operator)DaggerAntiCommutatorc                   V    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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
    Fc                 f    |                      ||          }||S t          j        | ||          }|S )N)evalr   __new__)clsABrobjs        `/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/physics/quantum/anticommutator.pyr   zAntiCommutator.__new__S   s5    HHQNN=Hl31%%
    c           	         |r|st           j        S ||k    rt          d          |dz  z  S |j        s|j        rt          d          |z  |z  S |                                \  }}|                                \  }}||z   }|rEt          t          |  | t          j        |          t          j        |                              S |                    |          dk    r | ||          S d S )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 	6M661::ad?" 	"q/ 	"1::a<>! **,,C**,,Cb 	TsF|SS)<)<cnS>Q>Q%R%RSSS 99Q<<13q!99 r   c                 X   | j         d         }| j         d         }t          |t                    rft          |t                    rQ	  |j        |fi |}n2# t          $ r% 	  |j        |fi |}n# t          $ r d}Y nw xY wY nw xY w| |j        di |S  ||z  ||z  z   j        di |S )z Evaluate anticommutator r   r   N )args
isinstancer   _eval_anticommutatorNotImplementedErrordoit)selfhintsr   r   comms        r   r+   zAntiCommutator.doito   s   IaLIaLa"" 		*z!X'>'> 		* -q-a99599&       111!==u==DD*      DDD  
  ty))5)))!ac	((%(((s6   A 
B A/.B/A>;B=A>>BBc                     t          t          | j        d                   t          | j        d                             S )Nr   r   )r
   r	   r'   )r,   s    r   _eval_adjointzAntiCommutator._eval_adjoint   s.    fTYq\22F49Q<4H4HIIIr   c                     | j         j        d|                    | j        d                   d|                    | j        d                   dS )N(r   ,r   ))	__class____name___printr'   r,   printerr'   s      r   
_sympyreprzAntiCommutator._sympyrepr   sX    N###W^^	!& & & &&~~dil;;;;
 	
r   c                     d|                     | j        d                   d|                     | j        d                   dS )N{r   r3   r   })r7   r'   r8   s      r   	_sympystrzAntiCommutator._sympystr   sE     NN49Q<(((('..1*F*F*F*FH 	Hr   c                 ,    |j         | j        d         g|R  }t          |                    t          d                     }t          |                     |j         | j        d         g|R             }t          |                    dd           }|S )Nr   r3   r   r<   r=   )leftright)r7   r'   r   rA   parens)r,   r9   r'   pforms       r   _prettyzAntiCommutator._pretty   s    ty|3d333EKK
3889EKKty|(Kd(K(K(KLLMELLcL==>r   c                 N    dt          fd| j        D                       z  S )Nz\left\{%s,%s\right\}c                 ,    g | ]} j         |gR  S r&   )r7   ).0argr'   r9   s     r   
<listcomp>z)AntiCommutator._latex.<locals>.<listcomp>   s:     3= 3= 3=+.NGN3&&&&3= 3= 3=r   )tupler'   r8   s    ``r   _latexzAntiCommutator._latex   sJ    )E 3= 3= 3= 3= 3=26)3= 3= 3= -> -> > 	>r   N)r6   
__module____qualname____doc__r   r   classmethodr   r+   r0   r:   r>   rD   rK   r&   r   r   r
   r
      s        : :v N     [() ) ) J J J
 
 
H H H  > > > > >r   N)rN   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>rX      s    1 1                   & & & & & & " " " " " " 7 7 7 7 7 7 3 3 3 3 3 3 / / / / / / @> @> @> @> @>T @> @> @> @> @>r   