�
��g� � �R � d Z ddlmZmZmZ ddlmZmZmZ ddl m
Z
d
d�Zd� Zd� Z
d S )z1Gosper's algorithm for hypergeometric summation. � )�S�Dummy�symbols)�Poly�parallel_poly_from_expr�factor)�is_sequenceTc �b � t | |f|dd�� � \ \ }}}|� � � |� � � }}|� � � |� � � }
} |j || z }}t d� � }
t ||
z ||
|j �� � }|� |
� |� � � � }t |�
� � � � � � � }t |� � D ]$}|j r|dk r|�
|� � �%t |� � D ]�}|� |
� |
� � � � }|� |� � }|
� |� | � � � � }
t% d|dz � � D ]}||� | � � z }���|� |� � }|s<|� � � }|
� � � }
|� � � }||
|fS )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 �e/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/concrete/gosper.py�
gosper_normalr7 s� � �L *�
�A�����/� /� /�K�F�Q��C�
�4�4�6�6�1�7�7�9�9�q�A��4�4�6�6�1�7�7�9�9�q�A��5�!�A�#�q�A�
�c�
�
�A��Q��U�A�q���,�,�,�A� ���A�I�I�a�L�L�!�!�A����� � �%�%�'�'�(�(�E�
��Z�Z� � ���|� �q�1�u�u��L�L��O�O�O��
�E�]�]� � ��
�E�E�!�'�'�1�"�+�+����
�E�E�!�H�H��
�E�E�!�'�'�1�"�+�+�����q�!�a�%��� � �A�
����!�����A�A� �
���Q���A�� �
�I�I�K�K��
�I�I�K�K��
�I�I�K�K���a��7�N� c �t � ddl m} || |� � }|�dS |� � � \ }}t |||� � \ }}}|� d� � }t |� � � � � } t |� � � � � }
t |� � � � � }| |
k s*|� � � |� � � k r|t | |
� � z
h}ne| s|| z
dz t
j h}nN|| z
dz |�
| dz
� � |�
| dz
� � z
|� � � z h}t |� � D ]$}
|
j r|
dk r|�
|
� � �%|sdS t |� � }
t d|
dz z t �� � } |� � � j |� }t% |||�� � }||� d� � z ||z z
|z
}dd lm} ||� � � |� � }|�dS |� � � � |� � }|D ]}||vr|� |d� � }�|j rdS |� � � |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 )� hypersimpN�����r zc:%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"