
    Xh                         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
e          Zej                            ee          d             ZdS )z+The anti-commutator: ``{A,B} = A*B + B*A``.    )Expr)KindDispatcher)Mul)Integer)S)
prettyForm)Dagger)_OperatorKindOperatorKindAntiCommutatorc                       e Zd ZdZdZ edd          Ze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
    FAntiCommutator_kind_dispatcherT)commutativec                 8    d | j         D             } | j        | S )Nc              3   $   K   | ]}|j         V  d S N)kind).0as     v/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/sympy/physics/quantum/anticommutator.py	<genexpr>z&AntiCommutator.kind.<locals>.<genexpr>X   s$      //QV//////    )args_kind_dispatcher)self	arg_kindss     r   r   zAntiCommutator.kindV   s'    //TY///	$t$i00r   c                 f    |                      ||          }||S t          j        | ||          }|S r   )evalr   __new__)clsABrobjs        r   r   zAntiCommutator.__new__[   s5    HHQNN=Hl31%%
r   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    r   bcancacbncbc_parts           r   r   zAntiCommutator.evalb   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                 P   ddl m} | j        d         }| j        d         }t          ||          rat          ||          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   )Operatorr'   N )sympy.physics.quantum.operatorr4   r   
isinstance_eval_anticommutatorNotImplementedErrordoit)r   hintsr4   r!   r"   comms         r   r:   zAntiCommutator.doitw   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:7B 9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   rA   r'   })rE   r   rF   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   rA   r'   rJ   rK   )leftright)rE   r   r   rO   parens)r   rG   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 r5   )rE   )r   argr   rG   s     r   
<listcomp>z)AntiCommutator._latex.<locals>.<listcomp>   s:     3= 3= 3=+.NGN3&&&&3= 3= 3=r   )tupler   rF   s    ``r   _latexzAntiCommutator._latex   sJ    )E 3= 3= 3= 3= 3=26)3= 3= 3= -> -> > 	>r   N)rD   
__module____qualname____doc__r)   r   r   propertyr   r   classmethodr   r:   r>   rH   rL   rR   rX   r5   r   r   r   r      s        : :v N%~&FTXYYY1 1 X1     [() ) )&J J J
 
 
H H H  > > > > >r   c                     t           S )z8Find the kind of an anticommutator of two OperatorKinds.)r   )e1e2s     r   find_op_kindra      s
     r   N)r[   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   registerra   r5   r   r   <module>rl      s   1 1             * * * * * *       & & & & & & " " " " " " 7 7 7 7 7 7 / / / / / / B B B B B B B B J> J> J> J> J>T J> J> J>Z  ))-GG  HG  r   