o
    jg8(                     @   s   d Z ddl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mZ ddlmZ dd	lmZ d
dgZdd Z		dddZ		dddZ		dddZdddZdddZddd
ZdS )z.Functions for reordering operator expressions.    N)Add)Mul)Integer)Pow)
CommutatorAntiCommutator)BosonOp)	FermionOpnormal_ordernormal_ordered_formc                 C   sl   g }| j D ].}t|tr.t|j d tr.|j d dkr.t|j d D ]
}||j d  q"q|| q|S )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n r   ^/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/physics/quantum/operatorordering.py_expand_powers   s   

r   F
   c                 C   s  t | }g }d}|t|d k r|| ||d  }}tdd ||fD r0|| |d7 }q|jt|jf}	|jt|jf}
|	|
krN|| |d7 }q|d7 }|jr|jst|trt|tr|j	d |j	d kr|rqd}nt
||}||| |  n^||| d  nTt|trt|tr|j	d |j	d kr|rd}nt||}|| | |  n+|| | d  n |j|jkrt|trt|tr|| |  n|||  |t|d k s|t|d kr||d  ||kr| S t|  }t|||d |dS )an  
    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                 s   s     | ]}t |ttf V  qd S )N)r   r	   r   ).0fr   r   r   	<genexpr>6   s    z._normal_ordered_form_factor.<locals>.<genexpr>   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exprr   r   r   _normal_ordered_form_factor&   sd   




)r/   c                 C   sH   g }| j D ]}t|trt||||d}|| q|| qt| S )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   r/   r   r   )r.   r!   r   r    	new_termstermnew_termr   r   r   _normal_ordered_form_termsj   s   

r3   c                 C   sN   ||krt d | S t| trt| |||dS t| tr%t| |||dS | S )a  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   r3   r   r/   )r.   r!   r   r    r   r   r   r      s    


c                 C   s  t | }d}g }|t|d k rt|| trl|| jrlt||d  ts-|||  n||d  jr<|||  n|| jd ||d  jd krZ|||d  ||   n|||d  ||   |d7 }net|| tr|| jrt||d  ts|||  nH||d  jr|||  n9|| jd ||d  jd kr|||d   ||   n|||d   ||   |d7 }n|||  |d7 }|t|d k s|t|d kr||d  ||kr| S t| 	 }t
|||d dS )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   r.   r   r   r   _normal_order_factor   sL    
 
%r8   c                 C   sF   g }| j D ]}t|trt|||d}|| q|| qt| S )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.
    r7   )r   r   r   r8   r   r   )r.   r   r    r0   r1   r2   r   r   r   _normal_order_terms   s   

r9   c                 C   sJ   ||krt d | S t| trt| ||dS t| tr#t| ||dS | S )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
    r4   r7   )r5   r6   r   r   r9   r   r8   )r.   r   r    r   r   r   r
      s   


)Fr   r   )r   r   )__doc__r5   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   r/   r3   r   r8   r9   r
   r   r   r   r   <module>   s2    
D


/
<