K iddlmZddlmZddlmZmZddlmZm Z ddl m Z ddl m Z mZddlmZedd Zd Zy ) )Mul) count_ops)preorder_traversal bottom_up)binomial factorial)gamma) gammasimp _gammasimp)timethiscombsimpc|jtd}tdt|Dr t |St |d}t |}|S)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) piecewisec3tK|]0}t|txr|jdj 2yw)rN) isinstancer args is_integer).0nodes ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/simplify/combsimp.py zcombsimp..3s8 .  dE " B499Q<+B+B'B B .s68T)as_comb)rewriter anyrr r _gamma_as_comb)exprs rr r sVN <<< /D  .&t, .. dd +D $ D KcJ|jt}d}t||S)ze Helper function for combsimp. Rewrites expression in terms of factorials and binomials c  |js|S|j}ggg}|D]}t|ts||js$||j r)|dj |jdg||zn)|dj |jdg|| zd||<|dr|ds|Sd}tdD]}d}|t||ks|||}t|dzt||D]w}|||} || z} | |d|z vsd}|d|z j| |||=|||=|t| t|t| kr|n| xx|dk(rdndz cc<n|dz }|t||kr|r\t|Dcgc] }|||z c}|dDcgc] }t|c}zt|dDcgc] }t|c}z S|Scc}wcc}wcc}w)NrFT)is_Mulas_powers_dictrr is_Integer is_positiveextendrrangelenremoverrr) rvrvd nd_fact_argskhitmiaijajsums rfz_gamma_as_comb..fEsgyyI!Bx  A!Y'CF,=,=q6%% O**AFF1I;s1v+=> O**AFF1I;Aw+>?A  Al1oIq AAc,q/**!!_Q'q1uc,q/&:;A%a+Br'Cl1q511"$QU+2237(OA.(OA.HS " )" 1."3578&'1f!58 FA%c,q/** , S1!SV)1)!_5.6?q\5..003)!_6.7@l6.1// / 25.6.s"G7:G< H )rrr)rr6s rrr<s' << "D+Z T1 rN) sympy.corersympy.core.functionrsympy.core.traversalrr(sympy.functions.combinatorial.factorialsrrsympy.functionsr sympy.simplify.gammasimpr r sympy.utilities.timeutilsr r rrrr?s9)>H!:. *--`6r