� ��g�H���ddlmZmZmZmZmZddlmZmZddl m Z ddl m Z ddl mZmZmZddlmZmZddlmZmZd�Zd �ZGd �d e��Zd S) �)�Function�S�Mul�Pow�Add)�ordered�default_sort_key)� expand_func)�Dummy)�gamma�sqrt�sin)�factor�cancel)�sift�uniqc�>�|�t��}|�t��}d�|D��}|s|S||z}||����t��z}|r�t d�t |��D���\}}}|�tt ||������}t|d����tt ||������St|d���S)a  Simplify expressions with gamma functions. Explanation =========== This function takes as input an expression containing gamma functions or functions that can be rewritten in terms of gamma functions and tries to minimize the number of those functions and reduce the size of their arguments. The algorithm works by rewriting all gamma functions as expressions involving rising factorials (Pochhammer symbols) and applies recurrence relations and other transformations applicable to rising factorials, to reduce their arguments, possibly letting the resulting rising factorial to cancel. Rising factorials with the second argument being an integer are expanded into polynomial forms and finally all other rising factorial are rewritten in terms of gamma functions. Then the following two steps are performed. 1. Reduce the number of gammas by applying the reflection theorem gamma(x)*gamma(1-x) == pi/sin(pi*x). 2. Reduce the number of gammas by applying the multiplication theorem gamma(x)*gamma(x+1/n)*...*gamma(x+(n-1)/n) == C*gamma(n*x). It then reduces the number of prefactors by absorbing them into gammas where possible and expands gammas with rational argument. All transformation rules can be found (or were derived from) here: .. [1] https://functions.wolfram.com/GammaBetaErf/Pochhammer/17/01/02/ .. [2] https://functions.wolfram.com/GammaBetaErf/Pochhammer/27/01/0005/ Examples ======== >>> from sympy.simplify import gammasimp >>> from sympy import gamma, Symbol >>> from sympy.abc import x >>> n = Symbol('n', integer = True) >>> gammasimp(gamma(x)/gamma(x - 3)) (x - 3)*(x - 2)*(x - 1) >>> gammasimp(gamma(n + 3)) gamma(n + 3) c�<�h|]}t|t���|��S�)� isinstancer )�.0�is �h/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/simplify/gammasimp.py� <setcomp>zgammasimp.<locals>.<setcomp>Cs'�� 3� 3� 3�A�j��E�2�2� 3�a� 3� 3� 3�c�^�g|]*}t��||jd�|jD���f��+S)c�0�g|]}t|d�����S)F��as_comb)� _gammasimp)r�as r� <listcomp>z(gammasimp.<locals>.<listcomp>.<listcomp>Ks2��$?�$?�$?�12� �1�e�,�,�,�$?�$?�$?r)r �func�args)r�fis rr"zgammasimp.<locals>.<listcomp>Js`��"�"�"���W�W�b�'�"�'�$?�$?�68�g�$?�$?�$?�@� A�"�"�"rFr) �rewriter �atomsr�as_dummy�zipr�xreplace�dictr )�expr�f�gammas�dum�fun�simp�ds r� gammasimpr3 s��d �<�<�� � �D� � � �8���A� 3� 3�� 3� 3� 3�F� ��� ���K�A� �D�M�M�O�O� !� !�(� +� +�+�A��K��"�"��a�j�j�"�"�"�#���S�$� �M�M�$�s�3��}�}�-�-� .� .���!�U�+�+�+�4�4�T�#�c�4�.�.�5I�5I�J�J�J� �d�E� *� *� *�*rc�\���|�td���}�r|�td���}n|�td���}d��fd� �t|��}�|��}||krt|��}|�td���}|S)a; Helper function for gammasimp and combsimp. Explanation =========== Simplifies expressions written in terms of gamma function. If as_comb is True, it tries to preserve integer arguments. See docstring of gammasimp for more information. This was part of combsimp() in combsimp.py. c�L�td|dz �����S�N�)�_rf�expand��ns r�<lambda>z_gammasimp.<locals>.<lambda>as��#�a�!�a�%���)�)�*�*�rc�&�t|dz��Sr6�r �r!�bs rr<z_gammasimp.<locals>.<lambda>es���q�1�u���rc�F�t||z��t|��z S�Nr>r?s rr<z_gammasimp.<locals>.<lambda>hs���q�1�u���e�A�h�h�.�rrc�"���(�)�*�+�,�-�|jr|Sd��,�+fd��+�dkr!|j��/fd�|jD���}�dz �|js|S�dkrX|���\}}|s|S|r6�/t j|���dz��t j|��zS�dz ��dk�r?t|j�+d���\}}t |�}t |�}|���\}�*td��D]�} ttt j |������}t|��D]_\} } | jrSt�*�,��/fd �| jD������\} �*| || <�*�t"��sn�`t |�}| dkr �+|��sn�*|c}�*��||z�*z }|jr�+�*��s �+|��s|S�dz ��d kr |} �/|d ��}|| kr|S�g} g}g}g}d �}tt|j����}|r�|������\}}||��\}}|r| �|��n|d ur|�|��||��\}}|r|�|��n|d ur|�|��|���.�s�| ||f|||ffD�]K\}}}g}|�r8|���}|jr|�|���4t|��D]�\} }||zdz }|js�|�t.j��|�t3t.j|z����|�| ��|dkr.t|��D]}|�d|z |z���n2|dkr,t| ��D]}|�| |z ���n|�|��|��8||dd�<��M| |||f|| ||ffD�]+\}}}} |D]}!|D]}"|!d|"zz }|jrn��n�n|�|!��|�|"��|dkr.t|��D]}|�d|"z|z���n7|dkr1t| ��D] }| �d|"zdz |z ���!|�|"t.jz��|�dd|"zdz z��| �t9t.j������"��-d��(�(fd�}#| ||f|||ffD]\}}}|#|||����dk�r3�)�-fd�}$i�-�-fd��)�)�-fd�}%| |z|z|zD] }|%|���| ||f|||ffD]�\}}}g}|r�|���}&d}'|'r�d }'|$||&��}"|"�H|�|"��|"|&kr&|�|"|&z ��|%|"|&z ��|&dz }&d}'|$||&dz ��}"|"�Q|�|"��|"|&dz kr,|�|&dz |"z ��|%|&dz |"z ��|&dz}&d}'|'��|�|&��|��||dd�<��t d�| D���t d�|D���z t |�zt |�z S)z/ Simplify products of gamma functions further. c��|�t��}|�td���}|�t��|kr|}|S)Nc��td|dz ������td���S)Nr7c�F�t||z��t|��z SrBr>r?s rr<zU_gammasimp.<locals>.rule_gamma.<locals>.gamma_rat.<locals>.<lambda>.<locals>.<lambda>ts��E�!�a�%�L�L��q���,A�r)r8r9�replacer:s rr<zC_gammasimp.<locals>.rule_gamma.<locals>.gamma_rat.<locals>.<lambda>ss8��C��A��E�>�>�3C�3C�-�-��'�#�A�A�B�B�r)�countr rG)�x�was�xxs r� gamma_ratz1_gammasimp.<locals>.rule_gamma.<locals>.gamma_ratpsS���'�'�%�.�.�C����5�#C�#C�D�D�B��x�x�����$�$����Hrc����t|t��rdS|js|jr t �fd�|jD����S|jr(|jjs |j j r�|j ��SdS)NTc3�.�K�|]}�|��V��dSrBr)r�xi� gamma_factors �r� <genexpr>zG_gammasimp.<locals>.rule_gamma.<locals>.gamma_factor.<locals>.<genexpr>~s-�����=�=��<�<��+�+�=�=�=�=�=�=rF) rr �is_Add�is_Mul�anyr$�is_Pow�exp� is_integer�base� is_positive)rIrPs �rrPz4_gammasimp.<locals>.rule_gamma.<locals>.gamma_factorys�����!�U�#�#� ��t��x� >�1�8� >��=�=�=�=�a�f�=�=�=�=�=�=��x� ,�Q�U�-� ,���1C� ,�#�|�A�F�+�+�+��5rrc�.��g|]}�|�dz����S�r7r)rrI�level� rule_gammas ��rr"z2_gammasimp.<locals>.rule_gamma.<locals>.<listcomp>�s)���K�K�K�A�z�z�!�U�Q�Y�7�7�K�K�Krr7�T)�binaryc�F��g|]}��|�z ���dz����Sr[r)rr!�ddrLr\r]s ����rr"z2_gammasimp.<locals>.rule_gamma.<locals>.<listcomp>�sH���'U�'U�'U�GH�J�J�y�y��2������ �B�B�'U�'U�'Ur��c���|tjurdgfS|���\}}|jr0t |t ��rd|jdg|zfSd|g|zfSd|gfS)NTrF)r�One� as_base_exp� is_Integerrr r$)�pr@�es r� explicatez1_gammasimp.<locals>.rule_gamma.<locals>.explicate�sv���A�E�z�z��R�x���=�=�?�?�D�A�q��|� "��a��'�'�(��!�&��)��Q��.�.� �1�#�a�%�<�'��q�c�z�!rFNc�� � �tt|����� tt� ����D�]� � � fd�t� dzt� ����D��}|D]�\}}|jdkr�|jdkr�|j}� g}ttd|����}|D]C\}}||z}|jr2||vr.|�|��|�|��|sn�D��t|��D]'\� }� |} |�| ��| |� <�(|j|d|dd�fccS�ݐ�dS)Nc�<��g|]}�|��z dz|f��Sr[r)r�jr�us ��rr"z@_gammasimp.<locals>.rule_gamma.<locals>._run.<locals>.<listcomp>+s/���P�P�P�a�Q�q�T�A�a�D�[�A�-�q�1�P�P�Prr7r) �listr�range�lenrh�qrg�remove�append� enumerate) �coeffs�dj�onermr;�got�getr2�m�crrns @@r�_runz,_gammasimp.<locals>.rule_gamma.<locals>._run&s�������f���&�&���s�1�v�v���:�:�A�P�P�P�P�P�5��Q���A���;O�;O�P�P�P�B�"$�:�:���Q��5�A�:�:�#�%�1�*�*� #��A�#$�#�C�"&�u�Q��{�{�"3�"3�C�(*�)�)���1�$%�a�C��#$�<�!.�A��H�H�$'�J�J�q�M�M�M�$'�J�J�q�M�M�M�+.�%.�(-��� (�(1�#���+�+���1�$%�a�D�� &� � �a� 0� 0� 0�)*��A���#&�5�#�a�&�#�a�b�b�'�#9�9�9�9�9�9��%:�:�:rc�����i}|D]B}|���\}�|��g���|���Ct|t���}|D]�t|���}g} �|��} | �n�| \} } } | D]D} �| zdz }t t | | z ����D]}|�||z ����E| �| zz}|�dtjzt| dz ��dz z| tj |z zz��|�|���Ĉfd�|D��|z|�<��g}|D] �||�z }�||dd�<dS)N)�keyTr7r^c���g|]}�|z��Srr)rr|�resids �rr"zE_gammasimp.<locals>.rule_gamma.<locals>._mult_thm.<locals>.<listcomp>ks���"=�"=�"=��5�1�9�"=�"=�"=r) � as_coeff_Add� setdefaultrt�sortedr rp�intr�Pi�Half)r.�numer�denom�rats�gr|�keysrv�new�runr;�ui�otherrn�con�kr�r}s @�r� _mult_thmz1_gammasimp.<locals>.rule_gamma.<locals>._mult_thm@s����� ���9�9�A� �~�~�/�/�H�A�u��O�O�E�2�.�.�5�5�a�8�8�8�8��d�(8�9�9�9��!�D�D�E�#�D��K�0�0�F��C�(�"�d�6�l�l���;�!�(+� ��2�u�"'�6�6�A�"'�!�)�a�-�C�%*�3�q�2�v�;�;�%7�%7�6�6�� %� � �S�1�W� 5� 5� 5� 5�6� ����n��� � �a���f��!�a�%���� �%;�%&���#��%6�&7�8�8�8�� � �3����3(�8#>�"=�"=�"=�f�"=�"=�"=��"C�D��K�K���!�%�%�E���e��$�A�A���q�q�q� � � rc���|sdS� |��\}}|D]�}� |\}}||ks9|�|��s%|t��ks|t��kr�Mtt||z ��j��}t|j��}t|j��} |dkr|dks| dkr|cS��dS)Nr)� intersection�setrqr� free_symbols) �lrI�S1�T1�y�S2�T2r!r@r|� compute_ST�invs ��r� find_fuzzyz2_gammasimp.<locals>.rule_gamma.<locals>.find_fuzzys�������F�#��A�����B�� !� !�A� ��V�F�B���R�x�x�����(;�(;�x�%'�3�5�5�[�[�B�#�%�%�K�K� ��F�1�Q�3�K�K�4�5�5�A��A�N�+�+�A��A�N�+�+�A��A�v�v�1�q�5�5�A��E�E� ����� !� !rc����|�vr�|S|j|�t���d�|�t��D����fS)Nc��h|] }|j�� Sr)rV)rris rrzE_gammasimp.<locals>.rule_gamma.<locals>.compute_ST.<locals>.<setcomp>�s��8�8�8�1���8�8�8r)r�r'r�unionr)r,r�s �rr�z2_gammasimp.<locals>.rule_gamma.<locals>.compute_ST�s_����3�;�;��t�9�$��)�4�:�:�h�+?�+?�+E�+E�8�8�� � �3���8�8�8�,:�,:�;�;rc�$���|���|<dSrBr)r,r�r�s ��r� update_STz1_gammasimp.<locals>.rule_gamma.<locals>.update_ST�s���&�J�t�,�,��D� � � rc�,�g|]}t|����Srr>�rr�s rr"z2_gammasimp.<locals>.rule_gamma.<locals>.<listcomp>�s��4�4�4�!�U�1�X�X�4�4�4rc�,�g|]}t|����Srr>r�s rr"z2_gammasimp.<locals>.rule_gamma.<locals>.<listcomp>�s��3�3�3��E�!�H�H�3�3�3r)�is_Atomr#r$rS�args_cncr� _from_argsr�as_numer_denomrpror� make_argsrurRr�hasr �pop�extendrWrtrgrr�rrsr�r )0r,r\r$�nc�T�F� gamma_indr2�nd�ipassr�nirJ� numer_gammas� denom_gammas� numer_others� denom_othersrj�newargsr;�isgr�r.r�r�r��g1�g2r��ng�dg�no�dorIr�r�r�r�r��contr}r�rarPrLr�rr]s0 ` @@@@@@��rr]z_gammasimp.<locals>.rule_gammajs� ���������� �<� ��K� � � � � � � � � �A�:�:��4�9�K�K�K�K�K���K�K�K�L�D� �Q�J�E��{� ��K� �A�:�:��}�}���H�D�"�� �� �� V�!�z�#�.��"6�"6��� �B�B�3�>�RT�CU�CU�U�U� �Q�J�E� �A�:�:��� �<��=�=�=�D�A�q��Q��I��Q��A��%�%�'�'�F�B���q��� � ���G�C�M�"�$5�$5�6�6�7�7��&�t�_�_�"�"�E�A�r��y�"�!$�'U�'U�'U�'U�'U�'U�'U�LN�G�'U�'U�'U�"�,�n�.�.���B�#%��Q��!�v�v�e�}�}�"�!�E���$�Z���Q�;�;�|�|�B�'7�'7�;��E��R���B�B��R�<��?�D��K� �\�\�"�%5�%5� ���b�9I�9I� �� � �Q�J�E� �A�:�:� ���!�z�$��*�*���3�;�;��K�  � � �� �� �� � "� "� "��w�t�y�)�)�*�*��� '��;�;�=�=�/�/�1�1�D�A�q��Y�q�\�\�F�C��� '��#�#�A�&�&�&�&������#�#�A�&�&�&��Y�q�\�\�F�C��� '��#�#�A�&�&�&�&������#�#�A�&�&�&�� '��` +��l�L�*:�!�<��>�)@� � �$���u����'������B��}�!�� � �2���� �!*�6�!2�!2�'�'���2���G�a�K�� �|�%�$�� � �Q�T�*�*�*�� � �S���b��\�\�2�2�2�� � �1� � � ��q�5�5�%*�1�X�X�9�9�� %� � �Q��V�a�Z� 8� 8� 8� 8�9���U�U�%*�A�2�Y�Y�6�6�� %� � �b�S�1�W� 5� 5� 5� 5���� � �2����)�'�, ��q�q�q� � �%1�,� �$0�$2�$0�,� �$0�$2�#3� *� *���B��B� *�� � ��!#�%�%�A� !�A�a�C��A� �|�&� %��&�%�����I�I�a�L�L�L��I�I�a�L�L�L��1�u�u�!&�q���/�/�A��I�I�a��c�A�g�.�.�.�.�/��Q���!&��r���3�3�A��I�I�a��c�A�g��k�2�2�2�2��I�I�a�!�&�j�)�)�)��I�I�a�!�A�#��'�l�+�+�+��I�I�d�1�4�j�j�)�)�)�+*��: :� :� :�42 �2 �2 �2 �2 �h&2�<��$N�%1�<��$N�$P� +� +���5�%�� �!�U�E�*�*�*�*� �A�:�:�  !� !� !� !� !� !�0�C� ;� ;� ;� ;� ;�  -� -� -� -� -� -�$�|�3�l�B�\�Q� � ��� �$������l�L�*:�!�<��>�)@� � �$���u����"�� � � � �A��D��(�$��&�J�u�a�0�0���=�!�L�L��O�O�O� �A�v�v� %� � �Q�q�S� 1� 1� 1� )� �!�A�#������F�A�#'�D�&�J�u�a�!�e�4�4���=�!�L�L��O�O�O� �A��E�z�z� %� � �a�!�e�Q�Y� 7� 7� 7� )� �1�q�5�!�)� 4� 4� 4���F�A�#'�D�#�(�$�J�J�q�M�M�M�+�"�. ��q�q�q� � ��4�4�|�4�4�4�5��3�3�l�3�3�3�4�5��<� �!�#&� �#5�6� 6rc�f�|jrtt|����nt|��SrB)� is_Rationalr r r:s rr<z_gammasimp.<locals>.<lambda>�s&��1�=�F�+�e�A�h�h�'�'�'�e�A�h�h�r)r)rGr r8r)r,rrJr]s ` @rr r Ts����� �<�<��*�*� ,� ,�D��0��|�|�C� %� %�'�'����|�|�C� .� .�0�0��[6�[6�[6�[6�[6�[6�[6�z ��,�,�C� �:�c�?�?�D� �s�{�{��d�|�|�� �<�<��F�F� H� H�D� �Krc�$�eZdZed���ZdS)r8c �L��|jrw|s tjSt|��}|dkr"t �fd�t |��D���S|dkr*dt �fd�t d| dz��D���z SdS|jrv|���\}}|jrX|dkr$t�|��t�|z|��zS|dkr(t�|��t�|z|z| ��z S�jr�����\}}|jr�|dkr5t||��t||z|��zt||��z S|dkrAt||��t||z| ��zt||z|z| ��z SdSdSdS)Nrc���g|]}�|z��Srr�rrr!s �rr"z_rf.eval.<locals>.<listcomp>�s���5�5�5�q�Q��U�5�5�5rr7c���g|]}�|z ��Srrr�s �rr"z_rf.eval.<locals>.<listcomp>�s���?�?�?��q�1�u�?�?�?r) rgrrer�rrprRr�r8)�clsr!r@r;r|�_b�_as ` r�evalz_rf.eval�s���� �<� N�� ��u� ��A���A��1�u�u��5�5�5�5�E�!�H�H�5�5�5�6�6��Q�����?�?�?�?�e�A��r�A�v�.>�.>�?�?�?�@�@�@����x� >����(�(���2��<�>��1�u�u�"�1�b�z�z�#�a�"�f�a�.�.�8�8��Q���"�1�b�z�z�#�a�"�f�q�j�1�"�*=�*=�=�=��x� N����(�(���2��<�N��1�u�u�"�2�q�z�z�#�b�1�f�a�.�.�8��R����C�C��Q���"�2�q�z�z�#�b�1�f�q�b�/�/�9�#�b�1�f�q�j�1�"�:M�:M�M�M� N� N�N�N��rN)�__name__� __module__� __qualname__� classmethodr�rrrr8r8�s2�������N�N��[�N�N�Nrr8N)� sympy.corerrrrr�sympy.core.sortingrr �sympy.core.functionr �sympy.core.symbolr �sympy.functionsr r r� sympy.polysrr�sympy.utilities.iterablesrrr3r r8rrr�<module>r�s��1�1�1�1�1�1�1�1�1�1�1�1�1�1�8�8�8�8�8�8�8�8�+�+�+�+�+�+�#�#�#�#�#�#�,�,�,�,�,�,�,�,�,�,�&�&�&�&�&�&�&�&�0�0�0�0�0�0�0�0�G+�G+�G+�T|�|�|�~ N�N�N�N�N�(�N�N�N�N�Nr
Memory