
    \h5H                         S SK JrJrJrJrJr  S SKJrJr  S SK	J
r
  S SKJr  S SKJrJrJr  S SKJrJr  S SKJrJr  S rS	 r " S
 S\5      rg)    )FunctionSMulPowAdd)ordereddefault_sort_key)expand_func)Dummy)gammasqrtsin)factorcancel)siftuniqc                    U R                  [        5      n U R                  [        5      nU Vs1 s H  n[	        U[        5      (       d  M  UiM     nnU(       d  U $ X-  nXR                  5       R                  [        5      -  nU(       a  [        [        U5       VVs/ s H?  n[        5       XDR                  " UR                   Vs/ s H  n[        USS9PM     sn6 4PMA     snn6 u  pgnU R                  [        [        Xv5      5      5      n	[        U	SS9R                  [        [        Xh5      5      5      $ [        U SS9$ s  snf s  snf s  snnf )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
             P/var/www/auris/envauris/lib/python3.13/site-packages/sympy/simplify/gammasimp.py	gammasimpr*   
   s(   d <<D
 	

8A3AjE2aF3KA	MMO!!(++A aj" ! Wb''68gg$?6=
1e,g$? @ A " #$ MM$s3}-.!U+44T#c.5IJJdE** 4$?"s#   EE )E
	E	E
E
c                 *  ^^ U R                  [        S 5      n T(       a  U R                  [        S 5      n OU R                  [        S 5      n SUU4S jjm[        U 5      nT" U5      n X:w  a  [        U 5      n U R                  [        S 5      n U $ )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                 <    [        SU S-
  R                  5       5      $ N   )_rfexpandns    r)   <lambda>_gammasimp.<locals>.<lambda>a   s    #a!a%)*    c                     [        US-   5      $ r-   r   r$   bs     r)   r3   r4   e   s    q1ur5   c                 4    [        X-   5      [        U 5      -  $ Nr7   r8   s     r)   r3   r4   h   s    queAh.r5   c                   >^(^)^*^+^,^- U R                   (       a  U $ S nU+4S jm+US:X  a8  U R                  " U R                   Vs/ s H  nT/" X1S-   5      PM     sn6 n US-  nU R                  (       d  U $ US:X  a^  U R	                  5       u  pEU(       d  U $ U(       a7  T/" [
        R                  " U5      US-   5      [
        R                  " U5      -  $ US-  nUS:X  GaW  [        U R                  T+SS9u  pg[        U6 n[        U6 n	U	R                  5       u  p[        S5       H  n[        [        [
        R                  " U
5      5      5      n[        U5       H}  u  pUR                  (       d  M  [        UR                   Vs/ s H  nT/" U" X-  5      US-   5      PM     sn6 R                  5       u  pXU'   UR!                  ["        5      (       a  M}    O   [        U6 n
US:X  a  T+" U
5      (       d    OXpM     X-  U-  n U R                  (       a  T+" U5      (       d  T+" U
5      (       d  U $ US-  nUS:X  a   U nT/" U S	5      n U U:X  a  U $ M  / n/ n/ n/ nS
 n[        [        U R                  5      5      nU(       a  UR%                  5       R                  5       u  nn	U" U5      u  nnU(       a  UR'                  U5        OUSL a  UR'                  U5        U" U	5      u  nnU(       a  UR'                  U5        OUSL a  UR'                  U5        U(       a  M  T.(       Gd  UUU4UUU44 GHG  u  nnn/ nU(       Ga0  UR%                  5       m*T*R(                  (       a  UR+                  T*5        M<  [        U5       H  u  nnT*U-   S-
  nUR,                  (       d  M!  UR+                  [.        R0                  5        UR+                  [3        [.        R0                  T*-  5      5        UR%                  U5        US:  a%  UR'                  U*4S j[        U5       5       5        O+US:  a%  UR'                  U*4S j[        U* 5       5       5          O   UR+                  T*5        U(       a  GM0  UUSS& GMJ     UUUU4UUUU44 GH  u  nn n!n" U H)  nU  H  m-UST--  -
  nUR,                  (       d  M    O   M)    O   M;  UR5                  U5        U R5                  T-5        US:  a%  U!R'                  U-4S j[        U5       5       5        O+US:  a%  U"R'                  U-4S j[        U* 5       5       5        UR+                  T-[.        R6                  -   5        U!R+                  SST--  S-
  -  5        U"R+                  [9        [.        R0                  5      5        GM     S m(U(4S jn#UUU4UUU44 H  u  nnnU#" UUU5        M     US:  Ga/  U)U,4S jn$0 m,U,4S jm)U)U,4S jn%UU-   U-   U-    H  n U%" U 5        M     UUU4UUU44 H  u  nnn/ nU(       a  UR%                  5       n&Sn'U'(       a  Sn'U$" UU&5      m-T-b=  UR5                  T-5        T-U&:w  a  UR+                  T-U&-  5        U%" T-U&-  5        U&S-  n&Sn'U$" UU&S-
  5      m-T-bF  UR5                  T-5        T-U&S-
  :w  a%  UR+                  U&S-
  T--  5        U%" U&S-
  T--  5        U&S-  n&Sn'U'(       a  M  UR+                  U&5        U(       a  M  UUSS& M     [        U V&s/ s H  n&[#        U&5      PM     sn&6 [        U V&s/ s H  n&[#        U&5      PM     sn&6 -  [        U6 -  [        U6 -  $ s  snf s  snf s  sn&f s  sn&f )z.Simplify products of gamma functions further. c                     U R                  [        5      nU R                  [        S 5      nUR                  [        5      U:  a  Un U $ )Nc                 f    [        SU S-
  R                  5       5      R                  [         S 5      $ )Nr.   c                 4    [        X-   5      [        U 5      -  $ r;   r7   r8   s     r)   r3   U_gammasimp.<locals>.rule_gamma.<locals>.gamma_rat.<locals>.<lambda>.<locals>.<lambda>t   s    E!%Lq,Ar5   )r/   r0   replacer1   s    r)   r3   C_gammasimp.<locals>.rule_gamma.<locals>.gamma_rat.<locals>.<lambda>s   s+    CAE>>3C -'#AB-Cr5   )countr   rA   )xwasxxs      r)   	gamma_rat1_gammasimp.<locals>.rule_gamma.<locals>.gamma_ratp   s@    ''%.C5 #C DBxx$Hr5   c                 f  > [        U [        5      (       a  gU R                  (       d  U R                  (       a  [	        U4S jU R
                   5       5      $ U R                  (       aH  U R                  R                  (       d  U R                  R                  (       a  T" U R                  5      $ g)NTc              3   4   >#    U  H  nT" U5      v   M     g 7fr;    ).0xigamma_factors     r)   	<genexpr>G_gammasimp.<locals>.rule_gamma.<locals>.gamma_factor.<locals>.<genexpr>~   s     =f<++f   F)r   r   is_Addis_Mulanyr   is_Powexp
is_integerbaseis_positive)rD   rN   s    r)   rN   4_gammasimp.<locals>.rule_gamma.<locals>.gamma_factory   sf    !U##xx188=aff===xxQUU--1C1C#AFF++r5   r   r.      T)binary      c                     U [         R                  L a  S / 4$ U R                  5       u  pUR                  (       a2  [	        U[
        5      (       a  SUR                  S   /U-  4$ SU/U-  4$ SU /4$ )NTr   F)r   Oneas_base_exp
is_Integerr   r   r   )pr9   es      r)   	explicate1_gammasimp.<locals>.rule_gamma.<locals>.explicate   sl    AEEzRx==?DA||a''!&&)Q.. 1#a%<'qcz!r5   Fc              3   4   >#    U  H  nS T-
  U-   v   M     g7f)r.   NrK   rL   kg1s     r)   rO   1_gammasimp.<locals>.rule_gamma.<locals>.<genexpr>   s     (FXR!XrQ   c              3   0   >#    U  H  nT* U-
  v   M     g 7fr;   rK   rh   s     r)   rO   rk      s     (D)Q"q)s   Nc              3   4   >#    U  H  nS T-  U-   v   M     g7f)r[   NrK   rL   ri   ys     r)   rO   rk     s     !<8a!A#'8rQ   c              3   :   >#    U  H  nS T-  S-
  U-
  v   M     g7f)r[   r.   NrK   rn   s     r)   rO   rk     s     !Ay!!A#'A+ys   c                    [        [        U 5      5      n[        [        U5      5       GH%  n[        US-   [        U5      5       Vs/ s H  o1U   X   -
  S-  U4PM     nnU H  u  pSUR                  S:X  d  M  UR
                  S:w  d  M)  UR
                  nU/n[        [        SU5      5      nU HN  u  pXi-  n
U
R                  (       d  M  X;   d  M#  UR                  U
5        UR                  U5        U(       a  MN    O   M  [        U5       H  u  p#X   nU R                  U5        XU'   M      UR
                  US   USS  4s  s  $    GM(     g s  snf )Nr.   r   )
listr   rangelenrc   qrb   removeappend	enumerate)coeffsur!   jdjoner2   gotgetr(   mcs               r)   _run,_gammasimp.<locals>.rule_gamma.<locals>._run"  s)    f&s1vA;@QA;OP;OaqTAD[A-q1;OBP"$55A:#%%1* #A#$#C"&uQ{"3C(*$%C#$<<<AH$'JJqM$'JJqM+.3(- )+ !)(1#$%D &a 0)*A )7 $'55#a&#ab'#99% #% 'Ps   Ec                   > 0 nU  H6  nUR                  5       u  pVUR                  U/ 5      R                  U5        M8     [        U[        S9nU H  n[        X6   5      n/ n	 T" U5      n
U
c  OU
u  pnU H;  nXn-   S-
  n[        [        X-
  5      5       H  nUR                  UU-
  5        M     M=     XU-   -  nUR                  S[        R                  -  [        US-
  5      S-  -  U[        R                  U-
  -  -  5        U	R                  U5        M  U Vs/ s H  oVU-   PM	     snU	-   X6'   M     / nU H
  nXCU   -  nM     X@S S & g s  snf )N)keyr.   r[   )
as_coeff_Add
setdefaultrw   sortedr	   rs   intr   PiHalf)r"   numerdenomratsgr   residkeysry   newrunr2   uiotherrz   conri   r   s                    r)   	_mult_thm1_gammasimp.<locals>.rule_gamma.<locals>._mult_thm<  sh   
 A ~~/HAOOE2.55a8  
 d(89!E#DK0FC"6l;! (+u "'A"')a-C%*3qv;%7 %S1W 5 &8 "'
  n af!a%
%;%&#%6&7 8 

33 8 7="=f19f"="CDK? "D !Ee$A " q	 #>s   Ec                   > U (       d  g T
" U5      u  p#U  H  nTU   u  pVX6:w  d2  UR                  U5      (       d  U[        5       :w  d  U[        5       :w  a  MC  [        [        X-  5      R                  5      n[        UR                  5      n[        UR                  5      n	US:X  d  M  US:  d  U	S:  d  M  Us  $    g )Nr   )intersectionsetrt   r   free_symbols)lrD   S1T1ro   S2T2r$   r9   r   
compute_STinvs             r)   
find_fuzzy2_gammasimp.<locals>.rule_gamma.<locals>.find_fuzzy{  s    #AA VFBx(;(;%'35[B#%K  F13K445AANN+AANN+AAv1q5AE  r5   c                    > U T;   a  TU    $ U R                   U R                  [        5      R                  U R                  [        5       Vs1 s H  oR
                  iM     sn5      4$ s  snf r;   )r   r   r   unionr   rV   )r   rd   r   s     r)   r   2_gammasimp.<locals>.rule_gamma.<locals>.compute_ST  s\    3;t9$))4::h+?+E+E(,

3818,: ; ;8s   A*
c                    > T" U 5      TU '   g r;   rK   )r   r   r   s    r)   	update_ST1_gammasimp.<locals>.rule_gamma.<locals>.update_ST  s    &t,D	r5   )is_Atomr   r   rS   args_cncr   
_from_argsr   as_numer_denomrs   rr   r   	make_argsrx   rR   r   hasr   popextendrW   rw   rb   r   r   r   rv   r   r   )0r   levelrG   rD   r   ncTF	gamma_indr(   ndddipassr!   nir$   rE   numer_gammasdenom_gammasnumer_othersdenom_othersre   newargsr2   isgr   r"   r   r   r   g2ngdgnodor   r   r   r   contr   r   rj   rN   r   ro   r   
rule_gammas0                                           @@@@@@r)   r   _gammasimp.<locals>.rule_gammaj   s    <<K		 A:99KAz!QY7KLDQJE{{K A:}}HD!#.."6	B3>>RTCUUUQJE A:		<=DAQIQA%%'FBqGCMM"$567&t_EAyyy!$LNGG'ULSqJy	BG'U ",n.  #%Q!vve}}! - $ZQ;|B'7'7B " <?DKK\"%5%5b9I9IQJE A:!$*3;K	  
	" wtyy)*;;=//1DAqq\FC##A&##A&q\FC##A&##A& g  lL*:!<>)@$u B}}

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

1q5!LL(FU1X(FFU!LL(D%)(DD "3 

2% f(  q	1)@> %1,$0$2$0,$0$2#3BB
 !#A !AaCA ||| % "$
 %   IIaLIIaL1u		!<58!<<Q		!AuaRy!AAIIa!&&j)IIa!A#'l+IId144j)' #3T:42h &2<$N%1<$N$P5%!UE*$P A:
!0 C;-$|3lB\Q$ R 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+ f.  q	7)@> |4|!U1X|45l3lE!Hl345< !#&#56 	6u	 L4'U@	 53s   ]]!]&/]+
c                 b    U R                   (       a  [        [        U 5      5      $ [        U 5      $ r;   )is_Rationalr
   r   r1   s    r)   r3   r4     s    1==+eAh'FeAhFr5   )r   )rA   r   r/   r   )r   r   rE   r   s    ` @r)   r   r   T   s     <<*,D ||C%' ||C.0W6 W6r
 ,Cc?D{d|<<FHD Kr5   c                   $    \ rS rSr\S 5       rSrg)r/   i  c                 T   UR                   (       a  U(       d  [        R                  $ [        U5      nUS:  a$  [	        [        U5       Vs/ s H  oAU-   PM	     sn6 $ US:  a,  S[	        [        SU* S-   5       Vs/ s H  oAU-
  PM	     sn6 -  $ g UR                  (       ag  UR                  5       u  pVUR                   (       aD  US:  a  [        X5      [        X-   U5      -  $ US:  a  [        X5      [        X-   U-   U* 5      -  $ UR                  (       a  UR                  5       u  pWUR                   (       aa  US:  a&  [        Xr5      [        Xr-   U5      -  [        Xu5      -  $ US:  a.  [        Xr5      [        Xu-   U* 5      -  [        Xr-   U-   U* 5      -  $ g g g s  snf s  snf )Nr   r.   )	rb   r   r`   r   r   rs   rR   r   r/   )clsr$   r9   r2   r!   r   _b_as           r)   eval_rf.eval  sp   <<uuAA1uE!H5HqUH566QeArAv.>?.>1u.>?@@@  xx(<<1u"1z#afa.88Q"1z#afqj1"*===xx(<<1u"2z#bfa.8RCCQ"2z#bfqb/9#bfqj1":MMM     6?s   F <F%
rK   N)__name__
__module____qualname____firstlineno__classmethodr   __static_attributes__rK   r5   r)   r/   r/     s    N Nr5   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/   rK   r5   r)   <module>r      s<    1 1 8 + # , , & 0G+TxvN( Nr5   