�
��g � � � d dl mZ d dlmZ d dlmZmZ d dlmZm Z d dl
mZ d dlm
Z
mZ d dlmZ ed� � d � � � Zd
� ZdS )� )�Mul)� count_ops)�preorder_traversal� bottom_up)�binomial� factorial)�gamma)� gammasimp�
_gammasimp)�timethis�combsimpc �� � | � t d�� � } t d� t | � � D � � � � rt | � � S t | 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)� piecewisec 3 �f K � | ],}t |t � � o|j d j V � �-dS )r N)�
isinstancer �args�
is_integer)�.0�nodes �g/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/simplify/combsimp.py� <genexpr>zcombsimp.<locals>.<genexpr>3 sQ � � � � .� .�� �d�E�"�"�B�4�9�Q�<�+B�'B� .� .� .� .� .� .� T)�as_comb)�rewriter �anyr r
r �_gamma_as_comb)�exprs r r
r
s� � �N �<�<���<�/�/�D�
� .� .�&�t�,�,�.� .� .� .� .� �������d�d�+�+�+�D��$���D��Kr c �\ � | � t � � } d� }t | |� � S )ze
Helper function for combsimp.
Rewrites expression in terms of factorials and binomials
c
�p �
� | j s| S | � � � �
g g g}�
D ]�}t |t � � r��
| j rt�
| j r1|d � |j d g�
| z � � n1|d � |j d g�
| z � � d�
|<