U
    ZÊ¹j^  ã                   @   sr   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	„ d	eƒZdS )z+The anti-commutator: ``{A,B} = A*B + B*A``.é    )ÚExpr)ÚMul)ÚInteger)ÚS)Ú
prettyForm)ÚOperator)ÚDaggerÚAntiCommutatorc                   @   sX   e Zd ZdZ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
    Fc                 C   s*   |   ||¡}|d k	r|S t | ||¡}|S )N)Úevalr   Ú__new__)ÚclsÚAÚBÚrÚobj© r   úc/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/physics/quantum/anticommutator.pyr   S   s
    zAntiCommutator.__new__c                 C   s¢   |r|st jS ||kr&tdƒ|d  S |js2|jrBtdƒ| | S | ¡ \}}| ¡ \}}|| }|r†tt|Ž | t |¡t |¡ƒƒS | |¡dkrž| ||ƒS d S )Né   é   )r   ZZeror   Úis_commutativeZargs_cncr   Z
_from_argsÚcompare)r   ÚaÚbÚcaZncaÚcbÚncbZc_partr   r   r   r
   Z   s     zAntiCommutator.evalc                 K   s¨   | j d }| j d }t|tƒr�t|tƒr�z|j|f|Ž}W n@ tk
rz   z|j|f|Ž}W n tk
rt   d}Y nX Y nX |dk	r�|jf |ŽS || ||  jf |ŽS )z Evaluate anticommutator r   r   N)ÚargsÚ
isinstancer   Z_eval_anticommutatorÚNotImplementedErrorÚdoit)ÚselfÚhintsr   r   Zcommr   r   r   r   o   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   )r%   r   r&   r   r   r   Ú	_sympystrˆ   s     ÿzAntiCommutator._sympystrc                 G   s^   |j | jd f|žŽ }t| tdƒ¡Ž }t| |j | jd f|žŽ ¡Ž }t|jddd�Ž }|S )Nr   ú,r   Ú{Ú})ÚleftÚright)r%   r   r   r.   Zparens)r    r'   r   Z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 |fˆ žŽ ‘qS r   )r%   )Ú.0Úarg©r   r'   r   r   Ú
<listcomp>”   s    z)AntiCommutator._latex.<locals>.<listcomp>)Útupler   r&   r   r2   r   Ú_latex“   s    ÿzAntiCommutator._latexN)r$   Ú
__module__Ú__qualname__Ú__doc__r   r   Úclassmethodr
   r   r"   r(   r)   r/   r5   r   r   r   r   r	      s   ;
N)r8   Zsympy.core.exprr   Zsympy.core.mulr   Zsympy.core.numbersr   Zsympy.core.singletonr   Z sympy.printing.pretty.stringpictr   Zsympy.physics.quantum.operatorr   Zsympy.physics.quantum.daggerr   Ú__all__r	   r   r   r   r   Ú<module>   s   ÿ	