� ��g���R�dZddlmZmZmZddlmZmZmZddl 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|dd���\\}}}|���|���}}|���|���} } |j|| z } } t d��} t || z|| |j���}|�| �|����}t|� ��� ����}t|��D]$}|j r|dkr|� |���%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�onerrr� 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_normalr7s���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�ddlm}|||��}|�dS|���\}}t|||��\}}}|�d��}t |�����} t |�����} t |�����} | | ks*|���|���kr| t| | ��z h} ne| s| | z dzt j h} nN| | z dz|� | dz ��|� | dz ��z |���z h} t| ��D]$} | j r| dkr| � | ���%| sdSt| ��} td| dzzt���}|���j|�}t%|||���}||�d��z||zz |z }dd lm}||���|��}|�dS|����|��}|D]}||vr|�|d��}�|jrdS|���|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�����rzc:%s)�clsr)�solve)�sympy.simplifyr:�as_numer_denomr7rr�degreer�max�Zero�nthrrrrr� get_domain�injectr�sympy.solvers.solversr=�coeffsr!�subs�is_zero)r"r$r:r2r&r'r*r,r-�N�M�Kr/r4rGr�x�Hr=�solution�coeffs r6� gosper_termrQSs���4)�(�(�(�(�(�� �!�Q���A��y��t� � � � � �D�A�q��A�q�!�$�$�G�A�q�!� ���� � �A� �!�(�(�*�*� � �A� �!�(�(�*�*� � �A� �!�(�(�*�*� � �A� �Q���A�D�D�F�F�a�d�d�f�f�$�$� ��Q����]�O��� �>� ��U�Q�Y��� ��� ��U�Q�Y����q�1�u������a�!�e� � �4�a�d�d�f�f�<� =�� ��V�V�����|� �q�1�u�u� �H�H�Q�K�K�K�� ���t� �A���A� �V�q�1�u�%�5� 1� 1� 1�F� "�Q�\�\�^�^� "�F� +�F� �V�Q�v�&�&�&�A� �!�'�'�!�*�*� �q��s��Q��A�+�+�+�+�+�+��u�Q�X�X�Z�Z��(�(�H����t� � � � � ����"�"�A��!�!�� �� � ����u�a� � �A���y�)��t��y�y�{�{�1�}�Q�Y�Y�[�[�(�(r8c��d}t|��r|\}}}nd}t||��}|�dS|r||z}n�||dzz�||��||z�||��z }|tjurJ ||dzz�||��||z�||��z }n#t $rd}YnwxYwt|��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 rQrHr�NaN�limit�NotImplementedErrorr)r"�k� indefiniter)r+r#�results r6� gosper_sumrY�s��P�J��1�~�~�����1�a�a�� ��A�q���A��y��t�� ��1�����Q��U�)�!�!�!�Q�'�'�1�Q�3�*�*�Q��*:�*:�:�� �Q�U�?�?� ��Q��U�)�*�*�1�a�0�0�A�a�C�;�;�q�!�3D�3D�D����&� � � ����� ���� �&�>�>�s�<6B3�3 C�CN)T)�__doc__� sympy.corerrr� sympy.polysrrr�sympy.utilities.iterablesr r7rQrY�r8r6�<module>r_s���7�7�(�(�(�(�(�(�(�(�(�(�=�=�=�=�=�=�=�=�=�=�1�1�1�1�1�1�H�H�H�H�VN)�N)�N)�b?�?�?�?�?r8
Memory