K idZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZdd lmZmZd gZGd d eZej,j/eed Zy)z"The commutator: [A,B] = A*B - B*A.)Add)Expr)KindDispatcher)Mul)Pow)S) prettyForm)Dagger) _OperatorKind OperatorKind Commutatorc~eZdZdZdZeddZedZdZ e dZ d Z d Z d Zd Zd ZdZdZdZy)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 FCommutator_kind_dispatcherT) commutativecFd|jD}|j|S)Nc34K|]}|jywN)kind).0as f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/commutator.py z"Commutator.kind..fs/QVV/s)args_kind_dispatcher)self arg_kindss rrzCommutator.kindds#/TYY/ $t$$i00c`|j||}||Stj|||}|Sr)evalr__new__)clsABrobjs rr zCommutator.__new__is2 HHQN =Hll31% rc |r|stjS||k(rtjS|js |jrtjS|j\}}|j\}}||z}|r?t t ||t j |t j |S|j |dk(rtj|||zSy)N)rZerois_commutativeargs_cncr _from_argscompare NegativeOne)r!rbcancacbncbc_parts rrzCommutator.evalpsa66M 666M  q//66M**,C**,Cb sF|S)Q%RS S 99Q<1 ==Q* * rc|j}|jr|jrt|dkr|S|j}|j r|jdz}| }t ||jd}||dz z|z}td|D]}|||dz |z z|z||zzz }||jzS)Nr'T) commutator) exp is_integer is_constantabsbase is_negativer expandrange) rr"r#signr7r;commresultis r _expand_powzCommutator._expand_powsee~~S__%6#c(a-Kvv ??662:D$C$"))T):a4'q# ;A dS1Wq[)D047: :F ;FMMO##rc 6|jd}|jd}t|trXg}|jD]?}t||}t|tr|j }|j |At|St|trXg}|jD]?}t||}t|tr|j }|j |At|St|t r|jd}t |jdd}|} t|| } t|| } t| tr| j } t| tr| j } t || } t | |} t| | St|t r|}|jd}t |jdd} t||} t|| } t| tr| j } t| tr| j } t | | } t || } t| | St|tr|j||dSt|tr|j||dS|S)Nrr'r5) r isinstancerr _eval_expand_commutatorappendrrrC)rhintsr"r#sargstermr@rr.ccomm1comm2firstseconds rrFz"Commutator._eval_expand_commutatorsS IIaL IIaL a E #!$*dJ/779D T"  # ;  3 E #!!T*dJ/779D T"  # ;  3 q AQVVABZ AAq!$Eq!$E%,557%,5575ME]Fuf% % 3 Aq AQVVABZ Aq!$Eq!$E%,557%,557qMEE]Fuf% % 3 ##Aq!, , 3 ##Aq"- - rc pddlm}|jd}|jd}t||r4t||r( |j|fi|}||j di|S||z||zz j di|S#t $r, d|j|fi|z}n#t $rd}YnwxYwYcwxYw)z Evaluate commutator r)Operatorr'r5N)sympy.physics.quantum.operatorrQrrE_eval_commutatorNotImplementedErrordoit)rrHrQr"r#r@s rrVzCommutator.doits < IIaL IIaL a "z!X'> )q))!5u5  tyy)5))!ac (%(('  0a00 rc zdt|jDcgc]}|j|g|c}zScc}w)Nz\left[%s,%s\right])tuplerr_)rrarargs r_latexzCommutator._latexsB%26))/=+.NGNN3 & &/=)>> >/=s8 N)r^ __module__ __qualname____doc__r)rrpropertyrr classmethodrrCrFrVrXrbrfrlrprRrrr r suFNN%&BPTU 11++($ :x)&F H>rctS)z8Find the kind of an anticommutator of two OperatorKinds.)r )e1e2s r find_op_kindrys  rN)rssympy.core.addrsympy.core.exprrsympy.core.kindrsympy.core.mulrsympy.core.powerrsympy.core.singletonr sympy.printing.pretty.stringpictr sympy.physics.quantum.daggerr sympy.physics.quantum.kindr r __all__r rregisterryrRrrrsf( * "7/B b>b>J %%m]CDr