
    ga                         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 d
gZ G d d
e          ZdS )z"The commutator: [A,B] = A*B - B*A.    )Add)Expr)Mul)Pow)S)
prettyForm)Dagger)Operator
Commutatorc                   b    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 Zd ZdS )r   a?  The standard commutator, in an unevaluated state.

    Explanation
    ===========

    Evaluating a commutator is defined [1]_ as: ``[A, B] = A*B - B*A``. This
    class returns the commutator in an unevaluated form. To evaluate the
    commutator, use the ``.doit()`` method.

    Canonical ordering of a commutator is ``[A, B]`` for ``A < B``. The
    arguments of the commutator are put into canonical order using ``__cmp__``.
    If ``B < A``, then ``[B, A]`` is returned as ``-[A, B]``.

    Parameters
    ==========

    A : Expr
        The first argument of the commutator [A,B].
    B : Expr
        The second argument of the commutator [A,B].

    Examples
    ========

    >>> from sympy.physics.quantum import Commutator, Dagger, Operator
    >>> from sympy.abc import x, y
    >>> A = Operator('A')
    >>> B = Operator('B')
    >>> C = Operator('C')

    Create a commutator and use ``.doit()`` to evaluate it:

    >>> comm = Commutator(A, B)
    >>> comm
    [A,B]
    >>> comm.doit()
    A*B - B*A

    The commutator orders it arguments in canonical order:

    >>> comm = Commutator(B, A); comm
    -[A,B]

    Commutative constants are factored out:

    >>> Commutator(3*x*A, x*y*B)
    3*x**2*y*[A,B]

    Using ``.expand(commutator=True)``, the standard commutator expansion rules
    can be applied:

    >>> Commutator(A+B, C).expand(commutator=True)
    [A,C] + [B,C]
    >>> Commutator(A, B+C).expand(commutator=True)
    [A,B] + [A,C]
    >>> Commutator(A*B, C).expand(commutator=True)
    [A,C]*B + A*[B,C]
    >>> Commutator(A, B*C).expand(commutator=True)
    [A,B]*C + B*[A,C]

    Adjoint operations applied to the commutator are properly applied to the
    arguments:

    >>> Dagger(Commutator(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/commutator.pyr   zCommutator.__new__a   s5    HHQNN=Hl31%%
    c           	         |r|st           j        S ||k    rt           j        S |j        s|j        rt           j        S |                                \  }}|                                \  }}||z   }|rEt	          t	          |  | t	          j        |          t	          j        |                              S |                    |          dk    rt           j         | ||          z  S d S )N   )r   Zerois_commutativeargs_cncr   
_from_argscompareNegativeOne)r   abcancacbncbc_parts           r   r   zCommutator.evalh   s     	a 	6M666M 	q/ 	6M **,,C**,,Cb 	TsF|SS)<)<cnS>Q>Q%R%RSSS 99Q<<1=Q** r   c                    |j         }|j        r'|                                rt          |          dk    r| S |j        }|j        r|j        dz  }| }t          ||                              d          }||dz
  z  |z  }t          d|          D ]}|||dz
  |z
  z  |z  ||z  z  z  }||                                z  S )Nr   T)
commutator)	exp
is_integeris_constantabsbaseis_negativer   expandrange)	selfr   r   signr)   r-   commresultis	            r   _expand_powzCommutator._expand_pow}   s    e~ 	S__%6%6 	#c((a--Kv? 	62:D$C$""))T)::a4'q# 	; 	;AdS1Wq[)D047::FFFMMOO##r   c                 :   | j         d         }| j         d         }t          |t                    rcg }|j         D ]P}t          ||          }t          |t                    r|                                }|                    |           Qt          | S t          |t                    rcg }|j         D ]P}t          ||          }t          |t                    r|                                }|                    |           Qt          | S t          |t                    r|j         d         }t          |j         dd           }|}	t          ||	          }
t          ||	          }t          |
t                    r|
                                }
t          |t                    r|                                }t          ||
          }t          ||          }t          ||          S t          |t                    r|}|j         d         }t          |j         dd           }	t          ||          }
t          ||	          }t          |
t                    r|
                                }
t          |t                    r|                                }t          |
|	          }t          ||          }t          ||          S t          |t                    r|                     ||d          S t          |t                    r|                     ||d          S | S )Nr   r   r'   )	args
isinstancer   r   _eval_expand_commutatorappendr   r   r6   )r1   hintsr   r   sargstermr3   r   r    ccomm1comm2firstseconds                 r   r:   z"Commutator._eval_expand_commutator   s   IaLIaLa 3	.E # #!$**dJ// :7799DT"""";3 *	.E # #!!T**dJ// :7799DT"""";3 !	.q	AQVABBZ AAq!$$Eq!$$E%,, 85577%,, 855775MME]]Fuf%%%3 	.Aq	AQVABBZ Aq!$$Eq!$$E%,, 85577%,, 85577qMMEE]]Fuf%%%3 	.##Aq!,,,3 	.##Aq"--- r   c                 ^   | j         d         }| j         d         }t          |t                    rit          |t                    rT	  |j        |fi |}n5# t          $ r( 	 d |j        |fi |z  }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 commutator r   r   r'   N )r8   r9   r
   _eval_commutatorNotImplementedErrordoit)r1   r<   r   r   r3   s        r   rH   zCommutator.doit   s	   IaLIaLa"" 		*z!X'>'> 		* )q)!55u55&       0a0<<e<<<DD*      DDD  
  ty))5)))!ac	((%(((s6   A 
B A21B2B>B BBBc                     t          t          | j        d                   t          | j        d                             S )Nr   r   )r   r	   r8   )r1   s    r   _eval_adjointzCommutator._eval_adjoint   s.    &1..ty|0D0DEEEr   c                     | j         j        d|                    | j        d                   d|                    | j        d                   dS )N(r   ,r   ))	__class____name___printr8   r1   printerr8   s      r   
_sympyreprzCommutator._sympyrepr   sX    N###W^^	!& & & &&~~dil;;;;
 	
r   c                     d|                     | j        d                   d|                     | j        d                   dS )N[r   rM   r   ])rQ   r8   rR   s      r   	_sympystrzCommutator._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   rM   r   rV   rW   )leftright)rQ   r8   r   r[   parens)r1   rS   r8   pforms       r   _prettyzCommutator._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 rE   )rQ   ).0argr8   rS   s     r   
<listcomp>z%Commutator._latex.<locals>.<listcomp>   s:     /= /= /=+.NGN3&&&&/= /= /=r   )tupler8   rR   s    ``r   _latexzCommutator._latex   sJ    % /= /= /= /= /=26)/= /= /= )> )> > 	>r   N)rP   
__module____qualname____doc__r   r   classmethodr   r6   r:   rH   rJ   rT   rX   r^   re   rE   r   r   r   r      s        F FN N   + + [+($ $ $ : : :x) ) ) F F F
 
 
H H H  > > > > >r   N)rh   sympy.core.addr   sympy.core.exprr   sympy.core.mulr   sympy.core.powerr   sympy.core.singletonr    sympy.printing.pretty.stringpictr   sympy.physics.quantum.daggerr	   sympy.physics.quantum.operatorr
   __all__r   rE   r   r   <module>rs      s    ( (                                     " " " " " " 7 7 7 7 7 7 / / / / / / 3 3 3 3 3 3 X> X> X> X> X> X> X> X> X> X>r   