a
    RG5d                     @   s4   d dl mZ dd Zdd Zdd Zefdd	Zd
S )    )as_intc                 C   sp   t | } d| fd| dfdi}d}td| d d D ]8}|| | d  | }| ||| | f< || | |f< q2|S )a  Return a dictionary containing pairs :math:`{(k1,k2) : C_kn}` where
    :math:`C_kn` are binomial coefficients and :math:`n=k1+k2`.

    Examples
    ========

    >>> from sympy.ntheory import binomial_coefficients
    >>> binomial_coefficients(9)
    {(0, 9): 1, (1, 8): 9, (2, 7): 36, (3, 6): 84,
     (4, 5): 126, (5, 4): 126, (6, 3): 84, (7, 2): 36, (8, 1): 9, (9, 0): 1}

    See Also
    ========

    binomial_coefficients_list, multinomial_coefficients
    r         r   rangendak r   U/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/ntheory/multinomial.pybinomial_coefficients   s    "r   c                 C   s^   t | } dg| d  }d}td| d d D ],}|| | d  | }| ||< || | < q,|S )aL   Return a list of binomial coefficients as rows of the Pascal's
    triangle.

    Examples
    ========

    >>> from sympy.ntheory import binomial_coefficients_list
    >>> binomial_coefficients_list(9)
    [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

    See Also
    ========

    binomial_coefficients, multinomial_coefficients
    r   r   r   r   r   r   r   binomial_coefficients_list   s    r   c           	      C   s  t | } t |}| s$|ri S ddiS | dkr4t|S | d| krV|dkrVtt| |S |gdg| d   }t|di}|rd}n| }|| d k r~|| }|rd||< ||d< |dkr||d   d7  < d}d}d}n,|d7 }|d }|t| }||  d7  < t|| D ]@}|| r||  d8  < ||t| 7 }||  d7  < q|d  d8  < || ||d   |t|< q|S )a  Return a dictionary containing pairs ``{(k1,k2,..,km) : C_kn}``
    where ``C_kn`` are multinomial coefficients such that
    ``n=k1+k2+..+km``.

    Examples
    ========

    >>> from sympy.ntheory import multinomial_coefficients
    >>> multinomial_coefficients(2, 5) # indirect doctest
    {(0, 5): 1, (1, 4): 5, (2, 3): 10, (3, 2): 10, (4, 1): 5, (5, 0): 1}

    Notes
    =====

    The algorithm is based on the following result:

    .. math::
        \binom{n}{k_1, \ldots, k_m} =
        \frac{k_1 + 1}{n - k_1} \sum_{i=2}^m \binom{n}{k_1 + 1, \ldots, k_i - 1, \ldots}

    Code contributed to Sage by Yann Laigle-Chapuy, copied with permission
    of the author.

    See Also
    ========

    binomial_coefficients_list, binomial_coefficients
    r   r   r   r   )r   r   dict!multinomial_coefficients_iteratortupler   )	mr   trjtjstartvr   r   r   r   multinomial_coefficients7   sJ    
r   c                 c   s\  t | } t |}| d| k s$|dkr@t| |}| E dH  nt||}i }| D ]\}}|||td|< qV|}|gdg| d   }||}|td|}	|||	 fV  |rd}
n| }
|
| d k rX||
 }|
rd||
< ||d< |dkr
||
d   d7  < d}
n|
d7 }
||
  d7  < |d  d8  < ||}|td|}	|||	 fV  qdS )aq  multinomial coefficient iterator

    This routine has been optimized for `m` large with respect to `n` by taking
    advantage of the fact that when the monomial tuples `t` are stripped of
    zeros, their coefficient is the same as that of the monomial tuples from
    ``multinomial_coefficients(n, n)``. Therefore, the latter coefficients are
    precomputed to save memory and time.

    >>> from sympy.ntheory.multinomial import multinomial_coefficients
    >>> m53, m33 = multinomial_coefficients(5,3), multinomial_coefficients(3,3)
    >>> m53[(0,0,0,1,2)] == m53[(0,0,1,0,2)] == m53[(1,0,2,0,0)] == m33[(0,1,2)]
    True

    Examples
    ========

    >>> from sympy.ntheory.multinomial import multinomial_coefficients_iterator
    >>> it = multinomial_coefficients_iterator(20,3)
    >>> next(it)
    ((3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 1)
    r   r   Nr   )r   r   itemsfilter)r   r   _tuplemcZmc1r   r   r   t1br   r   r   r   r   r      s>    


r   N)sympy.utilities.miscr   r   r   r   r   r   r   r   r   r   <module>   s   J