+
    i                    ~   ^ RI Ht ^ RIHt ^ RIHtHtHtHt ^ RI	H
t
 ^ RIHtHtHt ^ RIHt ^ RIHtHtHt ^ RIHt ^ R	IHt ^ R
IHt ^ RIHt ^ RIHt ^ RI H!t"H#t#H$t%  ! R R]4      t& ! R R]&4      t! ! R R]&4      t' ! R R]&4      t( ! R R]&4      t) ! R R]&4      t* ! R R]&4      t+]*t,]+t- ! R R]&4      t.R# )    )annotations)reduce)SsympifyDummyMod)cacheit)DefinedFunctionArgumentIndexError	PoleError)	fuzzy_and)IntegerpiI)Eq)gmpy)sieve)binomial_mod)Poly)	factorialprodsqrtc                       ] tR t^tRtR tRtR# )CombinatorialFunctionz(Base class for combinatorial functions. c                	z    ^ RI Hp V! V 4      pVR,          pV! V4      VR,          V! V 4      ,          8:  d   V# V # )r   )combsimpmeasureratio)sympy.simplify.combsimpr   )selfkwargsr   exprr   s   &,   f/var/www/html/photoedit/myenv/lib/python3.14/site-packages/sympy/functions/combinatorial/factorials.py_eval_simplify$CombinatorialFunction._eval_simplify   s=    4 ~#4=F7OGDM99K     N)__name__
__module____qualname____firstlineno____doc__r$   __static_attributes__r'   r&   r#   r   r      s
    2r&   r   c                  ,   ] tR t^$t$ RtR(R lt. ^N^N^N^N^N^N^N^#N^#NRN^?NRN^NRNRNRNRNRNR	NR
NRNRNRNRNRNRNRNRNRNRNRNRNRNt. tR]R&   ]	R 4       t
]	R 4       t]	R 4       tR tR tR)R ltR tR  tR! tR" tR# tR$ tR% tR&tR'# )*r   a  Implementation of factorial function over nonnegative integers.
By convention (consistent with the gamma function and the binomial
coefficients), factorial of a negative integer is complex infinity.

The factorial is very important in combinatorics where it gives
the number of ways in which `n` objects can be permuted. It also
arises in calculus, probability, number theory, etc.

There is strict relation of factorial with gamma function. In
fact `n! = gamma(n+1)` for nonnegative integers. Rewrite of this
kind is very useful in case of combinatorial simplification.

Computation of the factorial is done using two algorithms. For
small arguments a precomputed look up table is used. However for bigger
input algorithm Prime-Swing is used. It is the fastest algorithm
known and computes `n!` via prime factorization of special class
of numbers, called here the 'Swing Numbers'.

Examples
========

>>> from sympy import Symbol, factorial, S
>>> n = Symbol('n', integer=True)

>>> factorial(0)
1

>>> factorial(7)
5040

>>> factorial(-2)
zoo

>>> factorial(n)
factorial(n)

>>> factorial(2*n)
factorial(2*n)

>>> factorial(S(1)/2)
factorial(1/2)

See Also
========

factorial2, RisingFactorial, FallingFactorial
c                	    ^ RI HpHp V^8X  dG   V! V P                  ^ ,          ^,           4      V! ^ V P                  ^ ,          ^,           4      ,          # \	        W4      h)r   )gamma	polygamma)'sympy.functions.special.gamma_functionsr0   r1   argsr   )r    argindexr0   r1   s   &&  r#   fdifffactorial.fdiffU   sH    Nq=1)*9Q		!q8H+III$T44r&   i;  i  i  i  i#  iS i{/  i! im  i isX iU iP
 ioik iIi/L iSi} i#z	list[int]_small_factorialsc                	~   V^!8  d   V P                   V,          # \        \        V4      4      . r2\        P                  ! ^V^,           4       FK  p^Tre Wd,          pV^ 8  d   V^,          ^8X  d   WT,          pK(  K*   T^8  g   K:  TP                  T4       KM  	  \        P                  ! V^,           V^,          ^,           4       F*  pW,          ^,          ^8X  g   K  VP                  V4       K,  	  \        \        P                  ! V^,          ^,           V^,           4      4      p\        V4      pWx,          # )!   )_small_swingint_sqrtr   
primerangeappendr   )	clsnNprimesprimepq	L_product	R_products	   &&       r#   _swingfactorial._swingd   s   r6##A&&E!Hrv))!QU3!1KA1uq5A:JA & q5MM!$ 4 ))!a%A:J!#q(MM%( ; U--adQhA>?IVI&&r&   c                	~    V^8  d   ^# V P                  V^,          4      ^,          V P                  V4      ,          # )   )
_recursiverH   )r?   r@   s   &&r#   rL   factorial._recursive   s1    q5NN1a4(!+SZZ]::r&   c                	   \        V4      pVP                  '       Ed_   VP                  '       d   \        P                  # V\        P
                  J d   \        P
                  # VP                  '       Ed   VP                  '       d   \        P                  # VP                  pV^8  df   V P                  '       g9   ^p\        ^^4       F&  pW#,          pV P                  P                  V4       K(  	  V P                  V^,
          ,          pM^\        e   \        P                  ! V4      pM?\        V4      P!                  R4      pV P#                  V4      ^W,
          ,          ,          p\%        V4      # R# R# )   N1)r   	is_Numberis_zeror   OneInfinity
is_Integeris_negativeComplexInfinityrD   r7   ranger>   _gmpyfacbincountrL   r   )r?   r@   resultibitss   &&   r#   evalfactorial.eval   s   AJ;;;yyyuuajjzz!===,,,A2v"444%&F%*1b\ & # 5 5 < <V D &2 "%!6!6qs!; *!&1  #1v||C0!$!21qx=!@"6?*=  r&   c                	   ^\        \        V4      4      rC^.V,          p^p\        P                  ! ^V^,           4       Fg  pV^8  d$   ^ W,          rV'       d   Wh,          pW,          pK  Wd8  d   WV,          V,          V,          WV&   KM  V\	        WvV4      ,          V,          pKi  	  \        V4       F9  w  rV	^ 8X  g   V
^8X  d   K  V
^ 8X  d    ^ # V\	        WV4      ,          V,          pK;  	  V#    )r;   r<   r   r=   pow	enumerate)r    r@   rE   resrA   pwmrC   yexbss   &&&        r#   _facmodfactorial._facmod   s    CaMQ SU%%aQ/E1u!*1FAKAuea#e**Q. 0  mFBQw"'Qwc"!n$q(C $ 
r&   c                	"   V P                   ^ ,          pVP                  '       Edi   VP                  '       EdT   VP                  '       Ed?   \        V4      pW2,
          pVP                  '       d   \
        P                  # VP                  pV^8X  dE   V'       d
   RV,          # VRJ d,   V^,
          P                  '       d   \
        P                  # R# R# VP                  '       d   VP                  '       d   \        \        W$V34      w  r$pV'       dU   V^,
          V8  dG   V P                  V^,
          V4      p\        Wc^,
          V4      pV^,          '       d   V) pWa,          # V P                  W#4      pWa,          # R# R# R# R# R# )r   FN)r3   
is_integeris_nonnegativeabsis_nonpositiver   Zerois_primerU   mapr;   rm   re   )r    rE   r@   aqdisprimefcs   &&     r#   	_eval_Modfactorial._eval_Mod   s.   IIaL<<<A,,,QBAvv++6
 !Av E)rAv.E.E.E vv /F)\\\alll"3r
3HA"AEAI!\\!a%4 !VR0Q33"$B 6M "\\!06M '3\! 2>,<r&   c                	,    ^ RI Hp V! V^,           4      # r   r0   r2   r0   )r    r@   	piecewiser!   r0   s   &&&, r#   _eval_rewrite_as_gamma factorial._eval_rewrite_as_gamma   s    AQU|r&   c                	    ^ RI Hp VP                  '       d-   VP                  '       d   \	        RRR7      pV! WD^V34      # R# R# )r   )Productr^   T)integerN)sympy.concrete.productsr   rr   rq   r   )r    r@   r!   r   r^   s   &&,  r#   _eval_rewrite_as_Product"factorial._eval_rewrite_as_Product   s>    3c4(A1!Qi(( !-r&   c                	    V P                   ^ ,          P                  '       d(   V P                   ^ ,          P                  '       d   R# R# R# r   TNr3   rq   rr   r    s   &r#   _eval_is_integerfactorial._eval_is_integer   5    99Q<"""tyy|'B'B'B (C"r&   c                	    V P                   ^ ,          P                  '       d(   V P                   ^ ,          P                  '       d   R# R# R# r   r   r   s   &r#   _eval_is_positivefactorial._eval_is_positive   r   r&   c                	    V P                   ^ ,          pVP                  '       d(   VP                  '       d   V^,
          P                  # R# R# r   Nr   r    xs   & r#   _eval_is_evenfactorial._eval_is_even  :    IIaL<<<A,,,E))) -<r&   c                	    V P                   ^ ,          pVP                  '       d(   VP                  '       d   V^,
          P                  # R# R# r   r   r   s   & r#   _eval_is_compositefactorial._eval_is_composite  r   r&   c                	x    V P                   ^ ,          pVP                  '       g   VP                  '       d   R# R# r   )r3   rr   is_nonintegerr   s   & r#   _eval_is_realfactorial._eval_is_real  s+    IIaLq  /r&   c                	   V P                   ^ ,          P                  V4      pVP                  V^ 4      pVP                  '       d   \        P
                  # VP                  '       g   V P                  V4      # \        RV ,          4      h)r   zCannot expand %s around 0)	r3   as_leading_termsubsrR   r   rS   is_infinitefuncr   )r    r   logxcdirargarg0s   &&&&  r#   _eval_as_leading_termfactorial._eval_as_leading_term  sf    iil**1-xx1~<<<55L!!!99S>!3t<==r&   r'   Nrc   T)r(   r)   r*   r+   r,   r5   r:   r7   __annotations__classmethodrH   rL   r`   rm   r|   r   r   r   r   r   r   r   r   r-   r'   r&   r#   r   r   $   s   .`5	 "$&)+-/2479=?BDHJNPV$&+-46<>FHO 	  % '. 09 ;DL $&y%' '< ; ; &+ &+P<"<)*
*

>r&   r   c                      ] tR tRtRtR# )MultiFactoriali  r'   N)r(   r)   r*   r+   r-   r'   r&   r#   r   r     s    r&   r   c                  r    ] tR tRtRt]]R 4       4       t]R 4       tR t	R t
R tRR ltR	 tR
 tR tRtR# )subfactoriali  a<  The subfactorial counts the derangements of $n$ items and is
defined for non-negative integers as:

.. math:: !n = \begin{cases} 1 & n = 0 \\ 0 & n = 1 \\
                (n-1)(!(n-1) + !(n-2)) & n > 1 \end{cases}

It can also be written as ``int(round(n!/exp(1)))`` but the
recursive definition with caching is implemented for this function.

An interesting analytic expression is the following [2]_

.. math:: !x = \Gamma(x + 1, -1)/e

which is valid for non-negative integers `x`. The above formula
is not very useful in case of non-integers. `\Gamma(x + 1, -1)` is
single-valued only for integral arguments `x`, elsewhere on the positive
real axis it has an infinite number of branches none of which are real.

References
==========

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

Examples
========

>>> from sympy import subfactorial
>>> from sympy.abc import n
>>> subfactorial(n + 1)
subfactorial(n + 1)
>>> subfactorial(5)
44

See Also
========

factorial, uppergamma,
sympy.utilities.iterables.generate_derangements
c                	    V'       g   \         P                  # V^8X  d   \         P                  # ^^ r2\        ^V^,           4       F  pY4^,
          W2,           ,          r2K  	  V# rc   )r   rS   ru   rX   )r    r@   z1z2r^   s   &&   r#   _evalsubfactorial._evalG  sP     55L!V66M1a!e_!ebg.B %Ir&   c                	(   VP                   '       d   VP                  '       d$   VP                  '       d   V P                  V4      # V\        P
                  J d   \        P
                  # V\        P                  J d   \        P                  # R # R # N)rQ   rU   rr   r   r   NaNrT   )r?   r   s   &&r#   r`   subfactorial.evalT  sa    ===~~~#"4"4"4yy~%uu

"zz! # r&   c                	    V P                   ^ ,          P                  '       d(   V P                   ^ ,          P                  '       d   R# R# R# r   )r3   is_oddrr   r   s   &r#   r   subfactorial._eval_is_even^  s4    99Q<499Q<#>#>#> $?r&   c                	    V P                   ^ ,          P                  '       d(   V P                   ^ ,          P                  '       d   R# R# R# r   r   r   s   &r#   r   subfactorial._eval_is_integerb  r   r&   c                	    ^ RI Hp \        R4      p\        P                  V,          \        V4      ,          p\        V4      V! WT^ V34      ,          # )r   )	summationr^   )sympy.concrete.summationsr   r   r   NegativeOner   )r    r   r!   r   r^   fs   &&,   r#   _eval_rewrite_as_factorial'subfactorial._eval_rewrite_as_factorialf  s?    7#JMM1y|+~	!C[ 999r&   c                	   ^ RI Hp ^ RIHpHp \
        P                  V^,           ,          V! \        ) \        ,          V,          4      ,          V! V^,           R4      ,          V! V^,           4      ,           V! R4      ,          # )r   )exp)r0   
lowergammarp   )	&sympy.functions.elementary.exponentialr   r2   r0   r   r   r   r   r   )r    r   r   r!   r   r0   r   s   &&&,   r#   r   #subfactorial._eval_rewrite_as_gammal  s\    >Oa(aRU3Y7
37B8OOa.!"%b'* 	*r&   c                	X    ^ RI Hp V! V^,           R4      \        P                  ,          # )r   )
uppergammarp   )r2   r   r   Exp1)r    r   r!   r   s   &&, r#   _eval_rewrite_as_uppergamma(subfactorial._eval_rewrite_as_uppergammar  s    F#'2&qvv--r&   c                	    V P                   ^ ,          P                  '       d(   V P                   ^ ,          P                  '       d   R# R# R# r   r   r   s   &r#   _eval_is_nonnegative!subfactorial._eval_is_nonnegativev  r   r&   c                	    V P                   ^ ,          P                  '       d(   V P                   ^ ,          P                  '       d   R# R# R# r   )r3   is_evenrr   r   s   &r#   _eval_is_oddsubfactorial._eval_is_oddz  s5    99Q<DIIaL$?$?$? %@r&   r'   Nr   )r(   r)   r*   r+   r,   r   r	   r   r`   r   r   r   r   r   r   r   r-   r'   r&   r#   r   r     s[    'R 	  	 " ":*.r&   r   c                  L    ] tR tRtRt]R 4       tR tR tR t	R t
RR ltR	tR
# )
factorial2i  a  The double factorial `n!!`, not to be confused with `(n!)!`

The double factorial is defined for nonnegative integers and for odd
negative integers as:

.. math:: n!! = \begin{cases} 1 & n = 0 \\
                n(n-2)(n-4) \cdots 1 & n\ \text{positive odd} \\
                n(n-2)(n-4) \cdots 2 & n\ \text{positive even} \\
                (n+2)!!/(n+2) & n\ \text{negative odd} \end{cases}

References
==========

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

Examples
========

>>> from sympy import factorial2, var
>>> n = var('n')
>>> n
n
>>> factorial2(n + 1)
factorial2(n + 1)
>>> factorial2(5)
15
>>> factorial2(-1)
1
>>> factorial2(-5)
1/3

See Also
========

factorial, RisingFactorial, FallingFactorial
c                	   VP                   '       d   VP                  '       g   \        R 4      hVP                  '       dW   VP                  '       d#   V^,          p^V,          \        V4      ,          # \        V4      \        V^,
          4      ,          # VP                  '       d>   V\        P                  ^V,
          ^,          ,          ,          \        V) 4      ,          # \        R 4      hR# )z<argument must be nonnegative integer or negative odd integerN)
rQ   rU   
ValueErrorrr   r   r   r   r   r   r   )r?   r   ks   && r#   r`   factorial2.eval  s     ===>>>  "> ? ?
 !!!;;;aAa4)A,.. ~
37(;;; zzzAMMa#gq[99Jt<LLL : ; ;! r&   c                	    V P                   ^ ,          pVP                  '       dS   VP                  '       d   R# VP                  '       d+   VP                  '       d   R# VP
                  '       d   R# R# R# R# r   FTN)r3   rq   r   r   is_positiverR   r    r@   s   & r#   r   factorial2._eval_is_even  sT    IIaL<<<xxxyyy===999    r&   c                	    V P                   ^ ,          pVP                  '       dC   V^,           P                  '       d   R# VP                  '       d   V^,           P                  # R# R# r   )r3   rq   rr   r   r   s   & r#   r   factorial2._eval_is_integer  sP     IIaL<<<A%%%xxxA---  r&   c                	    V P                   ^ ,          pVP                  '       d   V^,           P                  # VP                  '       d+   VP                  '       d   R# VP
                  '       d   R# R# R# r   )r3   r   rr   r   r   rR   r   s   & r#   r   factorial2._eval_is_odd  sU     IIaL888E)))999}}}yyy  r&   c                	    V P                   ^ ,          pVP                  '       dJ   V^,           P                  '       d   R# VP                  '       d   V^,           ^,          P                  # R# R# r   )r3   rq   rr   r   r   r   s   & r#   r   factorial2._eval_is_positive  sT     IIaL<<<A%%%xxxQ!,,,  r&   c                	   ^ RI Hp ^ RIHp ^ RIHp ^V^,          ,          V! V^,          ^,           4      ,          V! ^\        \        V^4      ^ 4      3V! ^\        ,          4      \        \        V^4      ^4      34      ,          # )r   )r   	Piecewiser   )	(sympy.functions.elementary.miscellaneousr   $sympy.functions.elementary.piecewiser   r2   r0   r   r   r   )r    r@   r   r!   r   r   r0   s   &&&,   r#   r   !factorial2._eval_rewrite_as_gamma  si    ABA1Q3xacAg&Ar#a)Q7G3HadRAq	1-.*0 0 	0r&   r'   Nr   )r(   r)   r*   r+   r,   r   r`   r   r   r   r   r   r-   r'   r&   r#   r   r     s:    #J ; ;.
!.
	-0 0r&   r   c                  V    ] tR tRtRt]R 4       tRR ltR tR t	R t
RR	 ltR
 tRtR# )RisingFactoriali  a  
Rising factorial (also called Pochhammer symbol [1]_) is a double valued
function arising in concrete mathematics, hypergeometric functions
and series expansions. It is defined by:

.. math:: \texttt{rf(y, k)} = (x)^k = x \cdot (x+1) \cdots (x+k-1)

where `x` can be arbitrary expression and `k` is an integer. For
more information check "Concrete mathematics" by Graham, pp. 66
or visit https://mathworld.wolfram.com/RisingFactorial.html page.

When `x` is a `~.Poly` instance of degree $\ge 1$ with a single variable,
`(x)^k = x(y) \cdot x(y+1) \cdots x(y+k-1)`, where `y` is the
variable of `x`. This is as described in [2]_.

Examples
========

>>> from sympy import rf, Poly
>>> from sympy.abc import x
>>> rf(x, 0)
1
>>> rf(1, 5)
120
>>> rf(x, 5) == x*(1 + x)*(2 + x)*(3 + x)*(4 + x)
True
>>> rf(Poly(x**3, x), 2)
Poly(x**6 + 3*x**5 + 3*x**4 + x**3, x, domain='ZZ')

Rewriting is complicated unless the relationship between
the arguments is known, but rising factorial can
be rewritten in terms of gamma, factorial, binomial,
and falling factorial.

>>> from sympy import Symbol, factorial, ff, binomial, gamma
>>> n = Symbol('n', integer=True, positive=True)
>>> R = rf(n, n + 2)
>>> for i in (rf, ff, factorial, binomial, gamma):
...  R.rewrite(i)
...
RisingFactorial(n, n + 2)
FallingFactorial(2*n + 1, n + 2)
factorial(2*n + 1)/factorial(n - 1)
binomial(2*n + 1, n + 2)*factorial(n + 2)
gamma(2*n + 2)/gamma(n)

See Also
========

factorial, factorial2, FallingFactorial

References
==========

.. [1] https://en.wikipedia.org/wiki/Pochhammer_symbol
.. [2] Peter Paule, "Greatest Factorial Factorization and Symbolic
       Summation", Journal of Symbolic Computation, vol. 20, pp. 235-268,
       1995.

c                	  a \        S4      o\        V4      pS\        P                  J g   V\        P                  J d   \        P                  # S\        P                  J d   \	        V4      # VP
                  '       Ed   VP                  '       d   \        P                  # VP                  '       d   S\        P                  J d   \        P                  # S\        P                  J d3   VP                  '       d   \        P                  # \        P                  # \        S\        4      '       dK   SP                  p\        V4      ^8w  d   \        R4      h\!        V3R l\#        \%        V4      4      ^4      # \!        V3R l\#        \%        V4      4      ^4      # S\        P                  J d   \        P                  # S\        P                  J d   \        P                  # \        S\        4      '       dc   SP                  p\        V4      ^8w  d   \        R4      h^\!        V3R l\#        ^\'        \%        V4      4      ^,           4      ^4      ,          # ^\!        V3R l\#        ^\'        \%        V4      4      ^,           4      ^4      ,          # VP(                  R8X  d9   SP(                  '       d%   SP*                  '       d   \        P,                  # R# R# R# )rd   0rf only defined for polynomials on one generatorc                4   < V SP                  V4      ,          # r   shiftrr^   r   s   &&r#   <lambda>&RisingFactorial.eval.<locals>.<lambda>Q  s    ./nr&   c                $   < V SV,           ,          # r   r'   r   s   &&r#   r   r   U      q!a%yr&   c                6   < V SP                  V) 4      ,          # r   r   r   s   &&r#   r   r   d  s    01177A2;r&   c                $   < V SV,
          ,          # r   r'   r   s   &&r#   r   r   h  s    ,-q1uIr&   FN)r   r   r   rS   r   rU   rR   r   rT   NegativeInfinityr   
isinstancer   genslenr   r   rX   r;   rs   rq   rV   ru   r?   r   r   r  s   &f& r#   r`   RisingFactorial.eval5  s,   AJAJ:aee55L!%%ZQ<\\\yyyuu===AJJ zz)a000888#$#5#55#$::-%a..#$66D"4y1}&0 2K 'L !L (. /=.3CFmQ(@ !@ $**@*/A-$< < AJJ zz)a000 zz)%a..#$66D"4y1}&0 2K 'L !L () 1@05aSVq0I1*N (N !N $%V -6,1!SQ[1_,Eq&J $J J <<5 |||vv !.| !r&   c                	   ^ RI Hp ^ RIHp V'       gm   V^ 8*  R8X  dH   \        P
                  V,          V! ^V,
          4      ,          V! V) V,
          ^,           4      ,          # V! W,           4      V! V4      ,          # V! V! W,           4      V! V4      ,          V^ 8  3\        P
                  V,          V! ^V,
          4      ,          V! V) V,
          ^,           4      ,          R34      # r   r   r   Tr   r   r2   r0   r   r   r    r   r   r   r!   r   r0   s   &&&&,  r#   r   &RisingFactorial._eval_rewrite_as_gammap  s    BAQ4}}a'a!e4uaR!VaZ7HHH<%(**15\E!H$a!e,]]AeAEl*UA26A:->>EG 	Gr&   c                	4    \        W,           ^,
          V4      # rc   )FallingFactorialr    r   r   r!   s   &&&,r#   !_eval_rewrite_as_FallingFactorial1RisingFactorial._eval_rewrite_as_FallingFactorial{  s    	1--r&   c                	T   ^ RI Hp VP                  '       d   VP                  '       d|   V! \        W!,           ^,
          4      \        V^,
          4      ,          V^ 8  3\        P
                  V,          \        V) 4      ,          \        V) V,
          4      ,          R34      # R# R# r   r   TNr   r   rq   r   r   r   r    r   r   r!   r   s   &&&, r#   r   *RisingFactorial._eval_rewrite_as_factorial~  s}    B<<<ALLL1519%iA&66A>!)QB-/	1"q&0AA4HJ J )<r&   c                	|    VP                   '       d*   \        V4      \        W,           ^,
          V4      ,          # R# )rd   Nrq   r   binomialr  s   &&&,r#   _eval_rewrite_as_binomial)RisingFactorial._eval_rewrite_as_binomial  s,    <<<Q<(1519a"888 r&   Nc                	   ^ RI Hp V'       d   VP                  V\        P                  4      pV\        P                  J d-   V! W,           4      P                  RRR7      V! V4      ,          # V\        P                  J dY   \        P                  V,          V! ^V,
          4      ,          V! V) V,
          ^,           4      P                  RRR7      ,          # V P                  V4      P                  RRR7      # r   r   	tractableT)deep)r2   r0   r   r   rT   rewriter  r   r    r   r   limitvarr!   r0   k_lims   &&&&,  r#   _eval_rewrite_as_tractable*RisingFactorial._eval_rewrite_as_tractable  s    AFF8QZZ0E

"ae,,[t,DuQxOP!,,,q(q1u5qb1fqj8I8Q8QR]dh8Q8iij||E"**;T*BBr&   c                	    \        V P                  ^ ,          P                  V P                  ^,          P                  V P                  ^,          P                  34      # r   r   r3   rq   rr   r   s   &r#   r    RisingFactorial._eval_is_integer  E    $))A,11499Q<3J3J))A,557 8 	8r&   r'   r   r   )r(   r)   r*   r+   r,   r   r`   r   r  r   r  r#  r   r-   r'   r&   r#   r   r     s>    ;z 8 8t	G.J9C8r&   r   c                  V    ] tR tRtRt]R 4       tRR ltR tR t	R t
RR	 ltR
 tRtR# )r  i  ap  
Falling factorial (related to rising factorial) is a double valued
function arising in concrete mathematics, hypergeometric functions
and series expansions. It is defined by

.. math:: \texttt{ff(x, k)} = (x)_k = x \cdot (x-1) \cdots (x-k+1)

where `x` can be arbitrary expression and `k` is an integer. For
more information check "Concrete mathematics" by Graham, pp. 66
or [1]_.

When `x` is a `~.Poly` instance of degree $\ge 1$ with single variable,
`(x)_k = x(y) \cdot x(y-1) \cdots x(y-k+1)`, where `y` is the
variable of `x`. This is as described in

>>> from sympy import ff, Poly, Symbol
>>> from sympy.abc import x
>>> n = Symbol('n', integer=True)

>>> ff(x, 0)
1
>>> ff(5, 5)
120
>>> ff(x, 5) == x*(x - 1)*(x - 2)*(x - 3)*(x - 4)
True
>>> ff(Poly(x**2, x), 2)
Poly(x**4 - 2*x**3 + x**2, x, domain='ZZ')
>>> ff(n, n)
factorial(n)

Rewriting is complicated unless the relationship between
the arguments is known, but falling factorial can
be rewritten in terms of gamma, factorial and binomial
and rising factorial.

>>> from sympy import factorial, rf, gamma, binomial, Symbol
>>> n = Symbol('n', integer=True, positive=True)
>>> F = ff(n, n - 2)
>>> for i in (rf, ff, factorial, binomial, gamma):
...  F.rewrite(i)
...
RisingFactorial(3, n - 2)
FallingFactorial(n, n - 2)
factorial(n)/2
binomial(n, n - 2)*factorial(n - 2)
gamma(n + 1)/2

See Also
========

factorial, factorial2, RisingFactorial

References
==========

.. [1] https://mathworld.wolfram.com/FallingFactorial.html
.. [2] Peter Paule, "Greatest Factorial Factorization and Symbolic
       Summation", Journal of Symbolic Computation, vol. 20, pp. 235-268,
       1995.

c                	J  a \        S4      o\        V4      pS\        P                  J g   V\        P                  J d   \        P                  # VP                  '       d   SV8X  d   \	        S4      # VP
                  '       Ed   VP                  '       d   \        P                  # VP                  '       d   S\        P                  J d   \        P                  # S\        P                  J d3   VP                  '       d   \        P                  # \        P                  # \        S\        4      '       dK   SP                  p\        V4      ^8w  d   \!        R4      h\#        V3R l\%        \'        V4      4      ^4      # \#        V3R l\%        \'        V4      4      ^4      # S\        P                  J d   \        P                  # S\        P                  J d   \        P                  # \        S\        4      '       dc   SP                  p\        V4      ^8w  d   \!        R4      h^\#        V3R l\%        ^\)        \'        V4      4      ^,           4      ^4      ,          # ^\#        V3R l\%        ^\)        \'        V4      4      ^,           4      ^4      ,          # R# )rd   z0ff only defined for polynomials on one generatorc                6   < V SP                  V) 4      ,          # r   r   r   s   &&r#   r   'FallingFactorial.eval.<locals>.<lambda>  s    ./!or&   c                $   < V SV,
          ,          # r   r'   r   s   &&r#   r   r-    r   r&   r   c                4   < V SP                  V4      ,          # r   r   r   s   &&r#   r   r-    s    011771:r&   c                $   < V SV,           ,          # r   r'   r   s   &&r#   r   r-  	  s    AEr&   N)r   r   r   rq   r   rU   rR   rS   r   rT   r  r   r  r   r  r  r   r   rX   r;   rs   r  s   &f& r#   r`   FallingFactorial.eval  s   AJAJ:aee55L\\\a1fQ<\\\yyyuu===AJJ zz)a000888#$#5#55#$::-%a..#$66D"4y1}&0 2K 'L !L (. />.3CFmQ(@ !@ $**@*/A-$< < AJJ zz)a000 zz)%a..#$66D"4y1}&0 2K 'L !L () 1?05aSVq0I1*N (N !N $%V,B,1!SQ[1_,Eq&J $J JS r&   c                	   ^ RI Hp ^ RIHp V'       gl   V^ 8  R8X  d9   \        P
                  V,          V! W!,
          4      ,          V! V) 4      ,          # V! V^,           4      V! W,
          ^,           4      ,          # V! V! V^,           4      V! W,
          ^,           4      ,          V^ 8  3\        P
                  V,          V! W!,
          4      ,          V! V) 4      ,          R34      # r  r	  r
  s   &&&&,  r#   r   'FallingFactorial._eval_rewrite_as_gamma  s    BAA$}}a'ae4uaRy@@Q<%	"2221q5\E!%!),,a1f5]]AeAEl*UA2Y6=? 	?r&   c                	4    \        W,
          ^,           V4      # rc   )rfr  s   &&&,r#    _eval_rewrite_as_RisingFactorial1FallingFactorial._eval_rewrite_as_RisingFactorial  s    !%!)Qr&   c                	`    VP                   '       d   \        V4      \        W4      ,          # R # r   r  r  s   &&&,r#   r  *FallingFactorial._eval_rewrite_as_binomial  s#    <<<Q<(1.00 r&   c                	T   ^ RI Hp VP                  '       d   VP                  '       d|   V! \        V4      \        V) V,           4      ,          V^ 8  3\        P
                  V,          \        W!,
          ^,
          4      ,          \        V) ^,
          4      ,          R34      # R# R# r  r  r  s   &&&, r#   r   +FallingFactorial._eval_rewrite_as_factorial  s}    B<<<ALLL1iQ//a8!)AEAI"66y!a7HH$OQ Q )<r&   Nc                	   ^ RI Hp V'       d   VP                  V\        P                  4      pV\        P                  J dJ   \        P
                  V,          V! W!,
          4      P                  RRR7      ,          V! V) 4      ,          # V\        P                  J d;   V! V^,           4      V! W,
          ^,           4      P                  RRR7      ,          # V P                  V4      P                  RRR7      # r  )r2   r0   r   r   rT   r   r  r  r   s   &&&&,  r#   r#  +FallingFactorial._eval_rewrite_as_tractable%  s    AFF8QZZ0E

"q(qu)=)=kPT)=)UUX]_`^`Xaab!,,,a!euQUQY'7'?'?RV'?'WWX||E"**;T*BBr&   c                	    \        V P                  ^ ,          P                  V P                  ^,          P                  V P                  ^,          P                  34      # r&  r'  r   s   &r#   r   !FallingFactorial._eval_is_integer/  r)  r&   r'   r   r   )r(   r)   r*   r+   r,   r   r`   r   r6  r  r   r#  r   r-   r'   r&   r#   r  r    s?    <| 2J 2Jh	? 1QC8r&   r  c                      ] tR tRtRtRR lt]R 4       t]R 4       tR t	R t
R tRR	 ltRR ltR tR tR tR tRtR
# )r  i<  a 
  Implementation of the binomial coefficient. It can be defined
in two ways depending on its desired interpretation:

.. math:: \binom{n}{k} = \frac{n!}{k!(n-k)!}\ \text{or}\
            \binom{n}{k} = \frac{(n)_k}{k!}

First, in a strict combinatorial sense it defines the
number of ways we can choose `k` elements from a set of
`n` elements. In this case both arguments are nonnegative
integers and binomial is computed using an efficient
algorithm based on prime factorization.

The other definition is generalization for arbitrary `n`,
however `k` must also be nonnegative. This case is very
useful when evaluating summations.

For the sake of convenience, for negative integer `k` this function
will return zero no matter the other argument.

To expand the binomial when `n` is a symbol, use either
``expand_func()`` or ``expand(func=True)``. The former will keep
the polynomial in factored form while the latter will expand the
polynomial itself. See examples for details.

Examples
========

>>> from sympy import Symbol, Rational, binomial, expand_func
>>> n = Symbol('n', integer=True, positive=True)

>>> binomial(15, 8)
6435

>>> binomial(n, -1)
0

Rows of Pascal's triangle can be generated with the binomial function:

>>> for N in range(8):
...     print([binomial(N, i) for i in range(N + 1)])
...
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]

As can a given diagonal, e.g. the 4th diagonal:

>>> N = -4
>>> [binomial(N, i) for i in range(1 - N)]
[1, -4, 10, -20, 35]

>>> binomial(Rational(5, 4), 3)
-5/128
>>> binomial(Rational(-5, 4), 3)
-195/128

>>> binomial(n, 3)
binomial(n, 3)

>>> binomial(n, 3).expand(func=True)
n**3/6 - n**2/2 + n/3

>>> expand_func(binomial(n, 3))
n*(n - 2)*(n - 1)/6

In many cases, we can also compute binomial coefficients modulo a
prime p quickly using Lucas' Theorem [2]_, though we need to include
`evaluate=False` to postpone evaluation:

>>> from sympy import Mod
>>> Mod(binomial(156675, 4433, evaluate=False), 10**5 + 3)
28625

Using a generalisation of Lucas's Theorem given by Granville [3]_,
we can extend this to arbitrary n:

>>> Mod(binomial(10**18, 10**12, evaluate=False), (10**5 + 3)**2)
3744312326

References
==========

.. [1] https://www.johndcook.com/blog/binomial_coefficients/
.. [2] https://en.wikipedia.org/wiki/Lucas%27s_theorem
.. [3] Binomial coefficients modulo prime powers, Andrew Granville,
    Available: https://web.archive.org/web/20170202003812/http://www.dms.umontreal.ca/~andrew/PDF/BinCoeff.pdf
c                	d   ^ RI Hp V^8X  dJ   V P                  w  r4\        W44      V! ^ V^,           4      V! ^ W4,
          ^,           4      ,
          ,          # V^8X  dJ   V P                  w  r4\        W44      V! ^ W4,
          ^,           4      V! ^ V^,           4      ,
          ,          # \	        W4      h)r   )r1   )r2   r1   r3   r  r   )r    r4   r1   r@   r   s   &&   r#   r5   binomial.fdiff  s    Eq=99DAA>9QA#6!QUQY'$( ) )]99DAA>9Q	#:!QU#$$ % % %T44r&   c                	D   VP                   '       Ed   VP                   '       d   V^ 8  d   \        V4      \        V4      r!W!8  d   \        P                  # W!^,          8  d	   W,
          p\        e    \        \        P                  ! W4      4      # W,
          ^rC\        ^V^,           4       F  pV^,          pWC,          V,          pK  	  \        V4      # W,
          ^rC\        ^V^,           4       F  pV^,          pWC,          pK  	  V\        V4      ,          # R# r   )	rU   r;   r   ru   rY   r   bincoefrX   
_factorial)r    r@   r   ry   r]   r^   s   &&&   r#   r   binomial._eval  s     <<<|||Q1vs1v1566MaZA
 $"5==#677E16q!a%AFA#Z1_F ) v&E16q!a%AFAKF ) 
1--3 r&   c                	   \        \        W34      w  rW,
          pVP                  VP                  rTVP                  '       g!   V'       g   VR J d#   VP                  '       d   \
        P                  # V^,
          P                  '       g(   V'       g   VR J d   V^,
          P                  '       d   V# VP                  '       d   VP                  '       g#   V'       d+   V'       d#   VP                  '       d   \
        P                  # VP                  '       d.   V P                  W4      pV'       d   VP                  RR7      # T# R# VR J d   V'       d   \
        P                  # VP                  '       dD   ^ RIHp V! V^,           4      V! V^,           4      V! W,
          ^,           4      ,          ,          # R# )FT)basicr   N)rw   r   rr   rq   rR   r   rS   rV   ru   	is_numberr   expandrW   r2   r0   )r?   r@   r   ry   n_nonnegn_isintrg   r0   s   &&&     r#   r`   binomial.eval  s$   7QF#E,,all'999(g&6III55LE???Gu,<UOOOH<<<}}}g!---vviio14szzz-=#=  7$$$[[[EQ<q1ueAEAI.>!>?? r&   c                	   V P                   w  r#\        ;QJ d    R  W#V3 4       F  '       g   K   RM	  RM! R  W#V3 4       4      '       d   \        R4      h\        ;QJ d    R W#V3 4       F  '       d   K   RM	  RM! R W#V3 4       4      '       Ed   \	        \
        W#34      w  r#\        V4      ^rTV^ 8  d   \        P                  # V^ 8  d%   V) V,           ^,
          pV^,          '       d   RM^pW28  d   \        P                  # VP                  p\        V4      pV'       Ed)   WB8  dL   Y#rV'       g	   V'       d8   V\        Wt,          W,          4      ,          V,          pWt,          W,          rKG  EMW#,
          p	W98  d   Yr^p
\        ^V^,           4       F  pW,          V,          p
K  	  T
p\        V^,           V	^,           4       F  pW,          V,          pK  	  W\,          p\        V	^,           V^,           4       F  pW[,          V,          pK  	  V\        W,          V,          V^,
          V4      ,          pWT,          pEM"\        V4      V8  d   V^8w  d   \        W#V4      pM\        \        V4      4      p\        P                   ! ^V^,           4       F  pWV,
          8  d   W^,          V,          pK!  W^,          8  d   K0  W8  d'   W.,          W>,          8  d   W^,          V,          pKZ  K\  Y#r^ ;ppV^ 8  d<   \        W~,          W,          V,           8  4      pW~,          W,          rVV,          pKB  V^ 8  g   K  V\        WV4      ,          pWT,          pK  	  \        WQ,          4      # R# )c              3  <   "   T F  qP                   R J x  K  	  R# 5i)FN)rq   .0r   s   & r#   	<genexpr>%binomial._eval_Mod.<locals>.<genexpr>  s     8i||u$is   TFz"Integers expected for binomial Modc              3  8   "   T F  qP                   x  K  	  R # 5ir   )rU   rP  s   & r#   rR  rS    s     /Y||Ys   Nrp   )r3   anyr   allrw   r;   rs   r   ru   rv   r  rX   re   r<   r   r   r=   )r    rE   r@   r   rx   rg   rz   rA   Kry   kfr^   dfMrC   r   as   &&               r#   r|   binomial._eval_Mod  s   yy38qQi83338qQi888ABB3/aAY/333/aAY///sQF#DA!fa 1uvv1uBFQJaCCbQ uvvkkGRBw6qq!(1616"::R? w1  ! Au 1B"1a!e_TBY -B"1q5!a%0TBY 1IC"1q5!a%0!ebj 1 3rurz26266CICqA!q&"1+ aM"--aQ7E1u}!i"na 9qy0"%)b.C 1  !1"#a!e #QY19q=$A BA#$:qzq1HC73u2#66CIC' 8* SW:W 0r&   c                V   V P                   ^ ,          pVP                  '       d   \        V P                   !  # V P                   ^,          pW#,
          P                  '       d	   W#,
          pVP                  '       d   VP                  '       d   \
        P                  # VP                  '       d   \
        P                  # V P                   ^ ,          ^rB\        ^V^,           4       F  pWBV,
          V,           ,          pK  	  V\        V4      ,          # \        V P                   !  # )z
Function to expand binomial(n, k) when m is positive integer
Also,
n is self.args[0] and k is self.args[1] while using binomial(n, k)
)r3   rQ   r  rU   rR   r   rS   rV   ru   rX   rE  )r    hintsr@   r   r]   r^   s   &,    r#   _eval_expand_funcbinomial._eval_expand_func3  s     IIaL;;;TYY''IIaLCA<<<yyyuuvv IIaL!6q!a%A!eai'F )
1--TYY''r&   c                	d    \        V4      \        V4      \        W,
          4      ,          ,          # r   )r   r    r@   r   r!   s   &&&,r#   r   #binomial._eval_rewrite_as_factorialN  s!    |Yq\)AE*::;;r&   c                	    ^ RI Hp V! V^,           4      V! V^,           4      V! W,
          ^,           4      ,          ,          # r   r   )r    r@   r   r   r!   r0   s   &&&&, r#   r   binomial._eval_rewrite_as_gammaQ  s0    AQU|U1q5\%	*::;;r&   Nc                	B    V P                  W4      P                  R 4      # )r  )r   r  )r    r@   r   r!  r!   s   &&&&,r#   r#  #binomial._eval_rewrite_as_tractableU  s    **1088EEr&   c                	`    VP                   '       d   \        W4      \        V4      ,          # R # r   )rq   ffr   rb  s   &&&,r#   r  *binomial._eval_rewrite_as_FallingFactorialX  s#    <<<a8il** r&   c                	    V P                   w  rVP                  '       d   VP                  '       d   R # VP                  RJ d   R# R# TFN)r3   rq   r    r@   r   s   &  r#   r   binomial._eval_is_integer\  s5    yy<<<ALLL\\U" #r&   c                	   V P                   w  rVP                  '       da   VP                  '       dM   VP                  '       g%   VP                  '       g   VP                  '       d   R # VP                  RJ d   R# R# R# R# rl  )r3   rq   rr   rV   r   rm  s   &  r#   r   binomial._eval_is_nonnegativec  sX    yy<<<ALLL1===AIIIe# $ )<r&   c                	R    ^ RI Hp V P                  V4      P                  WVR7      # )r   r   )r   r   )r2   r0   r  r   )r    r   r   r   r0   s   &&&& r#   r   binomial._eval_as_leading_termk  s$    A||E"88D8QQr&   r'   rc   r   r   )r(   r)   r*   r+   r,   r5   r   r   r`   r|   r_  r   r   r#  r  r   r   r   r-   r'   r&   r#   r  r  <  si    [z5 . .< @ @.Qf(6<<F+Rr&   r  N)/
__future__r   	functoolsr   
sympy.corer   r   r   r   sympy.core.cacher	   sympy.core.functionr
   r   r   sympy.core.logicr   sympy.core.numbersr   r   r   sympy.core.relationalr   sympy.external.gmpyr   rY   sympy.ntheoryr   sympy.ntheory.residue_ntheoryr   sympy.polys.polytoolsr   mathr   rE  r   r   r<   r   r   r   r   r   r  r5  ri  r  r'   r&   r#   <module>r     s    "  - - $ N N & - - $ -  6 & = =O &s>% s>j	* 	_( _Dp0& p0p^8+ ^8BY8, Y8x qR$ qRr&   