
    sga                         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y)z"The commutator: [A,B] = A*B - B*A.    )Add)Expr)Mul)Pow)S)
prettyForm)Dagger)Operator
Commutatorc                   Z    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y)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                 `    | j                  ||      }||S t        j                  | ||      }|S )N)evalr   __new__)clsABrobjs        S/var/www/html/venv/lib/python3.12/site-packages/sympy/physics/quantum/commutator.pyr   zCommutator.__new__a   s2    HHQN=Hll31%
    c           	         |r|st         j                  S ||k(  rt         j                  S |j                  s|j                  rt         j                  S |j                         \  }}|j                         \  }}||z   }|r?t	        t	        |  | t	        j
                  |      t	        j
                  |                  S |j                  |      dk(  rt         j                   | ||      z  S y )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66M666Mq//66M **,C**,CbsF|S)<cnnS>Q%RSS 99Q<1==Q** r   c                    |j                   }|j                  r|j                         rt        |      dk  r| S |j                  }|j
                  r|j                  dz  }| }t        ||      j                  d      }||dz
  z  |z  }t        d|      D ]  }|||dz
  |z
  z  |z  ||z  z  z  } ||j                         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    ee~~S__%6#c(a-Kvv??662:D$C$"))T):a4'q# 	;AdS1Wq[)D047::F	;FMMO##r   c                 6   | j                   d   }| j                   d   }t        |t              rXg }|j                   D ]?  }t        ||      }t        |t              r|j	                         }|j                  |       A t        | S t        |t              rXg }|j                   D ]?  }t        ||      }t        |t              r|j	                         }|j                  |       A t        | S t        |t              r|j                   d   }t        |j                   dd   }|}	t        ||	      }
t        ||	      }t        |
t              r|
j	                         }
t        |t              r|j	                         }t        ||
      }t        ||      }t        ||      S t        |t              r|}|j                   d   }t        |j                   dd   }	t        ||      }
t        ||	      }t        |
t              r|
j	                         }
t        |t              r|j	                         }t        |
|	      }t        ||      }t        ||      S t        |t              r| j                  ||d      S t        |t              r| j                  ||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   sS   IIaLIIaLaE #!$*dJ/779DT"	#
 ;3E #!!T*dJ/779DT"	#
 ;3q	AQVVABZ AAq!$Eq!$E%,557%,5575ME]Fuf%%3Aq	AQVVABZ Aq!$Eq!$E%,557%,557qMEE]Fuf%%3##Aq!,,3##Aq"-- r   c                 t   | j                   d   }| j                   d   }t        |t              r8t        |t              r(	  |j                  |fi |}| |j
                  di |S  ||z  ||z  z
  j
                  di |S # t        $ r, 	 d |j                  |fi |z  }n# t        $ r d}Y nw xY wY cw xY w)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    IIaLIIaLa"z!X'> )q))!5u5  tyy)5))!ac	(%(( '   0a00<e<<D*  D  s6    B 	B7B#"B7#B1.B70B11B76B7c                 r    t        t        | j                  d         t        | j                  d               S )Nr   r   )r   r	   r8   )r1   s    r   _eval_adjointzCommutator._eval_adjoint   s)    &1.tyy|0DEEr   c                     | j                   j                  d|j                  | j                  d         d|j                  | j                  d         dS )N(r   ,r   ))	__class____name___printr8   r1   printerr8   s      r   
_sympyreprzCommutator._sympyrepr   sC    NN##W^^		!&&~~diil;
 	
r   c                     d|j                  | j                  d         d|j                  | j                  d         dS )N[r   rM   r   ])rQ   r8   rR   s      r   	_sympystrzCommutator._sympystr   s5    NN499Q<('..1*FH 	Hr   c                 "    |j                   | j                  d   g| }t        |j                  t        d             }t        |j                   |j                   | j                  d   g|        }t        |j	                  dd       }|S )Nr   rM   r   rV   rW   )leftright)rQ   r8   r   r[   parens)r1   rS   r8   pforms       r   _prettyzCommutator._pretty   s}    tyy|3d3EKK
389EKKtyy|(Kd(KLMELLcL=>r   c           
      z    dt        | j                  D cg c]  } |j                  |g|  c}      z  S c c}w )Nz\left[%s,%s\right])tupler8   rQ   )r1   rS   r8   args       r   _latexzCommutator._latex   sB    %26))/=+.NGNN3&&/= )> > 	> /=s   8
N)rP   
__module____qualname____doc__r   r   classmethodr   r6   r:   rH   rJ   rT   rX   r^   rb   rE   r   r   r   r      sT    FN N + +($ :x) F
H>r   N)re   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>rp      s:    (       " 7 / 3 X> X>r   