
    \h                         S SK Jr  S SKJr  S SKJrJr  S SKJrJ	r	  S SK
Jr  S SKJrJr  S SKJr  \" S5      S	 5       rS
 rg)    )Mul)	count_ops)preorder_traversal	bottom_up)binomial	factorial)gamma)	gammasimp
_gammasimp)timethiscombsimpc                     U R                  [        SS9n [        S [        U 5       5       5      (       a  [	        U 5      $ [        U SS9n [        U 5      n U $ )a/  
Simplify combinatorial expressions.

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

This function takes as input an expression containing factorials,
binomials, Pochhammer symbol and other "combinatorial" functions,
and tries to minimize the number of those functions and reduce
the size of their arguments.

The algorithm works by rewriting all combinatorial functions as
gamma functions and applying gammasimp() except simplification
steps that may make an integer argument non-integer. See docstring
of gammasimp for more information.

Then it rewrites expression in terms of factorials and binomials by
rewriting gammas as factorials and converting (a+b)!/a!b! into
binomials.

If expression has gamma functions or combinatorial functions
with non-integer argument, it is automatically passed to gammasimp.

Examples
========

>>> from sympy.simplify import combsimp
>>> from sympy import factorial, binomial, symbols
>>> n, k = symbols('n k', integer = True)

>>> combsimp(factorial(n)/factorial(n - 3))
n*(n - 2)*(n - 1)
>>> combsimp(binomial(n+1, k+1)/binomial(n, k))
(n + 1)/(k + 1)

F)	piecewisec              3      #    U  H:  n[        U[        5      =(       a    UR                  S    R                  (       + v   M<     g7f)r   N)
isinstancer	   args
is_integer).0nodes     O/var/www/auris/envauris/lib/python3.13/site-packages/sympy/simplify/combsimp.py	<genexpr>combsimp.<locals>.<genexpr>3   s7      .,D dE"B499Q<+B+B'BB,s   AAT)as_comb)rewriter	   anyr   r
   r   _gamma_as_comb)exprs    r   r   r      s[    N <<</D
 .&t,. . .dd+D$DK    c                 H    U R                  [        5      n S n[        X5      $ )zY
Helper function for combsimp.

Rewrites expression in terms of factorials and binomials
c           
      d   U R                   (       d  U $ U R                  5       n/ / /nU H  n[        U[        5      (       d  M  X   R                  (       d  M/  X   R
                  (       a(  US   R                  UR                  S   /X   -  5        O(US   R                  UR                  S   /X   * -  5        SX'   M     US   (       a
  US   (       d  U $ Sn[        S5       H  nSnU[        X%   5      :  d  M  X%   U   n[        US-   [        X%   5      5       Hw  nX%   U   n	Xy-   n
XSU-
     ;   d  M  SnUSU-
     R                  U
5        X%   U	 X%   U	 U[        U
[        U5      [        U	5      :  a  UOU	5      ==   US:X  a  SOS-  ss'     O   US-  nU[        X%   5      :  a  M  M     U(       ac  [        U Vs/ s H	  o3X   -  PM     snUS    Vs/ s H  n[        U5      PM     sn-   6 [        US    Vs/ s H  n[        U5      PM     sn6 -  $ U $ s  snf s  snf s  snf )Nr      F   T)is_Mulas_powers_dictr   r   
is_Integeris_positiveextendr   rangelenremover   r   r   )rvrvdnd_fact_argskhitmiaijajsums              r   f_gamma_as_comb.<locals>.fE   sL   yyI!BxA!Y''CF,=,=,=6%% O**AFF1I;sv+=> O**AFF1I;w+>?  Al1oIqAAc,/**!_Q'q1uc,/&:;A%+B'C1q511"$QU+2237(OA.(OA.HS	" )"1."357 8&'1f!5 8  <  FA% c,/** , S1SSV)S1)!_5., 6?q\,5. . 003)!_6., 7@l,6. 1// / 	 2 5.6.s   H# H(
H-
)r   r   r   )r   r7   s     r   r   r   <   s%     <<	"D+Z Tr   N)
sympy.corer   sympy.core.functionr   sympy.core.traversalr   r   (sympy.functions.combinatorial.factorialsr   r   sympy.functionsr	   sympy.simplify.gammasimpr
   r   sympy.utilities.timeutilsr   r   r    r   r   <module>rA      s9     ) > H ! : . 
*- -`6r   