
    Ed                     R    d Z ddlmZmZmZ ddlmZmZmZ ddl	m
Z
 d
dZd Zd Zd	S )z1Gosper's algorithm for hypergeometric summation.     )SDummysymbols)Polyparallel_poly_from_exprfactor)is_sequenceTc                 b   t          | |f|dd          \  \  }}}|                                |                                }}|                                |                                }
}	|j        ||	z  }}t	          d          }t          ||z   |||j                  }|                    |
                    |                    }t          |
                                                                          }t          |          D ]$}|j        r|dk     r|                    |           %t          |          D ]}|                    |
                    |
                     }|                    |          }|
                    |                    |                     }
t%          d|dz             D ]}||                    |           z  }|                    |          }|s<|                                }|
                                }
|                                }||
|fS )a`  
    Compute the Gosper's normal form of ``f`` and ``g``.

    Explanation
    ===========

    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)field	extensionhdomainr      )r   LCmoniconer   r   r   	resultantcomposesetground_rootskeys
is_Integerremovesortedgcdshiftquorange
mul_groundas_expr)fgnpolyspqoptaAbBCZr   DRrootsridjs                        5lib/python3.11/site-packages/sympy/concrete/gosper.pygosper_normalr7      s   L *	
A/ / /KFQC 446617799qA446617799qA5!A#qAc

AQUAq,,,A	AIIaLL!!A  %%''((EZZ  | 	q1u 	LLOOOE]]  EE!''1"++EE!HHEE!''1"++q!a% 	 	A!AA	 	
QA IIKKIIKKIIKKa7N    c                 t   ddl m}  || |          }|dS |                                \  }}t          |||          \  }}}|                    d          }t          |                                          }	t          |                                          }
t          |                                          }|	|
k    s*|                                |                                k    r|t          |	|
          z
  h}ne|	s||	z
  dz   t
          j	        h}nN||	z
  dz   |
                    |	dz
            |
                    |	dz
            z
  |                                z  h}t          |          D ]$}|j        r|dk     r|                    |           %|sdS t          |          }t          d|dz   z  t                    } |                                j        | }t%          |||          }||                    d          z  ||z  z
  |z
  }dd	lm}  ||                                |          }|dS |                                                    |          }|D ]}||vr|                    |d          }|j        rdS |                                |z  |                                z  S )
a&  
    Compute Gosper's hypergeometric term for ``f``.

    Explanation
    ===========

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

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

    and `f_k` does not 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 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)

    r   )	hypersimpNr   zc:%s)clsr   )solve)sympy.simplifyr:   as_numer_denomr7   r   r   degreer   maxZeronthr   r   r   r   r   
get_domaininjectr   sympy.solvers.solversr=   coeffsr!   subsis_zero)r"   r$   r:   r2   r&   r'   r*   r,   r-   NMKr/   r4   rG   r   xHr=   solutioncoeffs                        r6   gosper_termrQ   S   s   4 )(((((	!QA tDAqAq!$$GAq!	A	!((**A	!((**A	!((**A	Q >ADDFFaddff$ >Q]O >UQYUQYq1ua!e4addff<=VV  | 	q1u 	HHQKKK tAAVq1u%5111F"Q\\^^"F+FVQv&&&A	!''!**qsQA++++++uQXXZZ((H t			""A ! !  	!ua  Ay )tyy{{1}QYY[[((r8   c                    d}t          |          r|\  }}}nd}t          | |          }|dS |r| |z  }n| |dz   z                      ||          | |z                      ||          z
  }|t          j        u rJ	 | |dz   z                      ||          | |z                      ||          z
  }n# t          $ r d}Y nw xY wt          |          S )aB  
    Gosper's hypergeometric summation algorithm.

    Explanation
    ===========

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

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

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

    Examples
    ========

    >>> from sympy.concrete.gosper import gosper_sum
    >>> from sympy import factorial
    >>> from sympy.abc import 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

    FTNr   )r	   rQ   rH   r   NaNlimitNotImplementedErrorr   )r"   k
indefiniter)   r+   r#   results          r6   
gosper_sumrY      s   P J1~~ 1aa
AqA t 	1QU)!!!Q''1Q3**Q*:*::QU? 	QU)**1a00AaC;;q!3D3DD&    &>>s   <6B3 3CCN)T)__doc__
sympy.corer   r   r   sympy.polysr   r   r   sympy.utilities.iterablesr	   r7   rQ   rY    r8   r6   <module>r_      s    7 7 ( ( ( ( ( ( ( ( ( ( = = = = = = = = = = 1 1 1 1 1 1H H H HVN) N) N)b? ? ? ? ?r8   