
    \h8(                         S r SSK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SKJr  SS	KJr  S
S/rS r  SS jr  SS jr  SS jrSS jrSS jrSS jrg)z.Functions for reordering operator expressions.    N)Add)Mul)Integer)Pow)
CommutatorAntiCommutator)BosonOp)	FermionOpnormal_ordernormal_ordered_formc                 d   / nU R                    H  n[        U[        5      (       at  [        UR                   S   [        5      (       aR  UR                   S   S:  a?  [	        UR                   S   5       H!  nUR                  UR                   S   5        M#     M  UR                  U5        M     U$ )z
Helper function for normal_ordered_form and normal_order: Expand a
power expression to a multiplication expression so that that the
expression can be handled by the normal ordering functions.
   r   )args
isinstancer   r   rangeappend)factorsnew_factorsfactorns       ^/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/quantum/operatorordering.py_expand_powersr      s     K,,vs##v{{1~w77KKNQ&6;;q>*""6;;q>2 + v&      c                    [        U 5      n/ nSnU[        U5      S-
  :  Gac  XF   XFS-      p[        S Xx4 5       5      (       a  UR                  U5        US-  nMM  UR                  [        UR                  5      4n	UR                  [        UR                  5      4n
X::  a  UR                  U5        US-  nM  US-  nUR                  (       Ga1  UR                  (       Gd  [        U[        5      (       ay  [        U[        5      (       ad  UR                  S   UR                  S   :w  a-  U(       a  SnO[        Xx5      nUR                  X-  U-   5        GOUR                  X-  S-   5        O[        U[        5      (       a{  [        U[        5      (       af  UR                  S   UR                  S   :w  a.  U(       a  SnO[        Xx5      nUR                  U* U-  U-   5        OUR                  U* U-  S-   5        OmUR                  UR                  :X  a@  [        U[        5      (       a+  [        U[        5      (       a  UR                  U* U-  5        OUR                  X-  5        U[        U5      S-
  :  a  GMc  U[        U5      S-
  :X  a  UR                  US   5        XT:X  a  U $ [        U6 R                  5       n[        UUUS-   US9$ )aV  
Helper function for normal_ordered_form_factor: Write multiplication
expression with bosonic or fermionic operators on normally ordered form,
using the bosonic and fermionic commutation relations. The resulting
operator expression is equivalent to the argument, but will in general be
a sum of operator products instead of a simple product.
r   r   c              3   X   #    U  H   n[        U[        [        45      (       + v   M"     g 7f)N)r   r
   r	   ).0fs     r   	<genexpr>._normal_ordered_form_factor.<locals>.<genexpr>6   s"     P1:a)W!5666s   (*   recursive_limit_recursive_depthindependent)r   lenanyr   is_annihilationstrnamer   r	   r   r   r
   r   r   expandr   )productr%   r#   r$   r   r   r   currentnextkey_1key_2cexprs                r   _normal_ordered_form_factorr3   &   s~    W%GK	A
c'lQ

GENPPPPw'FA((#gll*;<%%s499~6>w'FA	Q"""4+?+?+?'7++
40I0I<<?diil2"&w5&&t~'9:&&t~'9:GY//JtY4O4O<<?diil2"*79&&uw':;&&uw':;%%)=)==w	**z$	/J/Juw/t~.O c'lQ
R 	CL172;'K '')"43B4Dq4H/:< 	<r   c                     / nU R                    HF  n[        U[        5      (       a  [        XRX1S9nUR	                  U5        M5  UR	                  U5        MH     [        U6 $ )z
Helper function for normal_ordered_form: loop through each term in an
addition expression and call _normal_ordered_form_factor to perform the
factor to an normally ordered expression.
r"   )r   r   r   r3   r   r   )r2   r%   r#   r$   	new_termstermnew_terms          r   _normal_ordered_form_termsr8   j   s_     I		dC  2!1LH X&T"  	?r   c                     X2:  a  [         R                  " S5        U $ [        U [        5      (       a  [	        U UUUS9$ [        U [
        5      (       a  [        U UUUS9$ U $ )aL  Write an expression with bosonic or fermionic operators on normal
ordered form, where each term is normally ordered. Note that this
normal ordered form is equivalent to the original expression.

Parameters
==========

expr : expression
    The expression write on normal ordered form.
independent : bool (default False)
    Whether to consider operator with different names as operating in
    different Hilbert spaces. If False, the (anti-)commutation is left
    explicit.
recursive_limit : int (default 10)
    The number of allowed recursive applications of the function.

Examples
========

>>> from sympy.physics.quantum import Dagger
>>> from sympy.physics.quantum.boson import BosonOp
>>> from sympy.physics.quantum.operatorordering import normal_ordered_form
>>> a = BosonOp("a")
>>> normal_ordered_form(a * Dagger(a))
1 + Dagger(a)*a
Too many recursions, abortingr"   )warningswarnr   r   r8   r   r3   )r2   r%   r#   r$   s       r   r   r      ss    : )56$)$:I;K6AC 	C 
D#		*4;J<L7BD 	D
 r   c                    [        U 5      nSn/ nU[        U5      S-
  :  Ga  [        X4   [        5      (       a  X4   R                  (       a  [        X4S-      [        5      (       d  UR                  X4   5        GOX4S-      R                  (       a  UR                  X4   5        GO_X4   R                  S   X4S-      R                  S   :w  a  UR                  X4S-      X4   -  5        OUR                  X4S-      X4   -  5        US-  nO[        X4   [        5      (       a  X4   R                  (       a  [        X4S-      [        5      (       d  UR                  X4   5        OX4S-      R                  (       a  UR                  X4   5        OyX4   R                  S   X4S-      R                  S   :w  a  UR                  X4S-      * X4   -  5        OUR                  X4S-      * X4   -  5        US-  nOUR                  X4   5        US-  nU[        U5      S-
  :  a  GM  U[        U5      S-
  :X  a  UR                  US   5        XS:X  a  U $ [        U6 R                  5       n[        UUUS-   S9$ )z
Helper function for normal_order: Normal order a multiplication expression
with bosonic or fermionic operators. In general the resulting operator
expression will not be equivalent to original product.
r   r   r!   r#   r$   )r   r&   r   r	   r(   r   r   r
   r   r+   r   )r,   r#   r$   r   r   r   r2   s          r   _normal_order_factorr?      s:    W%G	AK
c'lQ
wz7++
**g!eng66""7:.q5>11&&wz2zq)WU^-@-@-CC#**7q5>GJ+FG#**7q5>GJ+FGFAY//j((g!eni88""7:.q5>11&&wz2zq)WU^-@-@-CC#**GEN?WZ+GH#**GEN?WZ+GHFA wz*	QG c'lQ
J 	CL172;'K '')D,;-=-AC 	Cr   c                     / nU R                    HG  n[        U[        5      (       a  [        UUUS9nUR	                  U5        M6  UR	                  U5        MI     [        U6 $ )z
Helper function for normal_order: look through each term in an addition
expression and call _normal_order_factor to perform the normal ordering
on the factors.
r>   )r   r   r   r?   r   r   )r2   r#   r$   r5   r6   r7   s         r   _normal_order_termsrA      sa     I		dC  +D<K=MOH X&T"  	?r   c                     X!:  a  [         R                  " S5        U $ [        U [        5      (       a
  [	        XUS9$ [        U [
        5      (       a
  [        XUS9$ U $ )a  Normal order an expression with bosonic or fermionic operators. Note
that this normal order is not equivalent to the original expression, but
the creation and annihilation operators in each term in expr is reordered
so that the expression becomes normal ordered.

Parameters
==========

expr : expression
    The expression to normal order.

recursive_limit : int (default 10)
    The number of allowed recursive applications of the function.

Examples
========

>>> from sympy.physics.quantum import Dagger
>>> from sympy.physics.quantum.boson import BosonOp
>>> from sympy.physics.quantum.operatorordering import normal_order
>>> a = BosonOp("a")
>>> normal_order(a * Dagger(a))
Dagger(a)*a
r:   r>   )r;   r<   r   r   rA   r   r?   )r2   r#   r$   s      r   r   r      se    2 )56$"44DF 	F	D#		#D5EG 	G r   )F
   r   )rC   r   )__doc__r;   sympy.core.addr   sympy.core.mulr   sympy.core.numbersr   sympy.core.powerr   sympy.physics.quantumr   r   sympy.physics.quantum.bosonr	   sympy.physics.quantum.fermionr
   __all__r   r3   r8   r   r?   rA   r    r   r   <module>rN      su    4    &   < / 3 & MO12A<H IK01* BD)*,^9Cx($r   