
    \h                         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S
KJrJr  S/r " S S\5      r\R,                  R/                  \\5      S 5       rg)z"The commutator: [A,B] = A*B - B*A.    )Add)Expr)KindDispatcher)Mul)Pow)S)
prettyForm)Dagger)_OperatorKindOperatorKind
Commutatorc                       \ rS rSrSrSr\" SSS9r\S 5       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
FCommutator_kind_dispatcherT)commutativec                 F    S U R                    5       nU R                  " U6 $ )Nc              3   8   #    U  H  oR                   v   M     g 7fN)kind).0as     X/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/quantum/commutator.py	<genexpr>"Commutator.kind.<locals>.<genexpr>f   s     /YVVYs   )args_kind_dispatcher)self	arg_kindss     r   r   Commutator.kindd   s!    /TYY/	$$i00    c                 `    U R                  X5      nUb  U$ [        R                  " XU5      nU$ r   )evalr   __new__)clsABrobjs        r   r#   Commutator.__new__i   s.    HHQN=Hll31%
r    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$   r   bcancacbncbc_parts           r   r"   Commutator.evalp   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)	r   r%   r&   signr<   r@   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:   )	r   
isinstancer   r   _eval_expand_commutatorappendr   r   rH   )r   hintsr%   r&   sargstermrE   r   r2   ccomm1comm2firstseconds                 r   rL   "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                    SSK Jn  U R                  S   nU R                  S   n[        X25      (       a9  [        XB5      (       a)   UR                  " U40 UD6nUb  UR                  " S0 UD6$ X4-  XC-  -
  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   )Operatorr+   r:   N )sympy.physics.quantum.operatorrX   r   rK   _eval_commutatorNotImplementedErrordoit)r   rN   rX   r%   r&   rE   s         r   r]   Commutator.doit   s     	<IIaLIIaLa""z!'>'> ))!5u5 yy)5))ac	(%(( '   a00<e<<D*  D  s*   B 
B>B**B:6B>9B::B>c                 r    [        [        U R                  S   5      [        U R                  S   5      5      $ )Nr+   r   )r   r
   r   )r   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   r   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   rd   r+   ])rh   r   ri   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   rd   r+   rn   ro   )leftright)rh   r   r	   rt   parens)r   rj   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   rh   )r   rj   r   args       r   _latexCommutator._latex   sB    %26))/=2;3NN3&&)/= )> > 	> /=s   :
rY   N)rg   
__module____qualname____firstlineno____doc__r-   r   r   propertyr   r#   classmethodr"   rH   rL   r]   r`   rk   rp   rw   r|   __static_attributes__rY   r    r   r   r      su    FN N%&BPTU1 1 + +($ :x)&F
H>r    c                     [         $ )z8Find the kind of an anticommutator of two OperatorKinds.)r   )e1e2s     r   find_op_kindr      s
     r    N)r   sympy.core.addr   sympy.core.exprr   sympy.core.kindr   sympy.core.mulr   sympy.core.powerr   sympy.core.singletonr    sympy.printing.pretty.stringpictr	   sympy.physics.quantum.daggerr
   sympy.physics.quantum.kindr   r   __all__r   r   registerr   rY   r    r   <module>r      sf    (    *    " 7 / B b> b>J %%m]C Dr    