ó
Ąź\c           @  s   d  d l  m Z m Z d  d l m Z m Z m Z m Z d  d l m	 Z	 d  d l
 m Z m Z m Z d  d l m Z m Z d  d l m Z d  d l m Z m Z d  d l m Z d  d	 l m Z d  d
 l m Z d  d l m Z d e f d     YZ d e f d     YZ  d e f d     YZ! d e f d     YZ" d e f d     YZ# d e f d     YZ$ d e f d     YZ% e$ Z& e% Z' d e f d     YZ( d S(   i˙˙˙˙(   t   print_functiont   division(   t   St   sympifyt   Dummyt   Mod(   t   cacheit(   t   reducet   ranget   HAS_GMPY(   t   Functiont   ArgumentIndexError(   t	   fuzzy_and(   t   Integert   pi(   t   Eq(   t   sieve(   t   Poly(   t   sqrtt   CombinatorialFunctionc           B  s   e  Z d  Z d   Z RS(   s(   Base class for combinatorial functions. c         C  s@   d d l  m } | |   } | |  | | |   k r< | S|  S(   Ni˙˙˙˙(   t   combsimp(   t   sympy.simplify.combsimpR   (   t   selft   ratiot   measuret   rationalt   inverseR   t   expr(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_simplify   s
    (   t   __name__t
   __module__t   __doc__R   (    (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR      s   t	   factorialc        !   B  s  e  Z d  Z d d  Z d d d d d d d d d d d d	 d
 d d d d d d d d d d d d d d d d d d d d g! Z g  Z e d    Z e d    Z e d    Z	 d    Z
 d!   Z d"   Z d#   Z d$   Z d%   Z d&   Z d'   Z d(   Z RS()   sŁ  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
    i   c         C  sb   d d l  m } m } | d k rO | |  j d d  | d |  j d d  St |  |   d  S(   Ni˙˙˙˙(   t   gammat	   polygammai   i    (   t   sympyR!   R"   t   argsR   (   R   t   argindexR!   R"   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   fdiffS   s    -i   i   i   i#   i;  i?   iľ  iç   iť  i­  i#  iSŤ i{/  i! im´  ińĚ isX iUň iÇP
 ioăikÖ iIi/L iSůŞi}î i#áéc   	      C  so  | d k  r |  j  | St t |   g  } } x t j d | d  D]s } d | } } xA t r | | } | d k r | d @d k r | | 9} q q] Pq] W| d k rG | j |  qG qG WxJ t j | d | d d  D]* } | | d @d k rÝ | j |  qÝ qÝ Wd } } x0 t j | d d | d  D] } | | 9} q4Wx | D] } | | 9} qOW| | Sd  S(   Ni!   i   i   i    i   (   t   _small_swingt   intt   _sqrtR   t
   primeranget   Truet   append(	   t   clst   nt   Nt   primest   primet   pt   qt	   L_productt	   R_product(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _swingb   s,    	
%
%c         C  s6   | d k  r d S|  j  | d  d |  j |  Sd  S(   Ni   i   (   t
   _recursiveR6   (   R-   R.   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR7      s    c         C  s5  t  |  } | j r1| t j k r+ t j S| t j k rA t j S| j r1| j rZ t j S| j	 } | d k  rÉ |  j
 sľ d } x4 t d d  D]  } | | 9} |  j
 j |  q Wn  |  j
 | d } nX t rń d d l m } | j |  } n0 t |  j d  } |  j |  d | | } t |  Sq1n  d  S(   Ni   i   i˙˙˙˙(   t   gmpyt   1i   (   R   t	   is_NumberR   t   Zerot   Onet   Infinityt
   is_Integert   is_negativet   ComplexInfinityR2   t   _small_factorialsR   R,   R	   t   sympy.core.compatibilityR8   t   fact   bint   countR7   R   (   R-   R.   t   resultt   iR8   t   bits(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   eval   s.    					
c         C  s6  d t  t |   } } d g | } d } x t j d | d  D] } | d k r d | | } } x! | r | | 7} | | } qi Wn  | | k  rŻ | | | | | | <qC | t | | |  | } qC Wxb t |  D]T \ }	 }
 |	 d k sÚ |
 d k rqÚ n  |
 d k rd S| t |
 |	 |  | } qÚ W| S(   Ni   i   i    (   R(   R)   R   R*   t   powt	   enumerate(   R   R.   R3   t   resR/   t   pwt   mR1   t   yt   ext   bs(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _facmodŻ   s&    	
c         C  sA  |  j  d } | j r=| j r=| j r=t |  } | | } | j rK d S| j } | d k r | rn d | S| t k r:| d j r:d Sq=| j r=| j r=t t	 | | | f  \ } } } | r| d | k  r|  j
 | d |  } t | | d |  } | d r,| } q,n |  j
 | |  } t | |  Sn  d  S(   Ni    i   i˙˙˙˙i   i   (   R$   t
   is_integert   is_nonnegativet   abst   is_nonpositivet   is_primet   FalseR>   t   mapR(   RR   RJ   R   (   R   R3   R.   t   aqt   dt   isprimet   fc(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt	   _eval_ModÍ   s*    
		!
c         K  s   d d l  m } | | d  S(   Ni˙˙˙˙(   R!   i   (   R#   R!   (   R   R.   t   kwargsR!   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_rewrite_as_gammaë   s    c         K  sN   d d l  m } | j rJ | j rJ t d d t } | | | d | f  Sd  S(   Ni˙˙˙˙(   t   ProductRG   t   integeri   (   R#   Ra   RT   RS   R   R+   (   R   R.   R_   Ra   RG   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_rewrite_as_Productď   s    c         C  s(   |  j  d j r$ |  j  d j r$ t Sd  S(   Ni    (   R$   RS   RT   R+   (   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_is_integerő   s     c         C  s(   |  j  d j r$ |  j  d j r$ t Sd  S(   Ni    (   R$   RS   RT   R+   (   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_is_positiveů   s     c         C  s.   |  j  d } | j r* | j r* | d j Sd  S(   Ni    i   (   R$   RS   RT   (   R   t   x(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_is_evený   s    c         C  s.   |  j  d } | j r* | j r* | d j Sd  S(   Ni    i   (   R$   RS   RT   (   R   Rf   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_is_composite  s    c         C  s'   |  j  d } | j s | j r# t Sd  S(   Ni    (   R$   RT   t   is_nonintegerR+   (   R   Rf   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_is_real  s    (   R   R   R   R&   R'   RA   t   classmethodR6   R7   RI   RR   R^   R`   Rc   Rd   Re   Rg   Rh   Rj   (    (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR    "   s$   /6$"								t   MultiFactorialc           B  s   e  Z RS(    (   R   R   (    (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRl     s   t   subfactorialc           B  s_   e  Z d  Z e e d     Z e d    Z d   Z d   Z d   Z	 d   Z
 d   Z RS(   s  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 incase of non-integers. :math:`\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] http://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
    ========

    sympy.functions.combinatorial.factorials.factorial,
    sympy.utilities.iterables.generate_derangements,
    sympy.functions.special.gamma_functions.uppergamma
    c         C  sJ   | s t  j S| d k r  t  j S| d |  j | d  |  j | d  S(   Ni   i   (   R   R<   R;   t   _eval(   R   R.   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRn   ;  s
    c         C  s[   | j  rW | j r( | j r( |  j |  S| t j k r> t j S| t j k rW t j Sn  d  S(   N(   R:   R>   RT   Rn   R   t   NaNR=   (   R-   t   arg(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRI   D  s    	c         C  s(   |  j  d j r$ |  j  d j r$ t Sd  S(   Ni    (   R$   t   is_oddRT   R+   (   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRg   N  s     c         C  s(   |  j  d j r$ |  j  d j r$ t Sd  S(   Ni    (   R$   RS   RT   R+   (   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRd   R  s     c         K  s(   d d l  m } | | d d  t j S(   Ni˙˙˙˙(   t
   uppergammai   (   R#   Rr   R   t   Exp1(   R   Rp   R_   Rr   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_rewrite_as_uppergammaV  s    c         C  s(   |  j  d j r$ |  j  d j r$ t Sd  S(   Ni    (   R$   RS   RT   R+   (   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_is_nonnegativeZ  s     c         C  s(   |  j  d j r$ |  j  d j r$ t Sd  S(   Ni    (   R$   t   is_evenRT   R+   (   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_is_odd^  s     (   R   R   R   Rk   R   Rn   RI   Rg   Rd   Rt   Ru   Rw   (    (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRm     s   )
				t
   factorial2c           B  sJ   e  Z d  Z e d    Z d   Z d   Z d   Z d   Z d   Z	 RS(   s3  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
    >>> var('n')
    n
    >>> factorial2(n + 1)
    factorial2(n + 1)
    >>> factorial2(5)
    15
    >>> factorial2(-1)
    1
    >>> factorial2(-5)
    1/3

    See Also
    ========

    factorial, RisingFactorial, FallingFactorial
    c         C  sĽ   | j  rĄ | j s! t d   n  | j rg | j rO | d } d | t |  St |  t | d  S| j r | t j	 d | d t |  St d   n  d  S(   Ns<   argument must be nonnegative integer or negative odd integeri   i   (
   R:   R>   t
   ValueErrorRT   Rv   R    Rx   Rq   R   t   NegativeOne(   R-   Rp   t   k(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRI     s    				
	"c         C  sP   |  j  d } | j rL | j r# t S| j rL | j r9 t S| j rI t SqL n  d  S(   Ni    (   R$   RS   Rq   RX   Rv   t   is_positiveR+   t   is_zero(   R   R.   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRg      s    					c         C  sB   |  j  d } | j r> | d j r' t S| j r> | d j Sn  d  S(   Ni    i   i   (   R$   RS   RT   R+   Rq   (   R   R.   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRd   Ź  s    		c         C  sK   |  j  d } | j r! | d j S| j rG | j r7 t S| j rG t Sn  d  S(   Ni    i   (   R$   Rq   RT   Rv   R|   RX   R}   R+   (   R   R.   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRw   ś  s    				c         C  sF   |  j  d } | j rB | d j r' t S| j rB | d d j Sn  d  S(   Ni    i   i   (   R$   RS   RT   R+   Rq   Rv   (   R   R.   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRe   Â  s    		c         K  s   d d l  m } m } m } d | d | | d d  | d t t | d  d  f | d t  t t | d  d  f  S(   Ni˙˙˙˙(   R!   t	   PiecewiseR   i   i   i    (   R#   R!   R~   R   R   R   R   (   R   R.   R_   R!   R~   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR`   Í  s    ;(
   R   R   R   Rk   RI   Rg   Rd   Rw   Re   R`   (    (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRx   c  s   #		
		t   RisingFactorialc           B  sS   e  Z d  Z e d    Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(   s  
    Rising factorial (also called Pochhammer symbol) is a double valued
    function arising in concrete mathematics, hypergeometric functions
    and series expansions. It is defined by:

    .. math:: rf(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 http://mathworld.wolfram.com/RisingFactorial.html page.

    When `x` is a Poly instance of degree >= 1 with a single variable,
    `rf(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 Peter Paule, "Greatest
    Factorial Factorization and Symbolic Summation", Journal of
    Symbolic Computation, vol. 20, pp. 235-268, 1995.

    Examples
    ========

    >>> from sympy import rf, symbols, factorial, ff, binomial, Poly
    >>> from sympy.abc import x
    >>> n, k = symbols('n k', integer=True)
    >>> 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')

    Rewrite

    >>> rf(x, k).rewrite(ff)
    FallingFactorial(k + x - 1, k)
    >>> rf(x, k).rewrite(binomial)
    binomial(k + x - 1, k)*factorial(k)
    >>> rf(n, k).rewrite(factorial)
    factorial(k + n - 1)/factorial(n - 1)

    See Also
    ========

    factorial, factorial2, FallingFactorial

    References
    ==========

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

    c           s'  t       t  |  }   t j k s6 | t j k r= t j S  t j k rV t |  S| j r#| t j k ru t j S| j rL  t j k r t j S  t j	 k r˝ | j
 rł t j	 St j Sq t   t  r!  j } t |  d k rö t d   qIt   f d   t d t |   d  Sq t   f d   t d t |   d  Sq#  t j k rbt j S  t j	 k rxt j St   t  rę  j } t |  d k rąt d   q d t   f d   t d t t |   d  d  Sq#d t   f d   t d t t |   d  d  Sn  d  S(   Ni   s0   rf only defined for polynomials on one generatorc           s   |    j  |  j   S(   N(   t   shiftt   expand(   t   rRG   (   Rf   (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   <lambda>*  s    i    c           s   |    | S(   N(    (   R   RG   (   Rf   (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   .  t    c           s   |    j  |  j   S(   N(   R   R   (   R   RG   (   Rf   (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   =  s    c           s   |    | S(   N(    (   R   RG   (   Rf   (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   A  s    (   R   R   Ro   R<   R    R>   R;   R|   R=   t   NegativeInfinityRq   t
   isinstanceR   t   genst   lenRy   R   R   R(   RU   (   R-   Rf   R{   R   (    (   Rf   sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRI     sH    
			
		'c         K  s(   d d l  m } | | |  | |  S(   Ni˙˙˙˙(   R!   (   R#   R!   (   R   Rf   R{   R_   R!   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR`   E  s    c         K  s   t  | | d |  S(   Ni   (   t   FallingFactorial(   R   Rf   R{   R_   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt!   _eval_rewrite_as_FallingFactorialI  s    c         K  s6   | j  r2 | j  r2 t | | d  t | d  Sd  S(   Ni   (   RS   R    (   R   Rf   R{   R_   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_rewrite_as_factorialL  s    c         K  s,   | j  r( t |  t | | d |  Sd  S(   Ni   (   RS   R    t   binomial(   R   Rf   R{   R_   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_rewrite_as_binomialP  s    	c         C  s1   t  |  j d j |  j d j |  j d j f  S(   Ni    i   (   R   R$   RS   RT   (   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRd   T  s    c         C  s9   d d  l  j } | j |  j d j   |  j d j    S(   Ni˙˙˙˙i    i   (   t   sage.allt   allt   rising_factorialR$   t   _sage_(   R   t   sage(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   X  s    (   R   R   R   Rk   RI   R`   R   R   R   Rd   R   (    (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   Ř  s   47					R   c           B  sS   e  Z d  Z e d    Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(   sF  
    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:: ff(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 http://mathworld.wolfram.com/FallingFactorial.html page.

    When `x` is a Poly instance of degree >= 1 with single variable,
    `ff(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 Peter Paule, "Greatest
    Factorial Factorization and Symbolic Summation", Journal of
    Symbolic Computation, vol. 20, pp. 235-268, 1995.

    >>> from sympy import ff, factorial, rf, gamma, polygamma, binomial, symbols, Poly
    >>> from sympy.abc import x, k
    >>> n, m = symbols('n m', 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)

    Rewrite

    >>> ff(x, k).rewrite(gamma)
    (-1)**k*gamma(k - x)/gamma(-x)
    >>> ff(x, k).rewrite(rf)
    RisingFactorial(-k + x + 1, k)
    >>> ff(x, m).rewrite(binomial)
    binomial(x, m)*factorial(m)
    >>> ff(n, m).rewrite(factorial)
    factorial(n)/factorial(-m + n)

    See Also
    ========

    factorial, factorial2, RisingFactorial

    References
    ==========

    .. [1] http://mathworld.wolfram.com/FallingFactorial.html

    c           s-  t       t  |  }   t j k s6 | t j k r= t j S| j r\   | k r\ t    S| j r)| t j k r{ t j S| j rR  t j	 k r t j	 S  t j
 k rĂ | j rš t j
 St j	 Sq&t   t  r'  j } t |  d k rü t d   qOt   f d   t d t |   d  Sq&t   f d   t d t |   d  Sq)  t j	 k rht j	 S  t j
 k r~t j	 St   t  rđ  j } t |  d k rˇt d   q&d t   f d   t d t t |   d  d  Sq)d t   f d   t d t t |   d  d  Sn  d  S(	   Ni   s0   ff only defined for polynomials on one generatorc           s   |    j  |  j   S(   N(   R   R   (   R   RG   (   Rf   (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   ą  s    i    c           s   |    | S(   N(    (   R   RG   (   Rf   (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   ľ  R   s0   rf only defined for polynomials on one generatorc           s   |    j  |  j   S(   N(   R   R   (   R   RG   (   Rf   (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   Ă  s    c           s   |    | S(   N(    (   R   RG   (   Rf   (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   Ç  R   (   R   R   Ro   RS   R    R>   R;   R<   R|   R=   R   Rq   R   R   R   R   Ry   R   R   R(   RU   (   R-   Rf   R{   R   (    (   Rf   sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRI     sH    
			
		'c         K  s1   d d l  m } d | | | |  | |  S(   Ni˙˙˙˙(   R!   (   R#   R!   (   R   Rf   R{   R_   R!   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR`   Ę  s    c         K  s   t  | | d |  S(   Ni   (   t   rf(   R   Rf   R{   R_   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt    _eval_rewrite_as_RisingFactorialÎ  s    c         K  s$   | j  r  t |  t | |  Sd  S(   N(   RS   R    R   (   R   Rf   R{   R_   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   Ń  s    	c         K  s.   | j  r* | j  r* t |  t | |  Sd  S(   N(   RS   R    (   R   Rf   R{   R_   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   Ő  s    c         C  s1   t  |  j d j |  j d j |  j d j f  S(   Ni    i   (   R   R$   RS   RT   (   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRd   Ů  s    c         C  s9   d d  l  j } | j |  j d j   |  j d j    S(   Ni˙˙˙˙i    i   (   R   R   t   falling_factorialR$   R   (   R   R   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   Ý  s    (   R   R   R   Rk   RI   R`   R   R   R   Rd   R   (    (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   ^  s   55					R   c           B  s   e  Z d  Z d d  Z e d    Z e d    Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z RS(   sB  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{ff(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 what valued is 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

    References
    ==========

    .. [1] https://www.johndcook.com/blog/binomial_coefficients/

    i   c         C  sż   d d l  m } | d k r^ |  j \ } } t | |  | d | d  | d | | d  S| d k rŹ |  j \ } } t | |  | d | | d  | d | d  St |  |   d  S(   Ni˙˙˙˙(   R"   i   i    i   (   R#   R"   R$   R   R   (   R   R%   R"   R.   R{   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR&   9  s     c         C  s<  | j  r8| j  rä | d k rä t |  t |  } } | | k rJ t j S| | d k rg | | } n  t r d d l m } t | j | |   S| | d } } x3 t	 d | d  D] } | d 7} | | | } q¸ Wt |  S| | d } } x9 t	 d | d  D]$ } | d 7} | | 9} | | } q	W| Sn  d  S(   Ni    i   i˙˙˙˙(   R8   i   (
   R>   R(   R   R;   R	   RB   R8   R   t   bincoefR   (   R   R.   R{   R8   R[   RF   RG   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRn   H  s*    	



c         C  s]  t  t | | f  \ } } | | } | j | j } } | j s\ | sS | t k rc | j rc t j S| d j s | s | t k r | d j r | S| j rů | j sş | rÁ | rÁ | j rÁ t j	 S| j
 rY|  j | |  } | rň | j d t  S| Sn` | t k r| rt j S| j
 rYd d l m } | | d  | | d  | | | d  Sd  S(   Ni   t   basici˙˙˙˙(   R!   (   RY   R   RT   RS   R}   RX   R   R<   R?   R;   t	   is_numberRn   R   R+   R@   R#   R!   (   R-   R.   R{   R[   t   n_nonnegt   n_isintRL   R!   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRI   f  s(    
				c         C  s  |  j  \ } } t d   | | | f D  r= t d   n  t d   | | | f D  rt t | | f  \ } } t |  d } } | d k  r d S| d k  rÎ | | d } | d rĹ d n d } n  | | k rŢ d S| j } t |  } | rS| | k  r\| | } } x;| s!| rX| t | | | |  | } | | | | } } qWq| | }	 | |	 k r|	 | } }	 n  d }
 x) t	 d | d  D] } |
 | | }
 qW|
 } x- t	 | d |	 d  D] } | | | } qŇW| | 9} x- t	 |	 d | d  D] } | | | } qW| t
 |
 | | | d |  9} | | ;} n4t t |   } xt j d | d  D]} | | | k rŁ| | | } q|| | d k ršq|q|| | k rí| | | | k  r| | | } qq|| | } } d } } xM | d k rSt | | | | | k   } | | | | } } | | 7} qW| d k r|| t
 | | |  9} | | ;} q|q|Wt | |  Sd  S(   Nc         s  s   |  ] } | j  t k Vq d  S(   N(   RS   RX   (   t   .0Rf   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pys	   <genexpr>  s    s"   Integers expected for binomial Modc         s  s   |  ] } | j  Vq d  S(   N(   R>   (   R   Rf   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pys	   <genexpr>  s    i   i    i   i˙˙˙˙(   R$   t   anyRy   R   RY   R(   RU   RW   R   R   RJ   R)   R   R*   R   (   R   R3   R.   R{   RZ   RL   R\   R/   t   KR[   t   kfRG   t   dft   MR1   t   expt   a(    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR^   ~  sl    	

"	
c         K  sí   |  j  d } | j r# t |  j    S|  j  d } | j rU | | j  k rU | | } n  | j rÜ | t j k rt t j S| d k  r t j S|  j  d d } } x7 t d | d  D]" } | | | | 9} | | } qŻ W| Sn t |  j    Sd S(   s§   
        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)
        i    i   N(	   R$   R:   R   t   is_AddR>   R   R;   R<   R   (   R   t   hintsR.   R{   RF   RG   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_expand_funcÎ  s"    		c         K  s"   t  |  t  |  t  | |  S(   N(   R    (   R   R.   R{   R_   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   ę  s    c         K  s>   d d l  m } | | d  | | d  | | | d  S(   Ni˙˙˙˙(   R!   i   (   R#   R!   (   R   R.   R{   R_   R!   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR`   í  s    c         K  s   |  j  | |  j d  S(   Nt	   tractable(   R`   t   rewrite(   R   R.   R{   R_   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   _eval_rewrite_as_tractableń  s    c         K  s$   | j  r  t | |  t |  Sd  S(   N(   RS   t   ffR    (   R   R.   R{   R_   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   ô  s    	c         C  s<   |  j  \ } } | j r% | j r% t S| j t k r8 t Sd  S(   N(   R$   RS   R+   RX   (   R   R.   R{   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRd   ř  s
    c         C  sZ   |  j  \ } } | j rV | j rV | j s< | j s< | j r@ t S| j t k rV t Sn  d  S(   N(   R$   RS   RT   R?   Rv   R+   RX   (   R   R.   R{   (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyRu   ˙  s    (   R   R   R   R&   Rk   Rn   RI   R^   RĽ   R   R`   R¨   R   Rd   Ru   (    (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyR   ë  s   L	P						N()   t
   __future__R    R   t
   sympy.coreR   R   R   R   t   sympy.core.cacheR   RB   R   R   R	   t   sympy.core.functionR
   R   t   sympy.core.logicR   t   sympy.core.numbersR   R   t   sympy.core.relationalR   t   sympy.ntheoryR   t   sympy.polys.polytoolsR   t   mathR   R)   R   R    Rl   Rm   Rx   R   R   R   RŠ   R   (    (    (    sG   lib/python2.7/site-packages/sympy/functions/combinatorial/factorials.pyt   <module>   s(   "ęSu