ó
    ‰*£h{  ã                   óR   • S r SSKJrJrJr  SSKJrJrJr  SSK	J
r
  S	S jrS rS rg)
z1Gosper's algorithm for hypergeometric summation. é    )ÚSÚDummyÚsymbols)ÚPolyÚparallel_poly_from_exprÚfactor)Úis_sequencec                 ó˜  • [        X4USSS9u  u  pEnUR                  5       UR                  5       p‡UR                  5       UR                  5       p©UR                  Xy-  pË[	        S5      n[        X--   X-UR                  S9nUR                  U
R                  U5      5      nUR                  5       R                  5        Vs1 s H   nUR                  (       d  M  US:¼  d  M  UiM"     nn[        U5       H�  nUR                  U
R                  U75      5      nUR                  U5      nU
R                  UR                  U* 5      5      n
[!        SUS-   5       H  nUUR                  U* 5      -  nM     Mƒ     UR#                  U5      nU(       d0  UR%                  5       nU
R%                  5       n
UR%                  5       nXŠU4$ s  snf )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Úground_rootsÚkeysÚ
is_IntegerÚsortedÚgcdÚshiftÚquoÚrangeÚ
mul_groundÚas_expr)ÚfÚgÚnÚpolysÚpÚqÚoptÚaÚAÚbÚBÚCÚZr   ÚDÚRÚrÚrootsÚiÚdÚjs                        ÚR/home/mande/repo/quber/.venv/lib/python3.13/site-packages/sympy/concrete/gosper.pyÚgosper_normalr5      sq  € ôL *Ø	
ˆ�˜¨ñ/�K�F€QˆCð �4‰4‹6�1—7‘7“9€qØ�4‰4‹6�1—7‘7“9€qà�5‰5�!‘#€qÜˆc‹
€AäˆQ‰U�A §¡Ñ,€Aà	�‰�A—I‘I˜a“LÓ!€AØŸ™Ó(×-Ñ-Ô/ÓKÒ/�1°1·<µ<‹QÀAÈÁF�QÑ/€EÐKÜ�EŽ]ˆØ�E‰E�!—'‘'˜1˜"“+Óˆà�E‰E�!‹HˆØ�E‰E�!—'‘'˜1˜"“+Óˆä�q˜!˜a™%–ˆAØ�—‘˜!˜“ÑŠAó !ñ ð 	
�‰�Q‹€AæØ�I‰I‹KˆØ�I‰I‹KˆØ�I‰I‹Kˆà�ˆ7€Nùò# Ls   ÃGÃGÃ#Gc                 óÎ  • SSK Jn  U" X5      nUc  gUR                  5       u  pE[        XEU5      u  pgnUR	                  S5      n[        UR                  5       5      n	[        UR                  5       5      n
[        UR                  5       5      nXš:w  d"  UR                  5       UR                  5       :w  a  U[        Xš5      -
  1nO`U	(       d  X¹-
  S-   [
        R                  1nOAX¹-
  S-   UR                  U	S-
  5      UR                  U	S-
  5      -
  UR                  5       -  1n[        U5       H-  nUR                  (       a  US:  d  M  UR                  U5        M/     U(       d  g[        U5      n[        SUS-   -  [        S9nUR!                  5       R"                  " U6 n[%        XáUS9nUUR	                  S5      -  UU-  -
  U-
  nSS	KJn  U" UR+                  5       U5      nUc  gUR-                  5       R/                  U5      nU H  nUU;  d  M  UR/                  US5      nM     UR0                  (       a  gUR-                  5       U-  UR-                  5       -  $ )
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   )Ú	hypersimpNéÿÿÿÿr   zc:%s)Úclsr   )Úsolve)Úsympy.simplifyr7   Úas_numer_denomr5   r   r   Údegreer   ÚmaxÚZeroÚnthÚsetr   Úremover   r   Ú
get_domainÚinjectr   Úsympy.solvers.solversr:   Úcoeffsr   ÚsubsÚis_zero)r    r"   r7   r/   r$   r%   r(   r*   r+   ÚNÚMÚKr-   r2   rF   r   ÚxÚHr:   ÚsolutionÚcoeffs                        r4   Úgosper_termrP   N   sò  € õ4 )Ù�!‹€Aà�yØà×ÑÓ�D€Aä˜A !Ó$�G€Aˆ!Ø	�‰�‹€Aä	ˆ!�(‰(‹*‹€AÜ	ˆ!�(‰(‹*‹€AÜ	ˆ!�(‰(‹*‹€Aà	‹�A—D‘D“F˜aŸd™d›fÓ$Ø”�Q“‰]ˆO‰ÞØ‰U�Q‰YœŸ™Ð‰à‰U�Q‰Y˜Ÿ™˜q 1™u›¨¯©¨a°!©e«Ñ4°a·d±d³fÑ<Ð=ˆä�ŽVˆØ�|�|˜q 1�uØ�H‰H�QŽKñ ö ØäˆA‹€Aä�V˜q 1™uÑ%¬5Ñ1€FØ�\‰\‹^×"Ò" FÐ+€FäˆV˜vÑ&€AØ	ˆ!�'‰'�!‹*‰�q˜‘sÑ˜QÑ€Aå+Ù�Q—X‘X“Z Ó(€HàÑØà	�	‰	‹×Ñ˜Ó"€AãˆØ˜Õ Ø—‘�u˜aÓ ŠAñ ð 	‡y‡yØà�y‰y‹{˜1‰}˜QŸY™Y›[Ñ(Ð(ó    c                 óz  • Sn[        U5      (       a  Uu  pnOSn[        X5      nUc  gU(       a  X-  nOjXS-   -  R                  UW5      X-  R                  UW5      -
  nU[        R                  L a+   XS-   -  R                  X5      X-  R                  X5      -
  n[        U5      $ ! [         a    Sn Nf = f)aÒ  
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	   rP   rG   r   ÚNaNÚlimitÚNotImplementedErrorr   )r    ÚkÚ
indefiniter'   r)   r!   Úresults          r4   Ú
gosper_sumrY   Ÿ   s¿   € ðP €Jä�1‡~�~Ø‰ˆ‰aàˆ
ä�AÓ€Aà�yØæØ‘‰à˜‘U‘)×!Ñ! ! QÓ'¨1©3¯*©*°Q¸Ó*:Ñ:ˆà”Q—U‘UŠ?ðØ ™U™)×*Ñ*¨1Ó0°A±C·;±;¸qÓ3DÑD�ô �&‹>Ðøô 'ó Ø’ðús   Á6*B+ Â+B:Â9B:N)T)Ú__doc__Ú
sympy.corer   r   r   Úsympy.polysr   r   r   Úsympy.utilities.iterablesr	   r5   rP   rY   © rQ   r4   Ú<module>r_      s*   ðÙ 7ç (Ñ (ß =Ñ =Ý 1ôCòLN)ób?rQ   