U
    ZÊ¹jÑ  ã                   @   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   úX/var/www/html/bwave/python/venv/lib/python3.8/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}|r€d}n| }|| d k �r~|| }|r®d||< ||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  |r¸d}
n| }
|
| d k �rX||
 }|
ræd||
< ||d< |dk�r
||
d   d7  < d}
n|
d7 }
||
  d7  < |d  d8  < ||ƒ}|td|ƒƒ}	|||	 fV  q¼d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   Ú_tupleZmcZmc1r   r   r   Út1Úbr   r   r   r   r   r   �   s>    


r   N)Zsympy.utilities.miscr   r   r   r   r   r   r   r   r   r   Ú<module>   s   J