o
    3Ih                     @   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   d/home/air/sanwanet/gpt-api/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    	 