o
    oh                     @   s|   d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	 d dl
mZ d dlmZmZ d dlmZ edd	d Zd
d ZdS )    )Mul)	count_ops)preorder_traversal	bottom_up)binomial	factorial)gamma)	gammasimp
_gammasimp)timethiscombsimpc                 C   sD   | j tdd} tdd t| D rt| S t| d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                 s   s(    | ]}t |to|jd  j V  qdS )r   N)
isinstancer   args
is_integer).0node r   k/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/simplify/combsimp.py	<genexpr>3   s     zcombsimp.<locals>.<genexpr>T)as_comb)rewriter   anyr   r	   r
   _gamma_as_comb)exprr   r   r   r      s   'c                 C   s   |  t} dd }t| |S )ze
    Helper function for combsimp.

    Rewrites expression in terms of factorials and binomials
    c           
         s  | j s| S |   g g g} D ]5}t|trD | jrD | jr0|d |jd g |   n|d |jd g |    d |< q|d rM|d sO| S d}tdD ]p}d}|t	|| k r|| | }t|d t	|| D ]F}|| | }|| }	|	|d|  v rd}|d|  
|	 || |= || |=  t|	t|t|k r|n|  |dkrdnd7  <  nqr|d7 }|t	|| k saqU|rt fdd D d	d |d D   td
d |d D   S | S )Nr      F   Tc                    s   g | ]}| |  qS r   r   r   krvdr   r   
<listcomp>m   s    z-_gamma_as_comb.<locals>.f.<locals>.<listcomp>c                 S      g | ]}t |qS r   r   r   r   r   r   r"   m       c                 S   r#   r   r$   r   r   r   r   r"   n   r%   )is_Mulas_powers_dictr   r   
is_Integeris_positiveextendr   rangelenremover   r   r   )
rvnd_fact_argsr   hitmiaijajsumr   r    r   fE   s^   
  

z_gamma_as_comb.<locals>.f)r   r   r   )r   r7   r   r   r   r   <   s   

-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   r   r   <module>   s    
0