U
    -eE                    @   s  d Z ddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z# ddl$m%Z% ddl&m'Z'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZBmCZC ddlDmEZF dd ZGe
dZHdd ZIG dd  d eZJG d!d" d"eZKG d#d$ d$eZLG d%d& d&eZMG d'd( d(eZNG d)d* d*eZOG d+d, d,eZPG d-d. d.eZQG d/d0 d0eZRG d1d2 d2eZSG d3d4 d4eZTd5d5gZUG d6d7 d7eZVG d8d9 d9eWZXd:ZYd;ZZe[d<eZZ\d=d> Z]d\d@dAZ^ed]dBdCZ_edDdE Z`d^dFdGZadHdI ZbedJdK ZcdLdM ZdedNdO Zed_dQdRZfedSdT Zgd`dUdVZhG dWdX dXeZidad<d<dYdZd[Zjd<S )ba6  
This module implements some special functions that commonly appear in
combinatorial contexts (e.g. in power series); in particular,
sequences of rational numbers such as Bernoulli and Fibonacci numbers.

Factorials, binomial coefficients and related functions are located in
the separate 'factorials' module.
    )prod)defaultdict)Tuple)SSymbolAddDummy)cacheit)Expr)ArgumentIndexErrorFunction
expand_mul)	fuzzy_not)Mul)EIpiooRationalInteger)Eqis_leis_gt)
SYMPY_INTS)binomial	factorialsubfactoriallog)	Piecewise)isprime	is_square)bernoulli_poly
euler_polygenocchi_poly)MultisetPartitionTraversersympy_deprecation_warning)multisetmultiset_derangementsiterable)recurrence_memo)as_int)mpworkprec)ifibc                 C   s   t t| |d S N   )r   range)ab r5   f/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/sympy/functions/combinatorial/numbers.py_product&   s    r7   xc                 C   s   ||  dkS )Nr   r5   pnr5   r5   r6   _divides4   s    r<   c                   @   sX   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
dS )
carmichaela0  
    Carmichael Numbers:

    Certain cryptographic algorithms make use of big prime numbers.
    However, checking whether a big number is prime is not so easy.
    Randomized prime number checking tests exist that offer a high degree of
    confidence of accurate determination at low cost, such as the Fermat test.

    Let 'a' be a random number between $2$ and $n - 1$, where $n$ is the
    number whose primality we are testing. Then, $n$ is probably prime if it
    satisfies the modular arithmetic congruence relation:

    .. math :: a^{n-1} = 1 \pmod{n}

    (where mod refers to the modulo operation)

    If a number passes the Fermat test several times, then it is prime with a
    high probability.

    Unfortunately, certain composite numbers (non-primes) still pass the Fermat
    test with every number smaller than themselves.
    These numbers are called Carmichael numbers.

    A Carmichael number will pass a Fermat primality test to every base $b$
    relatively prime to the number, even though it is not actually prime.
    This makes tests based on Fermat's Little Theorem less effective than
    strong probable prime tests such as the Baillie-PSW primality test and
    the Miller-Rabin primality test.

    Examples
    ========

    >>> from sympy import carmichael
    >>> carmichael.find_first_n_carmichaels(5)
    [561, 1105, 1729, 2465, 2821]
    >>> carmichael.find_carmichael_numbers_in_range(0, 562)
    [561]
    >>> carmichael.find_carmichael_numbers_in_range(0,1000)
    [561]
    >>> carmichael.find_carmichael_numbers_in_range(0,2000)
    [561, 1105, 1729]

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Carmichael_number
    .. [2] https://en.wikipedia.org/wiki/Fermat_primality_test
    .. [3] https://www.jstor.org/stable/23248683?seq=1#metadata_info_tab_contents
    c                 C   s   t dddd t| S )Nzt
is_perfect_square is just a wrapper around sympy.ntheory.primetest.is_square
so use that directly instead.
        1.11$deprecated-carmichael-static-methodsZdeprecated_since_versionZactive_deprecations_target)r'   r!   r;   r5   r5   r6   is_perfect_squarej   s    zcarmichael.is_perfect_squarec                 C   s   t dddd ||  dkS )NzI
        divides can be replaced by directly testing n % p == 0.
        r>   r?   r@   r   r&   r9   r5   r5   r6   dividesv   s    zcarmichael.dividesc                 C   s   t dddd t| S )Nzi
is_prime is just a wrapper around sympy.ntheory.primetest.isprime so use that
directly instead.
        r>   r?   r@   )r'   r    rA   r5   r5   r6   is_prime   s    zcarmichael.is_primec                    s    dkr dks$t  s$ d dkr(dS d g}| fddtd d d dD  |D ]:}t|rv|dkrv dS t |r\t|d  d s\ dS q\dS td	d S )
Nr   r1      Fc                    s   g | ]} | d kr|qS r   r5   .0irA   r5   r6   
<listcomp>   s      z,carmichael.is_carmichael.<locals>.<listcomp>   Tz6The provided number must be greater than or equal to 0)r    extendr2   r!   r<   
ValueError)r;   ZdivisorsrI   r5   rA   r6   is_carmichael   s    (zcarmichael.is_carmichaelc                 C   sb   d|   kr|krVn n>| d dkr>dd t | d |dD S dd t | |dD S ntdd S )Nr   rE   c                 S   s   g | ]}t |r|qS r5   r=   rN   rG   r5   r5   r6   rJ      s     
 z?carmichael.find_carmichael_numbers_in_range.<locals>.<listcomp>r1   c                 S   s   g | ]}t |r|qS r5   rO   rG   r5   r5   r6   rJ      s     
 zQThe provided range is not valid. x and y must be non-negative integers and x <= y)r2   rM   )r8   yr5   r5   r6    find_carmichael_numbers_in_range   s
    z+carmichael.find_carmichael_numbers_in_rangec                 C   s6   d}g }t || k r2t|r(|| |d7 }q|S Nr1   rE   )lenr=   rN   append)r;   rI   Zcarmichaelsr5   r5   r6   find_first_n_carmichaels   s    


z#carmichael.find_first_n_carmichaelsN)__name__
__module____qualname____doc__staticmethodrB   rC   rD   rN   rQ   rU   r5   r5   r5   r6   r=   7   s   2






r=   c                   @   sV   e Zd ZdZedd Zeedeje	gdd Z
edddZd	d
 Zdd ZdS )	fibonaccia  
    Fibonacci numbers / Fibonacci polynomials

    The Fibonacci numbers are the integer sequence defined by the
    initial terms `F_0 = 0`, `F_1 = 1` and the two-term recurrence
    relation `F_n = F_{n-1} + F_{n-2}`.  This definition
    extended to arbitrary real and complex arguments using
    the formula

    .. math :: F_z = \frac{\phi^z - \cos(\pi z) \phi^{-z}}{\sqrt 5}

    The Fibonacci polynomials are defined by `F_1(x) = 1`,
    `F_2(x) = x`, and `F_n(x) = x*F_{n-1}(x) + F_{n-2}(x)` for `n > 2`.
    For all positive integers `n`, `F_n(1) = F_n`.

    * ``fibonacci(n)`` gives the `n^{th}` Fibonacci number, `F_n`
    * ``fibonacci(n, x)`` gives the `n^{th}` Fibonacci polynomial in `x`, `F_n(x)`

    Examples
    ========

    >>> from sympy import fibonacci, Symbol

    >>> [fibonacci(x) for x in range(11)]
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
    >>> fibonacci(5, Symbol('t'))
    t**4 + 3*t**2 + 1

    See Also
    ========

    bell, bernoulli, catalan, euler, harmonic, lucas, genocchi, partition, tribonacci

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Fibonacci_number
    .. [2] https://mathworld.wolfram.com/FibonacciNumber.html

    c                 C   s   t | S N)_ifibrA   r5   r5   r6   _fib   s    zfibonacci._fibNc                 C   s   |d t |d    S )N_symexpandr;   prevr5   r5   r6   _fibpoly   s    zfibonacci._fibpolyc                 C   s|   |t jkrt jS |jrx|d krVt|}|dk rFt j|d  t|  S t| |S n"|dk rftd| 	|
t|S d S )Nr   r1   zDFibonacci polynomials are defined only for positive integer indices.)r   Infinity
is_IntegerintNegativeOner[   r   r^   rM   rf   subsrb   clsr;   symr5   r5   r6   eval   s    
zfibonacci.evalc                 K   sD   ddl m} d|  |d d|d | |d d |   d S )Nr   sqrtrE      r1   (sympy.functions.elementary.miscellaneousrq   selfr;   kwargsrq   r5   r5   r6   _eval_rewrite_as_sqrt  s    zfibonacci._eval_rewrite_as_sqrtc                 K   s(   t j| dt j |   dt j d  S rR   )r   ZGoldenRatiorv   r;   rw   r5   r5   r6   _eval_rewrite_as_GoldenRatio  s    z&fibonacci._eval_rewrite_as_GoldenRatio)N)rV   rW   rX   rY   rZ   r^   r+   r   Onerb   rf   classmethodro   rx   rz   r5   r5   r5   r6   r[      s   )
r[   c                   @   s$   e Zd ZdZedd Zdd ZdS )lucasa  
    Lucas numbers

    Lucas numbers satisfy a recurrence relation similar to that of
    the Fibonacci sequence, in which each term is the sum of the
    preceding two. They are generated by choosing the initial
    values `L_0 = 2` and `L_1 = 1`.

    * ``lucas(n)`` gives the `n^{th}` Lucas number

    Examples
    ========

    >>> from sympy import lucas

    >>> [lucas(x) for x in range(11)]
    [2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123]

    See Also
    ========

    bell, bernoulli, catalan, euler, fibonacci, harmonic, genocchi, partition, tribonacci

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Lucas_number
    .. [2] https://mathworld.wolfram.com/LucasNumber.html

    c                 C   s2   |t jkrt jS |jr.t|d t|d  S d S r0   )r   rg   rh   r[   rm   r;   r5   r5   r6   ro   7  s    
z
lucas.evalc                 K   s8   ddl m} d|  d|d | |d d |   S )Nr   rp   rE   r1   rr   rs   ru   r5   r5   r6   rx   ?  s    zlucas._eval_rewrite_as_sqrtN)rV   rW   rX   rY   r|   ro   rx   r5   r5   r5   r6   r}     s   
r}   c                   @   sp   e Zd ZdZeeejejejgdd Z	eeejeje
d gdd Zeddd	Zd
d Zdd ZdS )
tribonaccia  
    Tribonacci numbers / Tribonacci polynomials

    The Tribonacci numbers are the integer sequence defined by the
    initial terms `T_0 = 0`, `T_1 = 1`, `T_2 = 1` and the three-term
    recurrence relation `T_n = T_{n-1} + T_{n-2} + T_{n-3}`.

    The Tribonacci polynomials are defined by `T_0(x) = 0`, `T_1(x) = 1`,
    `T_2(x) = x^2`, and `T_n(x) = x^2 T_{n-1}(x) + x T_{n-2}(x) + T_{n-3}(x)`
    for `n > 2`.  For all positive integers `n`, `T_n(1) = T_n`.

    * ``tribonacci(n)`` gives the `n^{th}` Tribonacci number, `T_n`
    * ``tribonacci(n, x)`` gives the `n^{th}` Tribonacci polynomial in `x`, `T_n(x)`

    Examples
    ========

    >>> from sympy import tribonacci, Symbol

    >>> [tribonacci(x) for x in range(11)]
    [0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149]
    >>> tribonacci(5, Symbol('t'))
    t**8 + 3*t**5 + 3*t**2

    See Also
    ========

    bell, bernoulli, catalan, euler, fibonacci, harmonic, lucas, genocchi, partition

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Tribonacci_numbers
    .. [2] https://mathworld.wolfram.com/TribonacciNumber.html
    .. [3] https://oeis.org/A000073

    c                 C   s   |d |d  |d  S )Nr_   r`   r5   rd   r5   r5   r6   _tribr  s    ztribonacci._tribrE   c                 C   s(   |d t |d   t d |d    S )Nr   r_   rE   r`   ra   rd   r5   r5   r6   	_tribpolyw  s    ztribonacci._tribpolyNc                 C   sZ   |t jkrt jS |jrVt|}|dk r.td|d krDt| |S | |t	|S d S )Nr   zITribonacci polynomials are defined only for non-negative integer indices.)
r   rg   rh   ri   rM   r   r   r   rk   rb   rl   r5   r5   r6   ro   |  s    
ztribonacci.evalc           
      K   s&  ddl m}m} dtj|d  d }d|dd|d   |dd|d   d }d||dd|d    |d |dd|d    d }d|d |dd|d    ||dd|d    d }||d  || ||   ||d  || ||    ||d  || ||    }	|	S )	Nr   cbrtrq   r`   rK   rE   r1      !   )rt   r   rq   r   ImaginaryUnit)
rv   r;   rw   r   rq   wr3   r4   cTnr5   r5   r6   rx     s    0<<z tribonacci._eval_rewrite_as_sqrtc                 K   sd   ddl m} ddlm}m} |dd|d  }d| tj|  |d d|  d	  }||tj S )
Nr   floorr   iJ  f   r   rK   rE      )#sympy.functions.elementary.integersr   rt   r   rq   r   ZTribonacciConstantHalf)rv   r;   rw   r   r   rq   r4   r   r5   r5   r6   #_eval_rewrite_as_TribonacciConstant  s
    &z.tribonacci._eval_rewrite_as_TribonacciConstant)N)rV   rW   rX   rY   rZ   r+   r   Zeror{   r   rb   r   r|   ro   rx   r   r5   r5   r5   r6   r   K  s   &r   c                   @   sp   e Zd ZU dZee ed< edd Ze	j
eddeddd	Zd
ddd	ZedddZdddZdd ZdS )	bernoulliaJ  
    Bernoulli numbers / Bernoulli polynomials / Bernoulli function

    The Bernoulli numbers are a sequence of rational numbers
    defined by `B_0 = 1` and the recursive relation (`n > 0`):

    .. math :: n+1 = \sum_{k=0}^n \binom{n+1}{k} B_k

    They are also commonly defined by their exponential generating
    function, which is `\frac{x}{1 - e^{-x}}`. For odd indices > 1,
    the Bernoulli numbers are zero.

    The Bernoulli polynomials satisfy the analogous formula:

    .. math :: B_n(x) = \sum_{k=0}^n (-1)^k \binom{n}{k} B_k x^{n-k}

    Bernoulli numbers and Bernoulli polynomials are related as
    `B_n(1) = B_n`.

    The generalized Bernoulli function `\operatorname{B}(s, a)`
    is defined for any complex `s` and `a`, except where `a` is a
    nonpositive integer and `s` is not a nonnegative integer. It is
    an entire function of `s` for fixed `a`, related to the Hurwitz
    zeta function by

    .. math:: \operatorname{B}(s, a) = \begin{cases}
              -s \zeta(1-s, a) & s \ne 0 \\ 1 & s = 0 \end{cases}

    When `s` is a nonnegative integer this function reduces to the
    Bernoulli polynomials: `\operatorname{B}(n, x) = B_n(x)`. When
    `a` is omitted it is assumed to be 1, yielding the (ordinary)
    Bernoulli function which interpolates the Bernoulli numbers and is
    related to the Riemann zeta function.

    We compute Bernoulli numbers using Ramanujan's formula:

    .. math :: B_n = \frac{A(n) - S(n)}{\binom{n+3}{n}}

    where:

    .. math :: A(n) = \begin{cases} \frac{n+3}{3} &
        n \equiv 0\ \text{or}\ 2 \pmod{6} \\
        -\frac{n+3}{6} & n \equiv 4 \pmod{6} \end{cases}

    and:

    .. math :: S(n) = \sum_{k=1}^{[n/6]} \binom{n+3}{n-6k} B_{n-6k}

    This formula is similar to the sum given in the definition, but
    cuts `\frac{2}{3}` of the terms. For Bernoulli polynomials, we use
    Appell sequences.

    For `n` a nonnegative integer and `s`, `a`, `x` arbitrary complex numbers,

    * ``bernoulli(n)`` gives the nth Bernoulli number, `B_n`
    * ``bernoulli(s)`` gives the Bernoulli function `\operatorname{B}(s)`
    * ``bernoulli(n, x)`` gives the nth Bernoulli polynomial in `x`, `B_n(x)`
    * ``bernoulli(s, a)`` gives the generalized Bernoulli function
      `\operatorname{B}(s, a)`

    .. versionchanged:: 1.12
        ``bernoulli(1)`` gives `+\frac{1}{2}` instead of `-\frac{1}{2}`.
        This choice of value confers several theoretical advantages [5]_,
        including the extension to complex parameters described above
        which this function now implements. The previous behavior, defined
        only for nonnegative integers `n`, can be obtained with
        ``(-1)**n*bernoulli(n)``.

    Examples
    ========

    >>> from sympy import bernoulli
    >>> from sympy.abc import x
    >>> [bernoulli(n) for n in range(11)]
    [1, 1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66]
    >>> bernoulli(1000001)
    0
    >>> bernoulli(3, x)
    x**3 - 3*x**2/2 + x/2

    See Also
    ========

    andre, bell, catalan, euler, fibonacci, harmonic, lucas, genocchi,
    partition, tribonacci, sympy.polys.appellseqs.bernoulli_poly

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Bernoulli_number
    .. [2] https://en.wikipedia.org/wiki/Bernoulli_polynomial
    .. [3] https://mathworld.wolfram.com/BernoulliNumber.html
    .. [4] https://mathworld.wolfram.com/BernoulliPolynomial.html
    .. [5] Peter Luschny, "The Bernoulli Manifesto",
           https://luschny.de/math/zeta/The-Bernoulli-Manifesto.html
    .. [6] Peter Luschny, "An introduction to the Bernoulli function",
           https://arxiv.org/abs/2009.06743

    argsc                 C   s   d}t t| d | d }td| d d D ]`}||t| d|   7 }|t| d d|  d | d|  9 }|td| d d| d  }q,| d dkrt| d d | }nt| d d| }|t| d |  S )Nr   rK      r1   r   	   )ri   r   r2   r   r7   r   )r;   sr3   jr5   r5   r6   _calc_bernoulli  s    & zbernoulli._calc_bernoullir1   r   r`      )r   rE   r   r   rE   r   Nc           	      C   s0  |t jkr| |S |jrt jS |jdks2|jdkrP|d k	rL|jrL|jrLt jS d S |d kr|t jkrjt jS |j	r|d j
rt jS |jr,t|}|dkrt|\}}tt|t|S |d }| j| }||kr| j| S t|d |d dD ]"}| |}|| j|< || j|< q|S n|jr,t||S d S )NFr1   i  r   )r   r{   is_zero
is_integeris_nonnegativerh   is_nonpositiveNaNr   is_oddis_positiver   	is_Numberri   r-   Zbernfracr   _highest_cacher2   r   r"   )	rm   r;   r8   r:   qcaseZhighest_cachedrI   r4   r5   r5   r6   ro   !  s:    






zbernoulli.evalc                 K   s4   ddl m} tdt|df| |d| | dfS )Nr   zetar1   T)&sympy.functions.special.zeta_functionsr   r   r   )rv   r;   r8   rw   r   r5   r5   r6   _eval_rewrite_as_zetaG  s    zbernoulli._eval_rewrite_as_zetac              	   C   s   t dd | jD sd S | jd |}t| jdkr@| jd ntj|}t|z |dkrjtd}n`|dkr|td }nHt	|r|dkr|dkrt
|n
t||}n| td| | }W 5 Q R X t||S )Nc                 s   s   | ]}|j V  qd S r\   	is_numberrH   r8   r5   r5   r6   	<genexpr>L  s     z(bernoulli._eval_evalf.<locals>.<genexpr>r   r1         ?)allr   
_to_mpmathrS   r   r{   r.   r-   Zmpfisintr   Zbernpolyr   r
   _from_mpmath)rv   precr;   r8   resr5   r5   r6   _eval_evalfK  s    $
  zbernoulli._eval_evalf)N)r1   )rV   rW   rX   rY   tTupler   __annotations__rZ   r   r   r{   r   r   r   r|   ro   r   r   r5   r5   r5   r6   r     s   
d
%
r   c                   @   sf   e Zd ZdZeeddgdd Zeeeje	gdd Z
edd Zedd
dZdddZd	S )bellaz  
    Bell numbers / Bell polynomials

    The Bell numbers satisfy `B_0 = 1` and

    .. math:: B_n = \sum_{k=0}^{n-1} \binom{n-1}{k} B_k.

    They are also given by:

    .. math:: B_n = \frac{1}{e} \sum_{k=0}^{\infty} \frac{k^n}{k!}.

    The Bell polynomials are given by `B_0(x) = 1` and

    .. math:: B_n(x) = x \sum_{k=1}^{n-1} \binom{n-1}{k-1} B_{k-1}(x).

    The second kind of Bell polynomials (are sometimes called "partial" Bell
    polynomials or incomplete Bell polynomials) are defined as

    .. math:: B_{n,k}(x_1, x_2,\dotsc x_{n-k+1}) =
            \sum_{j_1+j_2+j_2+\dotsb=k \atop j_1+2j_2+3j_2+\dotsb=n}
                \frac{n!}{j_1!j_2!\dotsb j_{n-k+1}!}
                \left(\frac{x_1}{1!} \right)^{j_1}
                \left(\frac{x_2}{2!} \right)^{j_2} \dotsb
                \left(\frac{x_{n-k+1}}{(n-k+1)!} \right) ^{j_{n-k+1}}.

    * ``bell(n)`` gives the `n^{th}` Bell number, `B_n`.
    * ``bell(n, x)`` gives the `n^{th}` Bell polynomial, `B_n(x)`.
    * ``bell(n, k, (x1, x2, ...))`` gives Bell polynomials of the second kind,
      `B_{n,k}(x_1, x_2, \dotsc, x_{n-k+1})`.

    Notes
    =====

    Not to be confused with Bernoulli numbers and Bernoulli polynomials,
    which use the same notation.

    Examples
    ========

    >>> from sympy import bell, Symbol, symbols

    >>> [bell(n) for n in range(11)]
    [1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975]
    >>> bell(30)
    846749014511809332450147
    >>> bell(4, Symbol('t'))
    t**4 + 6*t**3 + 7*t**2 + t
    >>> bell(6, 2, symbols('x:6')[1:])
    6*x1*x5 + 15*x2*x4 + 10*x3**2

    See Also
    ========

    bernoulli, catalan, euler, fibonacci, harmonic, lucas, genocchi, partition, tribonacci

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Bell_number
    .. [2] https://mathworld.wolfram.com/BellNumber.html
    .. [3] https://mathworld.wolfram.com/BellPolynomial.html

    r1   c                 C   s<   d}d}t d| D ]$}|| |  | }||||  7 }q|S r0   )r2   r;   re   r   r3   kr5   r5   r6   _bell  s    z
bell._bellc                 C   sT   d}d}t d| d D ]0}|| | d  |d  }||||d   7 }qtt| S rR   )r2   r   rb   r   r5   r5   r6   
_bell_poly  s    zbell._bell_polyc                 C   s   | dkr|dkrt jS | dks&|dkr,t jS t j}t j}td| | d D ]>}||t| | |d | ||d   7 }|| |  | }qJt|S )a  
        The second kind of Bell polynomials (incomplete Bell polynomials).

        Calculated by recurrence formula:

        .. math:: B_{n,k}(x_1, x_2, \dotsc, x_{n-k+1}) =
                \sum_{m=1}^{n-k+1}
                \x_m \binom{n-1}{m-1} B_{n-m,k-1}(x_1, x_2, \dotsc, x_{n-m-k})

        where
            `B_{0,0} = 1;`
            `B_{n,0} = 0; for n \ge 1`
            `B_{0,k} = 0; for k \ge 1`

        r   r1   rE   )r   r{   r   r2   r   _bell_incomplete_polyr   )r;   r   symbolsr   r3   mr5   r5   r6   r     s       
zbell._bell_incomplete_polyNc                 C   s   |t jkr |d krt jS td|js0|jdkr8td|jr|jr|d kr^t| t	|S |d kr|| 
t	|t|S | t	|t	||}|S d S )NzBell polynomial is not definedFza non-negative integer expected)r   rg   rM   is_negativer   rh   r   r   r   ri   r   rk   rb   r   )rm   r;   k_symr   rr5   r5   r6   ro     s    
z	bell.evalc                 K   s^   ddl m} |d k	s|d k	r | S |js*| S tdddd}dt ||| t| |dtjf S )Nr   Sumr   T)integerZnonnegativer1   )sympy.concrete.summationsr   r   r   r   r   r   rg   )rv   r;   r   r   rw   r   r   r5   r5   r6   _eval_rewrite_as_Sum  s    zbell._eval_rewrite_as_Sum)NN)NN)rV   rW   rX   rY   rZ   r+   r   r   r{   rb   r   r   r|   ro   r   r5   r5   r5   r6   r   c  s   @

r   c                   @   s|   e Zd ZdZedddZejfddZddd	Z	dd
dZ
dddZejfddZdd ZdddZdd ZdddZdS )harmonica  
    Harmonic numbers

    The nth harmonic number is given by `\operatorname{H}_{n} =
    1 + \frac{1}{2} + \frac{1}{3} + \ldots + \frac{1}{n}`.

    More generally:

    .. math:: \operatorname{H}_{n,m} = \sum_{k=1}^{n} \frac{1}{k^m}

    As `n \rightarrow \infty`, `\operatorname{H}_{n,m} \rightarrow \zeta(m)`,
    the Riemann zeta function.

    * ``harmonic(n)`` gives the nth harmonic number, `\operatorname{H}_n`

    * ``harmonic(n, m)`` gives the nth generalized harmonic number
      of order `m`, `\operatorname{H}_{n,m}`, where
      ``harmonic(n) == harmonic(n, 1)``

    This function can be extended to complex `n` and `m` where `n` is not a
    negative integer or `m` is a nonpositive integer as

    .. math:: \operatorname{H}_{n,m} = \begin{cases} \zeta(m) - \zeta(m, n+1)
            & m \ne 1 \\ \psi(n+1) + \gamma & m = 1 \end{cases}

    Examples
    ========

    >>> from sympy import harmonic, oo

    >>> [harmonic(n) for n in range(6)]
    [0, 1, 3/2, 11/6, 25/12, 137/60]
    >>> [harmonic(n, 2) for n in range(6)]
    [0, 1, 5/4, 49/36, 205/144, 5269/3600]
    >>> harmonic(oo, 2)
    pi**2/6

    >>> from sympy import Symbol, Sum
    >>> n = Symbol("n")

    >>> harmonic(n).rewrite(Sum)
    Sum(1/_k, (_k, 1, n))

    We can evaluate harmonic numbers for all integral and positive
    rational arguments:

    >>> from sympy import S, expand_func, simplify
    >>> harmonic(8)
    761/280
    >>> harmonic(11)
    83711/27720

    >>> H = harmonic(1/S(3))
    >>> H
    harmonic(1/3)
    >>> He = expand_func(H)
    >>> He
    -log(6) - sqrt(3)*pi/6 + 2*Sum(log(sin(_k*pi/3))*cos(2*_k*pi/3), (_k, 1, 1))
                           + 3*Sum(1/(3*_k + 1), (_k, 0, 0))
    >>> He.doit()
    -log(6) - sqrt(3)*pi/6 - log(sqrt(3)/2) + 3
    >>> H = harmonic(25/S(7))
    >>> He = simplify(expand_func(H).doit())
    >>> He
    log(sin(2*pi/7)**(2*cos(16*pi/7))/(14*sin(pi/7)**(2*cos(pi/7))*cos(pi/14)**(2*sin(pi/14)))) + pi*tan(pi/14)/2 + 30247/9900
    >>> He.n(40)
    1.983697455232980674869851942390639915940
    >>> harmonic(25/S(7)).n(40)
    1.983697455232980674869851942390639915940

    We can rewrite harmonic numbers in terms of polygamma functions:

    >>> from sympy import digamma, polygamma
    >>> m = Symbol("m", integer=True, positive=True)

    >>> harmonic(n).rewrite(digamma)
    polygamma(0, n + 1) + EulerGamma

    >>> harmonic(n).rewrite(polygamma)
    polygamma(0, n + 1) + EulerGamma

    >>> harmonic(n,3).rewrite(polygamma)
    polygamma(2, n + 1)/2 + zeta(3)

    >>> simplify(harmonic(n,m).rewrite(polygamma))
    Piecewise((polygamma(0, n + 1) + EulerGamma, Eq(m, 1)),
    (-(-1)**m*polygamma(m - 1, n + 1)/factorial(m - 1) + zeta(m), True))

    Integer offsets in the argument can be pulled out:

    >>> from sympy import expand_func

    >>> expand_func(harmonic(n+4))
    harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1)

    >>> expand_func(harmonic(n-4))
    harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n

    Some limits can be computed as well:

    >>> from sympy import limit, oo

    >>> limit(harmonic(n), n, oo)
    oo

    >>> limit(harmonic(n, 2), n, oo)
    pi**2/6

    >>> limit(harmonic(n, 3), n, oo)
    zeta(3)

    For `m > 1`, `H_{n,m}` tends to `\zeta(m)` in the limit of infinite `n`:

    >>> m = Symbol("m", positive=True)
    >>> limit(harmonic(n, m+1), n, oo)
    zeta(m + 1)

    See Also
    ========

    bell, bernoulli, catalan, euler, fibonacci, lucas, genocchi, partition, tribonacci

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Harmonic_number
    .. [2] https://functions.wolfram.com/GammaBetaErf/HarmonicNumber/
    .. [3] https://functions.wolfram.com/GammaBetaErf/HarmonicNumber2/

    Nc                    s  ddl m}  tjkr| |S  d kr,tj |jr8tjS  jrB|S |tjkr jrXtjS t	 tjrjtjS t
 tjr~| S n jr jrtd  |d td   d   S |jr|jr jdksԈ jdkr tjkrtjS tjS |jrt fddtdt|d D  S d S )Nr   r   r1   Fc                 3   s   | ]}|   V  qd S r\   r5   rH   r   r   r5   r6   r     s     z harmonic.eval.<locals>.<genexpr>)r   r   r   r{   r   r   rg   r   r   r   r   rh   r   r   r   ZComplexInfinityr   r   r2   ri   )rm   r;   r   r   r5   r   r6   ro     s.    


&zharmonic.evalc                 K   sv   ddl m}m} |jrr|jrrt|d|d tj t|dftj	| ||d d||d |d   || dfS d S )Nr   )gamma	polygammar1   T)
'sympy.functions.special.gamma_functionsr   r   r   r   r   r   
EulerGammar   rj   )rv   r;   r   rw   r   r   r5   r5   r6   _eval_rewrite_as_polygamma  s    (z#harmonic._eval_rewrite_as_polygammar1   c                 K   s   ddl m} | |S Nr   r   r   r   rewriterv   r;   r   rw   r   r5   r5   r6   _eval_rewrite_as_digamma  s    z!harmonic._eval_rewrite_as_digammac                 K   s   ddl m} | |S r   r   r   r5   r5   r6   _eval_rewrite_as_trigamma  s    z"harmonic._eval_rewrite_as_trigammac                 K   s<   ddl m} tddd}|d kr&tj}|||  |d|fS )Nr   r   r   Tr   r1   )r   r   r   r   r{   )rv   r;   r   rw   r   r   r5   r5   r6   r     s
    zharmonic._eval_rewrite_as_Sumc                 K   sP   ddl m} ddlm} t||d tj t|df|||||d  dfS )Nr   r   digammar1   T)r   r   r   r   r   r   r   r   )rv   r;   r   rw   r   r   r5   r5   r6   r     s
    zharmonic._eval_rewrite_as_zetac              	      s  ddl m} | jd }t| jdkr.| jd nd}|tjkr|jr|jd }||  |jr|jr fddt	|ddD t
 g }t| S |jr|jrƇ fddt	d|dD t
 g }t| S |jr| \}}|| }	||	|  }|	jr|jr|jr||k rdd	lm}
 dd
lm} ddlm}m}m} td}||d|| |  |d|	f }d||dt | | t| |
|t| t|  |d||d td f }td |t| |  |
d|  }|| | S | S )Nr   r   rE   r1   c                    s   g | ]}t j |  qS r5   r   r{   rG   Znnewr5   r6   rJ     s     z.harmonic._eval_expand_func.<locals>.<listcomp>r`   c                    s   g | ]}t j  |  qS r5   r   rG   r   r5   r6   rJ     s     r   r   )sincoscotr   )r   r   r   rS   r   r{   Zis_Addrh   r   r2   r   r   r   Zis_RationalZas_numer_denomr   &sympy.functions.elementary.exponentialr   r   r   (sympy.functions.elementary.trigonometricr   r   r   r   r   )rv   hintsr   r;   r   offresultr:   r   ur   r   r   r   r   r   t1t2t3r5   r   r6   _eval_expand_func  s>    

$$" $zharmonic._eval_expand_funcc           	      K   sn   ddl m} ddlm} | |}t|ts:|jdddS |tj }|j	rj|||||d  jdddS d S )Nr   r   r   Z	tractableT)deepr1   )
r   r   r   r   r   
isinstancer   r   r{   Z
is_nonzero)	rv   r;   r   Zlimitvarrw   r   r   Zpgargr5   r5   r6   _eval_rewrite_as_tractable  s    


z#harmonic._eval_rewrite_as_tractablec              	   C   s   t dd | jD sd S | jd |}t| jdkr@| jd ntj|}t|rd|dk rdtjS t	|4 |dkrt
|}nt|t||d  }W 5 Q R X t||S )Nc                 s   s   | ]}|j V  qd S r\   r   r   r5   r5   r6   r     s     z'harmonic._eval_evalf.<locals>.<genexpr>r   r1   )r   r   r   rS   r   r{   r-   r   r   r.   r   r   r
   r   )rv   r   r;   r   r   r5   r5   r6   r     s    $
$zharmonic._eval_evalfc                 C   sZ   ddl m} t| jdkr&| j\}}n| jd \}}|dkrR|||d |d  S td S )Nr   r   rE   r1   r1   )r   r   rS   r   r   )rv   argindexr   r;   r   r5   r5   r6   fdiff  s    zharmonic.fdiff)N)r1   )r1   )N)r1   N)r1   )rV   rW   rX   rY   r|   ro   r   r{   r   r   r   r   r   r   r   r   r   r5   r5   r5   r6   r     s    


$

r   c                   @   s:   e Zd ZdZedddZdddZdddZd	d
 ZdS )eulera8	  
    Euler numbers / Euler polynomials / Euler function

    The Euler numbers are given by:

    .. math:: E_{2n} = I \sum_{k=1}^{2n+1} \sum_{j=0}^k \binom{k}{j}
        \frac{(-1)^j (k-2j)^{2n+1}}{2^k I^k k}

    .. math:: E_{2n+1} = 0

    Euler numbers and Euler polynomials are related by

    .. math:: E_n = 2^n E_n\left(\frac{1}{2}\right).

    We compute symbolic Euler polynomials using Appell sequences,
    but numerical evaluation of the Euler polynomial is computed
    more efficiently (and more accurately) using the mpmath library.

    The Euler polynomials are special cases of the generalized Euler function,
    related to the Genocchi function as

    .. math:: \operatorname{E}(s, a) = -\frac{\operatorname{G}(s+1, a)}{s+1}

    with the limit of `\psi\left(\frac{a+1}{2}\right) - \psi\left(\frac{a}{2}\right)`
    being taken when `s = -1`. The (ordinary) Euler function interpolating
    the Euler numbers is then obtained as
    `\operatorname{E}(s) = 2^s \operatorname{E}\left(s, \frac{1}{2}\right)`.

    * ``euler(n)`` gives the nth Euler number `E_n`.
    * ``euler(s)`` gives the Euler function `\operatorname{E}(s)`.
    * ``euler(n, x)`` gives the nth Euler polynomial `E_n(x)`.
    * ``euler(s, a)`` gives the generalized Euler function `\operatorname{E}(s, a)`.

    Examples
    ========

    >>> from sympy import euler, Symbol, S
    >>> [euler(n) for n in range(10)]
    [1, 0, -1, 0, 5, 0, -61, 0, 1385, 0]
    >>> [2**n*euler(n,1) for n in range(10)]
    [1, 1, 0, -2, 0, 16, 0, -272, 0, 7936]
    >>> n = Symbol("n")
    >>> euler(n + 2*n)
    euler(3*n)

    >>> x = Symbol("x")
    >>> euler(n, x)
    euler(n, x)

    >>> euler(0, x)
    1
    >>> euler(1, x)
    x - 1/2
    >>> euler(2, x)
    x**2 - x
    >>> euler(3, x)
    x**3 - 3*x**2/2 + 1/4
    >>> euler(4, x)
    x**4 - 2*x**3 + x

    >>> euler(12, S.Half)
    2702765/4096
    >>> euler(12)
    2702765

    See Also
    ========

    andre, bell, bernoulli, catalan, fibonacci, harmonic, lucas, genocchi,
    partition, tribonacci, sympy.polys.appellseqs.euler_poly

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Euler_numbers
    .. [2] https://mathworld.wolfram.com/EulerNumber.html
    .. [3] https://en.wikipedia.org/wiki/Alternating_permutation
    .. [4] https://mathworld.wolfram.com/AlternatingPermutation.html

    Nc                 C   s   |j rtjS |tjkrP|d kr(tjd S ddlm} ||d d ||d  S |jdksd|jdkrhd S |d kr|j	r|j
rtjS |jrddlm} ||j}|j|dd}t|S n|jrt||S d S )	NrE   r   r   r1   Fr-   T)exact)r   r   r{   rj   Pir   r   r   r   r   r   r   r   mpmathr-   r   r   eulernumr   r#   )rm   r;   r8   r   r-   r   r5   r5   r6   ro   W  s&    


z
euler.evalc                 K   s   ddl m} |d kr|jrtddd}tddd}|d }tj||t||tj| |d|  d| d    d| tj|  |  |d|f|dd| d f }|S |rtddd}|t||t| d|  |tj	 ||   |d|fS d S )	Nr   r   r   Tr   r   rE   r1   )
r   r   is_evenr   r   r   r   rj   r   r   )rv   r;   r8   rw   r   r   r   ZEmr5   r5   r6   r   o  s$    zeuler._eval_rewrite_as_Sumc                 K   s   |d krDt tjd t|dfd|  t|d tj |d  dfS ddlm} t ||d d ||d  t|dft|d | |d  dfS )NrE   r`   r1   Tr   r   )r   r   r   r   genocchir   r   r   )rv   r;   r8   rw   r   r5   r5   r6   _eval_rewrite_as_genocchi}  s    $&zeuler._eval_rewrite_as_genocchic              	   C   sJ  t dd | jD sd S ddlm} t| jdkr@| jd d fn| j\}}||}|d k	rf||}t| ||r|dkr|d kr||n
|	||}n|dkr|d kr|j
n||d d ||d  }nD|d krdn|}d|| |d|d  || |d d    }|d kr4|d| 9 }W 5 Q R X t||S )	Nc                 s   s   | ]}|j V  qd S r\   r   rG   r5   r5   r6   r     s     z$euler._eval_evalf.<locals>.<genexpr>r   r   r1   r`   rE   r   )r   r   r   r-   rS   r   r.   r   r   Z	eulerpolyr   r   r   r
   r   )rv   r   r-   r   r8   r   rP   r5   r5   r6   r     s"    &


 04
zeuler._eval_evalf)N)N)N)	rV   rW   rX   rY   r|   ro   r   r   r   r5   r5   r5   r6   r     s   Q

r   c                   @   sp   e Zd ZdZedd ZdddZdd Zd	d
 ZdddZ	dd Z
dd Zdd Zdd Zdd Zdd ZdS )catalanaE  
    Catalan numbers

    The `n^{th}` catalan number is given by:

    .. math :: C_n = \frac{1}{n+1} \binom{2n}{n}

    * ``catalan(n)`` gives the `n^{th}` Catalan number, `C_n`

    Examples
    ========

    >>> from sympy import (Symbol, binomial, gamma, hyper,
    ...     catalan, diff, combsimp, Rational, I)

    >>> [catalan(i) for i in range(1,10)]
    [1, 2, 5, 14, 42, 132, 429, 1430, 4862]

    >>> n = Symbol("n", integer=True)

    >>> catalan(n)
    catalan(n)

    Catalan numbers can be transformed into several other, identical
    expressions involving other mathematical functions

    >>> catalan(n).rewrite(binomial)
    binomial(2*n, n)/(n + 1)

    >>> catalan(n).rewrite(gamma)
    4**n*gamma(n + 1/2)/(sqrt(pi)*gamma(n + 2))

    >>> catalan(n).rewrite(hyper)
    hyper((1 - n, -n), (2,), 1)

    For some non-integer values of n we can get closed form
    expressions by rewriting in terms of gamma functions:

    >>> catalan(Rational(1, 2)).rewrite(gamma)
    8/(3*pi)

    We can differentiate the Catalan numbers C(n) interpreted as a
    continuous real function in n:

    >>> diff(catalan(n), n)
    (polygamma(0, n + 1/2) - polygamma(0, n + 2) + log(4))*catalan(n)

    As a more advanced example consider the following ratio
    between consecutive numbers:

    >>> combsimp((catalan(n + 1)/catalan(n)).rewrite(binomial))
    2*(2*n + 1)/(n + 2)

    The Catalan numbers can be generalized to complex numbers:

    >>> catalan(I).rewrite(gamma)
    4**I*gamma(1/2 + I)/(sqrt(pi)*gamma(2 + I))

    and evaluated with arbitrary precision:

    >>> catalan(I).evalf(20)
    0.39764993382373624267 - 0.020884341620842555705*I

    See Also
    ========

    andre, bell, bernoulli, euler, fibonacci, harmonic, lucas, genocchi,
    partition, tribonacci, sympy.functions.combinatorial.factorials.binomial

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Catalan_number
    .. [2] https://mathworld.wolfram.com/CatalanNumber.html
    .. [3] https://functions.wolfram.com/GammaBetaErf/CatalanNumber/
    .. [4] http://geometer.org/mathcircles/catalan.pdf

    c                 C   s   ddl m} |jr|js$|jrP|jrPd| ||tj  |tj||d   S |jr|jr|d jrltj	S |d j
rtddS d S )Nr   r   r   rE   r1   r`   )r   r   rh   r   Zis_nonintegerr   r   r   r   r   r   r   )rm   r;   r   r5   r5   r6   ro     s    ,

zcatalan.evalr1   c                 C   sP   ddl m} ddlm} | jd }t||d|tj |d|d  |d  S )Nr   r   r   rE   r   )r   r   r   r   r   r   r   r   )rv   r   r   r   r;   r5   r5   r6   r     s    
zcatalan.fdiffc                 K   s   t d| ||d  S NrE   r1   )r   ry   r5   r5   r6   _eval_rewrite_as_binomial  s    z!catalan._eval_rewrite_as_binomialc                 K   s    t d| t |d t |  S r   r   ry   r5   r5   r6   _eval_rewrite_as_factorial  s    z"catalan._eval_rewrite_as_factorialTc                 K   s8   ddl m} d| ||tj  |tj||d   S )Nr   r   r   rE   )r   r   r   r   )rv   r;   Z	piecewiserw   r   r5   r5   r6   _eval_rewrite_as_gamma  s    zcatalan._eval_rewrite_as_gammac                 K   s$   ddl m} |d| | gdgdS )Nr   )hyperr1   rE   )Zsympy.functions.special.hyperr  )rv   r;   rw   r  r5   r5   r6   _eval_rewrite_as_hyper  s    zcatalan._eval_rewrite_as_hyperc                 K   sB   ddl m} |jr|js| S tdddd}||| | |d|fS )Nr   )Productr   T)r   ZpositiverE   )Zsympy.concrete.productsr  r   r   r   )rv   r;   rw   r  r   r5   r5   r6   _eval_rewrite_as_Product  s
    z catalan._eval_rewrite_as_Productc                 C   s    | j d jr| j d jrdS d S Nr   Tr   r   r   rv   r5   r5   r6   _eval_is_integer  s    zcatalan._eval_is_integerc                 C   s   | j d jrdS d S r  r   r   r  r5   r5   r6   _eval_is_positive  s    zcatalan._eval_is_positivec                 C   s$   | j d jr | j d d jr dS d S )Nr   rK   T)r   r   r   r  r5   r5   r6   _eval_is_composite#  s    zcatalan._eval_is_compositec                 C   s,   ddl m} | jd jr(| ||S d S )Nr   r   )r   r   r   r   r   r   )rv   r   r   r5   r5   r6   r   '  s    zcatalan._eval_evalfN)r1   )T)rV   rW   rX   rY   r|   ro   r   r   r   r   r  r  r  r
  r  r   r5   r5   r5   r6   r     s   O


r   c                   @   sj   e Zd ZdZedddZdddZddd	Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )r   aY  
    Genocchi numbers / Genocchi polynomials / Genocchi function

    The Genocchi numbers are a sequence of integers `G_n` that satisfy the
    relation:

    .. math:: \frac{-2t}{1 + e^{-t}} = \sum_{n=0}^\infty \frac{G_n t^n}{n!}

    They are related to the Bernoulli numbers by

    .. math:: G_n = 2 (1 - 2^n) B_n

    and generalize like the Bernoulli numbers to the Genocchi polynomials and
    function as

    .. math:: \operatorname{G}(s, a) = 2 \left(\operatorname{B}(s, a) -
              2^s \operatorname{B}\left(s, \frac{a+1}{2}\right)\right)

    .. versionchanged:: 1.12
        ``genocchi(1)`` gives `-1` instead of `1`.

    Examples
    ========

    >>> from sympy import genocchi, Symbol
    >>> [genocchi(n) for n in range(9)]
    [0, -1, -1, 0, 1, 0, -3, 0, 17]
    >>> n = Symbol('n', integer=True, positive=True)
    >>> genocchi(2*n + 1)
    0
    >>> x = Symbol('x')
    >>> genocchi(4, x)
    -4*x**3 + 6*x**2 - 1

    See Also
    ========

    bell, bernoulli, catalan, euler, fibonacci, harmonic, lucas, partition, tribonacci
    sympy.polys.appellseqs.genocchi_poly

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Genocchi_number
    .. [2] https://mathworld.wolfram.com/GenocchiNumber.html
    .. [3] Peter Luschny, "An introduction to the Bernoulli function",
           https://arxiv.org/abs/2009.06743

    Nc                 C   s   |t jkr| |S |jdks&|jdkr*d S |d krl|jrH|d jrHt jS |jr|ddt d|   t| S n|jr|t	||S d S )NFr1   rE   )
r   r{   r   r   r   r   r   r   r   r$   )rm   r;   r8   r5   r5   r6   ro   g  s    
zgenocchi.evalr1   c                 K   sX   |dkr0|j r0|jr0ddtd|   t| S dt||d| t||d d    S rR   )r   r   r   r   )rv   r;   r8   rw   r5   r5   r6   _eval_rewrite_as_bernoulliw  s    z#genocchi._eval_rewrite_as_bernoullic                 K   s"   ddl m} d| |d| | S )Nr   )dirichlet_etar_   r1   )r   r  )rv   r;   r8   rw   r  r5   r5   r6   _eval_rewrite_as_dirichlet_eta|  s    z'genocchi._eval_rewrite_as_dirichlet_etac                 C   s>   t | jdkr | jd dkr d S | jd }|jr:|jr:dS d S )Nr1   r   T)rS   r   r   r   rv   r;   r5   r5   r6   r    s
    
zgenocchi._eval_is_integerc                 C   sX   t | jdkr | jd dkr d S | jd }|jrT|jrT|jrJt|d jS |d jS d S )Nr1   r   rE   )rS   r   r   r   r   r   r   r  r5   r5   r6   _eval_is_negative  s    
zgenocchi._eval_is_negativec                 C   sT   t | jdkr | jd dkr d S | jd }|jrP|jrP|jsB|jrFdS |d jS d S )Nr1   r   FrE   )rS   r   r   r   r   r   r   r  r5   r5   r6   r
    s    
zgenocchi._eval_is_positivec                 C   sP   t | jdkr | jd dkr d S | jd }|jrL|jrL|jrB|jS |d jS d S Nr1   r   )rS   r   r   r   r   r   r   r  r5   r5   r6   _eval_is_even  s    
zgenocchi._eval_is_evenc                 C   sX   t | jdkr | jd dkr d S | jd }|jrT|jrT|jrFt|jS t|d jS d S r  )rS   r   r   r   r   r   r   r   r  r5   r5   r6   _eval_is_odd  s    

zgenocchi._eval_is_oddc                 C   s4   t | jdkr | jd dkr d S | jd }|d jS )Nr1   r      )rS   r   r   r  r5   r5   r6   _eval_is_prime  s    
zgenocchi._eval_is_primec                 C   s(   t dd | jD r$| t|S d S )Nc                 s   s   | ]}|j V  qd S r\   r   rG   r5   r5   r6   r     s     z'genocchi._eval_evalf.<locals>.<genexpr>)r   r   r   r   r   )rv   r   r5   r5   r6   r     s    zgenocchi._eval_evalf)N)r1   )r1   )rV   rW   rX   rY   r|   ro   r  r  r  r  r
  r  r  r  r   r5   r5   r5   r6   r   4  s   2

					r   c                   @   sD   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dd Z
dS )andrea  
    Andre numbers / Andre function

    The Andre number `\mathcal{A}_n` is Luschny's name for half the number of
    *alternating permutations* on `n` elements, where a permutation is alternating
    if adjacent elements alternately compare "greater" and "smaller" going from
    left to right. For example, `2 < 3 > 1 < 4` is an alternating permutation.

    This sequence is A000111 in the OEIS, which assigns the names *up/down numbers*
    and *Euler zigzag numbers*. It satisfies a recurrence relation similar to that
    for the Catalan numbers, with `\mathcal{A}_0 = 1` and

    .. math:: 2 \mathcal{A}_{n+1} = \sum_{k=0}^n \binom{n}{k} \mathcal{A}_k \mathcal{A}_{n-k}

    The Bernoulli and Euler numbers are signed transformations of the odd- and
    even-indexed elements of this sequence respectively:

    .. math :: \operatorname{B}_{2k} = \frac{2k \mathcal{A}_{2k-1}}{(-4)^k - (-16)^k}

    .. math :: \operatorname{E}_{2k} = (-1)^k \mathcal{A}_{2k}

    Like the Bernoulli and Euler numbers, the Andre numbers are interpolated by the
    entire Andre function:

    .. math :: \mathcal{A}(s) = (-i)^{s+1} \operatorname{Li}_{-s}(i) +
            i^{s+1} \operatorname{Li}_{-s}(-i) = \\ \frac{2 \Gamma(s+1)}{(2\pi)^{s+1}}
            (\zeta(s+1, 1/4) - \zeta(s+1, 3/4) \cos{\pi s})

    Examples
    ========

    >>> from sympy import andre, euler, bernoulli
    >>> [andre(n) for n in range(11)]
    [1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521]
    >>> [(-1)**k * andre(2*k) for k in range(7)]
    [1, -1, 5, -61, 1385, -50521, 2702765]
    >>> [euler(2*k) for k in range(7)]
    [1, -1, 5, -61, 1385, -50521, 2702765]
    >>> [andre(2*k-1) * (2*k) / ((-4)**k - (-16)**k) for k in range(1, 8)]
    [1/6, -1/30, 1/42, -1/30, 5/66, -691/2730, 7/6]
    >>> [bernoulli(2*k) for k in range(1, 8)]
    [1/6, -1/30, 1/42, -1/30, 5/66, -691/2730, 7/6]

    See Also
    ========

    bernoulli, catalan, euler, sympy.polys.appellseqs.andre_poly

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Alternating_permutation
    .. [2] https://mathworld.wolfram.com/EulerZigzagNumber.html
    .. [3] Peter Luschny, "An introduction to the Bernoulli function",
           https://arxiv.org/abs/2009.06743
    c                 C   s   |t jkrt jS |t jkr t jS |jr,t jS |dkr>td S |dkrPdt j S |jr|jrn|j	rnt
t|S |jrddlm} | d }t| tdd|  d|  ||  S d S )Nr`   rE   r_   r   r   r1   r   )r   r   rg   r   r{   r   ZCatalanrh   r   r   absr   r   r   r   r   r   )rm   r;   r   r   r5   r5   r6   ro     s"    




z
andre.evalc                 K   sz   ddl m} ddlm} ddlm} d||d  dt |d   ||d tjd |t| ||d tdd    S )	Nr   )r   r   r   rE   r1   r   rK   )	r   r   r   r   r   r   r   r   r{   )rv   r   rw   r   r   r   r5   r5   r6   r     s    4zandre._eval_rewrite_as_zetac                 K   s@   ddl m} t |d  || t t|d  || t   S )Nr   )polylogr1   )r   r  r   )rv   r   rw   r  r5   r5   r6   _eval_rewrite_as_polylog  s    zandre._eval_rewrite_as_polylogc                 C   s   | j d }|jr|jrdS d S r  r  r  r5   r5   r6   r    s    
zandre._eval_is_integerc                 C   s   | j d jrdS d S r  r	  r  r5   r5   r6   r
    s    zandre._eval_is_positivec              	   C   s   | j d jsd S | j d |d }t|d B t|d t|d  }}dt| | ||| f }W 5 Q R X t	||S )Nr      rE   )
r   r   r   r.   r-   ZsinpiZcospiZ	dirichletr
   r   )rv   r   r   spcpr   r5   r5   r6   r   "  s    (zandre._eval_evalfN)rV   rW   rX   rY   r|   ro   r   r  r  r
  r   r5   r5   r5   r6   r    s   9
r  r1   c                   @   s@   e Zd ZdZedd Zedd Zdd Zdd	 Z	d
d Z
dS )	partitionaN  
    Partition numbers

    The Partition numbers are a sequence of integers `p_n` that represent the
    number of distinct ways of representing `n` as a sum of natural numbers
    (with order irrelevant). The generating function for `p_n` is given by:

    .. math:: \sum_{n=0}^\infty p_n x^n = \prod_{k=1}^\infty (1 - x^k)^{-1}

    Examples
    ========

    >>> from sympy import partition, Symbol
    >>> [partition(n) for n in range(9)]
    [1, 1, 2, 3, 5, 7, 11, 15, 22]
    >>> n = Symbol('n', integer=True, negative=True)
    >>> partition(n)
    0

    See Also
    ========

    bell, bernoulli, catalan, euler, fibonacci, harmonic, lucas, genocchi, tribonacci

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Partition_(number_theory%29
    .. [2] https://en.wikipedia.org/wiki/Pentagonal_number_theorem

    c                 C   s   t t}| |k rt|  S t|| d D ]}d\}}}d}|d| d 7 }||kr`|t||  7 }|d7 }|| }||kr|t||  7 }|dkrqq4||d dkr|n| 7 }q4t| q&|S )Nr1   )r   r   r   r   rK   rE   )rS   _npartitionr2   rT   )r;   LZ_nvr:   rI   r   gpr5   r5   r6   
_partitionT  s$    
zpartition._partitionc                 C   sV   |j }|dkrtdn:|rR|jr(tjS |js8|d jr>tjS |jrRt| 	|S d S )NFz/Partition numbers are defined only for integersr1   )
r   rM   r   r   r   r   r{   rh   r   r"  )rm   r;   Zis_intr5   r5   r6   ro   l  s    
zpartition.evalc                 C   s   | j d jrdS d S r  r   r   r  r5   r5   r6   r  }  s    zpartition._eval_is_integerc                 C   s   | j d jrdS d S )Nr   Fr#  r  r5   r5   r6   r    s    zpartition._eval_is_negativec                 C   s   | j d }|jr|jrdS d S r  )r   r   r   r  r5   r5   r6   r
    s    
zpartition._eval_is_positiveN)rV   rW   rX   rY   rZ   r"  r|   ro   r  r  r
  r5   r5   r5   r6   r  3  s    

r  c                   @   s   e Zd ZdS )_MultisetHistogramN)rV   rW   rX   r5   r5   r5   r6   r$    s   r$  r`   r_   Nc           	         s   t  trdtdd   D s$tt  }t fdd D }t fdd D ||g S t  t }t	|}t	 }||krdg| ||g  t S tt
|t|}tt
t|d| } D ]}|||   d7  < qt|S dS )	a6  Return tuple used in permutation and combination counting. Input
    is a dictionary giving items with counts as values or a sequence of
    items (which need not be sorted).

    The data is stored in a class deriving from tuple so it is easily
    recognized and so it can be converted easily to a list.
    c                 s   s    | ]}t |to|d kV  qdS )r   N)r   ri   )rH   r   r5   r5   r6   r     s     z&_multiset_histogram.<locals>.<genexpr>c                 3   s   | ]} | d krdV  qdS )r   r1   Nr5   r   rA   r5   r6   r     s      c                    s    g | ]} | d kr | qS rF   r5   r   rA   r5   r6   rJ     s      z'_multiset_histogram.<locals>.<listcomp>r1   rF   N)r   dictr   valuesrM   sumr$  listsetrS   zipr2   _multiset_histogram)	r;   totitemsr   ZlensZlennr   drI   r5   rA   r6   r+    s$    
r+  Fc                 C   sF   zt | } W n( tk
r4   ttt| || Y S X tt| ||S )a`  Return the number of permutations of ``n`` items taken ``k`` at a time.

    Possible values for ``n``:

        integer - set of length ``n``

        sequence - converted to a multiset internally

        multiset - {element: multiplicity}

    If ``k`` is None then the total of all permutations of length 0
    through the number of items represented by ``n`` will be returned.

    If ``replacement`` is True then a given item can appear more than once
    in the ``k`` items. (For example, for 'ab' permutations of 2 would
    include 'aa', 'ab', 'ba' and 'bb'.) The multiplicity of elements in
    ``n`` is ignored when ``replacement`` is True but the total number
    of elements is considered since no element can appear more times than
    the number of elements in ``n``.

    Examples
    ========

    >>> from sympy.functions.combinatorial.numbers import nP
    >>> from sympy.utilities.iterables import multiset_permutations, multiset
    >>> nP(3, 2)
    6
    >>> nP('abc', 2) == nP(multiset('abc'), 2) == 6
    True
    >>> nP('aab', 2)
    3
    >>> nP([1, 2, 2], 2)
    3
    >>> [nP(3, i) for i in range(4)]
    [1, 3, 6, 6]
    >>> nP(3) == sum(_)
    True

    When ``replacement`` is True, each item can have multiplicity
    equal to the length represented by ``n``:

    >>> nP('aabc', replacement=True)
    121
    >>> [len(list(multiset_permutations('aaaabbbbcccc', i))) for i in range(5)]
    [1, 3, 9, 27, 81]
    >>> sum(_)
    121

    See Also
    ========
    sympy.utilities.iterables.multiset_permutations

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Permutation

    )r,   rM   r   _nPr+  )r;   r   replacementr5   r5   r6   nP  s
    ;r1  c                    s  |dkrdS t  tr|d kr>t fddt d D S rJ | S | krVdS | krft|S |dkrr S t | d  S nzt  tr|d krt fddt t d D S rЈ t | S | t k rt|t	dd  t
 D  S | t krdS |dkr  t S d}t  tt t
 D ]} | sNq< t  d8  <  | dkrd |<  t  d8  < |tt |d 7 } t  d7  < d |< n6 |  d8  < |tt |d 7 } |  d7  <  t  d7  < q<|S d S )Nr   r1   c                 3   s   | ]}t  |V  qd S r\   r/  rG   r;   r0  r5   r6   r     s     z_nP.<locals>.<genexpr>c                 3   s   | ]}t  |V  qd S r\   r2  rG   r3  r5   r6   r     s     c                 S   s   g | ]}|d krt |qS r   r   rG   r5   r5   r6   rJ     s      z_nP.<locals>.<listcomp>)r   r   r'  r2   r   r7   r$  _N_ITEMSr   _Mr(  rS   r/  )r;   r   r0  r,  rI   r5   r3  r6   r/    sT    
 $


r/  c                 C   s*  t | } t| }|d d }dg|  d  }|d|t|   |d| }| r|  }|d }|dd }tdt|t|D ]}||  ||d  7  < qt||D ](}||  ||d  |||   7  < qqPt t|}|d r|| }n||dd< tt	}	t
|D ]\}}
|
|	|< q|	S )a  for n = (m1, m2, .., mk) return the coefficients of the polynomial,
    prod(sum(x**i for i in range(nj + 1)) for nj in n); i.e. the coefficients
    of the product of AOPs (all-one polynomials) or order given in n.  The
    resulting coefficient corresponding to x**r is the number of r-length
    combinations of sum(n) elements with multiplicities given in n.
    The coefficients are given as a default dictionary (so if a query is made
    for a key that is not present, 0 will be returned).

    Examples
    ========

    >>> from sympy.functions.combinatorial.numbers import _AOP_product
    >>> from sympy.abc import x
    >>> n = (2, 2, 3)  # e.g. aabbccc
    >>> prod = ((x**2 + x + 1)*(x**2 + x + 1)*(x**3 + x**2 + x + 1)).expand()
    >>> c = _AOP_product(n); dict(c)
    {0: 1, 1: 3, 2: 6, 3: 8, 4: 8, 5: 6, 6: 3, 7: 1}
    >>> [c[i] for i in range(8)] == [prod.coeff(x, i) for i in range(8)]
    True

    The generating poly used here is the same as that listed in
    https://tinyurl.com/cep849r, but in a refactored form.

    rE   r1   rF   Nr`   )r(  r'  poprL   rS   r2   minreversedr   ri   	enumerate)r;   ordZneedrvniNwasrI   revr.  r   r5   r5   r6   _AOP_product/  s,    (
rA  c                    s*  t  trn|dkr>sd  S t fddt d D S |dk rNtdrdt | d |S t |S t  tr t }|dkrĈstdd  t	 D S t fd	dt|d D S rt
 t |S |d|d fkr t S |d|fkrdS tt t	 | S t
t |S dS )
a  Return the number of combinations of ``n`` items taken ``k`` at a time.

    Possible values for ``n``:

        integer - set of length ``n``

        sequence - converted to a multiset internally

        multiset - {element: multiplicity}

    If ``k`` is None then the total of all combinations of length 0
    through the number of items represented in ``n`` will be returned.

    If ``replacement`` is True then a given item can appear more than once
    in the ``k`` items. (For example, for 'ab' sets of 2 would include 'aa',
    'ab', and 'bb'.) The multiplicity of elements in ``n`` is ignored when
    ``replacement`` is True but the total number of elements is considered
    since no element can appear more times than the number of elements in
    ``n``.

    Examples
    ========

    >>> from sympy.functions.combinatorial.numbers import nC
    >>> from sympy.utilities.iterables import multiset_combinations
    >>> nC(3, 2)
    3
    >>> nC('abc', 2)
    3
    >>> nC('aab', 2)
    2

    When ``replacement`` is True, each item can have multiplicity
    equal to the length represented by ``n``:

    >>> nC('aabc', replacement=True)
    35
    >>> [len(list(multiset_combinations('aaaabbbbcccc', i))) for i in range(5)]
    [1, 3, 6, 10, 15]
    >>> sum(_)
    35

    If there are ``k`` items with multiplicities ``m_1, m_2, ..., m_k``
    then the total of all combinations of length 0 through ``k`` is the
    product, ``(m_1 + 1)*(m_2 + 1)*...*(m_k + 1)``. When the multiplicity
    of each item is 1 (i.e., k unique items) then there are 2**k
    combinations. For example, if there are 4 unique items, the total number
    of combinations is 16:

    >>> sum(nC(4, i) for i in range(5))
    16

    See Also
    ========

    sympy.utilities.iterables.multiset_combinations

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Combination
    .. [2] https://tinyurl.com/cep849r

    NrE   c                 3   s   | ]}t  |V  qd S r\   nCrG   r3  r5   r6   r     s     znC.<locals>.<genexpr>r1   r   zk cannot be negativec                 s   s   | ]}|d  V  qdS r1   Nr5   )rH   r   r5   r5   r6   r     s     c                 3   s   | ]}t  |V  qd S r\   rB  rG   r3  r5   r6   r     s     )r   r   r'  r2   rM   r   r$  r4  r   r6  rC  r5  rA  tupler+  )r;   r   r0  r>  r5   r3  r6   rC  c  s0    B
 
 rC  c                 C   s   | |  krdkrn nt jS d| |fkr0t jS | |kr>t jS || d krTt| dS || d krzd|  d t| d d S || d krt| dt| d S t| |S )Nr   r1   rE   rK   r   )r   r{   r   r   
_stirling1r;   r   r5   r5   r6   _eval_stirling1  s    
rH  c                 C   sn   ddgdg|d   }t d| d D ]<}t t||ddD ]$}|d ||  ||d   ||< q:q$t|| S Nr   r1   rE   r`   r2   r8  r   r;   r   rowrI   r   r5   r5   r6   rF    s
    $rF  c                 C   s   | |  krdkrn nt jS d| |fkr0t jS | |kr>t jS || d krTt| dS |dkrbt jS |dkr~td| d  d S t| |S )Nr   r1   rE   )r   r{   r   r   r   
_stirling2rG  r5   r5   r6   _eval_stirling2  s    
rN  c                 C   sj   ddgdg|d   }t d| d D ]8}t t||ddD ] }|||  ||d   ||< q:q$t|| S rI  rJ  rK  r5   r5   r6   rM    s
     rM  rE   c                 C   s   t | } t |}| dk r td|| kr.tjS |rLt| | d || d S |rhtj| |  t| | S |dkrzt| |S |dkrt| |S td| dS )a
  Return Stirling number $S(n, k)$ of the first or second (default) kind.

    The sum of all Stirling numbers of the second kind for $k = 1$
    through $n$ is ``bell(n)``. The recurrence relationship for these numbers
    is:

    .. math :: {0 \brace 0} = 1; {n \brace 0} = {0 \brace k} = 0;

    .. math :: {{n+1} \brace k} = j {n \brace k} + {n \brace {k-1}}

    where $j$ is:
        $n$ for Stirling numbers of the first kind,
        $-n$ for signed Stirling numbers of the first kind,
        $k$ for Stirling numbers of the second kind.

    The first kind of Stirling number counts the number of permutations of
    ``n`` distinct items that have ``k`` cycles; the second kind counts the
    ways in which ``n`` distinct items can be partitioned into ``k`` parts.
    If ``d`` is given, the "reduced Stirling number of the second kind" is
    returned: $S^{d}(n, k) = S(n - d + 1, k - d + 1)$ with $n \ge k \ge d$.
    (This counts the ways to partition $n$ consecutive integers into $k$
    groups with no pairwise difference less than $d$. See example below.)

    To obtain the signed Stirling numbers of the first kind, use keyword
    ``signed=True``. Using this keyword automatically sets ``kind`` to 1.

    Examples
    ========

    >>> from sympy.functions.combinatorial.numbers import stirling, bell
    >>> from sympy.combinatorics import Permutation
    >>> from sympy.utilities.iterables import multiset_partitions, permutations

    First kind (unsigned by default):

    >>> [stirling(6, i, kind=1) for i in range(7)]
    [0, 120, 274, 225, 85, 15, 1]
    >>> perms = list(permutations(range(4)))
    >>> [sum(Permutation(p).cycles == i for p in perms) for i in range(5)]
    [0, 6, 11, 6, 1]
    >>> [stirling(4, i, kind=1) for i in range(5)]
    [0, 6, 11, 6, 1]

    First kind (signed):

    >>> [stirling(4, i, signed=True) for i in range(5)]
    [0, -6, 11, -6, 1]

    Second kind:

    >>> [stirling(10, i) for i in range(12)]
    [0, 1, 511, 9330, 34105, 42525, 22827, 5880, 750, 45, 1, 0]
    >>> sum(_) == bell(10)
    True
    >>> len(list(multiset_partitions(range(4), 2))) == stirling(4, 2)
    True

    Reduced second kind:

    >>> from sympy import subsets, oo
    >>> def delta(p):
    ...    if len(p) == 1:
    ...        return oo
    ...    return min(abs(i[0] - i[1]) for i in subsets(p, 2))
    >>> parts = multiset_partitions(range(5), 3)
    >>> d = 2
    >>> sum(1 for p in parts if all(delta(i) >= d for i in p))
    7
    >>> stirling(5, 3, 2)
    7

    See Also
    ========
    sympy.utilities.iterables.multiset_partitions


    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind
    .. [2] https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind

    r   zn must be nonnegativer1   rE   zkind must be 1 or 2, not %sN)r,   rM   r   r   rN  rj   rH  )r;   r   r.  kindsignedr5   r5   r6   stirling  s    V

rQ  c                 C   s   || ks|dk rdS |d| fkr$dS |dkr0dS |dkr@| d S | | }|dkrT|S d| | krt |}|| dkr|ttd||  8 }|S dg| }td|d D ]8}t|d |D ]}||  |||  7  < q|d8 }qdt|d| d  S )zxReturn the partitions of ``n`` items into ``k`` parts. This
    is used by ``nT`` for the case when ``n`` is an integer.r   r1   rE   rK   N)r  r"  r'  r  r2   )r;   r   r.  r,  r:   rI   r   r5   r5   r6   _nTe  s,    


rR  c           	         s  t  trB|dkrt S t |trBt  t|}tt |S t  tsz@tt }|dkrrt	t |W S |t krt
| tW n tk
r   t  Y nX  t }|dkr|dkrdS |d|fkrdS |dks|dkrN|dkrNt|d\}}t fddt
d|d D }|s8|t |d 8 }|dkrJ|d7 }|S | t krx|dkrnt|S t||S t }|dkr| t S d}| t |d |D ]}|d7 }q|S )ap  Return the number of ``k``-sized partitions of ``n`` items.

    Possible values for ``n``:

        integer - ``n`` identical items

        sequence - converted to a multiset internally

        multiset - {element: multiplicity}

    Note: the convention for ``nT`` is different than that of ``nC`` and
    ``nP`` in that
    here an integer indicates ``n`` *identical* items instead of a set of
    length ``n``; this is in keeping with the ``partitions`` function which
    treats its integer-``n`` input like a list of ``n`` 1s. One can use
    ``range(n)`` for ``n`` to indicate ``n`` distinct items.

    If ``k`` is None then the total number of ways to partition the elements
    represented in ``n`` will be returned.

    Examples
    ========

    >>> from sympy.functions.combinatorial.numbers import nT

    Partitions of the given multiset:

    >>> [nT('aabbc', i) for i in range(1, 7)]
    [1, 8, 11, 5, 1, 0]
    >>> nT('aabbc') == sum(_)
    True

    >>> [nT("mississippi", i) for i in range(1, 12)]
    [1, 74, 609, 1521, 1768, 1224, 579, 197, 50, 9, 1]

    Partitions when all items are identical:

    >>> [nT(5, i) for i in range(1, 6)]
    [1, 2, 2, 1, 1]
    >>> nT('1'*5) == sum(_)
    True

    When all items are different:

    >>> [nT(range(5), i) for i in range(1, 6)]
    [1, 15, 25, 10, 1]
    >>> nT(range(5)) == sum(_)
    True

    Partitions of an integer expressed as a sum of positive integers:

    >>> from sympy import partition
    >>> partition(4)
    5
    >>> nT(4, 1) + nT(4, 2) + nT(4, 3) + nT(4, 4)
    5
    >>> nT('1'*4)
    5

    See Also
    ========
    sympy.utilities.iterables.partitions
    sympy.utilities.iterables.multiset_partitions
    sympy.functions.combinatorial.numbers.partition

    References
    ==========

    .. [1] https://web.archive.org/web/20210507012732/https://teaching.csse.uwa.edu.au/units/CITS7209/partition.pdf

    Nr1   rE   c                 3   s   | ]}t  |V  qd S r\   rB  rG   rA   r5   r6   r     s     znT.<locals>.<genexpr>r   )r   r   r  r,   r   rR  r$  rS   r)  nTr2   	TypeErrorr+  r4  divmodr'  rC  r5  r   rQ  r%   Zcount_partitionsr6  Z
enum_range)	r;   r   r   r>  r   r   r<  r,  discardr5   rA   r6   rS    sR    I


 



rS  c                   @   s\   e Zd ZdZedd Zedd Zedd Zeee	j
e	j
gdd	 Zed
d ZdS )motzkina  
    The nth Motzkin number is the number
    of ways of drawing non-intersecting chords
    between n points on a circle (not necessarily touching
    every point by a chord). The Motzkin numbers are named
    after Theodore Motzkin and have diverse applications
    in geometry, combinatorics and number theory.

    Motzkin numbers are the integer sequence defined by the
    initial terms `M_0 = 1`, `M_1 = 1` and the two-term recurrence relation
    `M_n = rac{2*n + 1}{n + 2} * M_{n-1} + rac{3n - 3}{n + 2} * M_{n-2}`.


    Examples
    ========

    >>> from sympy import motzkin

    >>> motzkin.is_motzkin(5)
    False
    >>> motzkin.find_motzkin_numbers_in_range(2,300)
    [2, 4, 9, 21, 51, 127]
    >>> motzkin.find_motzkin_numbers_in_range(2,900)
    [2, 4, 9, 21, 51, 127, 323, 835]
    >>> motzkin.find_first_n_motzkins(10)
    [1, 1, 2, 4, 9, 21, 51, 127, 323, 835]


    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Motzkin_number
    .. [2] https://mathworld.wolfram.com/MotzkinNumber.html

    c                 C   s   zt | } W n tk
r"   Y dS X | dkr| dkr8dS d}d}d}|| k rd| d | d| d |  |d  }|d7 }|}|}qD|| krdS dS ndS d S )NFr   )r1   rE   Tr1   rE   rK   )r,   rM   )r;   tn1tnrI   r3   r5   r5   r6   
is_motzkin7	  s&    (zmotzkin.is_motzkinc                 C   s   d|   kr|krn ng }| d  kr0|kr>n n
| d d}d}d}||kr|| krd| | d| d | d| d |  |d  }|d7 }|}t|}qJ|S tdd S )Nr   r1   rE   rK   zEThe provided range is not valid. This condition should satisfy x <= y)rT   ri   rM   )r8   rP   motzkinsrX  rY  rI   r3   r5   r5   r6   find_motzkin_numbers_in_rangeR	  s     

(
z%motzkin.find_motzkin_numbers_in_rangec                 C   s   zt | } W n tk
r(   tdY nX | dk r:tddg}| dkrR|d d}d}d}|| kr|| d| d | d| d |  |d  }|d7 }|}t|}q^|S )N.The provided number must be a positive integerr   r1   rE   rK   )r,   rM   rT   ri   )r;   r[  rX  rY  rI   r3   r5   r5   r6   find_first_n_motzkinsh	  s&    

(
zmotzkin.find_first_n_motzkinsc                 C   s0   d|  d |d  d|  d |d   | d  S )NrE   r1   r`   rK   r_   r5   rd   r5   r5   r6   _motzkin	  s    zmotzkin._motzkinc                 C   sL   zt |}W n tk
r(   tdY nX |dk r:tdt| |d S )Nr]  r   r1   )r,   rM   r   r_  r~   r5   r5   r6   ro   	  s    zmotzkin.evalN)rV   rW   rX   rY   rZ   rZ  r\  r^  r+   r   r{   r_  r|   ro   r5   r5   r5   r6   rW  	  s   $


rW  )r;   r   c                   s  ddl m} ddlm  ddlm dd | ||fddkrHtd	| dk	rt| t	rbt
d
| sltjS t| tk	rt| }t|}n<t| tkrtfdd|  D  dd |  D }d}|stjS t| }t| }t|}	n|dk	r| |stjS t|S |dk	rt|tr\tfdd| D  dd | D }nPt|srt|trt|}tfdd|D  tdd |D }nt
d|stjS tdd | D }t| }	d}tt|}
|
dkrt|	S t|}|
d |krtjS |
d |krV|	dkr@|dkr@tjS |	d |
krVt|
S |dk rj|dksp|r|dkrg }d} | D ]2\}}t|D ]}|| g|  | d7 } qqttdd t |D S ddl!m"} tt#|| t$ fdd| D   dt%fS )a  return the number of derangements for: ``n`` unique items, ``i``
    items (as a sequence or multiset), or multiplicities, ``m`` given
    as a sequence or multiset.

    Examples
    ========

    >>> from sympy.utilities.iterables import generate_derangements as enum
    >>> from sympy.functions.combinatorial.numbers import nD

    A derangement ``d`` of sequence ``s`` has all ``d[i] != s[i]``:

    >>> set([''.join(i) for i in enum('abc')])
    {'bca', 'cab'}
    >>> nD('abc')
    2

    Input as iterable or dictionary (multiset form) is accepted:

    >>> assert nD([1, 2, 2, 3, 3, 3]) == nD({1: 1, 2: 2, 3: 3})

    By default, a brute-force enumeration and count of multiset permutations
    is only done if there are fewer than 9 elements. There may be cases when
    there is high multiplicity with few unique elements that will benefit
    from a brute-force enumeration, too. For this reason, the `brute`
    keyword (default None) is provided. When False, the brute-force
    enumeration will never be used. When True, it will always be used.

    >>> nD('1111222233', brute=True)
    44

    For convenience, one may specify ``n`` distinct items using the
    ``n`` keyword:

    >>> assert nD(n=3) == nD('abc') == 2

    Since the number of derangments depends on the multiplicity of the
    elements and not the elements themselves, it may be more convenient
    to give a list or multiset of multiplicities using keyword ``m``:

    >>> assert nD('abc') == nD(m=(1,1,1)) == nD(m={1:3}) == 2

    r   )	integrate)laguerrer8   c                 S   s&   t | tstd| dk r"tddS )Nzexpecting integer valuesr   zvalue must not be negativeT)r   r   rT  rM   rb  r5   r5   r6   ok	  s
    
znD.<locals>.okNrE   zenter only 1 of i, n, or mz"items must be a list or dictionaryc                 3   s   | ]} |V  qd S r\   r5   )rH   _rc  r5   r6   r   	  s     znD.<locals>.<genexpr>c                 S   s   i | ]\}}|r||qS r5   r5   rH   r   r   r5   r5   r6   
<dictcomp>	  s       znD.<locals>.<dictcomp>c                 3   s"   | ]\}} |o |V  qd S r\   r5   )rH   rI   r   re  r5   r6   r   	  s     c                 S   s   i | ]\}}|| r||qS r5   r5   rf  r5   r5   r6   rg  	  s       c                 3   s   | ]} |V  qd S r\   r5   rG   re  r5   r6   r   	  s     c                 S   s   g | ]}|r|qS r5   r5   rG   r5   r5   r6   rJ   	  s      znD.<locals>.<listcomp>zexpecting iterablec                 s   s   | ]\}}|| V  qd S r\   r5   rf  r5   r5   r6   r   	  s     r1   r   c                 s   s   | ]
}d V  qdS rD  r5   rG   r5   r5   r6   r    
  s     )expc                    s   g | ]\}} || qS r5   r5   )rH   rI   r   )ra  r8   r5   r6   rJ   
  s    )&Zsympy.integrals.integralsr`  Z#sympy.functions.special.polynomialsra  Z	sympy.abcr8   countrM   r   r   rT  r   r   typer%  r(  r(   r   r&  r-  r'  rS   r   r*   strri   maxr{   r   r2   rL   r   r)   r   rh  r  r   r   )rI   Zbruter;   r   r`  r   msr>  countsZnkeybigZnvalr   r   rh  r5   )ra  rc  r8   r6   nD	  s    ,






rp  )NF)NF)NF)NrE   F)N)NN)krY   mathr   collectionsr   typingr   r   Z
sympy.corer   r   r   r   Zsympy.core.cacher	   Zsympy.core.exprr
   Zsympy.core.functionr   r   r   Zsympy.core.logicr   Zsympy.core.mulr   Zsympy.core.numbersr   r   r   r   r   r   Zsympy.core.relationalr   r   r   Zsympy.external.gmpyr   Z(sympy.functions.combinatorial.factorialsr   r   r   r   r   Z$sympy.functions.elementary.piecewiser   Zsympy.ntheory.primetestr    r!   Zsympy.polys.appellseqsr"   r#   r$   Zsympy.utilities.enumerativer%   Zsympy.utilities.exceptionsr'   Zsympy.utilities.iterablesr(   r)   r*   Zsympy.utilities.memoizationr+   Zsympy.utilities.miscr,   r   r-   r.   Zmpmath.libmpr/   r]   r7   rb   r<   r=   r[   r}   r   r   r   r   r   r   r   r  r  r  rE  r$  r4  r5  slicer6  r+  r1  r/  rA  rC  rH  rF  rN  rM  rQ  rR  rS  rW  rp  r5   r5   r5   r6   <module>   s    	 T4Z ?      r_

B4
3
^


l
+
 }