
    wgH                         d dl mZmZmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZmZmZ d dlmZmZ d dlmZmZ d Zd	 Z G d
 de      Zy)    )FunctionSMulPowAdd)ordereddefault_sort_key)expand_func)Dummy)gammasqrtsin)factorcancel)siftuniqc                    | j                  t              } | j                  t              }|D ch c]  }t	        |t              s| }}|s| S ||z  }|| j                         j                  t              z  }|rt        t        |      D cg c]?  }t               | |j                  |j                  D cg c]  }t        |d       c} fA c}} \  }}}| j                  t        t        ||                  }	t        |	d      j                  t        t        ||                  S t        | d      S c c}w c c}w c c}}w )a	  
    Simplify expressions with gamma functions.

    Explanation
    ===========

    This function takes as input an expression containing gamma
    functions or functions that can be rewritten in terms of gamma
    functions and tries to minimize the number of those functions and
    reduce the size of their arguments.

    The algorithm works by rewriting all gamma functions as expressions
    involving rising factorials (Pochhammer symbols) and applies
    recurrence relations and other transformations applicable to rising
    factorials, to reduce their arguments, possibly letting the resulting
    rising factorial to cancel. Rising factorials with the second argument
    being an integer are expanded into polynomial forms and finally all
    other rising factorial are rewritten in terms of gamma functions.

    Then the following two steps are performed.

    1. Reduce the number of gammas by applying the reflection theorem
       gamma(x)*gamma(1-x) == pi/sin(pi*x).
    2. Reduce the number of gammas by applying the multiplication theorem
       gamma(x)*gamma(x+1/n)*...*gamma(x+(n-1)/n) == C*gamma(n*x).

    It then reduces the number of prefactors by absorbing them into gammas
    where possible and expands gammas with rational argument.

    All transformation rules can be found (or were derived from) here:

    .. [1] https://functions.wolfram.com/GammaBetaErf/Pochhammer/17/01/02/
    .. [2] https://functions.wolfram.com/GammaBetaErf/Pochhammer/27/01/0005/

    Examples
    ========

    >>> from sympy.simplify import gammasimp
    >>> from sympy import gamma, Symbol
    >>> from sympy.abc import x
    >>> n = Symbol('n', integer = True)

    >>> gammasimp(gamma(x)/gamma(x - 3))
    (x - 3)*(x - 2)*(x - 1)
    >>> gammasimp(gamma(n + 3))
    gamma(n + 3)

    F)as_comb)rewriter   atomsr   
isinstanceas_dummyzipr   r   funcargs
_gammasimpxreplacedict)
exprfigammasfiadumfunsimpds
             ]/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/simplify/gammasimp.py	gammasimpr*   
   s,   d <<D
 	

8A3AjE2a3F3KA	DMMO!!(++A aj"  Wb'"''68gg$?12
1e,$? @ A" #S$ MM$s3}-.!U+44T#c4.5IJJdE** 4$?"s#   D<D<)E
8EE
E
c                    | j                  t        d       } r| j                  t        d       } n| j                  t        d       } dfd	t        |       } |      } | |k7  rt        |       } | j                  t        d       } | S )a;  
    Helper function for gammasimp and combsimp.

    Explanation
    ===========

    Simplifies expressions written in terms of gamma function. If
    as_comb is True, it tries to preserve integer arguments. See
    docstring of gammasimp for more information. This was part of
    combsimp() in combsimp.py.
    c                 <    t        d| dz
  j                               S N   )_rfexpandns    r)   <lambda>z_gammasimp.<locals>.<lambda>a   s    #a!a%)*     c                     t        |dz         S r-   r   r$   bs     r)   r3   z_gammasimp.<locals>.<lambda>e   s    q1u r4   c                 6    t        | |z         t        |       z  S Nr6   r7   s     r)   r3   z_gammasimp.<locals>.<lambda>h   s    q1ueAh. r4   c                   +,-. | j                   r| S d }-fd-|dk(  r6 | j                  | j                  D cg c]  } 0||dz          c} } |dz  }| j                  s| S |dk(  rS| j	                         \  }}|s| S |r5 0t        j                  |      |dz         t        j                  |      z  S |dz  }|dk(  r;t        | j                  -d      \  }}t        | }t        | }	|	j                         \  }
}t        d      D ]  }t        t        t        j                  |
                  }t        |      D ]r  \  }}|j                  st        |j                  D cg c]  } 0 |||z        |dz          c} j                         \  }}|||<   |j!                  t"              rr n t        | }
|dk(  r
 -|
      s n||
}}
 ||
z  |z  } | j                  r -|      s
 -|
      s| S |dz  }|dk(  r	 | } 0| d	      } | |k(  r| S g }g }g }g }d
 }t        t        | j                              }|r|j%                         j                         \  }}	 ||      \  }}|r|j'                  |       n|du r|j'                  |        ||	      \  }}|r|j'                  |       n|du r|j'                  |       |r/s|||f|||ffD ]0  \  }}}g }|r|j%                         }|j(                  r|j+                  |       1t        |      D ]  \  }}||z   dz
  }|j,                  s|j+                  t.        j0                         |j+                  t3        t.        j0                  |z               |j%                  |       |dkD  r(t        |      D ]  } |j+                  d|z
  | z           n+|dk  r&t        |       D ]  } |j+                  | | z
           n |j+                  |       |r||dd 3 ||||f||||ffD ]  \  }!}"}#}$	 |!D ]!  }|"D ]  }%|d|%z  z
  }|j,                  s n ! n 1|!j5                  |       |"j5                  |%       |dkD  r(t        |      D ]  } |#j+                  d|%z  | z           n0|dk  r+t        |       D ]  } |$j+                  d|%z  dz
  | z
          |!j+                  |%t.        j6                  z          |#j+                  dd|%z  dz
  z         |$j+                  t9        t.        j0                                d ++fd}&|||f|||ffD ]  \  }}} |&|||        |dk\  r,.fd}'i ..fd,,.fd}(||z   |z   |z   D ]
  }  |(|         |||f|||ffD ]  \  }}}g }|r|j%                         })d}*|*rd}* |'||)      }%|%<|j5                  |%       |%|)k7  r|j+                  |%|)z          |(|%|)z         |)dz  })d}* |'||)dz
        }%|%E|j5                  |%       |%|)dz
  k7  r%|j+                  |)dz
  |%z          |(|)dz
  |%z         |)dz  })d}*|*r|j+                  |)       |r||dd  t        |D )cg c]  })t#        |)       c}) t        |D )cg c]  })t#        |)       c}) z  t        | z  t        | z  S c c}w c c}w c c})w c c})w )z/ Simplify products of gamma functions further. c                     | j                  t              }| j                  t        d       }|j                  t              |k  r|} | S )Nc                 f    t        d| dz
  j                               j                  t         d       S )Nr.   c                 6    t        | |z         t        |       z  S r:   r6   r7   s     r)   r3   zU_gammasimp.<locals>.rule_gamma.<locals>.gamma_rat.<locals>.<lambda>.<locals>.<lambda>t   s    E!a%Lq,A r4   )r/   r0   replacer1   s    r)   r3   zC_gammasimp.<locals>.rule_gamma.<locals>.gamma_rat.<locals>.<lambda>s   s*    CAE>>3C -'#AB r4   )countr   r?   )xwasxxs      r)   	gamma_ratz1_gammasimp.<locals>.rule_gamma.<locals>.gamma_ratp   s@    ''%.C5 #C DBxx$Hr4   c                 (   t        | t              ry| j                  s| j                  rt	        fd| j
                  D              S | j                  r>| j                  j                  s| j                  j                  r | j                        S y)NTc              3   .   K   | ]  } |        y wr:    ).0xigamma_factors     r)   	<genexpr>zG_gammasimp.<locals>.rule_gamma.<locals>.gamma_factor.<locals>.<genexpr>~   s     =<+=s   F)r   r   is_Addis_Mulanyr   is_Powexp
is_integerbaseis_positive)rA   rJ   s    r)   rJ   z4_gammasimp.<locals>.rule_gamma.<locals>.gamma_factory   sc    !U#xx188=aff===xxQUU--1C1C#AFF++r4   r   r.      T)binary      c                     | t         j                  u rd g fS | j                         \  }}|j                  r-t	        |t
              rd|j                  d   g|z  fS d|g|z  fS d| gfS )NTr   F)r   Oneas_base_exp
is_Integerr   r   r   )pr8   es      r)   	explicatez1_gammasimp.<locals>.rule_gamma.<locals>.explicate   sk    AEEzRx==?DAq||a'!&&)Q.. 1#a%<'qcz!r4   FNc                    t        t        |             }t        t        |            D ]  }t        |dz   t        |            D cg c]  }||   ||   z
  dz  |f }}|D ]  \  }}|j                  dk(  s|j
                  dk7  s&|j
                  }|g}t        t        d|            }|D ]B  \  }	}||	z  }
|
j                  s|
|v s|j                  |
       |j                  |       |rB n t        |      D ]   \  }}||   }| j                  |       |||<   " |j
                  |d   |dd  fc c S   y c c}w )Nr.   r   )
listr   rangelenr\   qr[   removeappend	enumerate)coeffsur!   jdjoner2   gotgetr(   mcs               r)   _runz,_gammasimp.<locals>.rule_gamma.<locals>._run&  sH    f&s1v :A;@QA;OPaQqTAaD[A-q1PBP"$ :Q55A:#%%1* #A#$#C"&uQ{"3C(* )1$%aC#$<<AH$'JJqM$'JJqM+.(-) !)(1# +1$%aD &a 0)*A+ $'55#a&#ab'#99%::Ps   E c                    i }| D ]6  }|j                         \  }}|j                  |g       j                  |       8 t        |t              }|D ]  }t        ||         }g }		  |      }
|
n|
\  }}}|D ]:  }||z   dz
  }t        t        ||z
              D ]  }|j                  ||z
          < |||z   z  }|j                  dt        j                  z  t        |dz
        dz  z  |t        j                  |z
  z  z         |	j                  |       |D cg c]  }||z   	 c}|	z   ||<    g }|D ]
  }|||   z  } || d d  y c c}w )N)keyr.   rT   )
as_coeff_Add
setdefaultre   sortedr	   ra   intr   PiHalf)r"   numerdenomratsgro   residkeysrg   newrunr2   uiotherrh   conkrp   s                    r)   	_mult_thmz1_gammasimp.<locals>.rule_gamma.<locals>._mult_thm@  s   
  9A ~~/HAuOOE2.55a89
 d(89! DE#DK0FC"6l;! (+2u "' 6A"'!)a-C%*3q2v;%7 6 %S1W 566
  n af!a%
%;%&#%6&7 8 

33 8 7="=519"="CDK?DD ! %Ee$A% q	 #>s   Ec                 `   | sy  
|      \  }}| D ]  }|   \  }}||k7  s+|j                  |      s|t               k7  s|t               k7  r<t        t        ||z        j                        }t        |j                        }t        |j                        }	|dk(  s|dkD  s|	dkD  s|c S  y )Nr   )intersectionsetrb   r   free_symbols)lrA   S1T1yS2T2r$   r8   ro   
compute_STinvs             r)   
find_fuzzyz2_gammasimp.<locals>.rule_gamma.<locals>.find_fuzzy  s    #AB !A VFBRx(;%'35[B#%K  F1Q3K445AANN+AANN+AAv1q5AE !r4   c                     | v r|    S | j                   | j                  t              j                  | j                  t              D ch c]  }|j
                   c}      fS c c}w r:   )r   r   r   unionr   rP   )r   r]   r   s     r)   r   z2_gammasimp.<locals>.rule_gamma.<locals>.compute_ST  sZ    3;t9$))4::h+?+E+E(,

3818,: ; ;8s   A&
c                      |       | <   y r:   rG   )r   r   r   s    r)   	update_STz1_gammasimp.<locals>.rule_gamma.<locals>.update_ST  s    &t,D	r4   )is_Atomr   r   rM   args_cncr   
_from_argsr   as_numer_denomra   r`   r   	make_argsrf   rL   r   hasr   popextendrQ   re   r[   r   rw   r   rd   rx   r   )1r   levelrD   rA   r   ncTF	gamma_indr(   ndddipassr!   nir$   rB   numer_gammasdenom_gammasnumer_othersdenom_othersr^   newargsr2   isgr   r"   ry   rz   r   g1g2r   ngdgnodor   r   r   r   r|   contrp   r   rJ   r   r   
rule_gammas1                                              @@@@r)   r   z_gammasimp.<locals>.rule_gammaj   sk    <<K		 A:499KAz!UQY7KLDQJE{{K A:}}HD"!#.."6	B3>>RTCUUUQJE A:		<=DAqQIQA%%'FBq  GCMM"$567&t_ "EAryy!$LNGG'UGHJy2	B'U ",n. B #%Q!vve}!" $ZQ;|B'7RB  R<?DKK\"%5b9IQJE A:!$*3;K	  
	" wtyy)*;;=//1DAqq\FC##A&##A&q\FC##A&##A&   lL*:!<>)@  $u B}}

2 !*6!2 '2GaK ||$QTT*Sb\2

1q5%*1X 9 %QVaZ 89U%*A2Y 6 %bS1W 56' 

2) ,  q	5 B %1,$0$2$0,$0$2#3 *BB
  	!# %A !AaCA || %%
 %	 IIaLIIaL1u!&q /AIIacAg./Q!&r 3AIIacAgk23IIa!&&j)IIa!A#'l+IId144j)+ *X:42h &2<$N%1<$N$P +5%!UE*+ A:
!0 C;-$|3lB\Q  $  lL*:!<>)@  $u 

AD$&ua0=!LLO Av %QqS 1 )!A#FA#'D&ua!e4=!LLO AEz %a!eQY 7 )1q5!) 4FA#'D# $ JJqM+ .  q	7 > |4!U1X45l3E!H345< !#&#56 	6}	 L4'UH	 53s   Z<'[4[[
c                 X    | j                   rt        t        |             S t        |       S r:   )is_Rationalr
   r   r1   s    r)   r3   z_gammasimp.<locals>.<lambda>  s    1==+eAh' eAh r4   )r   )r?   r   r/   r   )r   r   rB   r   s    ` @r)   r   r   T   s     <<*,D ||C%' ||C.0[6z
 ,Cc?Ds{d|<<FHD Kr4   c                       e Zd Zed        Zy)r/   c                    |j                   rt|st        j                  S t        |      }|dkD  r"t	        t        |      D cg c]  }||z   	 c} S |dk  r*dt	        t        d| dz         D cg c]  }||z
  	 c} z  S y |j                  re|j                         \  }}|j                   rF|dkD  rt        ||      t        ||z   |      z  S |dk  r t        ||      t        ||z   |z   |       z  S |j                  r|j                         \  }}|j                   re|dkD  r)t        ||      t        ||z   |      z  t        ||      z  S |dk  r1t        ||      t        ||z   |       z  t        ||z   |z   |       z  S y y y c c}w c c}w )Nr   r.   )	r[   r   rY   rv   r   ra   rL   rs   r/   )clsr$   r8   r2   r!   ro   _b_as           r)   evalz_rf.eval  s   <<uuAA1uE!H5qQU566QeArAv.>?q1u?@@@  xx(2<<1u"1bz#a"fa.88Q"1bz#a"fqj1"*===xx(2<<1u"2qz#b1fa.8RCCQ"2qz#b1fqb/9#b1fqj1":MMM     6?s   F.F	
N)__name__
__module____qualname__classmethodr   rG   r4   r)   r/   r/     s    N Nr4   r/   N)
sympy.corer   r   r   r   r   sympy.core.sortingr   r	   sympy.core.functionr
   sympy.core.symbolr   sympy.functionsr   r   r   sympy.polysr   r   sympy.utilities.iterablesr   r   r*   r   r/   rG   r4   r)   <module>r      s<    1 1 8 + # , , & 0G+T|~N( Nr4   