� ��g=���B�ddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZddlmZdd lmZmZdd lmZdd lmZmZdd lmZddlmZddlmZddl m!Z!m"Z"m#Z#m$Z$ddl%m&Z&ddl'm(Z(m)Z)ddl*m+Z+m,Z,ddl-m.Z.ddl/m0Z0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7ddl8m9Z9ddl:m;Z;ddl<m=Z=ddl>m?Z?m@Z@ddlAmBZBmCZCmDZDddlEmFZFddlGmHZHdd lImJZJdd!lKmLZLdd"lMmNZNmOZOdd#lPmQZQdd$lRZRGd%�d&e e��ZSd'�ZTd(�ZUd)�ZVd*�ZWd+�ZXd,�ZYd-�ZZd.�Z[d/�Z\d0�Z]d1�Z^d$S)2�)�Tuple)� is_decreasing)�AccumulationBounds�)�ExprWithIntLimits)� AddWithLimits)� gosper_sum)�Expr)�Add)� Derivative�expand)�Mul)�Float�_illegal)�Eq)�S)�ordered)�Dummy�Wild�Symbol�symbols)� factorial)� bernoulli�harmonic)�exp�log)� Piecewise)�cot�csc)�hyper)�KroneckerDelta)�zeta)�Integral)�And)�apart)�PolynomialError�PolificationFailed)�parallel_poly_from_expr�Poly�factor)�together)� limit_seq)�O��residue)� FiniteSet�Interval)�siftNc��eZdZUdZdZeeeeefed<d�Z d�Z d�Z d�Z d�Z d �Zd �Zd �Zd �Zd �Zd�Zd�Zd�Zdd�Zd�Zd�ZdS)�SumaI Represents unevaluated summation. Explanation =========== ``Sum`` represents a finite or infinite series, with the first argument being the general form of terms in the series, and the second argument being ``(dummy_variable, start, end)``, with ``dummy_variable`` taking all integer values from ``start`` through ``end``. In accordance with long-standing mathematical convention, the end term is included in the summation. Finite sums =========== For finite sums (and sums with symbolic limits assumed to be finite) we follow the summation convention described by Karr [1], especially definition 3 of section 1.4. The sum: .. math:: \sum_{m \leq i < n} f(i) has *the obvious meaning* for `m < n`, namely: .. math:: \sum_{m \leq i < n} f(i) = f(m) + f(m+1) + \ldots + f(n-2) + f(n-1) with the upper limit value `f(n)` excluded. The sum over an empty set is zero if and only if `m = n`: .. math:: \sum_{m \leq i < n} f(i) = 0 \quad \mathrm{for} \quad m = n Finally, for all other sums over empty sets we assume the following definition: .. math:: \sum_{m \leq i < n} f(i) = - \sum_{n \leq i < m} f(i) \quad \mathrm{for} \quad m > n It is important to note that Karr defines all sums with the upper limit being exclusive. This is in contrast to the usual mathematical notation, but does not affect the summation convention. Indeed we have: .. math:: \sum_{m \leq i < n} f(i) = \sum_{i = m}^{n - 1} f(i) where the difference in notation is intentional to emphasize the meaning, with limits typeset on the top being inclusive. Examples ======== >>> from sympy.abc import i, k, m, n, x >>> from sympy import Sum, factorial, oo, IndexedBase, Function >>> Sum(k, (k, 1, m)) Sum(k, (k, 1, m)) >>> Sum(k, (k, 1, m)).doit() m**2/2 + m/2 >>> Sum(k**2, (k, 1, m)) Sum(k**2, (k, 1, m)) >>> Sum(k**2, (k, 1, m)).doit() m**3/3 + m**2/2 + m/6 >>> Sum(x**k, (k, 0, oo)) Sum(x**k, (k, 0, oo)) >>> Sum(x**k, (k, 0, oo)).doit() Piecewise((1/(1 - x), Abs(x) < 1), (Sum(x**k, (k, 0, oo)), True)) >>> Sum(x**k/factorial(k), (k, 0, oo)).doit() exp(x) Here are examples to do summation with symbolic indices. You can use either Function of IndexedBase classes: >>> f = Function('f') >>> Sum(f(n), (n, 0, 3)).doit() f(0) + f(1) + f(2) + f(3) >>> Sum(f(n), (n, 0, oo)).doit() Sum(f(n), (n, 0, oo)) >>> f = IndexedBase('f') >>> Sum(f[n]**2, (n, 0, 3)).doit() f[0]**2 + f[1]**2 + f[2]**2 + f[3]**2 An example showing that the symbolic result of a summation is still valid for seemingly nonsensical values of the limits. Then the Karr convention allows us to give a perfectly valid interpretation to those sums by interchanging the limits according to the above rules: >>> S = Sum(i, (i, 1, n)).doit() >>> S n**2/2 + n/2 >>> S.subs(n, -4) 6 >>> Sum(i, (i, 1, -4)).doit() 6 >>> Sum(-i, (i, -3, 0)).doit() 6 An explicit example of the Karr summation convention: >>> S1 = Sum(i**2, (i, m, m+n-1)).doit() >>> S1 m**2*n + m*n**2 - m*n + n**3/3 - n**2/2 + n/6 >>> S2 = Sum(i**2, (i, m+n, m-1)).doit() >>> S2 -m**2*n - m*n**2 + m*n - n**3/3 + n**2/2 - n/6 >>> S1 + S2 0 >>> S3 = Sum(i, (i, m, m-1)).doit() >>> S3 0 See Also ======== summation Product, sympy.concrete.products.product References ========== .. [1] Michael Karr, "Summation in Finite Terms", Journal of the ACM, Volume 28 Issue 2, April 1981, Pages 305-350 https://dl.acm.org/doi/10.1145/322248.322255 .. [2] https://en.wikipedia.org/wiki/Summation#Capital-sigma_notation .. [3] https://en.wikipedia.org/wiki/Empty_sum ��limitsc��tj||g|�Ri|��}t|d��s|Std�|jD����rt d���|S)Nr6c3�DK�|]}t|��dkpd|vV��dS)�N)�len)�.0�ls �i/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/concrete/summations.py� <genexpr>zSum.__new__.<locals>.<genexpr>�s5����<�<�A�s�1�v�v��{�'�d�a�i�<�<�<�<�<�<�z/Sum requires values for lower and upper bounds.)r�__new__�hasattr�anyr6� ValueError)�cls�functionr� assumptions�objs r=r@z Sum.__new__�sq���#�C��K�G�K�K�K�{�K�K���s�H�%�%� ��J� �<�<���<�<�<� <� <� P��N�O�O� O�� r?c�0�|jjs|jrdSdS�NT)rE�is_zero�has_empty_sequence��selfs r=� _eval_is_zerozSum._eval_is_zero�s*�� �=� � �D�$;� ��4� � r?c�,�|jrdS|jjSrI)rKrE�is_extended_realrLs r=�_eval_is_extended_realzSum._eval_is_extended_real�s�� � "� ��4��}�-�-r?c�B�|jr|jdur|jjSdSdS�NF)�has_finite_limits�has_reversed_limitsrE� is_positiverLs r=�_eval_is_positivezSum._eval_is_positive��5�� � !� -�d�&>�%�&G�&G��=�,� ,� -� -�&G�&Gr?c�B�|jr|jdur|jjSdSdSrS)rTrUrE� is_negativerLs r=�_eval_is_negativezSum._eval_is_negative�rXr?c�4�|jr|jjrdSdSdSrI)rTrE� is_finiterLs r=�_eval_is_finitezSum._eval_is_finite�s2�� � !� �d�m�&=� ��4� � � � r?c �@��|�dd��r|jjdi|��}n|j}i}|jD]}t |��}|r |||d<�|r�d�|���D���|�|��jdi|��}t|t��rt�fd�|D����}n|�|����}n|}|S|jj r=|� ��}||kr|���St|��St|j��D]�\}} | \} } } | | z } | dkrtjcS| jr| jr | dz| dz } } | }t#|| | | f��}|�K||jkr#|�|| | | f��}|�|cS|cS|j|g|j|d��R�cS|}��|�dd��r"t|t(��s |jdi|��S|S) N�deepTrc��i|]\}}||�� Sr5r5)r;�k�vs r=� <dictcomp>zSum.doit.<locals>.<dictcomp>�s��2�2�2�T�Q��A�q�2�2�2r?c�:��g|]}|������Sr5��xreplace)r;�i�undos �r=� <listcomp>zSum.doit.<locals>.<listcomp>�s%���;�;�;�!�Q�Z�Z��-�-�;�;�;r?�����rr5)�getrE�doitr6�)_dummy_with_inherited_properties_concrete�itemsrg� isinstance�tuple� is_Matrixr �_eval_matrix_sum� enumerater�Zero� is_integerrZ�eval_sum�eval_zeta_function�funcr)rM�hints�f�reps�xab�d�did�expanded�n�limitrh�a�b�dif�newf� zeta_functionris @r=rmzSum.doit�s���� �9�9�V�T� "� "� �"�� �"�+�+�U�+�+�A�A�� �A����;� !� !�C�9�#�>�>�A�� !� ��S��V� �� � �2�2�T�Z�Z�\�\�2�2�2�D�*�$�-�-��%�%�*�3�3�U�3�3�C��#�u�%�%� ��;�;�;�;�s�;�;�;�<�<������l�l�4�(�(������J� �=� "� *��{�{�}�}�H��x����}�}���&�#�D�)�)� )�!�$�+�.�.� � �H�A�u��G�A�q�!��a�%�C��b�y�y��v� � � ��~� �#�/� ��1�u�a�!�e�1���B���A��1�a�y�)�)�D��|��� �%�%�$(�$;�$;�A��1�a�y�$I�$I�M�$�0�,�,�,�,��K�K�K�$�4�9�Q�9���Q�R�R��9�9�9�9�9�9��A�A� �9�9�V�T� "� "� '��a��+�+� '��q�v������&��r?c��|\}}}td|g���td|g���td|g���}}}|�||z|z| z��} | �y|tjurmd| || |zz } | |} | || |z |z} t | t | | ��zt | dk| dk��f|df��SdSdS) z� Check whether the function matches with the zeta function. If it matches, then return a `Piecewise` expression because zeta function does not converge unless `s > 1` and `q > 0` �w��exclude�y�zNrrT)r�matchr�Infinityrr"r$) rMr{r6rhr�r�r�r�r��result�coeff�s�qs r=rxzSum.eval_zeta_functions������1�a��s�Q�C�(�(�(�$�s�Q�C�*@�*@�*@�$�s�UV�TW�BX�BX�BX�a�1�����!�a�%�!�)�!��,�-�-�� � �!�q�z�/�/���q� �V�A�Y�.�.�E��q� �A��q� �F�1�I�%��)�A��e�d�1�a�j�j�0�#�a�!�e�Q��U�2C�2C�D�t�T�l�S�S� S� � �/�/r?c�^�t|t��r||jvr tjS|jt |j��}}|�d��}|r|j |g|�R�}|\}}}||jvs ||jvrdSt||d���}|� ||��} | S)a� Differentiate wrt x as long as x is not in the free symbols of any of the upper or lower limits. Explanation =========== Sum(a*b*x, (x, 1, a)) can be differentiated wrt x or b but not `a` since the value of the sum is discontinuous in `a`. In a case involving a limit variable, the unevaluated derivative is returned. rkNT)�evaluate) rpr� free_symbolsrrurE�listr6�popryr ) rM�xr{r6r��_r�r��df�rvs r=�_eval_derivativezSum._eval_derivative s���" �a�� � � �Q�d�.?�%?�%?��6�M��M�4�� �#4�#4�6��� � �2���� � &��� �!�%�f�%�%�%�A����1�a� ��� � �!�q�~�"5�"5��4� ��1�t� ,� ,� ,�� �Y�Y�r�5� !� !��� r?c� �|jd\}}}|�|||z��}t|j��dkr|jd}n|j|jdd��}t |||dz|f�����S)Nrk�rr)�args�subsr:ryr4rm)rMr��steprbr��upper� new_upperr{s r=�_eval_difference_deltazSum._eval_difference_deltaCs����i��m� ��1�e��J�J�q�!�d�(�+�+� � �t�y�>�>�Q� � �� �!� �A�A��� �4�9�S�b�S�>�*�A��1�q�%�!�)�Y�/�0�0�5�5�7�7�7r?c ��|j}|�dd��r |jdi|��}tjt |����}g}g}|D]�}|�t��r�tjt |����}g}|D]M} t| t��r!|� | j di|�����8|� | ���N|� t|�����|� |����ddl m } m} t| |��g|�R�} | | |j���S)Nr`Tr)� factor_sum� sum_combine)r6r5)rErl�simplifyr � make_argsr �hasr4rrp�append�_eval_simplify�sympy.simplify.simplifyr�r�r6) rM�kwargsrE�terms�s_t�o_t�term�subterms� out_terms�subtermr�r�r�s r=r�zSum._eval_simplifyNs}���=�� �:�:�f�d� #� #� 3�(�x�(�2�2�6�2�2�H�� �f�X�.�.�/�/������� !� !�D��x�x��}�}� !��=�����6�6��� �'�2�2�G�!�'�3�/�/�2�!�(�(�)?��)?�)I�)I�&�)I�)I�J�J�J�J�"�(�(��1�1�1�1�� � �3� �?�+�+�+�+�� � �4� � � � � D�C�C�C�C�C�C�C��[�[��%�%�,��,�,�,���z�&���5�5�5�5r?c ��)�*�+�,�-�tdt���\}}}|jdd�,|jdd�*|jdd�-|j���}t |j��dkrtd����*jr�-jr tj S�*tj ur��-tj ur^t|�,dtj f�����o.t|�,tj df�����Sddlm}||��,�, i����}�- �*tj �-t#�,jdd� ��}|��,|i��}|�,t'�*�-���)|jrm|jD]Y\}}|dks%|���jtj ur)t|�,�*�-f��} | ���cS�Ztj S t1|�,��} | �| jd ur tjSn#t$rYnwxYw t1t7|���,��} | �| jd ur tjSn#t$rYnwxYwt9|�,tj f��} | j��,|z��} | �0| |d kr tj S| |d kr tjS| j�d�,|zt?d�,z ��|zzt?t?d�,z �� ��|zzz ��pZ| j�d�,|zt?d�,z �� |zzt?t?d�,z �� ��|zzz ��}|�d||dks@||dkr ||dks(||||cxkrdkrnn||dkr tj StjS t1�,|z�,��}|�|j r|dkr tjSn#t$rYnwxYw|��,�,dzi��}dd l!m"}ddl#m$}||||z ����} t1|�,��}|�E|j r>t7|��dkr tjSt7|��dkr tj Sn#t$rd }YnwxYw|dkr��,||�%�,�,dz��z dz z}|�&��} t1|�,��} | �+| j r$| dkr tj S| dkr tjSn#t$rYnwxYw t1t7|��d�,z z�,��}|�+|j r$|dkr tj S|dkr tjSn#t$rYnwxYw|�tj'�,|zz|z��}||�(�,��s"tS||�)��r tj Sd }ddl*m+}||�,�,���,�)��}|s�)}n;t[|t\��r&|jj rt'|j�)j��}|�utS||��stS| |��rTt_|�,�*�-f��} |�0��}|j rt|j��Sn#t$rYnwxYw| jj1r�| jj}te|��}t#dd����+�)�+�,fd�}�*�,�-fd�} tgdt |����D]u}!tij5||!��D]]}"|te|"��z }#tm|"�}$tm|#�}%tS|$�)��r||%��}&|&�|&ccS| |$|%��}'|'�|'ccS�^�v|jdd}(|��,|(i��}td|z���)ad Checks for the convergence of a Sum. Explanation =========== We divide the study of convergence of infinite sums and products in two parts. First Part: One part is the question whether all the terms are well defined, i.e., they are finite in a sum and also non-zero in a product. Zero is the analogy of (minus) infinity in products as :math:`e^{-\infty} = 0`. Second Part: The second part is the question of convergence after infinities, and zeros in products, have been omitted assuming that their number is finite. This means that we only consider the tail of the sum or product, starting from some point after which all terms are well defined. For example, in a sum of the form: .. math:: \sum_{1 \leq i < \infty} \frac{1}{n^2 + an + b} where a and b are numbers. The routine will return true, even if there are infinities in the term sequence (at most two). An analogous product would be: .. math:: \prod_{1 \leq i < \infty} e^{\frac{1}{n^2 + an + b}} This is how convergence is interpreted. It is concerned with what happens at the limit. Finding the bad terms is another independent matter. Note: It is responsibility of user to see that the sum or product is well defined. There are various tests employed to check the convergence like divergence test, root test, integral test, alternating series test, comparison tests, Dirichlet tests. It returns true if Sum is convergent and false if divergent and NotImplementedError if it cannot be checked. References ========== .. [1] https://en.wikipedia.org/wiki/Convergence_tests Examples ======== >>> from sympy import factorial, S, Sum, Symbol, oo >>> n = Symbol('n', integer=True) >>> Sum(n/(n - 1), (n, 4, 7)).is_convergent() True >>> Sum(n/(2*n + 1), (n, 1, oo)).is_convergent() False >>> Sum(factorial(n)/5**n, (n, 1, oo)).is_convergent() False >>> Sum(1/n**(S(6)/5), (n, 1, oo)).is_convergent() True See Also ======== Sum.is_absolutely_convergent sympy.concrete.products.Product.is_convergent zp q r)rDrrr�zNconvergence checking for more than one symbol containing series is not handled�r�T��integer�positiveNFrk��combsimp)�powsimp)�solveset�m)r�c���� tt|��j�f��������}|�|jrt jSdSdS#t$rYdSwxYw�N)r,r4�infrmr]r�true�NotImplementedError)�g_n�ing_val�intervalr��syms ���r=�_dirichlet_testz*Sum.is_convergent.<locals>._dirichlet_testqs|����'��C�#�x�|�Q�1G�(H�(H�(M�(M�(O�(O�QR�S�S�G��*�w�/@�*� �v� �+�*�*�*��*�����D�D����s�A A� A"�!A"c��� t|���}|�a|js)t|t��rI|j|jz jr7t |���f�����rtj SdSdSdSdS#t$rYdSwxYwr�) r,r]rpr�max�minr4�is_absolutely_convergentrr�r�)�g1_n�g2_n�lim_val� lower_limitr�� upper_limits ���r=�_bounded_convergent_testz3Sum.is_convergent.<locals>._bounded_convergent_testzs�����'��c�2�2�G��*��0A�*�"�7�,>�?�?�+�$�[�7�;�6�A�+� #�4�#�{�K�)H�I�I�b�b�d�d�.�'(�v� � +�*�.�.�+�*�*�*�� +�����D�D����s�A2A>�> B � B zFThe algorithm to find the Sum convergence of %s is not yet implemented)7rrr6rEr�r:r�r�r]rr��NegativeInfinityr�r4� is_convergentr�rgr�namer1� is_Piecewiser��as_set�supr,rJ�false�absr-�exprr�r� is_number�sympy.simplify.combsimpr��sympy.simplify.powsimpr�r�� gammasimp� NegativeOner�r�sympy.solvers.solvesetr��diffrpr0r#rm�is_Mul�set�range� itertools� combinationsr).rM�pr��r� sequence_termr��sym_ry�condr�r�� lim_val_abs�order� p_series_test� n_log_test�lim_comp�next_sequence_termr�r��ratio� lim_ratio�test_val� lim_evaluated�dict_val�check_intervalr��maxima� integral_val�integral_val_evaluatedr��argsetr�r�r��a_tuple�b_set�a_n�b_n�dirich�bc_test�_symr�r�r�r�r�s. @@@@@r=r�zSum.is_convergentus� �������T�'�t�,�,�,���1�a��k�!�n�Q����k�!�n�Q�'� ��k�!�n�Q�'� �� �.�.�0�0� � �}�)� *� *�Q� .� .�%�'I�J�J� J� � � �[�%:� ��6�M� �!�,� ,� ,��a�j�(�(��=�3��1�:�*>�?�?�M�M�O�O�Y��M�C��1C�Q�+G�H�H�V�V�X�X�Y� 8� 8� 8� 8� 8� 8�$�H�]�%;�%;�S�3�$�K�%H�%H�I�I�M�&�,�K��*�K��S�X�t�d�;�;�;��%�.�.��T�{�;�;� ����K��5�5�� � %� �+�0� -� -� ��d��4�<�<�4�;�;�=�=�#4�� �#B�#B��D�3� �[�"A�B�B�A��?�?�,�,�,�,�,�$C��6�M� ��}�c�2�2�7��!�g�o��&>�&>��g�~���"� � � � �D� ���� �#�C� �$6�$6��<�<�K��&�;�+>�%�+G�+G��w����"� � � � �D� �����-�#�q�z�!2�3�3��� �(�(��a��0�0� � � $��Q��"�$�$��v� ��Q��2�%�%��w���j�&�&�q�#�q�&��Q�s�U���Q��*>�s�C��#��J�J�;�?O�?O�QR�?R�*R�'S�T�T�X��j�&�&�q�#�q�&�3�q��u�:�:�+��1A�*A�#�s�1�S�5�z�z�k�BR�BR�TU�BU�*U�'V�W�W� � � !��1� ��!�!��A��!�#�#� �1� ��(9�(9��A��*�Q�-�4�4�4�4�1�4�4�4�4�4��A���9J�9J��6�M��7�N� � ��]�!2�C�8�8�H��#��(:�#�x�!�|�|��w����"� � � � �D� ����+�3�3�S�#��'�N�C�C��4�4�4�4�4�4�2�2�2�2�2�2�����!3�M�!A�B�B�C�C�� �!�%��-�-�I��$��)<�$��y�>�>�A�%�%��7�N��y�>�>�A�%�%��6�M���"� � � ��I�I�I� ���� ��>�>��M�&�+�+�C��q��9�9�:�<=�>�?�H��)�)�+�+�H� �#�H�c�2�2���&�7�+<�&���{�{� �v� ���{�{� �w����&� � � ��� ����  �%�c�-�&8�&8�1�S�5�&A�3�G�G�M��(�]�-D�(� �1�$�$��6�M� �1�$�$��7�N���"� � � � �D� ����!�&�&�q�}�s�Q�w�'?��'A�B�B����{���s�#�#� � �h�q�k�8�(L�(L� ��6�M���3�3�3�3�3�3���-�,�,�S�1�1�3��A�A��� @�%�N�N� �� � *� *� @�v�z�/C� @�%�f�j�(�,�?�?�N� � &� �=�.� 9� 9� '� �=�.�.� 9� 9� '� (�!�C��k�#B� D� D� ��-9�->�->�-@�-@�*�-�7�C� �!7�!A�B�B�B�C��*�����D�����. �:� �' '��:�?�D���Y�Y�F��c�4�(�(�(�A� � � � � � � � � � � � � � ��1�c�&�k�k�*�*� '� '��(�5�d�A�>�>� '� '�G�"�S��\�\�1�E��w�-�C��u�+�C�$�S�(�3�3�*�!0���!5�!5��!�-�#)�M�M�M�M�M�6�6�s�C�@�@�G��*�&������+� '��{�1�~�a� ��%�.�.��T�{�;�;� �!�#;�>K�#M�N�N� Ns��&I� I � I �3J� J�J�-Q � Q�Q�7S2�S2�2 T�T�*U?�,U?�? V � V �=W!�W!�! W.�-W.�5.\%�% \2�1\2c�t�tt|j��|j�����S)ax Checks for the absolute convergence of an infinite series. Same as checking convergence of absolute value of sequence_term of an infinite series. References ========== .. [1] https://en.wikipedia.org/wiki/Absolute_convergence Examples ======== >>> from sympy import Sum, Symbol, oo >>> n = Symbol('n', integer=True) >>> Sum((-1)**n, (n, 1, oo)).is_absolutely_convergent() False >>> Sum((-1)**n/n**2, (n, 1, oo)).is_absolutely_convergent() True See Also ======== Sum.is_convergent )r4r�rEr6r�rLs r=r�zSum.is_absolutely_convergent�s,��6�3�t�}�%�%�t�{�3�3�A�A�C�C�Cr?rTc�������t|��}t|��}|j�t|j��dkrt d���|jd\�����kdkr/��z dkrt jt jfS�dz�dz c��� �t j}|�rZ�jr�jrt|��z dz��}|r�� ���r%t���fd�t|��D���}nۉ� ����}|rQt|�d����|k}|dkr|t|��fS|dks|t jfS|}td|��D]_}�� ��|z��}t|�d����|kr|dkr|t|��fcS||z }�`��z dz|kr|t jfS�|z �td��} t!�� �| ��| ��f��} |r| ���} || z }���fd �} | ���\} } | | zd z }�����}td|d z��D]�}| |��\}}t'd |z��t)d |z��z ||z z}||krnt|rT|rR|�d��}|t jurt jt jfcSt|��|krn||z }|��d d� ��}��||zt|��fS) a Return an Euler-Maclaurin approximation of self, where m is the number of leading terms to sum directly and n is the number of terms in the tail. With m = n = 0, this is simply the corresponding integral plus a first-order endpoint correction. Returns (s, e) where s is the Euler-Maclaurin approximation and e is the estimated error (taken to be the magnitude of the first omitted term in the tail): >>> from sympy.abc import k, a, b >>> from sympy import Sum >>> Sum(1/k, (k, 2, 5)).doit().evalf() 1.28333333333333 >>> s, e = Sum(1/k, (k, 2, 5)).euler_maclaurin() >>> s -log(2) + 7/20 + log(5) >>> from sympy import sstr >>> print(sstr((s.evalf(), e.evalf()), full_prec=True)) (1.26629073187415, 0.0175000000000000) The endpoints may be symbolic: >>> s, e = Sum(1/k, (k, a, b)).euler_maclaurin() >>> s -log(a) + log(b) + 1/(2*b) + 1/(2*a) >>> e Abs(1/(12*b**2) - 1/(12*a**2)) If the function is a polynomial of degree at most 2n+1, the Euler-Maclaurin formula becomes exact (and e = 0 is returned): >>> Sum(k, (k, 2, b)).euler_maclaurin() (b**2/2 + b/2 - 1, 0) >>> Sum(k, (k, 2, b)).doit() b**2/2 + b/2 - 1 With a nonzero eps specified, the summation is ended as soon as the remainder term is less than the epsilon. rzMore than 1 limitrTc�B��g|]}����|z����Sr5�r�)r;rbr�r{rhs ���r=rjz'Sum.euler_maclaurin.<locals>.<listcomp>�s+���=�=�=�q�!�&�&��A��E�*�*�=�=�=r?r9Fr�c����tjur|�����dfS|�����|�����fS)Nr)rr�r�)r�r�r�rhs ���r=�fpointz#Sum.euler_maclaurin.<locals>.fpoint sH����A�J����y�y��A����)�)��9�9�Q��?�?�D�I�I�a��O�O�3� 3r?r�r�)�intrEr:r6rCrru� is_Integerr�� is_polynomialr r�r�r��evalfrr#rmr�rr�NaN)rMr�r��eps� eval_integralr�r��testrbr��Ir �fa�fb�iterm�g�ga�gb� term_evalfr�r�r{rhs @@@@r=�euler_maclaurinzSum.euler_maclaurin�s�������V ��F�F�� ��F�F�� �M�� �t�{� � �q� � ��0�1�1� 1��+�a�.���1�a� ��E�d�?�?��1�u��z�z��v�q�v�~�%��q�5�!�a�%�D�A�q���A� �F�� � ��|� &�� � &���1�q�5�1�9�%�%��� �!�/�/�!�,�,� ��=�=�=�=�=�=�E�!�H�H�=�=�=�>����v�v�a��|�|���,��t�z�z�!�}�}�-�-��3�D��t�|�|� �#�d�)�)�|�+�"�e�m�m�#�Q�V�|�+����q�!�����A��6�6�!�Q��U�+�+�D��4�:�:�a�=�=�)�)�C�/�/�D�A�I�I� �#�d�)�)�|�+�+�+���I�A�A��1�u�q�y�A�~�~��!�&�y� � ��F�A� �#�J�J�� �Q�V�V�A�q�\�\�A�q�!�9� -� -�� � ������A� �Q��� 4� 4� 4� 4� 4� 4� 4��������B��b��!� �� �F�F�1�I�I���q�!�a�%��� -� -�A��V�A�Y�Y�F�B���Q�q�S�>�>�)�A�a�C�.�.�0�"�r�'�:�D��1�u�u���� �t� �!�Z�Z��]�]� ����&�&��5�!�%�<�'�'�'��z�?�?�S�(�(��E� ��I�A����q�!�e��,�,�A�A��5�y�#�d�)�)�#�#r?c��t|��}t|��D]2\}}t|t��s|�|��||<�3d}g}t|j��D]@\}}|}||vr | }|d|ddz|ddz f}|�|���At||jzg|�R�S)a] Reverse the order of a limit in a Sum. Explanation =========== ``reverse_order(self, *indices)`` reverses some limits in the expression ``self`` which can be either a ``Sum`` or a ``Product``. The selectors in the argument ``indices`` specify some indices whose limits get reversed. These selectors are either variable names or numerical indices counted starting from the inner-most limit tuple. Examples ======== >>> from sympy import Sum >>> from sympy.abc import x, y, a, b, c, d >>> Sum(x, (x, 0, 3)).reverse_order(x) Sum(-x, (x, 4, -1)) >>> Sum(x*y, (x, 1, 5), (y, 0, 6)).reverse_order(x, y) Sum(x*y, (x, 6, 0), (y, 7, -1)) >>> Sum(x, (x, a, b)).reverse_order(x) Sum(-x, (x, b + 1, a - 1)) >>> Sum(x, (x, a, b)).reverse_order(0) Sum(-x, (x, b + 1, a - 1)) While one should prefer variable names when specifying which limits to reverse, the index counting notation comes in handy in case there are several symbols with the same name. >>> S = Sum(x**2, (x, a, b), (x, c, d)) >>> S Sum(x**2, (x, a, b), (x, c, d)) >>> S0 = S.reverse_order(0) >>> S0 Sum(-x**2, (x, b + 1, a - 1), (x, c, d)) >>> S1 = S0.reverse_order(1) >>> S1 Sum(x**2, (x, b + 1, a - 1), (x, d + 1, c - 1)) Of course we can mix both notations: >>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1) Sum(x*y, (x, b + 1, a - 1), (y, 6, 1)) >>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x) Sum(x*y, (x, b + 1, a - 1), (y, 6, 1)) See Also ======== sympy.concrete.expr_with_intlimits.ExprWithIntLimits.index, reorder_limit, sympy.concrete.expr_with_intlimits.ExprWithIntLimits.reorder References ========== .. [1] Michael Karr, "Summation in Finite Terms", Journal of the ACM, Volume 28 Issue 2, April 1981, Pages 305-350 https://dl.acm.org/doi/10.1145/322248.322255 rrr�) r�rtrpr �indexr6r�r4rE) rM�indices� l_indicesrh�indx�er6r�r<s r=� reverse_orderzSum.reverse_order!s���|��M�M� � ��+�+� 0� 0�G�A�t��d�C�(�(� 0�#�z�z�$�/�/� �!� �� ����!�$�+�.�.� � �H�A�u��A��I�~�~��B���1�X�u�Q�x�!�|�U�1�X��\�:�� �M�M�!� � � � ��1�t�}�$�.�v�.�.�.�.r?c��ddlm}|jjr-t |t |j��g|j�R���SdS)Nr)�Product)�sympy.concrete.productsr%rErPrrr6)rMr�r�r%s r=�_eval_rewrite_as_ProductzSum._eval_rewrite_as_ProductpsZ��3�3�3�3�3�3� �=� )� B��w�w�s�4�=�1�1�@�D�K�@�@�@�A�A� A� B� Br?N)rrrT)�__name__� __module__� __qualname__�__doc__� __slots__�tTuplerr �__annotations__r@rNrQrWr[r^rmrxr�r�r�r�r�rr#r'r5r?r=r4r4(sZ�������B�B�H�I� �6�&�$��,�-� .�.�.�.�������.�.�.� -�-�-�-�-�-����;�;�;�zT�T�T� !�!�!�F 8� 8� 8�%6�%6�%6�NbN�bN�bN�H D�D�D�:h$�h$�h$�h$�VM/�M/�M/�^B�B�B�B�Br?r4c�J�t|g|�Ri|���d���S)a� Compute the summation of f with respect to symbols. Explanation =========== The notation for symbols is similar to the notation used in Integral. summation(f, (i, a, b)) computes the sum of f with respect to i from a to b, i.e., :: b ____ \ ` summation(f, (i, a, b)) = ) f /___, i = a If it cannot compute the sum, it returns an unevaluated Sum object. Repeated sums can be computed by introducing additional symbols tuples:: Examples ======== >>> from sympy import summation, oo, symbols, log >>> i, n, m = symbols('i n m', integer=True) >>> summation(2*i - 1, (i, 1, n)) n**2 >>> summation(1/2**i, (i, 0, oo)) 2 >>> summation(1/log(n)**n, (n, 2, oo)) Sum(log(n)**(-n), (n, 2, oo)) >>> summation(i, (i, 0, n), (n, 0, m)) m**3/6 + m**2/2 + m/3 >>> from sympy.abc import x >>> from sympy import factorial >>> summation(x**n/factorial(n), (n, 0, oo)) exp(x) See Also ======== Sum Product, sympy.concrete.products.product F)r`)r4rm)r{rr�s r=� summationr0vs4��d �q� %�7� %� %� %�f� %� %� *� *�� *� 6� 6�6r?c�d������|\���t�����fd�t|��D���S)a� Returns the direct summation of the terms of a telescopic sum Explanation =========== L is the term with lower index R is the term with higher index n difference between the indexes of L and R Examples ======== >>> from sympy.concrete.summations import telescopic_direct >>> from sympy.abc import k, a, b >>> telescopic_direct(1/k, -1/(k+2), 2, (k, a, b)) -1/(b + 2) - 1/(b + 1) + 1/(a + 1) + 1/a c�v��g|]5}����|z������|z ��z��6Sr5r )r;r��L�Rr�r�rhs �����r=rjz%telescopic_direct.<locals>.<listcomp>�sB���G�G�G������1�q�5�!�!�A�F�F�1�a�!�e�$4�$4�4�G�G�Gr?)r r�)r3r4r�r6r�r�rhs`` @@@r=�telescopic_directr5�sF�������(�I�Q��1� �G�G�G�G�G�G�G�G�e�A�h�h�G�G�G� H�Hr?c���� �|\� }}�js�jrdStd��}� ���� � |z����}d}|r5||vr1||}|jr ��� � |z���zdksd}|�}t d��} ddlm} | ��� � |z���z|��pg}n#t$rYdSwxYw��� fd�|D��}t|��dkrdS|d}|dkr"t��t|��� ||f��S|dkrt��|� ||f��SdS)zi Tries to perform the summation using the telescopic property. Return None if not possible. Nrbrr���solvec���g|]>}|j� ����|z���z���j�<|��?Sr5)r r�r rJ)r;�sir3r4rhs ���r=rjztelescopic.<locals>.<listcomp>�s\���9�9�9�b�2�=�9����q�!�b�&�!�!�A�%�-�-�/�/�7�9�r�9�9�9r?r) �is_Addrr�r�r r�sympy.solvers.solversr8r�r:r5r�) r3r4r6r�r�rb�solr�r�r8rhs `` @r=� telescopicr>�s������ �I�Q��1��x��1�8���t� �S� � �A� �2�*�*�Q�V�V�A�q�1�u�%�%� &� &�C� �A� ��q�C�x�x� ��F��� � �����1�q�5�!1�!1�A�!5��!:�!:��A�  �y� �#�J�J�� � 3� 3� 3� 3� 3� 3��%����q�!�a�%�(�(�1�,�a�0�0�6�B�C�C��"� � � ��4�4� ����9�9�9�9�9�9�C�9�9�9�� �s�8�8�q�=�=��4� ��F���1�u�u� ��A�s�1�v�v��1�a�y�9�9�9� �Q��� ��A�q�1�a��)�4�4�4� �s�!.C� C�Cc�D� �|\� }}|jr tjS� |jvr |||z dzzS||kr|�� |��St |t ��rlt� fd�|jD����sLg}|jD]8}t|j |��}|�dS|� ||j f���9|j |�S|�t��r>ddlm}m}|�d�d���}|||d��r |||��S||z } | j} | r| dkrt+|� ||f��St |t ��rdSt-|���� ||f��} | �| S| rt+|� ||f��SdS)Nrc3�@�K�|]}�|jdjvV��dS)rN)r�r�)r;�argrhs �r=r>zeval_sum.<locals>.<genexpr>�s1�����C�C�S�1���� �0�0�C�C�C�C�C�Cr?)�deltasummation�_has_simple_deltac�,�t|t��Sr�)rpr4�r�s r=�<lambda>zeval_sum.<locals>.<lambda> s��j��C�(�(�r?c�*�|���Sr�)r*rEs r=rFzeval_sum.<locals>.<lambda> s��a�h�h�j�j�r?r�d)rJrrur�r�rprrBr�rwr�r�r�ryr�r!�deltarBrC�replacer �eval_sum_direct�eval_sum_symbolicr ) r{r6r�r��newargsrA�newexprrBrCr��definite�valuerhs @r=rwrw�s ����I�Q��1��y���v� �������!�a�%�!�)�}���A�v�v��v�v�a��|�|���!�Y��� $��C�C�C�C�A�F�C�C�C�C�C� $��G��v� 4� 4��"�3�8�V�4�4���?��4�4�������2�3�3�3�3��1�6�7�#� #��u�u�^���-�<�<�<�<�<�<�<�<� �I�I� (� (� � � � �� � �Q��q� � *� *� -�!�>�!�V�,�,� ,� �a�%�C��~�H��-�S�3�Y�Y��q�1�a��)�,�,�,��!�Y�����t� �a�h�h�j�j�1�a��)� 4� 4�E� ��� ��-��q�1�a��)�,�,�,�-�-r?c�����|\��}|�z }�jr������\}}|dkr+t|��|f��}|r||z}|tjur|Snu����\}} |����st| ��|f��} | r|| zS| ����st|��|f��} | r| | zS t�����n#t$rYnwxYw�j r������\}}|dkr1t|��|f��}|r||dzz|z}|tjur|SnX����\}} t|��|f��} t| ��|f��} d| | fvr| | z}|tjur|St���fd�t|dz��D���S)z� Evaluate expression directly, but perform some simple checks first to possibly result in a smaller expression and faster execution. rrNc�B��g|]}����|z����Sr5r )r;�jr�r�rhs ���r=rjz#eval_sum_direct.<locals>.<listcomp>]s+���=�=�=�����1�a�!�e�$�$�=�=�=r?) r��as_independentrKrr� as_two_termsr�r%r&r;r r�)r�r6r�r�� without_i�with_ir�r�r3r4�sR�sL�lsum�rsumr�rhs` @@r=rKrK s?����� �I�Q��1� �a�%�C� �{� � �/�/��2�2�� �6� ��>�>����A�q� �2�2�A�� ��a�K���A�E�>�>��H���$�$�&�&�D�A�q��5�5��8�8� �$�Q��A�q� �2�2��� ��R�4�K��5�5��8�8� �$�Q��A�q� �2�2��� ��a�4�K�  ��T�1�~�~���� � � � � �� ���� �{�� �/�/��2�2�� �6� ��>�>����A�q� �2�2�A�� ��s�Q�w�'�!�+���A�E�>�>��H���$�$�&�&�D�A�q�"�1�q�!�Q�i�0�0�D�"�1�q�!�Q�i�0�0�D��D�$�<�'�'��4�K���A�E�>�>��H� �=�=�=�=�=�=�e�C�!�G�n�n�=�=�=� >�>s�C&�& C3�2C3c� �|}|\}}}|�|��s |||z dzzS|jr�|�|��\}}|dkr+t||||f��}|r||z} | tjur| Snu|���\} } | �|��st| |||f��} | r| | zS| �|��st| |||f��} | r| | zS t||��}n#t$rYnwxYw|j r�|���\} } t| | |||f��}|r|S|�|��\}}|dkr4t||||f��}|r|||z dzz|z} | tjur| SnAt| |||f��}t| |||f��}d||fvr||z} | tjur| Std��}|� ||z��}|��||}|j r�|dkr�|tjur|tjus|tjur|tjur tjSt!|dz|dz��t!|dz|��z |dzz ���S|j rl|dkrf|dkr"t%|��t%|dz ��z St%|t'|����t%|dz t'|����z S|�tjtj���s�|�tjtj���s�td|g���}td|g���}td|g���}td ��}|���� ||z��}|��|�|������ ||z|z��}|��|�|��||z�|��}||z�|��}|||z||dzzz zd|z z } |||z dzz}t1|t3|tj��f| d f��St7||||f��} t9| t:t<f���r dd lm }dd l!m"}| j#tI|dd��j#z }|tK| ����}||j#z} g}!tM| ��D]�}" |||"��}|rt3|"|d��n tj'}#|#d krD|!�(tS|j|#j*�|���+��|#f��n#tX$rY��wxYw|!�(| d f��t1|!�S| dtjfvr| St[||||f��}$|$�|$St]||||f��} | �| S|�/��}%|%|krt|%|||f��SdS)Nrrr�rk�c1r��c2�c3�wexpT)�denomr7F)0r�r�rTrLrrrUr%r&r;r>rr�r r�r�rr rr�r�r��updater�rr�Oner rprr �sympy.simplify.radsimprar<r8r�rr+rr�r�r4r�rmr��eval_sum_hyper�eval_sum_residuer*)&r{r6�f_origrhr�r�rVrWr�r�r3r4rXrY�lrsumrZr[r�r�r]r^r_r`r"�e_expr�r�r<rar8� non_limit�den�den_symr�rcr��h�factoreds& r=rLrL`s��� �F��I�Q��1� �5�5��8�8���!�a�%�!�)�}�� �x� ��,�,�Q�/�/�� �6� ��>�>�!�&�1�a��)�4�4�A�� ��a�K���A�E�>�>��H���>�>�#�#�D�A�q��5�5��8�8� �&�q�1�a��)�4�4��� ��R�4�K��5�5��8�8� �&�q�1�a��)�4�4��� ��a�4�K�  � �!�Q�K�K���� � � � � �� ���� �x���~�~�����1��1�a�!�Q���+�+�� � ��L��,�,�Q�/�/�� �6� ��>�>�!�&�1�a��)�4�4�A�� ��q�1�u�q�y�)�A�-���A�E�>�>��H��%�Q��A�q� �2�2�D�$�Q��A�q� �2�2�D��D�$�<�'�'��4�K���A�E�>�>��H� �S� � �A� �W�W�Q��T�]�]�F� �� �1�I�� �<� I��A�v�v����O�O���1C�(C�(C���+�+�+����0C�0C��:�%�"�1�q�5�!�a�%�0�0�9�Q��U�A�3F�3F�F��Q��O�W�W�Y�Y�Y��� I�!�q�&�&���7�7�#�A�;�;��!�a�%���8�8�#�A�s�1�v�v�.�.��!�a�%��Q���1H�1H�H�H� �E�E�!�*�a�0� 1� 1�.� �E�E�!�*�a�0� 1� 1�.��$��� $� $� $�� �$��� $� $� $�� �$��� $� $� $���F�|�|�� �I�I�K�K� � �b�D�j� )� )�� �=��E�E�$�K�K�&�&�(�(�.�.�r�!�t�b�y�9�9�E�� ����������V�M�M�!�$�$����V�M�M�!�$�$���q�!�t�a�!�a�%�j�(�)�1�q�5�1���q�1�u�q�y�M�� �!�R��1�5�\�\�!2�Q��I�>�>�>� �q�1�a��)� $� $�� �a�#�c�� #� #� $� 4� 4� 4� 4� 4� 4� 3� 3� 3� 3� 3� 3�����q�r�r� �);�)H�H�I��%��� � �$�$�C��#�"2�2�G��D��W�%�%� � �����c�1� � �A�'(�5��1�a��d� � � �a�g�A��E�z�z�� � �S����a�f�)=�v�%F�%F�%K�%K�%M�%M�q�$Q�R�R�R��E��*�����H����� �K�K��D� � "� "� "��d�#� #� �T�1�5�M� !� !��H��v��1�a�y�)�)�A��}�����!�Q���+�+�A��}����}�}���H��6��� ��A�q�!�9�5�5�5��s%�/D� D � D �=A:V9�9 W�Wc ���|dkr(t|�|||z��|d��S|�|d��dkroddlm}||�|t ddd�������dkrt jdfSt|�||dz��|d��Sddlm}|||��}|�dSt|t��rdd lm }||��}dd l m }dd l m}dd lm} | t#|����\} } | �|��\} } | �|��\}}| |g}| |g}ggg}t'd ��D]�}||D]�}d}|jr|j}|j}|jsdSt1||��}|���dkrdS|���\}}||xx||zzcc<||xx||z g|zz cc<����|ddgz}|d}|d|dz }t7|||��}||��}|�|d��||��z|jfS)z) Returns (res, cond). Sums from a to oo. rr�rhTr�r)� hypersimpN)� nsimplifyr�)� hyperexpand)�fractionr�)�_eval_sum_hyperr�r�r�rrrurprprrqr�r��sympy.simplify.hyperexpandrrrdrsr*� as_coeff_mulr��is_Powr�baser r)�degree� all_coeffsr �convergence_statement)r{rhr�r�rp�hsrqr�rrrs�numerra�top�topl�bot�botl�ab�factors�paramsrb�fac�mulr�r�r��ap�bqr�rms r=rtrt�s����A�v�v��q�v�v�a��Q��/�/��A�6�6�6��v�v�a��|�|�q���4�4�4�4�4�4� �8�A�F�F�1�e�C���E�E�E�F�F� G� G�1� L� L��6�4�<� ��q�v�v�a��Q��/�/��A�6�6�6�1�1�1�1�1�1� ��1�a���B� �z��t��"�e����5�5�5�5�5�5� �Y�r�]�]��0�0�0�0�0�0�6�6�6�6�6�6�/�/�/�/�/�/��8�F�2�J�J�'�'�L�E�5��"�"�1�%�%�I�C���"�"�1�%�%�I�C�� �s��B��T�l�G��"�X�F� �1�X�X� #� #���1�:� #� #�C��C��z� ��g���h���~� ��4�4�4��S�!� � �A��x�x�z�z�Q����t�t�t��<�<�>�>�D�A�q� �q�E�E�E�Q��V�O�E�E�E� �1�I�I�I�!�A�#��s�� "�I�I�I�I� #� ���a�S��B� ���B� �1��b��e� �A� �b�"�a���A���� � �A� �6�6�!�Q�<�<� � �A��� &��(?� ?�?r?c���|\}}}|�|��durdS||z jrdSt||||f��}|tjkr�|tjur<t |�|| ��|| ��}|�t||df��Sn�d�}||��}t |||��} | �|| ��|krdSt |||dz��} | �|| ��|krdS| | c\} } \} } t| | ��} | dkrdSt| | z | f|df��S|tjur�t |�|| ��|d��} t ||d��} | �| �dS| \} } | \} } t| | ��} | dks"| � ��tj krdSt| | z| f|df��St |||��}|�||\}}|dkr_|j rV|�|tddd���|z��}|js|jr tjS|jr tjSdSt||df��SdS)NFTc�D��t�fd�tD����S)Nc3�B�K�|]}��|��V��dSr�)�count)r;r�r�s �r=r>z3eval_sum_hyper.<locals>.<lambda>.<locals>.<genexpr>9s-�����%C�%C�Q�a�g�g�a�j�j�%C�%C�%C�%C�%C�%Cr?)�sumrrEs`r=rFz eval_sum_hyper.<locals>.<lambda>9s$���#�%C�%C�%C�%C�(�%C�%C�%C�"C�"C�r?rrrhr�)�is_hypergeometricr r4rr�r�rtr�rr$r��EmptySetr�rrVrJrZ)r{�i_a_brhr�r��old_sum�res� n_illegal�had�res1�res2�cond1�cond2r�r��cs r=rere's����G�A�q�!����1����&�&��� �A�����t��!�a��A�Y���G��A�J��� ��"� "� "�!�!�&�&��Q�B�-�-��Q�B�7�7�C��� ��w��o�6�6�6��D�C�I��)�A�,�,�C�"�1�a��+�+�D��|�y�y�����4�4���"�1�a��Q��/�/�D��|�y�y�����4�4���+/�� (�M�T�5�=�D�%��u�e�$�$�D��u�}�}��t��d�T�k�4�0�7�D�/�B�B� B��A� ����q�v�v�a�!��}�}�a��3�3���q�!�Q�'�'�� �<�4�<��4�� ��e�� ��e��5�%� � �� �5�=�=�D�K�K�M�M�Q�Z�7�7��4��$��+�t�,�w��o�>�>�>� �!�Q�� "� "�C� �����1� ��:�:��{� .��F�F�1�e�C���E�E�E��I�J�J���=�.�A�I�.��:�%��]�.��-�-��4���w��o�.�.�.��r?c�8�����|\�}}d�}d�}d�}�fd�}td�����fd�}�j�hz rdS|js|tjtjfvsdS|js|tjtjfvsdS|tjkr|tjkrdS|����} | rd} | \} } n(|�tj�zz ���} | rd } | \} } ndS| ����| ����z d krdS||ftjtjfkrQ|| ��} | �dS| \}}|rdS|| | | �����fd �|D��}tj t|��zS|j r|tjusdS|| | ��s�|| ��}|jsdS|d krdS| � |��} | � |��} || | ��sdS| rJtj�ztj|z| � ��z| � ��z z�n)| � ��| � ��z �t��||z ||z f��S|| ��} | �dS| \}}|rZd �|D��}t|��}t|��}t!|��||z dzksdS|t|��krdS|| | | �����fd�||zD��}tj t|��z}|s�|���d i��zd z }��fd�t%t'|��d ��D��}��fd�t%d t'|����D��}|t|��zt|��z }|S|d z }��fd�t%t|��dzt'|����D��}|t|��z }|S)a�Compute the infinite summation with residues Notes ===== If $f(n), g(n)$ are polynomials with $\deg(g(n)) - \deg(f(n)) \ge 2$, some infinite summations can be computed by the following residue evaluations. .. math:: \sum_{n=-\infty, g(n) \ne 0}^{\infty} \frac{f(n)}{g(n)} = -\pi \sum_{\alpha|g(\alpha)=0} \text{Res}(\cot(\pi x) \frac{f(x)}{g(x)}, \alpha) .. math:: \sum_{n=-\infty, g(n) \ne 0}^{\infty} (-1)^n \frac{f(n)}{g(n)} = -\pi \sum_{\alpha|g(\alpha)=0} \text{Res}(\csc(\pi x) \frac{f(x)}{g(x)}, \alpha) Examples ======== >>> from sympy import Sum, oo, Symbol >>> x = Symbol('x') Doubly infinite series of rational functions. >>> Sum(1 / (x**2 + 1), (x, -oo, oo)).doit() pi/tanh(pi) Doubly infinite alternating series of rational functions. >>> Sum((-1)**x / (x**2 + 1), (x, -oo, oo)).doit() pi/sinh(pi) Infinite series of even rational functions. >>> Sum(1 / (x**2 + 1), (x, 0, oo)).doit() 1/2 + pi/(2*tanh(pi)) Infinite series of alternating even rational functions. >>> Sum((-1)**x / (x**2 + 1), (x, 0, oo)).doit() pi/(2*sinh(pi)) + 1/2 This also have heuristics to transform arbitrarily shifted summand or arbitrarily shifted summation range to the canonical problem the formula can handle. >>> Sum(1 / (x**2 + 2*x + 2), (x, -1, oo)).doit() 1/2 + pi/(2*tanh(pi)) >>> Sum(1 / (x**2 + 4*x + 5), (x, -2, oo)).doit() 1/2 + pi/(2*tanh(pi)) >>> Sum(1 / (x**2 + 1), (x, 1, oo)).doit() -1/2 + pi/(2*tanh(pi)) >>> Sum(1 / (x**2 + 1), (x, 2, oo)).doit() -1 + pi/(2*tanh(pi)) References ========== .. [#] http://www.supermath.info/InfiniteSeriesandtheResidueTheorem.pdf .. [#] Asmar N.H., Grafakos L. (2018) Residue Theory. In: Complex Analysis with Applications. Undergraduate Texts in Mathematics. Springer, Cham. https://doi.org/10.1007/978-3-319-94063-2_5 c�j�td�|���D����}td�|���D����}td�|���D����}td�|���D����}|r|p|o|S)z1Test if the rational function is an even functionc3�,K�|]\}|dzdkV��dS�r�rNr5�r;rhs r=r>z=eval_sum_residue.<locals>.is_even_function.<locals>.<genexpr>��,����?�?����Q��!��?�?�?�?�?�?r?c3�,K�|]\}|dzdkV��dSr�r5r�s r=r>z=eval_sum_residue.<locals>.is_even_function.<locals>.<genexpr>�r�r?c3�,K�|]\}|dzdkV��dS�r�rNr5r�s r=r>z=eval_sum_residue.<locals>.is_even_function.<locals>.<genexpr>��,����>�>�t���A��� �>�>�>�>�>�>r?c3�,K�|]\}|dzdkV��dSr�r5r�s r=r>z=eval_sum_residue.<locals>.is_even_function.<locals>.<genexpr>�r�r?)�all�monoms)r}ra� numer_even� denom_even� numer_odd� denom_odds r=�is_even_functionz*eval_sum_residue.<locals>.is_even_function�s����?�?�� � ���?�?�?�?�?� ��?�?�� � ���?�?�?�?�?� ��>�>�u�|�|�~�~�>�>�>�>�>� ��>�>�u�|�|�~�~�>�>�>�>�>� ��)�z�G�y�/F�Y�Gr?c��|���\}} t||f|��\\}}}n#ttf$rYdSwxYw||fSr�)�as_numer_denomr(r'r&)r{rhr}ra�opts r=�match_rationalz(eval_sum_residue.<locals>.match_rational�sk���'�'�)�)� ��u� �"9�5�%�.�!�"L�"L� �N�U�E�C�C��"�O�4� � � ��4�4� �����e�|�s�2�A�Ac��|������}t|d���}d|vrdS|d|d}}||fS)Nc��|jSr�)rvrEs r=rFz5eval_sum_residue.<locals>.get_poles.<locals>.<lambda>�s��a�l�r?TF)�sqf_part� all_rootsr2)ra�roots� int_roots� nonint_rootss r=� get_polesz#eval_sum_residue.<locals>.get_poles�sZ����� � �*�*�,�,���U�2�2�3�3�� �5�=�=��4�"'��+�u�U�|�<� ��,�&�&r?c���|����}|��|z��}|��|dz z��}| |z |z }|S)Nr)ry�coeff_monomial)rar�r�r��shiftrhs �r=� get_shiftz#eval_sum_residue.<locals>.get_shift�sX��� �L�L��O�O�� � � ��A�� &� &�� � � ��Q�q�S�� *� *����a��!� ��� r?r�c���|���|���z �����}|s |ttj�z��z}n|t tj�z��z}|Sr�)�as_exprr�rr�Pir)r}ra� alternating�residue_factorrhr�s ��r=�get_residue_factorz,eval_sum_residue.<locals>.get_residue_factor�si����-�-�/�/�E�M�M�O�O�;�A�A�!�Q�G�G��� ,� �c�!�$��(�m�m� +�N�N� �c�!�$��(�m�m� +�N��r?NFTr�c�2��g|]}t��|����Sr5r.�r;�rootr�r�s ��r=rjz$eval_sum_residue.<locals>.<listcomp>�s%���N�N�N��G�N�A�t�4�4�N�N�Nr?rc�,�g|]}t|����Sr5)r )r;r�s r=rjz$eval_sum_residue.<locals>.<listcomp>s��5�5�5�4�S��Y�Y�5�5�5r?rc�2��g|]}t��|����Sr5r.r�s ��r=rjz$eval_sum_residue.<locals>.<listcomp>*s%���V�V�V�T����4�0�0�V�V�Vr?c�>��g|]}���|i����Sr5rf�r;�i0r{rhs ��r=rjz$eval_sum_residue.<locals>.<listcomp>3�)���I�I�I�"�!�*�*�a��W�-�-�I�I�Ir?c�>��g|]}���|i����Sr5rfr�s ��r=rjz$eval_sum_residue.<locals>.<listcomp>4r�r?c�>��g|]}���|i����Sr5rfr�s ��r=rjz$eval_sum_residue.<locals>.<listcomp>=s)���R�R�R�"�!�*�*�a��W�%�%�R�R�Rr?)rr�r rr�r�r�ryr�r�r]r�r�rfr�r�r:rgr�r )r{r�r�r�r�r�r�r�r�r�r�r}ra�polesr�r��residuesr�� int_roots_max� int_roots_min�full_sum�half_sum�extraneous_neg�extraneous_posr�� extraneousrhr�r�s` @@@r=rfrfcs������J�G�A�q�!�H�H�H����'�'�'������ �c� � �A������� �~������t� �L��A�!�*�a�.@�!A�A�A��t� �L��A�!�*�a�.@�!A�A�A��t� �A� ���1�� �?�?��t� �N�1�a� � �E� � �� �� ��u�u���q�1�=�!�#3�3�Q�7�7�� � ��K� �L�E�5�5��4� �|�|�A������a���(�1�,�,��t� �1�v�!�$�a�j�1�1�1�� �%� � �� �=��4�"'�� �<� � ��4�+�+�E�5�+�F�F��N�N�N�N�N��N�N�N����u�s�8�}�}�$�$� �K��A���O�O��t� � �E�5� )� )�:�� �%� � ���� ��4� �A�:�:��4�� � �E�"�"��� � �E�"�"�����u�-�-� ��4� � 2�� �q� �A�M�5�$8�5�=�=�?�?�$J�U�]�]�_�_�$\�]�A�A�� � ���%�-�-�/�/�1�A���A�q��w��%��#8�9�9�9� �I�e� � �E� �}��t�#��I�|�� �5�5�9�5�5�5� ��I��� ��I��� ��9�~�~���!>��!B�B�B��4� ��I��� � ��4�'�'��u�k�B�B�N�V�V�V�V�V�Y��=U�V�V�V�H���u�s�8�}�}�$�H� � ��q�z�z�1�a�&�1�1�1�Q�6��J�I�I�I�I��c�!�f�f�a�8H�8H�I�I�I��I�I�I�I�I��a��Q���8H�8H�I�I�I���C��/�/�/�#�n�2E�2E�E��� ��!�|�H�S�R�R�R�R��c�)�n�n�q�6H�#�a�&�&�0Q�0Q�R�R�R�J� ��J��� '�F� �Mr?c���|j}|jD]V}|\}}}||z }|jrB|dkdkr |dz|dz }}| }t||||f��}|�|���cS�WdS)NrTr)rEr6r rKrm)� expressionr{r�rhr�r�r�r�s r=rsrsCs�����A��"� #� #�����1�a��!�e�� �>� #��a��D� � ��1�u�a�!�e�1���B��"�1�q�!�Q�i�0�0�D����y�y�{�{�"�"�"�� #� #r?c�� �|\}}}||g}gd�}i}i}|D]E� |j�� d��}|rd|� <�%t� fd�|D����rd|� <�F|r!|�|��t di|��SdS)z� Return a Dummy symbol that inherits as many assumptions as possible from the provided symbol and limits. If the symbol already has all True assumption shared by the limits then return None. )�extended_nonnegative� nonnegative�extended_nonpositive� nonpositive�extended_positiver��extended_negative�negativer��rational�finite�zero�real� extended_realNTc3�>�K�|]}t|d�z��V��dS)�is_N)�getattr)r;rh�assums �r=r>z<_dummy_with_inherited_properties_concrete.<locals>.<genexpr>js1�����6�6�q���E�E�M�*�*�6�6�6�6�6�6r?r~)r~)� _assumptionsrlr�rbr) r6r�r�r�r<�assumptions_to_consider�assumptions_to_keep�assumptions_to_add� assum_truer�s @r=rnrnRs�����G�A�q�!� �A��A�@�@�@������(�-�-���^�'�'��t�4�4� � � -�)-� �� &� &� �6�6�6�6�A�6�6�6� 6� 6� -�(,� �u� %���1��"�"�#5�6�6�6��0�0�/�0�0�0�1�1r?)_�typingrr-�sympy.calculus.singularitiesr�!sympy.calculus.accumulationboundsr�expr_with_intlimitsr�expr_with_limitsr�gosperr �sympy.core.exprr �sympy.core.addr �sympy.core.containers�sympy.core.functionr r �sympy.core.mulr�sympy.core.numbersrr�sympy.core.relationalr�sympy.core.singletonr�sympy.core.sortingr�sympy.core.symbolrrrr�(sympy.functions.combinatorial.factorialsr�%sympy.functions.combinatorial.numbersrr�&sympy.functions.elementary.exponentialrr�$sympy.functions.elementary.piecewiser�(sympy.functions.elementary.trigonometricrr�sympy.functions.special.hyperr �(sympy.functions.special.tensor_functionsr!�&sympy.functions.special.zeta_functionsr"�sympy.integrals.integralsr#�sympy.logic.boolalgr$�sympy.polys.partfracr%�sympy.polys.polyerrorsr&r'�sympy.polys.polytoolsr(r)r*�sympy.polys.rationaltoolsr+�sympy.series.limitseqr,�sympy.series.orderr-�sympy.series.residuesr/�sympy.sets.setsr0r1�sympy.utilities.iterablesr2r�r4r0r5r>rwrKrLrtrerfrsrnr5r?r=�<module>rs���"�"�"�"�"�"�6�6�6�6�6�6�@�@�@�@�@�@�2�2�2�2�2�2�+�+�+�+�+�+������� � � � � � �������'�'�'�'�'�'�2�2�2�2�2�2�2�2�������.�.�.�.�.�.�.�.�$�$�$�$�$�$�"�"�"�"�"�"�&�&�&�&�&�&�:�:�:�:�:�:�:�:�:�:�:�:�>�>�>�>�>�>�E�E�E�E�E�E�E�E�;�;�;�;�;�;�;�;�:�:�:�:�:�:�=�=�=�=�=�=�=�=�/�/�/�/�/�/�C�C�C�C�C�C�7�7�7�7�7�7�.�.�.�.�.�.�#�#�#�#�#�#�&�&�&�&�&�&�F�F�F�F�F�F�F�F�G�G�G�G�G�G�G�G�G�G�.�.�.�.�.�.�+�+�+�+�+�+� � � � � � �)�)�)�)�)�)�/�/�/�/�/�/�/�/�*�*�*�*�*�*�����K B�K B�K B�K B�K B�-�*�K B�K B�K B�\27�27�27�jI�I�I�0+5�+5�+5�\,-�,-�,-�^=?�=?�=?�@N6�N6�N6�b3@�3@�3@�l9/�9/�9/�x]�]�]�@ #� #� #�1�1�1�1�1r?
Memory