
    Цia                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJr  S
/r " S S
\5      rg)z"The commutator: [A,B] = A*B - B*A.    )Add)Expr)Mul)Pow)S)
prettyForm)Dagger)Operator
Commutatorc                   b    \ rS rSrSrSrS r\S 5       rS r	S r
S rS	 rS
 rS rS rS rSrg)r      as  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                 `    U R                  X5      nUb  U$ [        R                  " XU5      nU$ )N)evalr   __new__)clsABrobjs        _/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/sympy/physics/quantum/commutator.pyr   Commutator.__new__a   s.    HHQN=Hll31%
    c           	      
   U(       a  U(       d  [         R                  $ X:X  a  [         R                  $ UR                  (       d  UR                  (       a  [         R                  $ UR                  5       u  p4UR                  5       u  pVX5-   nU(       aA  [	        [	        U6 U " [        R
                  " U5      [        R
                  " U5      5      5      $ UR                  U5      S:X  a  [         R                  U " X!5      -  $ g )N   )r   Zerois_commutativeargs_cncr   
_from_argscompareNegativeOne)r   abcancacbncbc_parts           r   r   Commutator.evalh   s    a66M666Mq//66M **,**,sF|S)<cnnS>Q%RSS 99Q<1==Q** r   c                    UR                   nUR                  (       a$  UR                  5       (       a  [        U5      S::  a  U $ UR                  nUR
                  (       a  UR                  S-  nU* n[        XR5      R                  SS9nXTS-
  -  U-  n[        SU5       H  nXuUS-
  U-
  -  U-  XX-  -  -  nM     X7R                  5       -  $ )Nr   T)
commutator)	exp
is_integeris_constantabsbaseis_negativer   expandrange)	selfr   r   signr,   r0   commresultis	            r   _expand_powCommutator._expand_pow}   s    ee~~S__%6%6#c(a-Kvv??662:D$C$"))T):a4'q#AS1Wq[)D047::F MMO##r   c                    U R                   S   nU R                   S   n[        U[        5      (       a^  / nUR                    HD  n[        XS5      n[        U[        5      (       a  UR	                  5       nUR                  U5        MF     [        U6 $ [        U[        5      (       a^  / nUR                    HD  n[        X%5      n[        U[        5      (       a  UR	                  5       nUR                  U5        MF     [        U6 $ [        U[        5      (       a  UR                   S   n[        UR                   SS  6 nUn	[        X5      n
[        Xy5      n[        U
[        5      (       a  U
R	                  5       n
[        U[        5      (       a  UR	                  5       n[        Xz5      n[        X5      n[        X5      $ [        U[        5      (       a  UnUR                   S   n[        UR                   SS  6 n	[        Xx5      n
[        Xy5      n[        U
[        5      (       a  U
R	                  5       n
[        U[        5      (       a  UR	                  5       n[        X5      n[        X5      n[        X5      $ [        U[        5      (       a  U R                  X#S5      $ [        U[        5      (       a  U R                  X2S5      $ U $ )Nr   r   r*   )	args
isinstancer   r   _eval_expand_commutatorappendr   r   r9   )r4   hintsr   r   sargstermr6   r!   r"   ccomm1comm2firstseconds                 r   r>   "Commutator._eval_expand_commutator   sQ   IIaLIIaLaE!$*dJ//779DT"	 
 ;3E!!*dJ//779DT"	 
 ;3q	AQVVABZ AAq$Eq$E%,,557%,,557ME]Fu%%3Aq	AQVVABZ Aq$Eq$E%,,557%,,557ME]Fu%%3##A!,,3##A"-- r   c                    U R                   S   nU R                   S   n[        U[        5      (       a>  [        U[        5      (       a)   UR                  " U40 UD6nUb  UR
                  " S0 UD6$ X#-  X2-  -
  R
                  " S0 UD6$ ! [         a-     SUR                  " U40 UD6-  n NQ! [         a    Sn  N`f = ff = f)zEvaluate commutator r   r   r*   N )r<   r=   r
   _eval_commutatorNotImplementedErrordoit)r4   r@   r   r   r6   s        r   rM   Commutator.doit   s    IIaLIIaLa""z!X'>'> ))!5u5 yy)5))ac	(%(( '   a00<e<<D*  D  s*   
B 
CB..B>:C=B>>Cc                 r    [        [        U R                  S   5      [        U R                  S   5      5      $ )Nr   r   )r   r	   r<   )r4   s    r   _eval_adjointCommutator._eval_adjoint   s)    &1.tyy|0DEEr   c                     U R                   R                  < SUR                  U R                  S   5      < SUR                  U R                  S   5      < S3$ )N(r   ,r   ))	__class____name___printr<   r4   printerr<   s      r   
_sympyreprCommutator._sympyrepr   sC    NN##W^^		!&&~~diil;
 	
r   c                     SUR                  U R                  S   5      < SUR                  U R                  S   5      < S3$ )N[r   rT   r   ])rX   r<   rY   s      r   	_sympystrCommutator._sympystr   s5    NN499Q<('..1*FH 	Hr   c                    UR                   " U R                  S   /UQ76 n[        UR                  [        S5      5      6 n[        UR                  UR                   " U R                  S   /UQ76 5      6 n[        UR	                  SSS96 nU$ )Nr   rT   r   r^   r_   )leftright)rX   r<   r   rd   parens)r4   rZ   r<   pforms       r   _prettyCommutator._pretty   sy    tyy|3d3EKK
389EKKtyy|(Kd(KLMELLcL=>r   c           
      ~    S[        U R                   Vs/ s H  o1R                  " U/UQ76 PM     sn5      -  $ s  snf )Nz\left[%s,%s\right])tupler<   rX   )r4   rZ   r<   args       r   _latexCommutator._latex   sB    %26))/=2;3NN3&&)/= )> > 	> /=s   :
rJ   N)rW   
__module____qualname____firstlineno____doc__r   r   classmethodr   r9   r>   rM   rP   r[   r`   rg   rl   __static_attributes__rJ   r   r   r   r      sT    FN N + +($ :x) F
H>r   N)rq   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   rJ   r   r   <module>r}      s:    (       " 7 / 3 X> X>r   