ó
¡¼™\c           @  s¬   d  Z  d d l m Z m Z d d l m Z m Z m Z d d l m	 Z	 m
 Z
 d d l m Z m Z m Z d d l m Z d d l m Z e d „ Z d	 „  Z d
 „  Z d S(   s1   Gosper's algorithm for hypergeometric summation. iÿÿÿÿ(   t   print_functiont   division(   t   St   Dummyt   symbols(   t   is_sequencet   range(   t   Polyt   parallel_poly_from_exprt   factor(   t   solve(   t	   hypersimpc         C  sÞ  t  |  | f | d t d t ƒ\ \ } } } | j ƒ  | j ƒ  } } | j ƒ  | j ƒ  }	 }
 | j | |	 } } t d ƒ } t | | | | d | j ƒ} | j |
 j	 | ƒ ƒ } t
 | j ƒ  j ƒ  ƒ } x: t
 | ƒ D], } | j s÷ | d k  rÛ | j | ƒ qÛ qÛ Wx‡ t | ƒ D]y } | j |
 j | 
ƒ ƒ } | j | ƒ } |
 j | j | ƒ ƒ }
 x/ t d | d ƒ D] } | | j | ƒ 9} qsWqW| j | ƒ } | sÑ| j ƒ  } |
 j ƒ  }
 | j ƒ  } n  | |
 | f S(   s?  
    Compute the Gosper's normal form of ``f`` and ``g``.

    Given relatively prime univariate polynomials ``f`` and ``g``,
    rewrite their quotient to a normal form defined as follows:

    .. math::
        \frac{f(n)}{g(n)} = Z \cdot \frac{A(n) C(n+1)}{B(n) C(n)}

    where ``Z`` is an arbitrary constant and ``A``, ``B``, ``C`` are
    monic polynomials in ``n`` with the following properties:

    1. `\gcd(A(n), B(n+h)) = 1 \forall h \in \mathbb{N}`
    2. `\gcd(B(n), C(n+1)) = 1`
    3. `\gcd(A(n), C(n)) = 1`

    This normal form, or rational factorization in other words, is a
    crucial step in Gosper's algorithm and in solving of difference
    equations. It can be also used to decide if two hypergeometric
    terms are similar or not.

    This procedure will return a tuple containing elements of this
    factorization in the form ``(Z*A, B, C)``.

    Examples
    ========

    >>> from sympy.concrete.gosper import gosper_normal
    >>> from sympy.abc import n

    >>> gosper_normal(4*n+5, 2*(4*n+1)*(2*n+3), n, polys=False)
    (1/4, n + 3/2, n + 1/4)

    t   fieldt	   extensiont   ht   domaini    i   (   R   t   Truet   LCt   monict   oneR   R   R   t	   resultantt   composet   sett   ground_rootst   keyst
   is_Integert   removet   sortedt   gcdt   shiftt   quoR   t
   mul_groundt   as_expr(   t   ft   gt   nt   polyst   pt   qt   optt   at   At   bt   Bt   Ct   ZR   t   Dt   Rt   rootst   rt   it   dt   j(    (    s4   lib/python2.7/site-packages/sympy/concrete/gosper.pyt   gosper_normal   s0    #*c         C  s~  t  |  | ƒ } | d k r d S| j ƒ  \ } } t | | | ƒ \ } } } | j d ƒ } t | j ƒ  ƒ } t | j ƒ  ƒ }	 t | j ƒ  ƒ }
 | |	 k sµ | j ƒ  | j ƒ  k rÎ |
 t | |	 ƒ h } n_ | sñ |
 | d t d ƒ h } n< |
 | d | j	 | d ƒ | j	 | d ƒ | j ƒ  h } x: t
 | ƒ D], } | j sV| d k  r:| j | ƒ q:q:W| std St | ƒ } t d | d d t ƒ} | j ƒ  j | Œ  } t | | d | ƒ} | | j d ƒ | | | } t | j ƒ  | ƒ } | d k rd S| j ƒ  j | ƒ } x/ | D]' } | | k r$| j | d ƒ } q$q$W| t j k rbd S| j ƒ  | | j ƒ  Sd S(   s  
    Compute Gosper's hypergeometric term for ``f``.

    Suppose ``f`` is a hypergeometric term such that:

    .. math::
        s_n = \sum_{k=0}^{n-1} f_k

    and `f_k` doesn't depend on `n`. Returns a hypergeometric
    term `g_n` such that `g_{n+1} - g_n = f_n`.

    Examples
    ========

    >>> from sympy.concrete.gosper import gosper_term
    >>> from sympy.functions import factorial
    >>> from sympy.abc import n

    >>> gosper_term((4*n + 1)*factorial(n)/factorial(2*n + 1), n)
    (-n - 1/2)/(n + 1/4)

    iÿÿÿÿi   i    s   c:%st   clsR   N(   R   t   Nonet   as_numer_denomR5   R   R   t   degreeR   t   maxt   nthR   R   R   R   R   t
   get_domaint   injectR   R
   t   coeffsR    t   subst   Zero(   R!   R#   R1   R%   R&   R)   R+   R,   t   Nt   Mt   KR.   R3   R>   R   t   xt   Ht   solutiont   coeff(    (    s4   lib/python2.7/site-packages/sympy/concrete/gosper.pyt   gosper_termS   sD    $<c         C  sò   t  } t | ƒ r$ | \ } } } n t } t |  | ƒ } | d k rI d S| r\ |  | } nŒ |  | d j | | ƒ |  | j | | ƒ } | t j k rè y2 |  | d j | | ƒ |  | j | | ƒ } Wqè t	 k
 rä d } qè Xn  t
 | ƒ S(   s.  
    Gosper's hypergeometric summation algorithm.

    Given a hypergeometric term ``f`` such that:

    .. math ::
        s_n = \sum_{k=0}^{n-1} f_k

    and `f(n)` doesn't depend on `n`, returns `g_{n} - g(0)` where
    `g_{n+1} - g_n = f_n`, or ``None`` if `s_n` can not be expressed
    in closed form as a sum of hypergeometric terms.

    Examples
    ========

    >>> from sympy.concrete.gosper import gosper_sum
    >>> from sympy.functions import factorial
    >>> from sympy.abc import i, n, k

    >>> f = (4*k + 1)*factorial(k)/factorial(2*k + 1)
    >>> gosper_sum(f, (k, 0, n))
    (-factorial(n) + 2*factorial(2*n + 1))/factorial(2*n + 1)
    >>> _.subs(n, 2) == sum(f.subs(k, i) for i in [0, 1, 2])
    True
    >>> gosper_sum(f, (k, 3, n))
    (-60*factorial(n) + factorial(2*n + 1))/(60*factorial(2*n + 1))
    >>> _.subs(n, 5) == sum(f.subs(k, i) for i in [3, 4, 5])
    True

    References
    ==========

    .. [1] Marko Petkovsek, Herbert S. Wilf, Doron Zeilberger, A = B,
           AK Peters, Ltd., Wellesley, MA, USA, 1997, pp. 73--100

    i   N(   t   FalseR   R   RH   R7   R?   R   t   NaNt   limitt   NotImplementedErrorR	   (   R!   t   kt
   indefiniteR(   R*   R"   t   result(    (    s4   lib/python2.7/site-packages/sympy/concrete/gosper.pyt
   gosper_sumŸ   s     %.2N(   t   __doc__t
   __future__R    R   t
   sympy.coreR   R   R   t   sympy.core.compatibilityR   R   t   sympy.polysR   R   R	   t   sympy.solversR
   t   sympy.simplifyR   R   R5   RH   RP   (    (    (    s4   lib/python2.7/site-packages/sympy/concrete/gosper.pyt   <module>   s   H	L