
    \h                      S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
JrJrJr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJr  SSKJr  SSKJr  SSKJrJrJrJ r J!r!J"r"  SSK#J$r$J%r%J&r&J'r'  SSK(J)r)J*r*J+r+J,r,J-r-J.r.  SSK/J0r0J1r1J2r2  SSK3J4r4  SSK5J6r6  SSK7J8r8J9r9J:r:J;r;J<r<  SSK=J>r>  SSK?J@r@JArA  SSKBJCrCJDrD  SSKEJFrFJGrGJHrH  SSKIJJrJ  SSKKJLrL  SSKMJNrN  SSKOJPrPJQrQJRrR  SSKSJTrT  SSKUJVrV  SSKWJXrXJYrY  SSKZJ[r\  S  r]\" S!5      r^ " S" S#\5      r_ " S$ S%\5      r` " S& S'\5      ra " S( S)\5      rb " S* S+\5      rc " S, S-\5      rd " S. S/\5      re " S0 S1\5      rf " S2 S3\5      rg " S4 S5\5      rh " S6 S7\5      ri " S8 S9\5      rj " S: S;\5      rk " S< S=\5      rl " S> S?\5      rm " S@ SA\5      rn " SB SC\5      ro " SD SE\5      rp " SF SG\5      rq " SH SI\5      rr " SJ SK\5      rs " SL SM\5      rt " SN SO\5      ru " SP SQ\v5      rwSRrxSSry\z" ST\y5      r{SU r|SeSV jr}\SeSW j5       r~\SX 5       rSeSY jrSZ r\S[ 5       rS\ r\S] 5       rSfS^ jr\S_ 5       rSgS` jr " Sa Sb\5      rShSTSTSc.Sd jjrgT)ia6  
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.
    )annotations)prod)defaultdict)Callable)SSymbolAddDummy)cacheit)Dict)Expr)ArgumentIndexErrorDefinedFunction
expand_mul)	fuzzy_not)Mul)EIpiooRationalInteger)Eqis_leis_gtis_lt)
SYMPY_INTSremovelcmlegendrejacobi	kronecker)binomial	factorialsubfactoriallog)	Piecewise)	factorint_divisor_sigmais_carmichael find_carmichael_numbers_in_rangefind_first_n_carmichaels)_primepi)
_partition_partition_rec)isprime	is_square)bernoulli_poly
euler_polygenocchi_poly)cancel)MultisetPartitionTraversersympy_deprecation_warning)multisetmultiset_derangementsiterable)recurrence_memo)as_int)mpworkprec)ifibc                0    [        [        XS-   5      5      $ N   )r   range)abs     ]/var/www/auris/envauris/lib/python3.13/site-packages/sympy/functions/combinatorial/numbers.py_productrI   -   s    aQ      xc                  x    \ rS rSrSr\S 5       r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       rS	rg
)
carmichael;   a  
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.ntheory.factor_ import find_first_n_carmichaels, find_carmichael_numbers_in_range
>>> find_first_n_carmichaels(5)
[561, 1105, 1729, 2465, 2821]
>>> find_carmichael_numbers_in_range(0, 562)
[561]
>>> find_carmichael_numbers_in_range(0,1000)
[561]
>>> 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                .    [        SSSS9  [        U 5      $ )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deprecated_since_versionactive_deprecations_target)r9   r2   ns    rH   is_perfect_squarecarmichael.is_perfect_squaren   s#    !	 "(#I	
 |rJ   c                &    [        SSSS9  X-  S:H  $ )NzI
        divides can be replaced by directly testing n % p == 0.
        rP   rQ   rR   r   r8   )prV   s     rH   dividescarmichael.dividesz   s%    !	 "(#I	
 uzrJ   c                .    [        SSSS9  [        U 5      $ )Nzi
is_prime is just a wrapper around sympy.ntheory.primetest.isprime so use that
directly instead.
        rP   rQ   rR   )r9   r1   rU   s    rH   is_primecarmichael.is_prime   s#    !	 "(#I	
 qzrJ   c                .    [        SSSS9  [        U 5      $ )Nzr
is_carmichael is just a wrapper around sympy.ntheory.factor_.is_carmichael so use that
directly instead.
        1.13%deprecated-ntheory-symbolic-functionsrR   )r9   r+   rU   s    rH   r+   carmichael.is_carmichael   s$    !	 "(#J	
 QrJ   c                .    [        SSSS9  [        X5      $ )Nz
find_carmichael_numbers_in_range is just a wrapper around sympy.ntheory.factor_.find_carmichael_numbers_in_range so use that
directly instead.
        ra   rb   rR   )r9   r,   )rK   ys     rH   r,   +carmichael.find_carmichael_numbers_in_range   s$    !	 "(#J	
 055rJ   c                .    [        SSSS9  [        U 5      $ )Nz
find_first_n_carmichaels is just a wrapper around sympy.ntheory.factor_.find_first_n_carmichaels so use that
directly instead.
        ra   rb   rR   )r9   r-   rU   s    rH   r-   #carmichael.find_first_n_carmichaels   s$    !	 "(#J	
 (**rJ    N)__name__
__module____qualname____firstlineno____doc__staticmethodrW   r[   r^   r+   r,   r-   __static_attributes__ri   rJ   rH   rM   rM   ;   s    0d 	 	   	 	 	  	  	6 	6 	+ 	+rJ   rM   c                      \ rS rSrSr\S 5       r\\" S\R                  \
/5      S 5       5       r\SS j5       rS rS rS	 rS
rg)	fibonacci   aC  
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                    [        U 5      $ N)_ifibrU   s    rH   _fibfibonacci._fib   s    QxrJ   Nc                B    US   [         US   -  -   R                  5       $ )N_symexpandrV   prevs     rH   _fibpolyfibonacci._fibpoly   s$     R4R=(0022rJ   c                z   U[         R                  L a  [         R                  $ UR                  (       a  UcN  [        U5      nUS:  a#  [         R                  US-   -  [        U* 5      -  $ [        U R                  U5      5      $ US:  a  [        S5      eU R                  U5      R                  [        U5      $ g )Nr   rD   zDFibonacci polynomials are defined only for positive integer indices.)r   Infinity
is_IntegerintNegativeOnerr   r   rw   
ValueErrorr   subsr}   clsrV   syms      rH   evalfibonacci.eval   s    

?::<<{Fq5==1q51IqbMAA"388A;//q5$ &< = =||A++D#66 rJ   c                    SSK JnJn  [        R                  U-  U" [        R
                  U-  5      [        R                  U-  -  -
  U" S5      -  $ )Nr   )sqrtcos   )sympy.functionsr   r   r   GoldenRatioPi)selfrV   kwargsr   r   s        rH   _eval_rewrite_as_tractable$fibonacci._eval_rewrite_as_tractable  s<    -q 3qttAv;q}}a/?#??aHHrJ   c                p    SSK Jn  SU* -  U" S5      -  SU" S5      -   U-  U" S5      * S-   U-  -
  -  S-  $ )Nr   r      r   rD   (sympy.functions.elementary.miscellaneousr   r   rV   r   r   s       rH   _eval_rewrite_as_sqrtfibonacci._eval_rewrite_as_sqrt  sD    AA2wtAwT!Wq 0T!WHqL13D DEIIrJ   c                    [         R                  U-  S[         R                  * U-  -  -
  S[         R                  -  S-
  -  $ NrD   r   )r   r   r   rV   r   s      rH   _eval_rewrite_as_GoldenRatio&fibonacci._eval_rewrite_as_GoldenRatio
  s8    q 1q}}nq%8#881Q]]?1;LMMrJ   ri   ru   )rj   rk   rl   rm   rn   ro   rw   r=   r   Oner}   r   classmethodr   r   r   r   rp   ri   rJ   rH   rr   rr      sn    'R   dAEE4()3 * 3 7 7"IJNrJ   rr   c                  .    \ rS rSrSr\S 5       rS rSrg)lucasi  a}  
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                    U[         R                  L a  [         R                  $ UR                  (       a  [        US-   5      [        US-
  5      -   $ g rC   )r   r   r   rr   r   rV   s     rH   r   
lucas.eval5  s@    

?::<<QU#iA&666 rJ   c                X    SSK Jn  SU* -  SU" S5      -   U-  U" S5      * S-   U-  -   -  $ )Nr   r   r   rD   r   r   r   s       rH   r   lucas._eval_rewrite_as_sqrt=  s7    AA2wT!Wq(T!WHqL1+<<==rJ   ri   N)	rj   rk   rl   rm   rn   r   r   r   rp   ri   rJ   rH   r   r     s     > 7 7>rJ   r   c                      \ rS rSrSr\\" \R                  \R                  \R                  /5      S 5       5       r
\\" \R                  \R                  \S-  /5      S 5       5       r\SS j5       rS rS	 rS
rg)
tribonacciiI  a@  
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                $    US   US   -   US   -   $ )Nrz   r{   ri   r   s     rH   _tribtribonacci._tribp  s      R48#d2h./rJ   r   c                b    US   [         US   -  -   [         S-  US   -  -   R                  5       $ )Nr   rz   r   r{   r|   r   s     rH   	_tribpolytribonacci._tribpolyu  s6     R4R=(4748+;;CCEErJ   Nc                (   U[         R                  L a  [         R                  $ UR                  (       a^  [        U5      nUS:  a  [	        S5      eUc  [        U R                  U5      5      $ U R                  U5      R                  [        U5      $ g )Nr   zITribonacci polynomials are defined only for non-negative integer indices.)
r   r   r   r   r   r   r   r   r   r}   r   s      rH   r   tribonacci.evalz  s|    

?::<<AA1u  "@ A A{syy|,,}}Q',,T377 rJ   c           	        SSK JnJn  S[        R                  U" S5      -  -   S-  nSU" SSU" S5      -  -   5      -   U" SSU" S5      -  -
  5      -   S-  nSXS" SSU" S5      -  -   5      -  -   US-  U" SSU" S5      -  -
  5      -  -   S-  nSUS-  U" SSU" S5      -  -   5      -  -   XS" SSU" S5      -  -
  5      -  -   S-  nXaS-   -  Xg-
  Xh-
  -  -  XqS-   -  Xv-
  Xx-
  -  -  -   XS-   -  X-
  X-
  -  -  -   n	U	$ )	Nr   cbrtr   r{      r   rD      !   )r   r   r   r   ImaginaryUnit)
r   rV   r   r   r   wrF   rG   cTns
             rH   r    tribonacci._eval_rewrite_as_sqrt  s4   G!//DG++q0b1T"X:o&&b1T"X:o)>>!C4QtBxZ(((1a4R!DH*_0E+EEJAd2$r(
?+++aR!DH*_0E.EEJa%j1515/*a%j1515/*+a%j1515/*+ 	rJ   c                    SSK Jn  SSKJnJn  U" SSU" S5      -  -   5      nSU-  [
        R                  U-  -  US-  SU-  -
  S	-   -  nU" U[
        R                  -   5      $ )
Nr   floorr   iJ  f   r   r   r      )#sympy.functions.elementary.integersr   r   r   r   r   TribonacciConstantHalf)r   rV   r   r   r   r   rG   r   s           rH   #_eval_rewrite_as_TribonacciConstant.tribonacci._eval_rewrite_as_TribonacciConstant  s`    =Gs48|#$UQ))1,,1qs
Q?R!&&[!!rJ   ri   ru   )rj   rk   rl   rm   rn   ro   r=   r   Zeror   r   r}   r   r   r   r   r   rp   ri   rJ   rH   r   r   I  s    $L affaeeQUU+,0 - 0 affaeeT1W-.F / F 8 8	"rJ   r   c                      \ rS rSr% SrS\S'   \S 5       r\R                  \
" SS5      \
" SS5      \
" S	S
5      S.rSSSSS.r\SS j5       rSS jrS rSrg)	bernoullii  a*  
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

ztuple[Integer]argsc                   Sn[        [        U S-   U S-
  5      5      n[        SU S-  S-   5       HU  nX[        U SU-  -
  5      -  -  nU[	        U S-
  SU-  -
  S-   U SU-  -
  5      -  nU[	        SU-  S-   SU-  S-   5      -  nMW     U S-  S:X  a  [        U S-   S5      * U-
  nO[        U S-   S5      U-
  nU[        U S-   U 5      -  $ )Nr   r      rD   r   	   )r   r#   rE   r   rI   r   )rV   srF   js       rH   _calc_bernoullibernoulli._calc_bernoulli  s    QA&'q!Q$(#AYq1Q3w'''A!a%!A#+/1qs733A(1Q37AaC!G,,A	 $
 q5A:!a%##a'AQ"Q&A8AE1%%%rJ   rD   r   r   r{      )r   rD   r   r   r   r   Nc                   U[         R                  L a  U " U5      $ UR                  (       a  [         R                  $ UR                  SL d  UR                  SL a6  Ub2  UR
                  (       a!  UR                  (       a  [         R                  $ g UGc"  U[         R                  L a  [         R                  $ UR                  (       a$  US-
  R                  (       a  [         R                  $ UR                  (       a  [        U5      nUS:  a6  [        R                  " U5      u  p4[!        [        U5      [        U5      5      $ US-  nU R"                  U   nX::  a  U R$                  U   $ ['        US-   US-   S5       H0  nU R)                  U5      nXR$                  U'   XpR"                  U'   M2     W$ g UR                  (       a  [+        X5      $ g )NFrD   i  r   )r   r   is_zero
is_integeris_nonnegativer   is_nonpositiveNaNr   is_oddis_positiver   	is_Numberr   r?   bernfracr   _highest_cacherE   r   r3   )	r   rV   rK   rZ   qcasehighest_cachedirG   s	            rH   r   bernoulli.eval  sc   :q6MYY55L\\U"a&6&6%&?}!2B2BuuYAEEzvvqs//vvFs7;;q>DA#CFCF331u!$d!3&::a=( ~11q5!<A++A.A$%JJqM)*LL& = # & [[!!'' rJ   c                ^    SSK Jn  [        S[        US5      4U* U" SU-
  U5      -  S45      $ )Nr   zetarD   T)&sympy.functions.special.zeta_functionsr   r(   r   )r   rV   rK   r   r   s        rH   _eval_rewrite_as_zetabernoulli._eval_rewrite_as_zetaE  s2    ?!R1X!d1Q3l):D(ABBrJ   c                   [        S U R                   5       5      (       d  g U R                  S   R                  U5      n[        U R                  5      S:  a  U R                  S   O[        R
                  R                  U5      n[        U5         US:X  a  [        R                  " S5      nOUS:X  a  U[        R                  " S5      -
  nOr[        R                  " U5      (       a9  US:  a3  US:X  a  [        R                  " U5      O[        R                  " X#5      nOU* [        R                  " SU-
  U5      -  nS S S 5        [        R                  " WU5      $ ! , (       d  f       N%= f)Nc              3  8   #    U  H  oR                   v   M     g 7fru   	is_number.0rK   s     rH   	<genexpr>(bernoulli._eval_evalf.<locals>.<genexpr>J       2	1;;	   r   rD         ?)allr   
_to_mpmathlenr   r   r@   r?   mpfisintr   bernpolyr   r   _from_mpmath)r   precrV   rK   ress        rH   _eval_evalfbernoulli._eval_evalfI  s    2		222IIaL##D) ^a/TYYq\QUUFFtLd^AvffQia"&&+o!a)*abll1oR[[5Fb2771Q3?*    d++ ^s   B0E""
E0ri   ru   rD   )rj   rk   rl   rm   rn   __annotations__ro   r   r   r   r   r   r   r   r   r   r  rp   ri   rJ   rH   r   r     sx    bH  & &  8Aq>hq!n"bAQRFqQ'H#( #(JC,rJ   r   c                      \ rS rSrSr\\" SS/5      S 5       5       r\\" \R                  \
/5      S 5       5       r\S 5       r\SS j5       rSS	 jrS
rg)bellia  a  
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

rD   c                Z    SnSn[        SU 5       H  nX0U-
  -  U-  nX#X   -  -  nM     U$ rC   )rE   rV   r   r   rF   ks        rH   _bell
bell._bell  sC     q!AUq ATWA  rJ   c                    SnSn[        SU S-   5       H  nX0U-
  S-   -  US-
  -  nX#XS-
     -  -  nM!     [        [        U-  5      $ r   )rE   r   r}   r  s        rH   
_bell_polybell._bell_poly  s]     q!a%AUQYAE*ATa%[ A ! $(##rJ   c                ^   U S:X  a  US:X  a  [         R                  $ U S:X  d  US:X  a  [         R                  $ [         R                  n[         R                  n[        SX-
  S-   5       H6  nX4[        R                  X-
  US-
  U5      -  X%S-
     -  -  nX@U-
  -  U-  nM8     [        U5      $ )aT  
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   rD   r   )r   r   r   rE   r  _bell_incomplete_polyr   )rV   r  symbolsr   rF   ms         rH   r  bell._bell_incomplete_poly  s    " Fa55L1f!q&66MFFEEq!%!)$AT//q1ug' ')0Q8 8AUaA % !}rJ   Nc                   U[         R                  L a  Uc  [         R                  $ [        S5      eUR                  (       d  UR                  SL a  [        S5      eUR
                  (       a  UR                  (       a~  Uc#  [        U R                  [        U5      5      5      $ Uc.  U R                  [        U5      5      R                  [        U5      $ U R                  [        U5      [        U5      U5      nU$ g g )NzBell polynomial is not definedFza non-negative integer expected)r   r   r   is_negativer   r   r   r   r  r   r  r   r}   r  )r   rV   k_symr  rs        rH   r   	bell.eval  s    

?}zz! !ABB==ALLE1>??<<A,,}syyQ011~~c!f-224??--c!fc%j'J -<rJ   c                    SSK Jn  Uc  Ub  U $ UR                  (       d  U $ [        SSSS9nS[        -  U" Xa-  [        U5      -  US[        R                  45      -  $ )Nr   Sumr  T)integernonnegativerD   )sympy.concrete.summationsr!  r   r
   r   r$   r   r   )r   rV   r  r  r   r!  r  s          rH   _eval_rewrite_as_Sumbell._eval_rewrite_as_Sum  s_    17#6K K#t61us14)A,.Aqzz0BCCCrJ   ri   NN)rj   rk   rl   rm   rn   ro   r=   r  r   r   r}   r  r  r   r   r%  rp   ri   rJ   rH   r  r  a  s    >@ aV   aeeT]#$ $ $  8  &	DrJ   r  c                      \ rS rSr% Sr0 rS\S'   \SS j5       r\	R                  4S jrSS jrSS	 jrSS
 jr\	R                  4S jrS rSS jrS rSS jrSrg)harmonici  a,  
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/

z(dict[Integer, Callable[[int], Rational]]harmonic_cacheNc           	       ^ SSK Jn  T[        R                  L a  U " U5      $ Tc  [        R                  mUR                  (       a  [        R
                  $ TR                  (       a  U$ U[        R                  L ax  TR                  (       a  [        R                  $ [        T[        R                  5      (       a  [        R                  $ [        T[        R                  5      (       a  U" T5      $ g TR                  (       a8  TR                  (       a'  [        ST-
  US-   5      [        ST-
  5      -
  ST-
  -  $ UR                  (       a  UR                  (       aQ  TR                  SL d  TR                  SL a3  T[        R                  L a  [        R                  $ [        R                  $ UR                   (       a  TR                  (       aQ  TU R"                  ;  a#  [%        S/5      U4S j5       nX@R"                  T'   U R"                  T   " ['        U5      5      $ [)        U4S j[+        S['        U5      S-   5       5       6 $ g g )Nr   r   rD   Fc                <   > US   [         R                  U T-  -  -   $ )Nr{   )r   r   )rV   r   r  s     rH   fharmonic.eval.<locals>.f  s    #'8aeeadl#::rJ   c              3  .   >#    U  H
  oT* -  v   M     g 7fru   ri   )r   r  r  s     rH   r    harmonic.eval.<locals>.<genexpr>  s     C.B!W.Bs   )r   r   r   r   r   r   r   r  r   r   r   r   r   r   r   ComplexInfinityr   r*  r=   r   r	   rE   )r   rV   r  r   r-  s     `  rH   r   harmonic.eval  s   ?:q6M9A9966MYYH!**_}}uuq!%%zz!q!%%Aw !\\a..ac1Q3')AaC.8QqSAA\\}}!,,%"71;K;Ku;T,-Jq((AAEEA<< 2 22(!-; .;01**1---a0Q88CeAs1vz.BCDD   rJ   c           
     2   SSK JnJn  UR                  (       a~  UR                  (       al  [        U" SUS-   5      [        R                  -   [        US5      4[        R                  U-  U" US-
  S5      U" US-
  US-   5      -
  -  U" U5      -  S45      $ g g )Nr   )gamma	polygammarD   T)
'sympy.functions.special.gamma_functionsr4  r5  r   r   r(   r   
EulerGammar   r   )r   rV   r  r   r4  r5  s         rH   _eval_rewrite_as_polygamma#harmonic._eval_rewrite_as_polygamma  s    L<<AMMi1Q3/!,,>1aI]]A%1Q3):YqsAaC=P)PQ!H"$% % *<rJ   c                0    SSK Jn  U R                  U5      $ Nr   r5  r6  r5  rewriter   rV   r  r   r5  s        rH   _eval_rewrite_as_digamma!harmonic._eval_rewrite_as_digamma      E||I&&rJ   c                0    SSK Jn  U R                  U5      $ r;  r=  r?  s        rH   _eval_rewrite_as_trigamma"harmonic._eval_rewrite_as_trigamma  rB  rJ   c                f    SSK Jn  [        SSS9nUc  [        R                  nU" XR* -  USU45      $ )Nr   r   r  Tr"  rD   )r$  r!  r
   r   r   )r   rV   r  r   r!  r  s         rH   r%  harmonic._eval_rewrite_as_Sum  s6    1#t$9A1r7Q1I&&rJ   c           	         SSK Jn  SSKJn  [	        U" US-   5      [
        R                  -   [        US5      4U" U5      U" X!S-   5      -
  S45      $ )Nr   r   digammarD   T)r   r   r6  rK  r(   r   r7  r   )r   rV   r  r   r   rK  s         rH   r   harmonic._eval_rewrite_as_zeta  sL    ?C'!a%.1<<7AqBq'DaCL0$79 	9rJ   c                   SSK Jn  U R                  S   n[        U R                  5      S:X  a  U R                  S   OSnU[        R
                  :X  Ga)  UR                  (       a  UR                  S   nX5-
  nUR                  (       aW  UR                  (       aF  [        USS5       Vs/ s H  n[        R
                  Xg-   -  PM     sn[        U5      /-   n[        U6 $ UR                  (       aX  UR                  (       aG  [        SUS5       Vs/ s H  n[        R
                  * Xg-   -  PM     sn[        U5      /-   n[        U6 $ UR                  (       Ga"  UR                  5       u  pX-  nXU
-  -
  n	UR                  (       a  U	R                  (       a  U
R                  (       a  X:  a  SSKJn  SSKJn  SSKJnJnJn  [1        S	5      nX" SU
U-  U	-   -  USU45      -  nSU" U" S[2        -  U	-  U-  [	        U
5      -  5      U" U" [2        U-  [	        U
5      -  5      5      -  USU" U
S-
  [	        S5      -  5      45      -  n[2        S-  U" [2        U	-  U
-  5      -  U" SU
-  5      -   nUU-   U-
  $ U $ s  snf s  snf )
Nr   r   r   rD   r{   r&   r   )sinr   cotr  )r$  r!  r   r   r   r   is_Addr   r   rE   r)  r	   r  is_Rationalas_numer_denomr   &sympy.functions.elementary.exponentialr'   r   r   (sympy.functions.elementary.trigonometricrN  r   rO  r
   r   )r   hintsr!  rV   r  offnnewr   resultrZ   r   ur'   r   rN  r   rO  r  t1t2t3s                        rH   _eval_expand_funcharmonic._eval_expand_func  s#   1IIaL		Na/DIIaLQ:xxffQiw>>coo8=c1b8IJ8I1aeeTX.8IJhW[nM]]F<'^^9>q#r9JK9JAquufdh/9JKxX\~N^^F<'}}} '')FAI##!--AEJIVVc
ASa!eai1a)<<BSa"fqj1n!%<!=#&sBFad?';#<"=$%q%Q!A$*?#@B BB q&Ca1$55AE
BB7R<'1 K Ls   *J  Jc                   SSK Jn  SSKJn  U R	                  U5      n[        U[        5      (       d  UR	                  SSS9$ U[        R                  -
  nUR                  (       a"  U" U5      U" X!S-   5      -
  R	                  SSS9$ g )Nr   r   r<  	tractableT)deeprD   )
r   r   r6  r5  r>  
isinstancer)  r   r   
is_nonzero)	r   rV   r  limitvarr   r   r5  pgargs	            rH   r   #harmonic._eval_rewrite_as_tractable  sv    ?E\\)$"h''::k:55!%%i>>Gd1cl*33Kd3KK rJ   c                   [        S U R                   5       5      (       d  g U R                  S   R                  U5      n[        U R                  5      S:  a  U R                  S   O[        R
                  R                  U5      n[        R                  " U5      (       a  US:  a  [        R                  $ [        U5         US:X  a  [        R                  " U5      nO0[        R                  " U5      [        R                  " X2S-   5      -
  nS S S 5        [        R                  " WU5      $ ! , (       d  f       N%= f)Nc              3  8   #    U  H  oR                   v   M     g 7fru   r   r   s     rH   r   'harmonic._eval_evalf.<locals>.<genexpr>  r   r   r   rD   )r   r   r   r   r   r   r?   r  r   r@   r)  r   r   r  )r   r  rV   r  r  s        rH   r  harmonic._eval_evalf  s    2		222IIaL##D) ^a/TYYq\QUUFFtL88A;;1q555Ld^Avkk!nggaj2771c?2	 
   d++ ^s   AD11
D?c                    SSK Jn  [        U R                  5      S:X  a  U R                  u  p4OU R                  S-   u  p4US:X  a  XB" US-   US-   5      -  $ [        e)Nr   r   r   r	  rD   )r   r   r   r   r   )r   argindexr   rV   r  s        rH   fdiffharmonic.fdiff  sX    ?tyy>Q99DAq99t#DAq=tAaC1~%%$$rJ   ri   ru   r	  rD   N)rj   rk   rl   rm   rn   r*  r
  r   r   r   r   r8  r@  rD  r%  r   r]  r   r  rn  rp   ri   rJ   rH   r)  r)    sn    AH @BN<AE E@ /0ee %''' *+ 9"HL,	%rJ   r)  c                  F    \ rS rSrSr\S	S j5       rS	S jrS	S jrS r	Sr
g)
euleri  aL  
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                   UR                   (       a  [        R                  $ U[        R                  L a6  Uc  [        R                  S-  $ SSKJn  U" US-   S-  5      U" US-  5      -
  $ UR                  SL d  UR                  SL a  g Uc  UR                  (       a!  UR                  (       a  [        R                  $ UR                  (       a=  SSKJn  UR                  UR                   5      nUR"                  " USS9n[%        U5      $ g UR                  (       a  SS	KJn  [+        U5      nU" USS
9nU(       a  [-        S U 5       5      (       a  [/        S U 5       5      (       a{  SSKJn  [1        U Vs/ s H!  oR2                  (       d  M  UR4                  PM#     sn5      n	[7        U	5         UR8                  " X5      nS S S 5        [:        R<                  " WU	5      $ [?        X5      $ g s  snf ! , (       d  f       N6= f)Nr   r   rJ  rD   Fr?   T)exact)pure_complex)or_realc              3  ^   #    U  H#  oR                   =(       d    UR                  v   M%     g 7fru   )is_Floatr   r   rF   s     rH   r   euler.eval.<locals>.<genexpr>x  s     E1JJ6!,,6s   +-c              3  8   #    U  H  oR                   v   M     g 7fru   )ry  rz  s     rH   r   r{  y  s     51JJr   ) r   r   r   r   r   r6  rK  r   r   r   r   r   r   mpmathr?   r   r  eulernumr   sympy.core.evalfrv  r   r   anyminry  _precr@   	eulerpolyr   r  r4   )
r   rV   rK   rK  r?   r  rv  reimrF   r  s
             rH   r   
euler.eval_  sy   9955L!--yttAvGAaC7#gacl22\\U"a&6&6%&?YxxAMMvv%LL)kk!40s|#	  [[5AA40DEEEE5555%T@TZZGAGGT@Ad^,,q,C $((d33a##  A#^s   ;G3G35G88
Hc           
        SSK Jn  Uc  UR                  (       a  [        SSS9n[        SSS9nUS-  n[        R
                  U" U" [        XV5      [        R                  U-  USU-  -
  SU-  S-   -  -  -  SU-  [        R
                  U-  -  U-  -  USU45      USSU-  S-   45      -  nU$ U(       aJ  [        SSS9nU" [        X5      [        U5      -  SU-  -  U[        R                  -
  X-
  -  -  USU45      $ g )	Nr   r   r  TrG  r   r   rD   )
r$  r!  is_evenr
   r   r   r#   r   rr  r   )r   rV   rK   r   r!  r  r   Ems           rH   r%  euler._eval_rewrite_as_Sum  s   19c4(Ac4(AAA//CHQNammQ>N?@1Q3w!A#PQ'>R?S -Ta4**Q.-023Q)<>?AaC!G_%N NB Ic4(Ax~eAh.q!t3QZ154IIAqRS9UU rJ   c                F   UcS  [        [        R                  S-  [        US5      4SU-  * [	        US-   [        R
                  5      -  US-   -  S45      $ SSKJn  [        U" US-   S-  5      U" US-  5      -
  [        US5      4[	        US-   U5      * US-   -  S45      $ )Nr   r{   rD   Tr   rJ  )r(   r   r   r   genocchir   r6  rK  )r   rV   rK   r   rK  s        rH   _eval_rewrite_as_genocchieuler._eval_rewrite_as_genocchi  s    9add1fbBi0 !tehqsAFF&;;qsCTJL LC'1Q3'*WQqS\92a9E#AaC++qs3T:< 	<rJ   c           	     0   [        S U R                   5       5      (       d  g SSKJn  [	        U R                  5      S:X  a  U R                  S   S 4OU R                  u  p4UR                  U5      nUb  UR                  U5      n[        U5         UR                  " U5      (       a.  US:  a(  Uc  UR                  " U5      OUR                  " X45      nOUS:X  a>  Uc  UR                  O-UR                  " US-   S-  5      UR                  " US-  5      -
  nOBUc  SOUnSUR                  " U* U5      SUS-   -  UR                  " U* US-   S-  5      -  -
  -  nUc  USU-  -  nS S S 5        [        R                  " WU5      $ ! , (       d  f       N%= f)Nc              3  8   #    U  H  oR                   v   M     g 7fru   r   r   r   s     rH   r   $euler._eval_evalf.<locals>.<genexpr>  r   r   r   rt  rD   r{   r   r   )r   r   r}  r?   r   r   r@   r  r~  r  r   rK  r   r   r  )r   r  r?   r  rK   r  re   s          rH   r  euler._eval_evalf  sT   2		222'*499~':		!d#		LL=T"Ad^xx{{qAv()	bkk!nr||A7I7#$9"%%"**ac1W2E

STUVSV2WC yaArwwr1~AaC277A2!Qw;O0OOPC91a4KC    d++ ^s   CF
Fri   ru   )rj   rk   rl   rm   rn   r   r   r%  r  r  rp   ri   rJ   rH   rr  rr    s.    Ob $ $BV<,rJ   rr  c                  l    \ rS rSrSr\S 5       rSS jrS rS r	SS jr
S rS	 rS
 rS rS rS rSrg)catalani  a}  
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((-n, 1 - 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                   SSK Jn  UR                  (       a  UR                  (       d"  UR                  (       aS  UR
                  (       aB  SU-  U" U[        R                  -   5      -  U" [        R                  5      U" US-   5      -  -  $ UR                  (       aW  UR
                  (       aE  US-   R
                  (       a  [        R                  $ US-   R                  (       a  [        SS5      $ g g g )Nr   r4  r   r   rD   r{   )r6  r4  r   r   is_nonintegerr  r   r   r   r   r   r   )r   rV   r4  s      rH   r   catalan.eval  s    ALLQ--OOa4a!&&j))5=q1u+EFFLLQ]]A""vvAA&  +LrJ   c                    SSK Jn  SSKJn  U R                  S   n[        U5      U" SU[        R                  -   5      U" SUS-   5      -
  U" S5      -   -  $ )Nr   r&   r<  r   r   )rS  r'   r6  r5  r   r  r   r   )r   rm  r'   r5  rV   s        rH   rn  catalan.fdiff  sM    >EIIaLqz9QAFF
3i1q56IICPQFRSSrJ   c                ,    [        SU-  U5      US-   -  $ Nr   rD   )r#   r   s      rH   _eval_rewrite_as_binomial!catalan._eval_rewrite_as_binomial  s    !QQ''rJ   c                T    [        SU-  5      [        US-   5      [        U5      -  -  $ r  )r$   r   s      rH   _eval_rewrite_as_factorial"catalan._eval_rewrite_as_factorial  s&    1~1Q3)A,!>??rJ   c                    SSK Jn  SU-  U" U[        R                  -   5      -  U" [        R                  5      U" US-   5      -  -  $ )Nr   r  r   r   )r6  r4  r   r   )r   rV   	piecewiser   r4  s        rH   _eval_rewrite_as_gammacatalan._eval_rewrite_as_gamma  s;    A!tE!aff*%%uQVV}U1q5\'ABBrJ   c                0    SSK Jn  U" SU-
  U* /S/S5      $ )Nr   )hyperrD   r   )sympy.functions.special.hyperr  )r   rV   r   r  s       rH   _eval_rewrite_as_hypercatalan._eval_rewrite_as_hyper"  s     7a!eaR[1#q))rJ   c                    SSK Jn  UR                  (       a  UR                  (       d  U $ [	        SSSS9nU" X-   U-  USU45      $ )Nr   )Productr  T)r"  positiver   )sympy.concrete.productsr  r   r   r
   )r   rV   r   r  r  s        rH   _eval_rewrite_as_Product catalan._eval_rewrite_as_Product&  s@    3!1!1K#td3{Q1I..rJ   c                    U R                   S   R                  (       a   U R                   S   R                  (       a  gg g Nr   Tr   r   r   r   s    rH   _eval_is_integercatalan._eval_is_integer-  s/    99Q<""tyy|'B'B (C"rJ   c                B    U R                   S   R                  (       a  gg r  r   r   r  s    rH   _eval_is_positivecatalan._eval_is_positive1      99Q<&& 'rJ   c                    U R                   S   R                  (       a#  U R                   S   S-
  R                  (       a  gg g )Nr   r   T)r   r   r   r  s    rH   _eval_is_compositecatalan._eval_is_composite5  s4    99Q<""		!q(8'E'E (F"rJ   c                    SSK Jn  U R                  S   R                  (       a   U R	                  U5      R                  U5      $ g )Nr   r  )r6  r4  r   r   r>  r  )r   r  r4  s      rH   r  catalan._eval_evalf9  s5    A99Q<!!<<&22488 "rJ   ri   Nr	  )T)rj   rk   rl   rm   rn   r   r   rn  r  r  r  r  r  r  r  r  r  rp   ri   rJ   rH   r  r    sR    M^ 
' 
'T(@C
*/9rJ   r  c                  j    \ rS rSrSr\SS j5       rSS jrSS jrS r	S r
S	 rS
 rS rS rS rSrg)r  iF  a  
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                   U[         R                  L a  U " U5      $ UR                  SL d  UR                  SL a  g Ucg  UR                  (       a$  US-
  R
                  (       a  [         R                  $ UR                  (       a   SS[        S5      U-  -
  -  [        U5      -  $ g UR                  (       a  [        X5      $ g )NFrD   r   )
r   r   r   r   r   r   r   r   r   r5   )r   rV   rK   s      rH   r   genocchi.evaly  s    :q6M\\U"a&6&6%&?YxxQqS--vvAadAgI155  [[ && rJ   c                    US:X  aB  UR                   (       a1  UR                  (       a   SS[        S5      U-  -
  -  [        U5      -  $ S[        X5      SU-  [        XS-   S-  5      -  -
  -  $ r   )r   r   r   r   )r   rV   rK   r   s       rH   _eval_rewrite_as_bernoulli#genocchi._eval_rewrite_as_bernoulli  s`    6allq'7'7!A$'	?Yq\11IaOadYqQ3!)-D&DDEErJ   c                2    SSK Jn  SU-  U" SU-
  U5      -  $ )Nr   )dirichlet_etarz   rD   )r   r  )r   rV   rK   r   r  s        rH   _eval_rewrite_as_dirichlet_eta'genocchi._eval_rewrite_as_dirichlet_eta  s    H!tmAaC+++rJ   c                    [        U R                  5      S:  a  U R                  S   S:w  a  g U R                  S   nUR                  (       a  UR                  (       a  gg g )NrD   r   T)r   r   r   r   r   rV   s     rH   r  genocchi._eval_is_integer  sJ    tyy>A$))A,!"3IIaL<<A,, -<rJ   c                2   [        U R                  5      S:  a  U R                  S   S:w  a  g U R                  S   nUR                  (       aJ  UR                  (       a8  UR                  (       a  [        US-
  R                  5      $ US-  R                  $ g g )NrD   r   r   )r   r   r   r   r   r   r   r  s     rH   _eval_is_negativegenocchi._eval_is_negative  sq    tyy>A$))A,!"3IIaL<<A,,xx !A#!2!233aC<< -<rJ   c                &   [        U R                  5      S:  a  U R                  S   S:w  a  g U R                  S   nUR                  (       aD  UR                  (       a2  UR                  (       d  UR
                  (       a  gUS-  R                  $ g g )NrD   r   Fr   )r   r   r   r   r   r   r  r  s     rH   r  genocchi._eval_is_positive  se    tyy>A$))A,!"3IIaL<<A,,yyAHHaC==  -<rJ   c                   [        U R                  5      S:  a  U R                  S   S:w  a  g U R                  S   nUR                  (       a>  UR                  (       a,  UR                  (       a  UR
                  $ US-
  R                  $ g g NrD   r   )r   r   r   r   r  r   r   r  s     rH   _eval_is_evengenocchi._eval_is_even  sh    tyy>A$))A,!"3IIaL<<A,,yyyy aC$$$ -<rJ   c                >   [        U R                  5      S:  a  U R                  S   S:w  a  g U R                  S   nUR                  (       aP  UR                  (       a>  UR                  (       a  [        UR                  5      $ [        US-
  R                  5      $ g g r  )r   r   r   r   r  r   r   r   r  s     rH   _eval_is_oddgenocchi._eval_is_odd  sr    tyy>A$))A,!"3IIaL<<A,,yy ++ac..// -<rJ   c                    [        U R                  5      S:  a  U R                  S   S:w  a  g U R                  S   nUS-
  R                  $ )NrD   r      )r   r   r   r  s     rH   _eval_is_primegenocchi._eval_is_prime  sA    tyy>A$))A,!"3IIaL !}}rJ   c                    [        S U R                   5       5      (       a$  U R                  [        5      R	                  U5      $ g )Nc              3  8   #    U  H  oR                   v   M     g 7fru   r   r  s     rH   r   'genocchi._eval_evalf.<locals>.<genexpr>  s     .Iq{{Ir   )r   r   r>  r   r  )r   r  s     rH   r  genocchi._eval_evalf  s6    .DII...<<	*66t<< /rJ   ri   ru   r	  )rj   rk   rl   rm   rn   r   r   r  r  r  r  r  r  r  r  r  rp   ri   rJ   rH   r  r  F  sJ    0d ' 'F
, !%0=rJ   r  c                  F    \ rS rSrSr\S 5       rS rS rS r	S r
S rS	rg
)andrei  a  
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                D   U[         R                  L a  [         R                  $ U[         R                  L a  [         R                  $ UR                  (       a  [         R                  $ US:X  a  [        S5      * $ US:X  a  S[         R                  -  $ UR                  (       a}  UR                  (       a%  UR                  (       a  [        [        U5      5      $ UR                  (       a5  SSKJn  U* S-
  n[        U-  [!        SSU-  -
  SU-  5      -  U" U* 5      -  $ g g )Nr{   r   rz   r   r   rD   r   )r   r   r   r   r   r'   Catalanr   r   r  absrr  r   r   r   r   r   )r   rV   r   r  s       rH   r   
andre.eval  s    :55L!**_::9955L"WF7N"Waii<\\AII58}$GBqD!thqAvq!t44tQBx??  rJ   c           	         SSK Jn  SSKJn  SSKJn  SU" US-   5      -  S[        -  US-   -  -  U" US-   [        R                  S-  5      U" [        U-  5      U" US-   [        S5      S-  5      -  -
  -  $ )	Nr   )r   r  r   r   rD   r   r   )	rT  r   r6  r4  r   r   r   r   r   )r   r   r   r   r4  r   s         rH   r   andre._eval_rewrite_as_zeta   sr    @A?51:~21-ac1557#c"Q$i$qsAaDF2C&CCE 	ErJ   c                    SSK Jn  [        * US-   -  U" U* [        5      -  [        US-   -  U" U* [        * 5      -  -   $ )Nr   )polylogrD   )r   r  r   )r   r   r   r  s       rH   _eval_rewrite_as_polylogandre._eval_rewrite_as_polylog'  s>    Bac{WaR^+a!A#h!aR.HHHrJ   c                j    U R                   S   nUR                  (       a  UR                  (       a  gg g r  r  r  s     rH   r  andre._eval_is_integer+  s(    IIaL<<A,, -<rJ   c                B    U R                   S   R                  (       a  gg r  r  r  s    rH   r  andre._eval_is_positive0  r  rJ   c           	        U R                   S   R                  (       d  g U R                   S   R                  US-   5      n[        US-   5         [        R
                  " US-  5      [        R                  " US-  5      pCS[        R                  " U* U* XCU* 45      -  nS S S 5        [        R                  " WU5      $ ! , (       d  f       N%= f)Nr      r   )
r   r   r   r@   r?   sinpicospi	dirichletr   r  )r   r  r   spcpr  s         rH   r  andre._eval_evalf4  s    yy|%%IIaL##DG,d2gXXac]BHHQqSMBLL!rc2B3%788C    d++ s   AC  
Cri   N)rj   rk   rl   rm   rn   r   r   r   r  r  r  r  rp   ri   rJ   rH   r  r    s9    7r @ @&EI
,rJ   r  c                  <    \ rS rSrSrSrSr\S 5       rS r	S r
Srg)		partitioniD  a  
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

Tc                2   UR                   SL a  [        S5      eUR                  SL a  [        R                  $ UR
                  SL d  U[        R                  L a  [        R                  $ UR                  SL a  [        [        [        U5      5      5      $ g )NFn should be an integerT)
r   	TypeErrorr  r   r   r   r   r   r/   r>   r   s     rH   r   partition.evalg  ss    <<5 455==D 66M99QUU
55L<<4Zq	*++  rJ   c                >    U R                   S   R                  SL a  gg r  r  r  s    rH   r  partition._eval_is_positiver  s     99Q<&&$. /rJ   c                z    U R                   S   nS H&  u  p4X:X  d  M  X!-  U:X  d  M  [        R                  s  $    g )Nr   ))r   r   )   r   )   r   )r   r   r   )r   r   rV   rZ   rems        rH   	_eval_Modpartition._eval_Modv  s1    IIaL/FAv!%3,vv 0rJ   ri   N)rj   rk   rl   rm   rn   r   r   r   r   r  r  rp   ri   rJ   rH   r  r  D  s/    > JN, ,rJ   r  c                  J    \ rS rSrSrSrSr\\R                  4S j5       r
Srg)divisor_sigmai~  a  
Calculate the divisor function `\sigma_k(n)` for positive integer n

``divisor_sigma(n, k)`` is equal to ``sum([x**k for x in divisors(n)])``

If n's prime factorization is:

.. math ::
    n = \prod_{i=1}^\omega p_i^{m_i},

then

.. math ::
    \sigma_k(n) = \prod_{i=1}^\omega (1+p_i^k+p_i^{2k}+\cdots
    + p_i^{m_ik}).

Examples
========

>>> from sympy.functions.combinatorial.numbers import divisor_sigma
>>> divisor_sigma(18, 0)
6
>>> divisor_sigma(39, 1)
56
>>> divisor_sigma(12, 2)
210
>>> divisor_sigma(37)
38

See Also
========

sympy.ntheory.factor_.divisor_count, totient, sympy.ntheory.factor_.divisors, sympy.ntheory.factor_.factorint

References
==========

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

Tc                (   UR                   SL a  [        S5      eUR                  SL a  [        S5      eUR                   SL a  [        S5      eUR                  SL a  [        S5      eUR
                  SL a  SX-  -   $ U[        R                  L a  [        R                  $ UR                  SL a  UR                  SL a2  [        [        U5      R                  5        Vs/ s H  o3S-   PM	     sn6 $ UR                  SL a'  [        [        [        U5      [        U5      5      5      $ UR                  SL aP  [        [        U5      R                  5        VVs/ s H#  u  pC[!        XBUS-   -  -  S-
  XB-  S-
  -  5      PM%     snn6 $ g g s  snf s  snnf NFr  n should be a positive integerzk should be an integerz!k should be a nonnegative integerTrD   )r   r  r   r   r   r^   r   r   r   r   r   r)   valuesr*   r>   itemsr6   )r   rV   r  erZ   s        rH   r   divisor_sigma.eval  sd   <<5 455==E!=>><<5 455u$@AA::qt8O:55L<<4yyD IaL,?,?,AB,AqU,ABCC||t#q	6!9=>>yyE!S\]^S_SeSeSghSg41VQAE^a%7AD1H$EFSghii "  B is    F	*F
ri   Nrj   rk   rl   rm   rn   r   r   r   r   r   r   rp   ri   rJ   rH   r	  r	  ~  s/    'P JKuu j jrJ   r	  c                  J    \ rS rSrSrSrSr\\R                  4S j5       r
Srg)udivisor_sigmai  a  
Calculate the unitary divisor function `\sigma_k^*(n)` for positive integer n

``udivisor_sigma(n, k)`` is equal to ``sum([x**k for x in udivisors(n)])``

If n's prime factorization is:

.. math ::
    n = \prod_{i=1}^\omega p_i^{m_i},

then

.. math ::
    \sigma_k^*(n) = \prod_{i=1}^\omega (1+ p_i^{m_ik}).

Parameters
==========

k : power of divisors in the sum

    for k = 0, 1:
    ``udivisor_sigma(n, 0)`` is equal to ``udivisor_count(n)``
    ``udivisor_sigma(n, 1)`` is equal to ``sum(udivisors(n))``

    Default for k is 1.

Examples
========

>>> from sympy.functions.combinatorial.numbers import udivisor_sigma
>>> udivisor_sigma(18, 0)
4
>>> udivisor_sigma(74, 1)
114
>>> udivisor_sigma(36, 3)
47450
>>> udivisor_sigma(111)
152

See Also
========

sympy.ntheory.factor_.divisor_count, totient, sympy.ntheory.factor_.divisors,
sympy.ntheory.factor_.udivisors, sympy.ntheory.factor_.udivisor_count, divisor_sigma,
sympy.ntheory.factor_.factorint

References
==========

.. [1] https://mathworld.wolfram.com/UnitaryDivisorFunction.html

Tc           
        UR                   SL a  [        S5      eUR                  SL a  [        S5      eUR                   SL a  [        S5      eUR                  SL a  [        S5      eUR
                  SL a  SX-  -   $ UR                  (       a<  [        [        U5      R                  5        VVs/ s H  u  p4SX2U-  -  -   PM     snn6 $ g s  snnf r  )
r   r  r   r   r   r^   r   r   r)   r  )r   rV   r  rZ   r  s        rH   r   udivisor_sigma.eval  s    <<5 455==E!=>><<5 455u$@AA::qt8O<<	!0B0B0DE0D1s80DEFF Es   1C
ri   Nr  ri   rJ   rH   r  r    s/    3h JKuu G GrJ   r  c                  0    \ rS rSrSrSrSr\S 5       rSr	g)legendre_symboli	  a  
Returns the Legendre symbol `(a / p)`.

For an integer ``a`` and an odd prime ``p``, the Legendre symbol is
defined as

.. math ::
    \genfrac(){}{}{a}{p} = \begin{cases}
         0 & \text{if } p \text{ divides } a\\
         1 & \text{if } a \text{ is a quadratic residue modulo } p\\
        -1 & \text{if } a \text{ is a quadratic nonresidue modulo } p
    \end{cases}

Examples
========

>>> from sympy.functions.combinatorial.numbers import legendre_symbol
>>> [legendre_symbol(i, 7) for i in range(7)]
[0, 1, 1, -1, 1, -1, -1]
>>> sorted(set([i**2 % 7 for i in range(7)]))
[0, 1, 2, 4]

See Also
========

sympy.ntheory.residue_ntheory.is_quad_residue, jacobi_symbol

TFc                   UR                   SL a  [        S5      eUR                   SL a  [        S5      eUR                  SL d  UR                  SL a  [	        S5      eX-  R
                  SL a  [        R                  $ U[        R                  L a  [        R                  $ UR                  SL a7  UR                  SL a'  [        [        [        U5      [        U5      5      5      $ g g )NFa should be an integerzp should be an integerz p should be an odd prime integerT)r   r  r^   r   r   r   r   r   r   r   r    r>   )r   rF   rZ   s      rH   r   legendre_symbol.eval)  s    <<5 455<<5 455::!((e"3?@@E??d"66M:55L<<4ALLD$8XfQi344 %9rJ   ri   N
rj   rk   rl   rm   rn   r   r^   r   r   rp   ri   rJ   rH   r  r  	  s%    8 JH5 5rJ   r  c                  0    \ rS rSrSrSrSr\S 5       rSr	g)jacobi_symboli9  a  
Returns the Jacobi symbol `(m / n)`.

For any integer ``m`` and any positive odd integer ``n`` the Jacobi symbol
is defined as the product of the Legendre symbols corresponding to the
prime factors of ``n``:

.. math ::
    \genfrac(){}{}{m}{n} =
        \genfrac(){}{}{m}{p^{1}}^{\alpha_1}
        \genfrac(){}{}{m}{p^{2}}^{\alpha_2}
        ...
        \genfrac(){}{}{m}{p^{k}}^{\alpha_k}
        \text{ where } n =
            p_1^{\alpha_1}
            p_2^{\alpha_2}
            ...
            p_k^{\alpha_k}

Like the Legendre symbol, if the Jacobi symbol `\genfrac(){}{}{m}{n} = -1`
then ``m`` is a quadratic nonresidue modulo ``n``.

But, unlike the Legendre symbol, if the Jacobi symbol
`\genfrac(){}{}{m}{n} = 1` then ``m`` may or may not be a quadratic residue
modulo ``n``.

Examples
========

>>> from sympy.functions.combinatorial.numbers import jacobi_symbol, legendre_symbol
>>> from sympy import S
>>> jacobi_symbol(45, 77)
-1
>>> jacobi_symbol(60, 121)
1

The relationship between the ``jacobi_symbol`` and ``legendre_symbol`` can
be demonstrated as follows:

>>> L = legendre_symbol
>>> S(45).factors()
{3: 2, 5: 1}
>>> jacobi_symbol(7, 45) == L(7, 3)**2 * L(7, 5)**1
True

See Also
========

sympy.ntheory.residue_ntheory.is_quad_residue, legendre_symbol

TFc                   UR                   SL a  [        S5      eUR                   SL a  [        S5      eUR                  SL d  UR                  SL a  [	        S5      eU[
        R                  L d  U[
        R                  L a  [
        R                  $ X-  R                  SL a  [
        R                  $ UR                  SL a7  UR                  SL a'  [        [        [        U5      [        U5      5      5      $ g g )NFzm should be an integerr  z#n should be an odd positive integerT)r   r  r   r   r   r   r   r   r   r   r!   r>   )r   r  rV   s      rH   r   jacobi_symbol.evalp  s    <<5 455<<5 455==E!QXX%6BCC:aee55LE??d"66M<<4ALLD$8VF1Ivay122 %9rJ   ri   Nr  ri   rJ   rH   r  r  9  s&    2f JH3 3rJ   r  c                  0    \ rS rSrSrSrSr\S 5       rSr	g)kronecker_symboli  aG  
Returns the Kronecker symbol `(a / n)`.

Examples
========

>>> from sympy.functions.combinatorial.numbers import kronecker_symbol
>>> kronecker_symbol(45, 77)
-1
>>> kronecker_symbol(13, -120)
1

See Also
========

jacobi_symbol, legendre_symbol

References
==========

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

TFc                d   UR                   SL a  [        S5      eUR                   SL a  [        S5      eU[        R                  L d  U[        R                  L a  [        R                  $ UR                  SL a7  UR                  SL a'  [        [        [        U5      [        U5      5      5      $ g g )NFr  r  T)r   r  r   r   r   r"   r>   )r   rF   rV   s      rH   r   kronecker_symbol.eval  s    <<5 455<<5 455:aee55L<<4ALLD$8Yvay&)455 %9rJ   ri   Nr  ri   rJ   rH   r!  r!    s%    . JH6 6rJ   r!  c                  0    \ rS rSrSrSrSr\S 5       rSr	g)mobiusi  a  
Mobius function maps natural number to {-1, 0, 1}

It is defined as follows:
    1) `1` if `n = 1`.
    2) `0` if `n` has a squared prime factor.
    3) `(-1)^k` if `n` is a square-free positive integer with `k`
       number of prime factors.

It is an important multiplicative function in number theory
and combinatorics.  It has applications in mathematical series,
algebraic number theory and also physics (Fermion operator has very
concrete realization with Mobius Function model).

Examples
========

>>> from sympy.functions.combinatorial.numbers import mobius
>>> mobius(13*7)
1
>>> mobius(1)
1
>>> mobius(13*7*5)
-1
>>> mobius(13**2)
0

Even in the case of a symbol, if it clearly contains a squared prime factor, it will be zero.

>>> from sympy import Symbol
>>> n = Symbol("n", integer=True, positive=True)
>>> mobius(4*n)
0
>>> mobius(n**2)
0

References
==========

.. [1] https://en.wikipedia.org/wiki/M%C3%B6bius_function
.. [2] Thomas Koshy "Elementary Number Theory with Applications"
.. [3] https://oeis.org/A008683

TFc                Z   UR                   SL a  [        S5      eUR                  SL a  [        S5      eUR                  SL a  [
        R                  $ U[
        R                  L a  [
        R                  $ S nS [        R                  " U5       5        GH  u  p4UR                   SL a  UR                  SL a  UR                   SL a  UR                  SL a  [        [
        R                  U5      nUSL a  [
        R                  nMs  UR                  SL a  [        U5      n[        S UR                  5        5       5      (       a  [
        R                  nM  USL aB  [!        U5      S-  (       a  [
        R                  O[
        R                  nUc  UnGM  X'-  nGM  GM  GM    g    U$ )NFr  r  Tc              3  @   #    U  H  oR                  5       v   M     g 7fru   )as_base_exp)r   _s     rH   r   mobius.eval.<locals>.<genexpr>  s     ?.>]]__.>   c              3  *   #    U  H	  oS :  v   M     g7frp  ri   r   vs     rH   r   r*    s     ;*:Qq5*:   r   )r   r  r   r   r^   r   r   r   r   	make_argsr   r   r   r)   r  r  r   )r   rV   rX  r  r  ltfactorsr   s           rH   r   mobius.eval  sG   <<5 455==E!=>>::== :55L?cmmA.>?DA||t#(=||t#(=155!_:VVF\\T)'lG;'..*:;;;!"u-0\A-=AMM155!>%&F"KF %	 * # @$ rJ   ri   Nr  ri   rJ   rH   r%  r%    s&    +X JH rJ   r%  c                  0    \ rS rSrSrSrSr\S 5       rSr	g)primenui  a  
Calculate the number of distinct prime factors for a positive integer n.

If n's prime factorization is:

.. math ::
    n = \prod_{i=1}^k p_i^{m_i},

then ``primenu(n)`` or `\nu(n)` is:

.. math ::
    \nu(n) = k.

Examples
========

>>> from sympy.functions.combinatorial.numbers import primenu
>>> primenu(1)
0
>>> primenu(30)
3

See Also
========

sympy.ntheory.factor_.factorint

References
==========

.. [1] https://mathworld.wolfram.com/PrimeFactor.html
.. [2] https://oeis.org/A001221

Tc                H   UR                   SL a  [        S5      eUR                  SL a  [        S5      eUR                  SL a  [
        R                  $ U[
        R                  L a  [
        R                  $ UR                  SL a  [        [        [        U5      5      5      $ g NFr  r  T)r   r  r   r   r^   r   r   r   r   r   r)   r   s     rH   r   primenu.eval  s    <<5 455==E!=>>::55L:66M<<4S1&''  rJ   ri   N
rj   rk   rl   rm   rn   r   r   r   r   rp   ri   rJ   rH   r5  r5    s&    !D JN
( 
(rJ   r5  c                  0    \ rS rSrSrSrSr\S 5       rSr	g)
primeomegai+  a!  
Calculate the number of prime factors counting multiplicities for a
positive integer n.

If n's prime factorization is:

.. math ::
    n = \prod_{i=1}^k p_i^{m_i},

then ``primeomega(n)``  or `\Omega(n)` is:

.. math ::
    \Omega(n) = \sum_{i=1}^k m_i.

Examples
========

>>> from sympy.functions.combinatorial.numbers import primeomega
>>> primeomega(1)
0
>>> primeomega(20)
3

See Also
========

sympy.ntheory.factor_.factorint

References
==========

.. [1] https://mathworld.wolfram.com/PrimeFactor.html
.. [2] https://oeis.org/A001222

Tc                d   UR                   SL a  [        S5      eUR                  SL a  [        S5      eUR                  SL a  [
        R                  $ U[
        R                  L a  [
        R                  $ UR                  SL a+  [        [        [        U5      R                  5       5      5      $ g r7  )r   r  r   r   r^   r   r   r   r   sumr)   r  r   s     rH   r   primeomega.evalR  s    <<5 455==E!=>>::55L:66M<<4S1,,./00  rJ   ri   Nr9  ri   rJ   rH   r;  r;  +  s&    "F JN
1 
1rJ   r;  c                  0    \ rS rSrSrSrSr\S 5       rSr	g)totienti`  a%  
Calculate the Euler totient function phi(n)

``totient(n)`` or `\phi(n)` is the number of positive integers `\leq` n
that are relatively prime to n.

Examples
========

>>> from sympy.functions.combinatorial.numbers import totient
>>> totient(1)
1
>>> totient(25)
20
>>> totient(45) == totient(5)*totient(9)
True

See Also
========

sympy.ntheory.factor_.divisor_count

References
==========

.. [1] https://en.wikipedia.org/wiki/Euler%27s_totient_function
.. [2] https://mathworld.wolfram.com/TotientFunction.html
.. [3] https://oeis.org/A000010

Tc                   UR                   SL a  [        S5      eUR                  SL a  [        S5      eU[        R
                  L a  [        R
                  $ UR                  SL a  US-
  $ [        U[        5      (       a)  [	        [        S UR                  5        5       5      5      $ UR                  SL a2  [	        [        S [        U5      R                  5        5       5      5      $ g )NFr  r  TrD   c              3  @   #    U  H  u  pXS -
  -  US -
  -  v   M     g7frp  ri   r   rZ   r  s      rH   r   totient.eval.<locals>.<genexpr>  s      ?YTQ!c(AaC.Yr+  c              3  @   #    U  H  u  pXS -
  -  US -
  -  v   M     g7frp  ri   rC  s      rH   r   rD    s"     J5ITQ!c(AaC.5Ir+  )r   r  r   r   r   r   r^   rb  r   r   r  r   r)   r   s     rH   r   totient.eval  s    <<5 455==E!=>>:55L::q5LaT?QWWY??@@<<4TJYq\5G5G5IJJKK  rJ   ri   N
rj   rk   rl   rm   rn   r   r   r   r   rp   ri   rJ   rH   r@  r@  `  s'    < JKL LrJ   r@  c                  0    \ rS rSrSrSrSr\S 5       rSr	g)reduced_totienti  a-  
Calculate the Carmichael reduced totient function lambda(n)

``reduced_totient(n)`` or `\lambda(n)` is the smallest m > 0 such that
`k^m \equiv 1 \mod n` for all k relatively prime to n.

Examples
========

>>> from sympy.functions.combinatorial.numbers import reduced_totient
>>> reduced_totient(1)
1
>>> reduced_totient(8)
2
>>> reduced_totient(30)
4

See Also
========

totient

References
==========

.. [1] https://en.wikipedia.org/wiki/Carmichael_function
.. [2] https://mathworld.wolfram.com/CarmichaelFunction.html
.. [3] https://oeis.org/A002322

Tc           	        UR                   SL a  [        S5      eUR                  SL a  [        S5      eU[        R
                  L a  [        R
                  $ UR                  SL a  US-
  $ [        U[        5      (       aT  SnSU;   a  SUS   :  a  SUS   S-
  -  OUS   n[	        [        [        U5      /S UR                  5        5       Q76 5      $ UR                  SL ab  [        [        U5      S5      u  pU(       d  SnOSU:  a  SUS-
  -  n[	        [        U/S [        U5      R                  5        5       Q76 5      $ g )	NFr  r  TrD   r   c              3     #    U  H8  u  pUS :w  d  M  [        US-
  5      [        U5      [        US-
  5      -  -  v   M:     g7f)r   rD   N)r   rC  s      rH   r   'reduced_totient.eval.<locals>.<genexpr>  s<     "]VW[\V\#<3qs8CFC!H,<#<s
   A0Ac              3  @   #    U  H  u  pUS -
  XS -
  -  -  v   M     g7frp  ri   rC  s      rH   r   rL    s"     N9MqsA!Hn9Mr+  )r   r  r   r   r   r   r^   rb  r   r   r   r  r   r   r)   )r   rV   ts      rH   r   reduced_totient.eval  s!   <<5 455==E!=>>:55L::q5LaAAv)*QqTQ1Q4!8_qtSQ^"]"]^__<<4#a&!$DAQ!a%LSON19K9K9MNOPP  rJ   ri   NrG  ri   rJ   rH   rI  rI    s'    < JKQ QrJ   rI  c                  0    \ rS rSrSrSrSr\S 5       rSr	g)primepii  a  Represents the prime counting function pi(n) = the number
of prime numbers less than or equal to n.

Examples
========

>>> from sympy.functions.combinatorial.numbers import primepi
>>> from sympy import prime, prevprime, isprime
>>> primepi(25)
9

So there are 9 primes less than or equal to 25. Is 25 prime?

>>> isprime(25)
False

It is not. So the first prime less than 25 must be the
9th prime:

>>> prevprime(25) == prime(9)
True

See Also
========

sympy.ntheory.primetest.isprime : Test if n is prime
sympy.ntheory.generate.primerange : Generate all primes in a given range
sympy.ntheory.generate.prime : Return the nth prime

References
==========

.. [1] https://oeis.org/A000720

Tc                r   U[         R                  L a  [         R                  $ U[         R                  L a  [         R                  $ UR                  SL a  [        S5      e[        U[        S5      5      SL a  [         R                  $  [        U5      n[        [        U5      5      $ ! [
         a     g f = f)NFzn should be a realr   T)	r   r   NegativeInfinityr   is_realr  r   r   r.   r   s     rH   r   primepi.eval  s    

?::"""66M99011AaD>T!66M	AA !~  		s   
B) )
B65B6ri   Nr9  ri   rJ   rH   rQ  rQ    s&    "F JN rJ   rQ  c                      \ rS rSrSrSrg)_MultisetHistogrami	  ri   N)rj   rk   rl   rm   	__slots__rp   ri   rJ   rH   rW  rW  	  s    IrJ   rW  r{   rz   Nc                  ^  [        T [        5      (       a  [        S T R                  5        5       5      (       d  [        e[        T R                  5       5      n[        U 4S jT  5       5      n[        T  Vs/ s H  nT U   S:  d  M  T U   PM     snX!/-   5      $ [        T 5      m [        T 5      n[        U5      n[        T 5      nXV:X  a  S/U-  Xf/-   m [        T 5      $ [        [        U[        U5      5      5      n[        [        [        U5      SU-  5      5      nT  H  n	XU	   ==   S-  ss'   M     [        U5      $ s  snf )a"  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              3  Z   #    U  H!  n[        U[        5      =(       a    US :  v   M#     g7f)r   N)rb  r   r-  s     rH   r   &_multiset_histogram.<locals>.<genexpr>	  s#     E*Q:a%0!q&0*s   )+c              3  >   >#    U  H  nTU   S :  d  M  Sv   M     g7f)r   rD   Nri   )r   r  rV   s     rH   r   r[   	  s     -q!AaD1HAAqs   	r   rD   r   )rb  dictr   r  r   r=  rW  listsetr   ziprE   _multiset_histogram)
rV   totr  r  r   lenslennr  dr   s
   `         rH   rb  rb  	  s    !TE!((*EEE!((*o-q--!"?AadQh41Q4"?5,"NOOGF1v1v<DD<'A%a((Qd$%U4[$t),-AdGqLG "1%% #@s   9E
	Ec                     [        U 5      n [        [        XU5      5      $ ! [         a!    [        [        [	        U 5      X5      5      s $ f = f)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>   r   r   _nPrb  )rV   r  replacements      rH   nPrj  1	  sQ    vD1I 3q[)**  Ds.q11BCCDs   " (AAc           	     V  ^ ^ US:X  a  g[        T [        5      (       ac  Uc"  [        U U4S j[        T S-   5       5       5      $ T(       a  T U-  $ UT :  a  gUT :X  a  [	        U5      $ US:X  a  T $ [        T U-
  S-   T 5      $ [        T [        5      (       Ga  Uc)  [        U U4S j[        T [           S-   5       5       5      $ T(       a  T [           U-  $ UT [           :X  a?  [	        U5      [        T [            Vs/ s H  o3S:  d  M
  [	        U5      PM     sn5      -  $ UT [           :  a  gUS:X  a	  T [           $ Sn[        T 5      m [        [        T [           5      5       H  nT U   (       d  M  T [        ==   S-  ss'   T U   S:X  aH  ST U'   T [        ==   S-  ss'   U[        [        T 5      US-
  5      -  nT [        ==   S-  ss'   ST U'   O5T U==   S-  ss'   U[        [        T 5      US-
  5      -  nT U==   S-  ss'   T [        ==   S-  ss'   M     U$ g s  snf )Nr   rD   c              3  >   >#    U  H  n[        TUT5      v   M     g 7fru   rh  r   r   rV   ri  s     rH   r   _nP.<locals>.<genexpr>{	  s     D|!s1a--|   c              3  >   >#    U  H  n[        TUT5      v   M     g 7fru   rm  rn  s     rH   r   ro  	  s     H7G!s1a--7Grp  )rb  r   r=  rE   r$   rI   rW  _N_ITEMSr   _Mr_  r   rh  )rV   r  ri  r   rc  s   ` `  rH   rh  rh  s	  s    	Av!Z  9DuQU|DDDa4KU!VQ<!VH AEAIq))	A)	*	*9HuQrUQY7GHHHV9a<!B%ZQ<AbE%KEqUlilE%K LLL2Y!VV9 CQA3qu:&t"
Q419AaDfINI31!4a!e<<CfINIAaDaDAID31!4a!e<<CaDAID"
 ' J= 
+ &Ls   >	H&
H&
c           	     n   [        U 5      n [        U 5      nUS-   S-  nS/U R                  5       S-   -  nUR                  SU[	        U5      -
  -  5        USU nU (       a  U R                  5       nUS-   nUSS n[        S[        U[	        U5      5      5       H  nX7==   X7S-
     -  ss'   M     [        XR5       H  nX7==   X7S-
     XgU-
     -
  -  ss'   M     U (       a  M  [        [        U5      5      nUS-  (       a  X8-   nOXSS& [        [        5      n	[        U5       H	  u  pzXU'   M     U	$ )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.

r   rD   r]  Nr{   )r_  r=  popextendr   rE   r  reversedr   r   	enumerate)rV   ordneedrvniNwasr   revrf  r  s              rH   _AOP_productr  	  s)   6 	QA
a&C!Ga<D
aeegk	BIIddSWn%&	ETB
UUWFeq#aR/*AERAYE +qAERAYU++E   ! x|
C
QwX23CA"! HrJ   c                  ^ ^ [        T [        5      (       ag  Uc.  T(       d  ST -  $ [        U U4S j[        T S-   5       5       5      $ US:  a  [	        S5      eT(       a  [        T U-   S-
  U5      $ [        T U5      $ [        T [        5      (       a  T [           nUcB  T(       d  [        S T [            5       5      $ [        U U4S j[        US-   5       5       5      $ T(       a  [        T [           UT5      $ USUS-
  4;   a	  T [           $ USU4;   a  g[        [        T [           5      5      U   $ [        [        T 5      UT5      $ )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

r   c              3  >   >#    U  H  n[        TUT5      v   M     g 7fru   nCrn  s     rH   r   nC.<locals>.<genexpr>"
       Clr!Q,,lrp  rD   r   zk cannot be negativec              3  *   #    U  H	  oS -   v   M     g7frp  ri   )r   r  s     rH   r   r  ,
  s     15aE5r/  c              3  >   >#    U  H  n[        TUT5      v   M     g 7fru   r  rn  s     rH   r   r  -
  r  rp  )rb  r   r=  rE   r   r#   rW  rr  r   rt  r  rs  r  tuplerb  )rV   r  ri  r~  s   ` ` rH   r  r  	  s+   D !Z  9!tCeAElCCCq5344AEAIq))1~!'((bE911R5111CeAElCCCaiK001a!e*_V91a&[E!B%L)!,,%a(![99rJ   c                V   Xs=:X  a  S:X  a   [         R                  $   SX4;   a  [         R                  $ X:X  a  [         R                  $ XS-
  :X  a  [        U S5      $ XS-
  :X  a  SU -  S-
  [        U S5      -  S-  $ XS-
  :X  a  [        U S5      [        U S5      -  $ [	        X5      $ )Nr   rD   r   r   r   )r   r   r   r#   
_stirling1rV   r  s     rH   _eval_stirling1r  :
  s    {{uu QF{vv 	vuu	
!e1~	
!e!a!Q'))	
!e1~hq!n,,arJ   c                    SS/S/US-
  -  -   n[        SU S-   5       H4  n[        [        X5      SS5       H  nUS-
  X$   -  X$S-
     -   X$'   M     M6     [        X!   5      $ Nr   rD   r   r{   rE   r  r   rV   r  rowr   r   s        rH   r  r  M
  so    a&!ac
C1ac]s1xB'AcSV^cA#h.CF (  36?rJ   c                4   Xs=:X  a  S:X  a   [         R                  $   SX4;   a  [         R                  $ X:X  a  [         R                  $ XS-
  :X  a  [        U S5      $ US:X  a  [         R                  $ US:X  a  [	        SU S-
  -  S-
  5      $ [        X5      $ )Nr   rD   r   )r   r   r   r#   r   
_stirling2r  s     rH   _eval_stirling2r  V
  s    {{uu QF{vv 	vuu	
!e1~	
auu	
aq1q5zA~&&arJ   c                    SS/S/US-
  -  -   n[        SU S-   5       H1  n[        [        X5      SS5       H  nXBU   -  X$S-
     -   X$'   M     M3     [        X!   5      $ r  r  r  s        rH   r  r  i
  sk    a&!ac
C1ac]s1xB'AQZ#c(*CF (  36?rJ   c                d   [        U 5      n [        U5      nU S:  a  [        S5      eX:  a  [        R                  $ U(       a  [	        X-
  S-   X-
  S-   5      $ U(       a!  [        R
                  X-
  -  [        X5      -  $ US:X  a  [        X5      $ US:X  a  [	        X5      $ [        SU-  5      e)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 nonnegativerD   r   zkind must be 1 or 2, not %s)r>   r   r   r   r  r   r  )rV   r  rf  kindsigneds        rH   stirlingr  r
  s    l 	q	Aq	A1u011uvv quqy!%!)44	}}qu%oa&;;;qyq$$	q$$6:;;rJ   c           	        X:  d  US:  a  gUSU 4;   a  gUS:X  a  gUS:X  a  U S-  $ X-
  nUS::  a  U$ SU-  U :  aB  [        U5      nX!-
  S:  a-  U[        [         R                  " [        X!-
  5      5      5      -  nU$ S/U-  n[	        SUS-   5       H/  n[	        US-   U5       H  nXF==   XFU-
     -  ss'   M     US-  nM1     S[        USU-
  S 5      -   $ )ztReturn the partitions of ``n`` items into ``k`` parts. This
is used by ``nT`` for the case when ``n`` is an integer.r   rD   r   r   N)r0   r=  
fetch_itemslicerE   )rV   r  rf  rc  rZ   r   r  s          rH   _nTr  
  s    
 	uAQF{AvAv!t	AAvsax
 Q5193~00qu>??C
 
AA1a!e_Aq/ADA!eHD "	Q  Aa!efIrJ   c                  ^  [        T [        5      (       aN  Uc  [        T 5      $ [        U[        5      (       a+  [        T 5      m [        U5      n[	        [        T U5      5      $ [        T [        5      (       dP   [        [        T 5      5      nUS::  a  [        [        T 5      U5      $ U[        T 5      :X  a  [        U5      m [        eT [           nUc  US:X  a  gUSU4;   a  gUS:X  d	  US:X  aV  UcS  [        US5      u  pE[        U 4S j[        SUS-   5       5       5      nU(       d  U[!        T U5      S-  -  nUc  US-  nU$ UT ["           :X  a  Uc  [%        U5      $ ['        X15      $ [)        5       nUc  UR+                  T [,           5      $ SnUR/                  T [,           US-
  U5       H  nUS-  nM
     U$ ! [         a    [        T 5      m  GNf = f)a  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

rD   r   c              3  <   >#    U  H  n[        TU5      v   M     g 7fru   r  )r   r   rV   s     rH   r   nT.<locals>.<genexpr>n  s     3?aAq?s   r   )rb  r   r  r>   r   r  rW  r   r`  nTrE   r  rb  rr  divmodr=  r  rs  r  r  r7   count_partitionsrt  
enum_range)	rV   r  rY  r~  r  r  r|  rc  discards	   `        rH   r  r  
  s   R !Z  9Q<a$$q	Aq	A3q!9%%a+,,
	' CFAAv#a&!}$c!f!HO 	
"AyQ!VQF{AvaAIa|35AE?33"Q(A+B9!GB	AfI~97N~"$Ay!!!B%(( C<<"qsA.q /J9  	'#A&A	's   ;.G  * G   GGc                      \ rS rSrSr\S 5       r\S 5       r\S 5       r\\	" \
R                  \
R                  /5      S 5       5       r\S 5       rSrg	)
motzkini  a  
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                     [        U 5      n U S:  aF  U S;   a  gSnSnSnX :  a-  SU-  S-   U-  SU-  S-
  U-  -   US-   -  nUS-  nUnUnX :  a  M-  X :X  a  ggg! [         a     gf = f)NFr   )rD   r   TrD   r   r   )r>   r   )rV   tn1tnr   rF   s        rH   
is_motzkinmotzkin.is_motzkin  s    	q	A q5F{CBA&cAgr\QqS1WcM1AE:Q	 & w +  		s   A 
A'&A'c                0   SU s=::  a  U::  a  O  O|/ nU Ss=::  a  U::  a  O  OUR                  S5        SnSnSnXA::  aL  X@:  a  UR                  U5        SU-  S-   U-  SU-  S-
  U-  -   US-   -  nUS-  nUn[        U5      nXA::  a  ML  U$ [        S5      e)Nr   rD   r   r   zEThe provided range is not valid. This condition should satisfy x <= y)appendr   r   )rK   re   motzkinsr  r  r   rF   s          rH   find_motzkin_numbers_in_range%motzkin.find_motzkin_numbers_in_range  s    ;Q;HA{{"CBA'7OOB'cAgr\QqS1WcM1AE:QV ' O deerJ   c                J    [        U 5      n U S:  a  [        S5      eS/nU S:  a  UR                  S5        SnSnSnX@::  aG  UR                  U5        SU-  S-   U-  SU-  S-
  U-  -   US-   -  nUS-  nUn[        U5      nX@::  a  MG  U$ ! [         a    [        S5      ef = f)N.The provided number must be a positive integerr   rD   r   r   )r>   r   r  r   )rV   r  r  r  r   rF   s         rH   find_first_n_motzkinsmotzkin.find_first_n_motzkins  s    	Oq	A q5MNN36OOAfOOBA#'21q#-A6AFACQB f #  	OMNN	Os   B B"c                H    SU -  S-   US   -  SU -  S-
  US   -  -   U S-   -  $ )Nr   rD   r{   r   rz   ri   r   s     rH   _motzkinmotzkin._motzkin  s:     1q$r("acAgtBx%77QUCCrJ   c                     [        U5      nUS:  a  [        S5      e[        U R                  US-
  5      5      $ ! [         a    [        S5      ef = f)Nr  r   rD   )r>   r   r   r  r   s     rH   r   motzkin.eval  s]    	Oq	A q5MNNs||AE*++	  	OMNN	Os	   ; Ari   N)rj   rk   rl   rm   rn   ro   r  r  r  r=   r   r   r  r   r   rp   ri   rJ   rH   r  r    s    "H  4 f f*  , aeeQUU^$D % D , ,rJ   r  )rV   r  c               h  ^ SSK Jn  SSKJn  SSKJn  S mXU4R                  S5      S:w  a  [        S5      eU Gb  [        U [        5      (       a  [        S	5      eU (       d  [        R                  $ [        U 5      [        La  [        U 5      n[!        U5      nOd[        U 5      [        L aR  [#        U4S
 jU R%                  5        5       5        U R'                  5        V	V
s0 s H  u  pU
(       d  M  X_M     nn	n
SnW(       d  [        R                  $ [)        UR%                  5       5      n[!        UR%                  5       5      n[+        U5      nGO`Ub*  T" U5        U(       d  [        R                  $ [-        U5      $ UGb/  [        U[        5      (       aS  [#        U4S jUR'                  5        5       5        UR'                  5        V	V
s0 s H  u  pX-  (       d  M  X_M     nn	n
Ou[/        U5      (       d  [        U[0        5      (       aE  [        U5      n[#        U4S jU 5       5        [!        U V s/ s H  o (       d  M  U PM     sn 5      nO[        S5      eU(       d  [        R                  $ [)        S UR'                  5        5       5      n[)        UR%                  5       5      nSn[3        [5        W5      5      nUS:X  a  [-        W5      $ [+        U5      nUS-  W:  a  [        R                  $ US-  U:X  a0  WS:X  a  US:X  a  [        R6                  $ US-
  U:X  a  [9        U5      $ US:  a  Ub  U(       ap  WcI  / nSn UR'                  5        H1  u  p:[;        U
5       H  nUR=                  U /U-  5        U S-  n M     M3     [?        [)        S [A        U5       5       5      5      $ SSK!J"n  [?        [G        U" U" U* 5      [I        UR'                  5        V Vs/ s H  u  pU" X5      U-  PM     snn 6 -  US[J        45      5      5      $ s  sn
n	f s  sn
n	f s  sn f s  snn f )ay  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rK   c                f    [        U [        5      (       d  [        S5      eU S:  a  [        S5      eg)Nzexpecting integer valuesr   zvalue must not be negativeT)rb  r   r  r   r  s    rH   oknD.<locals>.ok7  s1    !Z((677q59::rJ   Nr   zenter only 1 of i, n, or mz"items must be a list or dictionaryc              3  4   >#    U  H  nT" U5      v   M     g 7fru   ri   )r   r)  r  s     rH   r   nD.<locals>.<genexpr>I  s     *z!1z   c              3  V   >#    U  H  u  pT" U5      =(       a    T" U5      v   M      g 7fru   ri   )r   r   r   r  s      rH   r   r  X  s!     6IDA1"Q%Is   &)c              3  4   >#    U  H  nT" U5      v   M     g 7fru   ri   )r   r   r  s     rH   r   r  \  s     !q!1qr  zexpecting iterablec              3  .   #    U  H  u  pX-  v   M     g 7fru   ri   )r   r  r.  s      rH   r   r  b  s     /s   rD   r   c              3  &   #    U  H  nS v   M	     g7frp  ri   r  s     rH   r   r  y  s     ?&>1&>s   )exp)&sympy.integrals.integralsr  #sympy.functions.special.polynomialsr  	sympy.abcrK   countr   rb  r   r  r   r   typer^  r_  r:   r   r  r  r=  r   r%   r<   strr   maxr   r$   rE   rw  r   r;   rS  r  r  r   r   )r   bruterV   r  r  r  rK   r   msr  r.  r~  countsnkeybignvalr   r  r  s                     @rH   nDr    sU   X 4< 	
ayt!566}a$$@AA66M7$QA!B!W_*qxxz**#$7792941$!$9B2A66M		"))+&2w	

166MA	
a6AGGI66'(wwy8ytqACdadyF8Fa[[Jq#..QA!q!!!1!Qqq!12F01166M///6==?#
c&k
C
axD!!v;D
1uqyvv
1uz1955L!8s?S>!1u%9AAqAHHaSUOFA " ' s?&;A&>??@@:3yaR&,lln/6&4dan/6 *7 "79:Ar
D E F Fc 3 9 2</6s*   %P6PP#&P#>
P)P)+P.)NF)Nr   Fru   r'  )rn   
__future__r   mathr   collectionsr   typingr   
sympy.corer   r   r	   r
   sympy.core.cacher   sympy.core.containersr   sympy.core.exprr   sympy.core.functionr   r   r   sympy.core.logicr   sympy.core.mulr   sympy.core.numbersr   r   r   r   r   r   sympy.core.relationalr   r   r   r   sympy.external.gmpyr   r   r   r    r!   r"   (sympy.functions.combinatorial.factorialsr#   r$   r%   rS  r'   $sympy.functions.elementary.piecewiser(   sympy.ntheory.factor_r)   r*   r+   r,   r-   sympy.ntheory.generater.   sympy.ntheory.partitions_r/   r0   sympy.ntheory.primetestr1   r2   sympy.polys.appellseqsr3   r4   r5   sympy.polys.polytoolsr6   sympy.utilities.enumerativer7   sympy.utilities.exceptionsr9   sympy.utilities.iterablesr:   r;   r<   sympy.utilities.memoizationr=   sympy.utilities.miscr>   r}  r?   r@   mpmath.libmprA   rv   rI   r}   rM   rr   r   r   r   r  r)  rr  r  r  r  r  r	  r  r  r  r!  r%  r5  r;  r@  rI  rQ  r  rW  rr  rs  r  rt  rb  rj  rh  r  r  r  r  r  r  r  r  r  r  r  ri   rJ   rH   <module>r     s   #  #  , , $ &   O O &  > > 9 9 T T  6 :_ _ + @ 6 L L ( B @ O O 7 '  &!
 c{x+ x+DNN NNp*>O *>hP" P"tt, t,|OD? ODrI% I%f],O ],NH9o H9dB= B=Xi,O i,d7 7t@jO @jFEG_ EGP-5o -5`D3O D3N$6 $6NM_ M`1(o 1(h21 21j/Lo /Ld7Qo 7Qt5o 5~  	
4&:?+D 	1 	1h 	0 	0f[:|& 	 	& 	 	i<X 	(  	( VwBz,o z,ztF tFrJ   