� ��gf�����ddlmZddlmZddlmZddlmZmZm Z ddl m Z ddl m Z ddlmZmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&ddl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7m8Z8ddl9m:Z:d�Z;d�Z<d�Z=d@d�Z>d�Z?d�Z@d�ZAdAd�ZBdAd �ZCd!�ZDdBd#�ZEd$�ZFdBd%�ZGd&�ZHdCd(�ZId)�ZJdBd*�ZKd+�ZLd,�ZMd-�ZNdBd.�ZOdAd/�ZPdAd0�ZQd1�ZRdAd2�ZSd3�ZTd4�ZUe:rBeVeWe8e;e<e=e?e@eBeCeDeEeFeGeIeKeMe>eNeOePeQeLeReSf����\Z;Z<Z=Z?Z@ZBZCZDZEZFZGZIZKZMZ>ZNZOZPZQZLZRZSeBe;feCe;fe7gZXeIeBe;feCe;fe;gfZYeNeEe;feNeEeHe;fe7gZZe@eHfe7gZ[e@e?e@eKe@eMe@e;fZ\e@e?eGe@e?eIfeBeDeIe@feZeXeFeYe@eFeFe[fe7gZ]d5�fd6�Z^dDd7�Z_d8�`��ZaebeVeceaeVeWed��jeea����������Zfd9�Zgd'ahd@d:�Zid;�Zjd<�Zkd=�Zld>�Zmd?�Znd'S)E�)� defaultdict��Add)�Expr)�Factors� gcd_terms� factor_terms�� expand_mul)�Mul)�pi�I)�Pow)�S)�ordered��Dummy)�sympify�� bottom_up)�binomial)�cosh�sinh�tanh�coth�sech�csch�HyperbolicFunction)�cos�sin�tan�cot�sec�csc�sqrt�TrigonometricFunction)� perfect_power)�factor)�greedy)�identity�debug)� SYMPY_DEBUGc�r�|���������S)zSimplification of rational polynomials, trying to simplify the expression, e.g. combine things like 3*x + 2*x, etc.... )�normalr(�expand��rvs �a/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/simplify/fu.py�TR0r3s*�� �9�9�;�;� � � � � &� &� (� (�(�c�(�d�}t||��S)z�Replace sec, csc with 1/cos, 1/sin Examples ======== >>> from sympy.simplify.fu import TR1, sec, csc >>> from sympy.abc import x >>> TR1(2*csc(x) + sec(x)) 1/cos(x) + 2/sin(x) c���t|t��r)|jd}tjt |��z St|t ��r)|jd}tjt|��z S|S�Nr)� isinstancer#�argsr�Onerr$r �r1�as r2�fzTR1.<locals>.f4se�� �b�#� � � ���� �A��5��Q���<� � ��C� � � ���� �A��5��Q���<� �� r4r�r1r=s r2�TR1r?(s#����� �R�� � �r4c�(�d�}t||��S)a@Replace tan and cot with sin/cos and cos/sin Examples ======== >>> from sympy.simplify.fu import TR2 >>> from sympy.abc import x >>> from sympy import tan, cot, sin, cos >>> TR2(tan(x)) sin(x)/cos(x) >>> TR2(cot(x)) cos(x)/sin(x) >>> TR2(tan(tan(x) - sin(x)/cos(x))) 0 c� �t|t��r,|jd}t|��t |��z St|t ��r,|jd}t |��t|��z S|Sr7)r8r!r9r rr"r;s r2r=zTR2.<locals>.fRsm�� �b�#� � � !���� �A��q�6�6�#�a�&�&�=� � ��C� � � !���� �A��q�6�6�#�a�&�&�=� �� r4rr>s r2�TR2rB@s#��$��� �R�� � �r4Fc�.���fd�}t||��S)a�Converts ratios involving sin and cos as follows:: sin(x)/cos(x) -> tan(x) sin(x)/(cos(x) + 1) -> tan(x/2) if half=True Examples ======== >>> from sympy.simplify.fu import TR2i >>> from sympy.abc import x, a >>> from sympy import sin, cos >>> TR2i(sin(x)/cos(x)) tan(x) Powers of the numerator and denominator are also recognized >>> TR2i(sin(x)**2/(cos(x) + 1)**2, half=True) tan(x/2)**2 The transformation does not take place unless assumptions allow (i.e. the base must be positive or the exponent must be an integer for both numerator and denominator) >>> TR2i(sin(x)**a/(cos(x) + 1)**a) sin(x)**a/(cos(x) + 1)**a c�f��� � �|js|S|���\� �� js�jr|S� fd�� � ���� � � fd�t � �����D��}� s|S������� fd�t ������D��}�s|S� � fd�}|� |��|�|��g}� D�]M}t |t��r�t|j dd���}|�vrS�|� |krA|� t|j d��� |z��dx� |<�|<��� r^d|z}|�vrU�|� |krC|� t|j dd z ��� |z��dx� |<�|<��t |t��rut|j dd���}|�vrS�|� |krA|� t|j d��� | z��dx� |<�|<��v� r�|j r�|j dtjur�t |j dt��r�t|j dj dd���}|�vrj�|� |krX�|js|jrD|� t|j dd z ��� | z��dx� |<�|<��O|rxt#|d �� ���D��z�t#d �����D���z }|t#d �|D���t#d �|D���z z}|S)Nc����|js|joS|jttfvp>�o<|jo5t |j��dkotd�|jD����S)N�c3�lK�|]/}td�tj|��D����V��0dS)c3�jK�|].}t|t��p|jo |jtuV��/dS�N)r8r�is_Pow�base)�.0�ais r2� <genexpr>z8TR2i.<locals>.f.<locals>.ok.<locals>.<genexpr>.<genexpr>�sR����,�,��#�2�s�+�+�K�r�y�/K�R�W��^�,�,�,�,�,�,r4N)�anyr � make_args�rLr<s r2rNz.TR2i.<locals>.f.<locals>.ok.<locals>.<genexpr>�sf����=�=�01��,�,��-��*�*�,�,�,�,�,�=�=�=�=�=�=r4) � is_integer� is_positive�funcr r�is_Add�lenr9rO)�k�e�halfs �r2�okzTR2i.<locals>.f.<locals>.ok�s������.���?���3��*�$�>��*=���*=��A�F� � �q� �*=��=�=�56�V�=�=�=�=�=�  @r4c�b��g|]+}�|�|���|��|��f��,S���pop)rLrW�nrZs ��r2� <listcomp>z#TR2i.<locals>.f.<locals>.<listcomp>��:���J�J�J�1�b�b��A�a�D�k�k�J�!�Q�U�U�1�X�X��J�J�Jr4c�b��g|]+}�|�|���|��|��f��,Sr\r])rLrW�drZs ��r2r`z#TR2i.<locals>.f.<locals>.<listcomp>�rar4c����g}|D]^}|jrUt|j��dkr=�rt|��nt |��}||kr|�||f���_|r}t |��D]\}\}}||=|||<�t|����}|D]<}||||z}�||��r|||<�%|�||f���=~dSdS�N�) rUrVr9r(r �append� enumerater �as_powers_dict) rc�ddone�newkrW�knew�i�vrYrZs ��r2� factorizez"TR2i.<locals>.f.<locals>.factorize�s����D�� /� /���8�/��A�F� � �a���(,�A�6�!�9�9�9�,�q�/�/�D��q�y�y�� � �Q��I�.�.�.��� �$-�d�O�O�#�#�L�A�y��4��!��"�D��G�G��D�z�0�0�2�2���-�-�A��!��t�A�w��A��r�!�Q�x�x�-� ��!���� � �a��V�,�,�,�,��D�D� � r4rF��evaluaterfrFc�"�g|] \}}|�||z�� Sr\r\�rL�brXs r2r`z#TR2i.<locals>.f.<locals>.<listcomp>�s%��<�<�<�T�Q��!�<�A�q�D�<�<�<r4c�"�g|] \}}|�||z�� Sr\r\rss r2r`z#TR2i.<locals>.f.<locals>.<listcomp>�s%��6�6�6�t�q�!�A�6�a��d�6�6�6r4c��g|] \}}||z�� Sr\r\rss r2r`z#TR2i.<locals>.f.<locals>.<listcomp>�s ��/�/�/���A��1��/�/�/r4c��g|] \}}||z�� Sr\r\rss r2r`z#TR2i.<locals>.f.<locals>.<listcomp>�s ��6N�6N�6N���1�q�!�t�6N�6N�6Nr4)�is_Mul�as_numer_denom�is_Atomri�list�keysr8r rr9rgr!rUrr:rRrSr �items) r1�ndonerjro�trWr<�a1rcr_rZrYs @@@�r2r=zTR2i.<locals>.fzs �������y� ��I�� � �"�"���1� �9� �� � ��I� @� @� @� @� @� � � � � ��J�J�J�J�J��Q�V�V�X�X���J�J�J��� ��I� � � � � ��J�J�J�J�J��Q�V�V�X�X���J�J�J��� ��I� � � � � � �& � �!�U����� �!�U���� ��� '� '�A��!�S�!�!� '����q� �E�2�2�2����6�6�a��d�a��d�l�l��H�H�S�����^�^�Q�q�T�1�2�2�2�"&�&�A�a�D�1�Q�4�4��,��Q��B��Q�w�w�1�R�5�A�a�D�=�=����#�a�f�Q�i��k�"2�"2�Q�q�T�!9�:�:�:�'+�+��!��q��u���A�s�#�#� '����q� �E�2�2�2����6�6�a��d�a��d�l�l��H�H�S�����^�^�a��d�U�2�3�3�3�"&�&�A�a�D�1�Q�4��� '�!�(� '�q�v�a�y�A�E�'9�'9��q�v�a�y�#�.�.�(:����q� ��q�)�E�:�:�:����6�6�a��d�a��d�l�l��!���l�� �+��H�H�S�����1��-�-��!��u�4�5�5�5�"&�&�A�a�D�1�Q�4�� � P��q�<�<�Q�W�W�Y�Y�<�<�<�<�>��6�6�q�w�w�y�y�6�6�6�7�8�B� �#�/�/��/�/�/�0��6N�6N��6N�6N�6N�1O�O� O�B�� r4r)r1rYr=s ` r2�TR2ir�^s4���8S�S�S�S�S�j �R�� � �r4c�j��ddlm��fd�}|�d�d���}t||��S)aRInduced formula: example sin(-a) = -sin(a) Examples ======== >>> from sympy.simplify.fu import TR3 >>> from sympy.abc import x, y >>> from sympy import pi >>> from sympy import cos >>> TR3(cos(y - x*(y - x))) cos(x*(x - y) + y) >>> cos(pi/2 + x) -sin(x) >>> cos(30*pi/2 + x) -cos(x) r��signsimpc �T��t|t��s|S|��|jd����}t|t��s|S|jdtjdz z jtjdz |jdz jcxurdur�nn�tttttttttttti}|t|��tjdz |jdz ��}|S)Nr�rFT)r8r&rTr9r�PirSrr r!r"r#r$�type)r1�fmapr�s �r2r=zTR3.<locals>.f�s�����"�3�4�4� ��I� �W�W�X�X�b�g�a�j�)�)� *� *���"�3�4�4� ��I� �G�A�J���a�� � ,���a��"�'�!�*�1D�0Q� Y� Y� Y� Y�UY� Y� Y� Y� Y� Y���c�3��S�#�s�C��c�3�O�D���d�2�h�h����Q������ 3�4�4�B�� r4c�,�t|t��SrI)r8r&��xs r2�<lambda>zTR3.<locals>.<lambda>�s��*�Q� 5�6�6�r4c�2�|�d�d���S)Nc��|jo|jSrI)� is_numberrx�r_s r2r�z'TR3.<locals>.<lambda>.<locals>.<lambda>�s��a�k�.�a�h�r4c� �|j|j�SrI�rTr9r�s r2r�z'TR3.<locals>.<lambda>.<locals>.<lambda>�s��f�a�f�a�f�o�r4��replacer�s r2r�zTR3.<locals>.<lambda>�s ��!�)�)� .� .� %� %�'�'�r4)�sympy.simplify.simplifyr�r�r)r1r=r�s @r2�TR3r��sg���$1�0�0�0�0�0� � � � � � ���6�6� '� '� (� (�B� �R�� � �r4c�2�|�d�d���S)a�Identify values of special angles. a= 0 pi/6 pi/4 pi/3 pi/2 ---------------------------------------------------- sin(a) 0 1/2 sqrt(2)/2 sqrt(3)/2 1 cos(a) 1 sqrt(3)/2 sqrt(2)/2 1/2 0 tan(a) 0 sqt(3)/3 1 sqrt(3) -- Examples ======== >>> from sympy import pi >>> from sympy import cos, sin, tan, cot >>> for s in (0, pi/6, pi/4, pi/3, pi/2): ... print('%s %s %s %s' % (cos(s), sin(s), tan(s), cot(s))) ... 1 0 0 zoo sqrt(3)/2 1/2 sqrt(3)/3 sqrt(3) sqrt(2)/2 sqrt(2)/2 1 1 1/2 sqrt(3)/2 sqrt(3) sqrt(3)/3 0 1 zoo 0 c�v�t|t��o$|jdtz x}jo|jdvS)Nr)rfrF�r��)r8r&r9r � is_Rational�q)r��rs r2r�zTR4.<locals>.<lambda>s?�� �q�/� 0� 0� E���q� �"� �_�Q� )� E�./�c�_�.D�r4c�r�|�|jdj|jdj���Sr7r�r�s r2r�zTR4.<locals>.<lambda>s-�� �F�F�>�1�6�!�9�>�1�6�!�9�>�2� 3� 3�r4r�r0s r2�TR4r�s0��0 �:�:� E� E� 4� 4�  5� 5�5r4c�>�����������fd�}t||��S)a+Helper for TR5 and TR6 to replace f**2 with h(g**2) Options ======= max : controls size of exponent that can appear on f e.g. if max=4 then f**4 will be changed to h(g**2)**2. pow : controls whether the exponent must be a perfect power of 2 e.g. if pow=True (and max >= 6) then f**6 will not be changed but f**8 will be changed to h(g**2)**4 >>> from sympy.simplify.fu import _TR56 as T >>> from sympy.abc import x >>> from sympy import sin, cos >>> h = lambda x: 1 - x >>> T(sin(x)**3, sin, cos, h, 4, False) (1 - cos(x)**2)*sin(x) >>> T(sin(x)**6, sin, cos, h, 6, False) (1 - cos(x)**2)**3 >>> T(sin(x)**6, sin, cos, h, 6, True) sin(x)**6 >>> T(sin(x)**8, sin, cos, h, 10, True) (1 - cos(x)**2)**4 c���|jr|jj�ks|S|jjs|S|jdkdkr|S|j�kdkr|S|jdkr|S|jdkr'��|jjd��dz��S|jdzdkrP|jdz}�|jjd����|jjd��dz��|zzS|jdkrd}n;�s|jdzr|S|jdz}n"t |j��}|s|S|jdz}��|jjd��dz��|zS)NrTrfrFr�)rJrKrT�exp�is_realr9r')r1rX�pr=�g�h�max�pows �����r2�_fz_TR56.<locals>._f=s���� � � �b�g�l�a�/�/��I��v�~� ��I� �F�Q�J�4� � ��I� �F�S�L�T� !� !��I� �6�Q�;�;��I� �6�Q�;�;��1�Q�Q�r�w�|�A��'�'��*�+�+� +��v��z�Q����F�A�I���q����a��)�)�!�!�A�A�b�g�l�1�o�,>�,>��,A�*B�*B�A�*E�E�E���1������� ��6�A�:���I��F�A�I���!�"�&�)�)�����I��F�A�I���1�Q�Q�r�w�|�A��'�'��*�+�+�Q�.� .r4r)r1r=r�r�r�r�r�s ````` r2�_TR56r�#sG�������4!/�!/�!/�!/�!/�!/�!/�!/�!/�F �R�� � �r4r�c�B�t|ttd�||���S)a�Replacement of sin**2 with 1 - cos(x)**2. See _TR56 docstring for advanced use of ``max`` and ``pow``. Examples ======== >>> from sympy.simplify.fu import TR5 >>> from sympy.abc import x >>> from sympy import sin >>> TR5(sin(x)**2) 1 - cos(x)**2 >>> TR5(sin(x)**-2) # unchanged sin(x)**(-2) >>> TR5(sin(x)**4) (1 - cos(x)**2)**2 c� �d|z Srer\r�s r2r�zTR5.<locals>.<lambda>u� ���Q��r4�r�r�)r�r r�r1r�r�s r2�TR5r�c�!��$ ��S�#���C�S� A� A� A�Ar4c�B�t|ttd�||���S)a�Replacement of cos**2 with 1 - sin(x)**2. See _TR56 docstring for advanced use of ``max`` and ``pow``. Examples ======== >>> from sympy.simplify.fu import TR6 >>> from sympy.abc import x >>> from sympy import cos >>> TR6(cos(x)**2) 1 - sin(x)**2 >>> TR6(cos(x)**-2) #unchanged cos(x)**(-2) >>> TR6(cos(x)**4) (1 - sin(x)**2)**2 c� �d|z Srer\r�s r2r�zTR6.<locals>.<lambda>�r�r4r�)r�rr r�s r2�TR6r�xr�r4c�(�d�}t||��S)aLowering the degree of cos(x)**2. Examples ======== >>> from sympy.simplify.fu import TR7 >>> from sympy.abc import x >>> from sympy import cos >>> TR7(cos(x)**2) cos(2*x)/2 + 1/2 >>> TR7(cos(x)**2 + 1) cos(2*x)/2 + 3/2 c��|jr |jjtkr |jdks|Sdtd|jjdz��zdz S)NrFrfr)rJrKrTrr�r9r0s r2r=zTR7.<locals>.f�sN��� � �b�g�l�c�1�1�b�f��k�k��I��C��"�'�,�q�/�)�*�*�*�A�-�-r4rr>s r2�TR7r��s#�� .�.�.� �R�� � �r4Tc�.���fd�}t||��S)aqConverting products of ``cos`` and/or ``sin`` to a sum or difference of ``cos`` and or ``sin`` terms. Examples ======== >>> from sympy.simplify.fu import TR8 >>> from sympy import cos, sin >>> TR8(cos(2)*cos(3)) cos(5)/2 + cos(1)/2 >>> TR8(cos(2)*sin(3)) sin(5)/2 + sin(1)/2 >>> TR8(sin(2)*sin(3)) -cos(5)/2 + cos(1)/2 c�D ��|js;|jr2|jjtt fvr|jjs|jjs|S� r�d�|� ��D��\}}t|d���}t|d���}||ks||krpt||z ��}|jrW|j dj rEt|j ��dkr-|j djrt!|����}|Stgt gdgi}t!j|��D]�}|jtt fvr4|t'|���|j d���K|jrx|jjrl|jdkra|jjtt fvrG|t'|j���|jj dg|jz����|d�|����|t}|t }|r|s(t|��dkst|��dks|S|d}t/t|��t|����}t1|��D]e} |���} |���} |�t | | z��t | | z ��zdz ���ft|��dkrv|���} |���} |�t | | z��t | | z ��zdz ��t|��dk�v|r4|�t |�������t|��dkrw|���} |���} |�t | | z�� t | | z ��zdz ��t|��dk�w|r4|�t |�������tt5t!|�����S)Nc�,�g|]}t|����Sr\r �rLrms r2r`z"TR8.<locals>.f.<locals>.<listcomp>�s��?�?�?�a�J�q�M�M�?�?�?r4F��firstrrFrf)rxrJrKrTrr r�rRrSry�TR8rr9r�rVrUr � as_coeff_MulrPr�rg� is_Integer�extend�min�ranger^r ) r1r_rc�newn�newdr9r<�c�srmr��a2r�s �r2r=zTR8.<locals>.f�s��� �I� � �I� � �G�L�S�#�J� &� &� �V� � '�"$�'�"5� '��I� � �?�?�2�+<�+<�+>�+>�?�?�?�D�A�q��q��&�&�&�D��q��&�&�&�D��q�y�y�D�A�I�I��t�D�y�)�)���9�1�����!7�1��B�G� � ��)�)�b�g�a�j�.?�)��b�o�o�/�/�0�B��I��R��b�$��+����r�"�"� %� %�A��v�#�s��#�#��T�!�W�W� �$�$�Q�V�A�Y�/�/�/�/��(� %�q�u�/� %�A�E�A�I�I��F�K�C��:�-�-��T�!�&�\�\�"�)�)�1�6�;�q�>�*:�1�5�*@�A�A�A�A��T� �!�!�!�$�$�$�$� ��I�� ��I��� �a� �3�q�6�6�A�:�:��Q���!����I��D�z�� ��A����A��� � ���q��� 9� 9�A������B������B� �K�K��R�"�W����B��G� � �4�a�7� 8� 8� 8� 8��!�f�f�q�j�j������B������B� �K�K��R�"�W����B��G� � �4�a�7� 8� 8� 8��!�f�f�q�j�j� � &� �K�K��A�E�E�G�G� � � %� %� %��!�f�f�q�j�j������B������B� �K�K�#�b�2�g�,�,���R�"�W���5�q�8� 9� 9� 9��!�f�f�q�j�j� � &� �K�K��A�E�E�G�G� � � %� %� %��:�c�4�j�)�)�*�*�*r4r�r1r�r=s ` r2r�r��s/���"5+�5+�5+�5+�5+�n �R�� � �r4c�(�d�}t||��S)acSum of ``cos`` or ``sin`` terms as a product of ``cos`` or ``sin``. Examples ======== >>> from sympy.simplify.fu import TR9 >>> from sympy import cos, sin >>> TR9(cos(1) + cos(2)) 2*cos(1/2)*cos(3/2) >>> TR9(cos(1) + 2*sin(1) + 2*sin(2)) cos(1) + 4*sin(3/2)*cos(1/2) If no change is made by TR9, no re-arrangement of the expression will be made. For example, though factoring of common term is attempted, if the factored expression was not changed, the original expression will be returned: >>> TR9(cos(3) + cos(3)*cos(2)) cos(3) + cos(2)*cos(3) c�B��|js|Sd�fd� �t|���S)NTc����|js|Stt|j����}t |��dkr�d}t t |����D]_}||}|�� t |dzt |����D]1}||}|�� ||z}�|��} | |kr| ||<d||<d}n�2�`|r%t d�|D���}|jr �|��}|St|�} | s|S| \} } } }}}|ru| | kr4| | zdzt||zdz ��zt||z dz ��zS| dkr||}}d| zt||zdz ��zt||z dz ��zS| | kr4| | zdzt||zdz ��zt||z dz ��zS| dkr||}}d| zt||zdz ��zt||z dz ��zS)NrFFrfTc��g|]}|�|��Sr\r\�rLr�s r2r`z.TR9.<locals>.f.<locals>.do.<locals>.<listcomp>/���7�7�7�b�B�7�r�7�7�7r4r������ rUr{rr9rVr�r� trig_splitrr )r1r�r9�hitrmrM�j�aj�was�new�split�gcd�n1�n2r<rt�iscos�dos �r2r�zTR9.<locals>.f.<locals>.do sT����9� �� �����(�(�)�)�D��4�y�y�A�~�~����s�4�y�y�)�)�"�"�A��a��B��z� �"�1�q�5�#�d�)�)�4�4� "� "��!�!�W���:�$� �2�g�� �b��g�g���#�:�:�&)�D��G�&*�D��G�"&�C�!�E� &�� �$��7�7�D�7�7�7�8�B��y�$��R��V�V��� ���%�E�� �� �',� $�C��R��A�u�� ;���8�8��r�6�!�8�C��Q��� �N�N�2�3��A��q�y�>�>�A�A���6�6��a�q�A��#�v�c�1�q�5�!�)�n�n�,�S�!�a�%���^�^�;�;���8�8��r�6�!�8�C��Q��� �N�N�2�3��A��q�y�>�>�A�A���6�6��a�q�A���u�S�!�a�%���^�^�+�C��Q��� �N�N�:�:r4�T)rU�process_common_addends)r1r�s @r2r=zTR9.<locals>.fsC����y� ��I�< ;�< ;�< ;�< ;�< ;�< ;�|&�b�"�-�-�-r4rr>s r2�TR9r��s'��.B.�B.�B.�H �R�� � �r4c�.���fd�}t||��S)a�Separate sums in ``cos`` and ``sin``. Examples ======== >>> from sympy.simplify.fu import TR10 >>> from sympy.abc import a, b, c >>> from sympy import cos, sin >>> TR10(cos(a + b)) -sin(a)*sin(b) + cos(a)*cos(b) >>> TR10(sin(a + b)) sin(a)*cos(b) + sin(b)*cos(a) >>> TR10(sin(a + b + c)) (-sin(a)*sin(b) + cos(a)*cos(b))*sin(c) + (sin(a)*cos(b) + sin(b)*cos(a))*cos(c) c����|jttfvr|S|j}|jd}|j�r��r"t t |j����}nt |j��}|���}tj |��}|jr�|tkr]t|��tt|��d���zt|��tt|��d���zzSt|��tt|��d���zt|��tt|��d���zz S|tkr?t|��t|��zt|��t|��zzSt|��t|��zt|��t|��zz S|S)NrFr�) rTrr r9rUr{rr^r� _from_args�TR10)r1r=�argr9r<rtr�s �r2r=zTR10.<locals>.f`s���� �7�3��*� $� $��I� �G���g�a�j�� �:� 9�� &��G�C�H�-�-�.�.����C�H�~�~����� � �A���t�$�$�A��x� 9���8�8��q�6�6�$�s�1�v�v�U�";�";�";�;��A���t�C��F�F�%�8�8�8�8�9�9��q�6�6�$�s�1�v�v�U�";�";�";�;��A���t�C��F�F�%�8�8�8�8�9�9���8�8��q�6�6�#�a�&�&�=�3�q�6�6�#�a�&�&�=�8�8��q�6�6�#�a�&�&�=�3�q�6�6�#�a�&�&�=�8�8�� r4rr�s ` r2r�r�Ns.���$�����6 �R�� � �r4c�R�t�t��d�}t||��S)a�Sum of products to function of sum. Examples ======== >>> from sympy.simplify.fu import TR10i >>> from sympy import cos, sin, sqrt >>> from sympy.abc import x >>> TR10i(cos(1)*cos(3) + sin(1)*sin(3)) cos(2) >>> TR10i(cos(1)*sin(3) + sin(1)*cos(3) + cos(3)) cos(3) + sin(4) >>> TR10i(sqrt(2)*cos(x)*x + sqrt(6)*sin(x)*x) 2*sqrt(2)*x*sin(x + pi/6) Nc ��� �|js|Sd� fd� � t|� d���}|j�r�tt��}|jD]�}d}|jrO|jD]G}|jr>|jtj ur+|j j r||� |��d}n�H|s%|tj � |����g}|D]�}t|ztfD]�}||vr�t!t#||����D]�}|||��t!t#||����D]y}|||��t%||||||z��} � | ��} | | kr-|� | ��d|||<d|||<n�z���׌�|r)t%|d�|���D��z�}n � |��}n|j���|S)NTc����|js|Stt|j����}t |��dkr�d}t t |����D]_}||}|�� t |dzt |����D]1}||}|�� ||z}�|��} | |kr| ||<d||<d}n�2�`|r%t d�|D���}|jr �|��}|St|ddi�} | s|S| \} } } }}}|r5| | z} | | kr| t||z ��zS| t||z��zS| | z} | | kr| t||z��zS| t||z ��zS)NrFFrfTc��g|]}|�|��Sr\r\r�s r2r`z0TR10i.<locals>.f.<locals>.do.<locals>.<listcomp>�r�r4�twor�)r1r�r9r�rmrMr�r�r�r�r�r�r�r�r<rt�samer�s �r2r�zTR10i.<locals>.f.<locals>.do�s�����9� �� �����(�(�)�)�D��4�y�y�A�~�~����s�4�y�y�)�)�"�"�A��a��B��z� �"�1�q�5�#�d�)�)�4�4� "� "��!�!�W���:�$� �2�g�� �b��g�g���#�:�:�&)�D��G�&*�D��G�"&�C�!�E� &�� �$��7�7�D�7�7�7�8�B��y�$��R��V�V��� ���/�$�/�/�E�� �� �&+� #�C��R��A�t�� &���f����8�8��s�1�q�5�z�z�>�)��3�q�1�u�:�:�~�%���f����8�8��s�1�q�5�z�z�>�)��3�q�1�u�:�:�~�%r4c�D�tt|j����SrI)�tupler� free_symbolsr�s r2r�z"TR10i.<locals>.f.<locals>.<lambda>�s��e�G�A�N�$;�$;�<�<�r4rrfc�4�g|]}td�|D�����S)c��g|]}|�|��Sr\r\r�s r2r`z/TR10i.<locals>.f.<locals>.<listcomp>.<listcomp>�s��(>�(>�(>��2�(>��(>�(>�(>r4r)rLrns r2r`z$TR10i.<locals>.f.<locals>.<listcomp>�s<��#-�#-�#-��$'�(>�(>�a�(>�(>�(>�#?�#-�#-�#-r4r�)rUr�rr{r9rxrJr�r�HalfrKr�rgr:�_ROOT3� _invROOT3r�rVr�values) r1�byradr<r�rMr9rtrmr�r�r�r�s @r2r=zTR10i.<locals>.f�sz����y� ��I�6 &�6 &�6 &�6 &�6 &�6 &�p$� ��<�<�>�>�� �i�& ���%�%�E��W� +� +�����8�"��f�"�"���9�"���1�6�)9�)9� "�� 2�*:�!�"�I�,�,�Q�/�/�/�"#�C�!�E���+��!�%�L�'�'��*�*�*���D�� *� *�� ��(�I�.�*�*�A��E�z�z�!&�s�5��8�}�}�!5�!5� *� *�A�$�Q�x��{�2� (�%*�3�u�Q�x�=�=�%9�%9� *� *��#(��8�A�;�#6�$,�&)�%��(�1�+��a��� �*C�&D�&D��&(�b��g�g��#&�#�:�:�$(�K�K��$4�$4�$4�26�E�!�H�Q�K�26�E�!�H�Q�K�$)�E� $.���*�� ��4�#-�#-�"�\�\�^�^�#-�#-�#-�-�/����R��V�V���M�i�& �P� r4)�_ROOT2�_rootsrr>s r2�TR10ir�~s7��&�~�����i�i�i�V �R�� � �r4Nc�.���fd�}t||��S)anFunction of double angle to product. The ``base`` argument can be used to indicate what is the un-doubled argument, e.g. if 3*pi/7 is the base then cosine and sine functions with argument 6*pi/7 will be replaced. Examples ======== >>> from sympy.simplify.fu import TR11 >>> from sympy import cos, sin, pi >>> from sympy.abc import x >>> TR11(sin(2*x)) 2*sin(x)*cos(x) >>> TR11(cos(2*x)) -sin(x)**2 + cos(x)**2 >>> TR11(sin(4*x)) 4*(-sin(x)**2 + cos(x)**2)*sin(x)*cos(x) >>> TR11(sin(4*x/3)) 4*(-sin(x/3)**2 + cos(x/3)**2)*sin(x/3)*cos(x/3) If the arguments are simply integers, no change is made unless a base is provided: >>> TR11(cos(2)) cos(2) >>> TR11(cos(4), 2) -sin(2)**2 + cos(2)**2 There is a subtle issue here in that autosimplification will convert some higher angles to lower angles >>> cos(6*pi/7) + cos(3*pi/7) -cos(pi/7) + cos(3*pi/7) The 6*pi/7 angle is now pi/7 but can be targeted with TR11 by supplying the 3*pi/7 base: >>> TR11(_, 3*pi/7) -sin(3*pi/7)**2 + cos(3*pi/7)**2 + cos(3*pi/7) c� ��|jttfvr|S�r�|j}|�dz��}tj}|jr|���\}}|jttfvr|S|jd|jdkr@t���}t���}|tur|dz|dzz |z Sd|z|z|z S|S|jdjs�|jd�d���\}}|j dzdkrq|j dz|z|j z }tt|����}tt|����}|jtkr d|z|z}n |dz|dzz }|S)NrFrT)�rational) rTrr rr:rxr�r9� is_Numberr�r��TR11) r1r=r�cor�r��mr�rKs �r2r=zTR11.<locals>.f,s��� �7�3��*� $� $��I� � %���A���$�q�&� � �A���B��x� )����(�(���A��v�c�3�Z�'�'�� ��w�q�z�Q�V�A�Y�&�&���I�I����I�I����8�8��q�D�1�a�4�K��+�+��Q�3�q�5��8�O��I�����%� %��7�1�:�*�*�D�*�9�9�D�A�q��s�Q�w�!�|�|��c�1�f�Q�h�q�s�l����S���N�N����S���N�N���7�c�>�>��1��Q��B�B��A���1���B�� r4r)r1rKr=s ` r2r�r�s0���T!�!�!�!�!�F �R�� � �r4c�(�d�}t||��S)a� Helper for TR11 to find half-arguments for sin in factors of num/den that appear in cos or sin factors in the den/num. Examples ======== >>> from sympy.simplify.fu import TR11, _TR11 >>> from sympy import cos, sin >>> from sympy.abc import x >>> TR11(sin(x/3)/(cos(x/6))) sin(x/3)/cos(x/6) >>> _TR11(sin(x/3)/(cos(x/6))) 2*sin(x/6) >>> TR11(sin(x/6)/(sin(x/3))) sin(x/6)/sin(x/3) >>> _TR11(sin(x/6)/(sin(x/3))) 1/(2*cos(x/6)) c��t|t��s|Sd�}t||�����\}}d�}||||��}||||��}|S)Nc�4�tt��}tj|��D]n}|���\}}|jrN|dkrH|jttfvr3|t|��� |j d���o|Sr7) r�setr rP� as_base_expr�rTrr r��addr9)�flatr9�firtrXs r2� sincos_argsz%_TR11.<locals>.f.<locals>.sincos_argsks����s�#�#�D��m�D�)�)� 5� 5���~�~�'�'���1��<�5�A��E�E��v�#�s��+�+��T�!�W�W� �)�)�!�&��)�4�4�4���Kr4c���|tD]a}|dz }||tvrt}n||tvrt}n�6t||��}||�|���b|S�NrF)r rr��remove)r1�num_args�den_args�nargrYrTs r2� handle_matchz&_TR11.<locals>.f.<locals>.handle_matchws|��!�� � ,� ,���A�v���8�C�=�(�(��D�D��X�c�]�*�*��D�D���"�d�^�^�����%�%�d�+�+�+�+��Ir4)r8r�mapry)r1r r rrs r2r=z_TR11.<locals>.fgs����"�d�#�#� ��I� � � �!��b�.?�.?�.A�.A�B�B���(� � � � �\�"�h�� 1� 1�� �\�"�h�� 1� 1��� r4rr>s r2�_TR11rRs$��*#�#�#�J �R�� � �r4c�.���fd�}t||��S)z�Separate sums in ``tan``. Examples ======== >>> from sympy.abc import x, y >>> from sympy import tan >>> from sympy.simplify.fu import TR12 >>> TR12(tan(x + y)) (tan(x) + tan(y))/(-tan(x)*tan(y) + 1) c����|jtks|S|jd}|jr��r"t t |j����}nt |j��}|���}tj|��}|jrtt|��d���}nt|��}t|��|zdt|��|zz z S|S)NrFr�rf) rTr!r9rUr{rr^rr��TR12)r1r�r9r<rt�tbr�s �r2r=zTR12.<locals>.f�s�����w�#�~�~��I��g�a�j�� �:� 1�� &��G�C�H�-�-�.�.����C�H�~�~����� � �A���t�$�$�A��x� ��#�a�&�&��.�.�.�����V�V����F�F�R�K�!�c�!�f�f�R�i�-�0� 0�� r4rr�s ` r2rr�s.��������& �R�� � �r4c�(�d�}t||��S)aKCombine tan arguments as (tan(y) + tan(x))/(tan(x)*tan(y) - 1) -> -tan(x + y). Examples ======== >>> from sympy.simplify.fu import TR12i >>> from sympy import tan >>> from sympy.abc import a, b, c >>> ta, tb, tc = [tan(i) for i in (a, b, c)] >>> TR12i((ta + tb)/(-ta*tb + 1)) tan(a + b) >>> TR12i((ta + tb)/(ta*tb - 1)) -tan(a + b) >>> TR12i((-ta - tb)/(ta*tb - 1)) tan(a + b) >>> eq = (ta + tb)/(-ta*tb + 1)**2*(-3*ta - 3*tc)/(2*(ta*tc - 1)) >>> TR12i(eq.expand()) -3*tan(a + b)*tan(a + c)/(2*(tan(a) + tan(b) - 1)) c�\�|js|js |js|S|���\}}|jr|js|Si}d�}t t j|����}t|��D�]1\}}||��}|r2|\} } td�| jD���} tj || <| ||<�E|jr@t|��}|jr)|� |j��tj ||<��|jr�|jjs |jjr�||j��}|r5|\} } td�| jD���} |j|| <| |jz||<��t|��}|jr)|� |j��tj ||<��3|s|Sd�}t t jt%|������} d} t| ��D�]�\}}||��}|s�|| ��}|rtj| |<n�|jr@t|��}|jr)| � |j��tj | |<�x|jrz|jjs |jjrb||j��}|rtj | |<nPt|��}|jr)| � |j��tj | |<����tj | |<d} td�|D���} || }|�tj ��}|�|r||| <n|�| ��| |xxt-| �� zcc<���| r9t | �t |�z t d�|���D���z }|S) Nc���t|��}|rU|\}}}|tjurC|jr>t |j��dkr(t d�|jD����r ||fSdSdSdSdSdS)NrFc3�@K�|]}t|t��V��dSrI)r8r!)rLrs r2rNz/TR12i.<locals>.f.<locals>.ok.<locals>.<genexpr>�s,����A�A�B�J�r�3�/�/�A�A�A�A�A�Ar4)� as_f_sign_1r� NegativeOnerxrVr9�all)�dirr�r=r�s r2rZzTR12i.<locals>.f.<locals>.ok�s����B���A�� ����1�a��� �%�%�!�(�%�s�1�6�{�{�a�7G�7G��A�A�!�&�A�A�A�A�A�8H��a�4�K�  � �%�%�%�%�7G�7G�7G�7Gr4c�(�g|]}|jd��S�r�r9�rL�_s r2r`z$TR12i.<locals>.f.<locals>.<listcomp>�s��4�4�4��!�&��)�4�4�4r4c�(�g|]}|jd��Sr r!r"s r2r`z$TR12i.<locals>.f.<locals>.<listcomp>�s��8�8�8�A�a�f�Q�i�8�8�8r4c���|jrPt|j��dkr:|j\}}t|t��rt|t��r ||fSdSdSdSdSr )rUrVr9r8r!)�nir<rts r2rZzTR12i.<locals>.f.<locals>.ok�sy���y� �S���\�\�Q�.�.��w���1��a��%�%� �*�Q��*<�*<� ��a�4�K� � �.�.� � � � r4FTc�(�g|]}|jd��Sr r!r"s r2r`z$TR12i.<locals>.f.<locals>.<listcomp>s��+�+�+�A�a�f�Q�i�+�+�+r4c�P�g|]#\}}td�|jD���dz |z��$S)c�,�g|]}t|����Sr\)r!rQs r2r`z/TR12i.<locals>.f.<locals>.<listcomp>.<listcomp>%s+��8(�8(�8(���A���8(�8(�8(r4rf)rr9)rLrmrXs r2r`z$TR12i.<locals>.f.<locals>.<listcomp>%se��1J�1J�1J�59�Q��36�8(�8(� !��8(�8(�8(�3)�+,�3-�/0�21�1J�1J�1Jr4)rUrxrJryr9r{r rPrhrrr:r(r�r�rRrKrSr r�extract_additivelyr^r!r})r1r_rc�dokrZ�d_argsrmrrr�rr��n_argsr�r&�ed�neweds r2r=zTR12i.<locals>.f�s��� � �R�Y� �"�)� ��I�� � �"�"���1��v� �Q�V� ��I��� � � ��c�m�A�&�&�'�'���v�&�&� *� *�E�A�r���2���A�� ����1��4�4�Q�V�4�4�4�5�����A����q� ���y� *��B�Z�Z���9�&��M�M�"�'�*�*�*� !��F�1�I���� *��� 1� *�R�W�5H� *��B�r�w�K�K��� *��D�A�q��8�8���8�8�8�9�A��V�C��F� !�2�6� �F�1�I�I�����B��y�*�� � �b�g�.�.�.�$%�E��q� ��� ��I� � � � �c�m�L��O�O�4�4�5�5�����v�&�&�% !�% !�E�A�r���2���A�� "��B��s�G�G���!� !� �F�1�I�I��y�!�#�B�Z�Z���9�.�"�M�M�"�'�2�2�2�()��F�1�I� ��� !��F�-� !�13��1D� !��B�r�w�K�K���%�()��F�1�I�I�!'����B�!�y�2� &� � �b�g� 6� 6� 6�,-�E��q� �$� ��E��q� ��C��+�+��+�+�+�,�A��Q��B��)�)�!�%�0�0�E�� ���"�C��F�F��G�G�A�J�J�J� �1�I�I�I�#�a�&�&�� �I�I�I�I� � K��f��c�6�l�*�3�1J�1J�=@�Y�Y�[�[�1J�1J�1J�,K�K�B�� r4rr>s r2�TR12ir0�s'��*a�a�a�F �R�� � �r4c�(�d�}t||��S)aChange products of ``tan`` or ``cot``. Examples ======== >>> from sympy.simplify.fu import TR13 >>> from sympy import tan, cot >>> TR13(tan(3)*tan(2)) -tan(2)/tan(5) - tan(3)/tan(5) + 1 >>> TR13(cot(3)*cot(2)) cot(2)*cot(5) + 1 + cot(3)*cot(5) c �P�|js|Stgtgdgi}tj|��D]f}|jttfvr4|t |���|jd���K|d�|���g|t}|t}t|��dkrt|��dkr|S|d}t|��dkr�|� ��}|� ��}|�dt|��t||z��z t|��t||z��z zz ��t|��dk��|r4|�t|� ������t|��dkr�|� ��}|� ��}|�dt|��t||z��zzt|��t||z��zz��t|��dk��|r4|�t|� ������t|�S)NrrFrf) rxr!r"r rPrTr�rgr9rVr^)r1r9r<rr��t1�t2s r2r=zTR13.<locals>.f;s,���y� ��I��R��b�$��+����r�"�"� %� %�A��v�#�s��#�#��T�!�W�W� �$�$�Q�V�A�Y�/�/�/�/��T� �!�!�!�$�$�$�$� ��I�� ��I�� �q�6�6�A�:�:�#�a�&�&�1�*�*��I��D�z���!�f�f�q�j�j������B������B� �K�K��S��W�W�S��b��\�\�1�C��G�G�C��R��L�L�4H�H�I� J� J� J��!�f�f�q�j�j� � &� �K�K��A�E�E�G�G� � � %� %� %��!�f�f�q�j�j������B������B� �K�K��C��G�G�C��R��L�L�0�0�3�r�7�7�3�r�B�w�<�<�3G�G� H� H� H��!�f�f�q�j�j� � &� �K�K��A�E�E�G�G� � � %� %� %��D�z�r4rr>s r2�TR13r5-s#�����< �R�� � �r4c�0��d�fd� �t|���S)a�Returns cos(x)*cos(2*x)*...*cos(2**(k-1)*x) -> sin(2**k*x)/(2**k*sin(x)) Examples ======== >>> from sympy.simplify.fu import TRmorrie, TR8, TR3 >>> from sympy.abc import x >>> from sympy import Mul, cos, pi >>> TRmorrie(cos(x)*cos(2*x)) sin(4*x)/(4*sin(x)) >>> TRmorrie(7*Mul(*[cos(x) for x in range(10)])) 7*sin(12)*sin(16)*cos(5)*cos(7)*cos(9)/(64*sin(1)*sin(3)) Sometimes autosimplification will cause a power to be not recognized. e.g. in the following, cos(4*pi/7) automatically simplifies to -cos(3*pi/7) so only 2 of the 3 terms are recognized: >>> TRmorrie(cos(pi/7)*cos(2*pi/7)*cos(4*pi/7)) -sin(3*pi/7)*cos(3*pi/7)/(4*sin(pi/7)) A touch by TR8 resolves the expression to a Rational >>> TR8(_) -1/8 In this case, if eq is unsimplified, the answer is obtained directly: >>> eq = cos(pi/9)*cos(2*pi/9)*cos(3*pi/9)*cos(4*pi/9) >>> TRmorrie(eq) 1/16 But if angles are made canonical with TR3 then the answer is not simplified without further work: >>> TR3(eq) sin(pi/18)*cos(pi/9)*cos(2*pi/9)/2 >>> TRmorrie(_) sin(pi/18)*sin(4*pi/9)/(8*sin(pi/9)) >>> TR8(_) cos(7*pi/18)/(16*sin(pi/9)) >>> TR3(_) 1/16 The original expression would have resolve to 1/16 directly with TR8, however: >>> TR8(eq) 1/16 References ========== .. [1] https://en.wikipedia.org/wiki/Morrie%27s_law Tc�`���|js|S|r0|���\}}�|d���|d��z Stt���i}g}|jD]�}|���\}}|jrXt|t��rC|jd� ��\} } �| � | ��|||<�x|� |����g} �D�]�} �| }|� ��|�r~d} |dx} }| |vr| dz } | dz} | |v�| dk�rtd| z|z| z��d| zz t|| z��z }d}g}t| ��D]N}| dz} t| | zd���}|� | ��t|||p||��}�Ot| ��D]W}|���} t| | zd���}||xx|zcc<||s|�| ���X| � ||z��nCt|�d��| z��}|� |||z��|��~���| rt#| |z�fd��D��z�}|S)NrrfrFFrpc�N��g|]!}�|D]}t||zd������"S)Frp)r)rLr<rWr9s �r2r`z'TRmorrie.<locals>.f.<locals>.<listcomp>�sX���&I�&I�&I�-.��Q��&I�&I�;<��A�a�C�%�(�(�(�&I�&I�&I�&Ir4)rxryrr{r9rr�r8rr�rg�sortr r�r�r^r r )r1r�r_rc�coss�otherr�rtrXr�r<r�rW�cc�ci�newarg�take�ccsrm�keyr9r=s @�r2r=zTRmorrie.<locals>.f�s�����y� ��I� � #��$�$�&�&�D�A�q��1�Q��7�7�1�1�Q��7�7�?� "��4� � �������� � �A��=�=�?�?�D�A�q��|� � �1�c� 2� 2� ���q� �.�.�0�0���A��Q����r�"�"�"���Q���� � �Q�������� -� -�A��Q��A� �F�F�H�H�H�� -����A�$���R��A�g�g���F�A��!�G�B��A�g�g��q�5�5� ��A��b����^�^�A�q�D�0��R��T���:�F��D��C�"�1�X�X�A�A���a���!�!�B�$��7�7�7��� � �2����"�4��9�d�.?�d�3�i�@�@���"�1�X�X�)�)�� �W�W�Y�Y��!�!�B�$��7�7�7���S� � � �T�)� � � �#�C�y�)��H�H�R�L�L�L���J�J�v�t�|�,�,�,�,��A�E�E�!�H�H�Q�J���A��L�L��D��G��,�,�,�5� -��8 � K��s�U�{�&I�&I�&I�&I�26�&I�&I�&I�I�K�B�� r4r�rr>s @r2�TRmorrierB\s5���v7�7�7�7�7�7�r �R�� � �r4c�.���fd�}t||��S)aConvert factored powers of sin and cos identities into simpler expressions. Examples ======== >>> from sympy.simplify.fu import TR14 >>> from sympy.abc import x, y >>> from sympy import cos, sin >>> TR14((cos(x) - 1)*(cos(x) + 1)) -sin(x)**2 >>> TR14((sin(x) - 1)*(sin(x) + 1)) -cos(x)**2 >>> p1 = (cos(x) + 1)*(cos(x) - 1) >>> p2 = (cos(y) - 1)*2*(cos(y) + 1) >>> p3 = (3*(cos(y) - 1))*(3*(cos(y) + 1)) >>> TR14(p1*p2*p3*(x - 1)) -18*(x - 1)*sin(x)**2*sin(y)**4 c �~����|js|S�rZ|���\}}|tjur5t |d���}t |d���}||ks||kr||z }|Sg}g}|jD]�}|jr>|���\}} | js|j s|� |���D|}n tj} t|��} | r| dj ttfvr=| tjur|� |��n|� || z����| \} } } |� | | j| | | |f����t!t#|����}t%|��}t!t'd����x}\} }} } } }|�rw|�d���|�r8|d��| j�r[�| j�rM�| �| k�r9�| �| k�r&|�d���t+�| �| ��}�| |kr5�fd�|D��}|| xx|zcc<|�d|��n@�| |kr4�fd�|D��}|| xx|zcc<|�d|��t/�| t��rt}nt}|� �|  �| z|�| jd��dzz|z�����nlj| �| kr��| �| kr��| �| kr�|�d����| }t/�| t��rt}nt}|� �|  �| z|�| jd��dzz|z����S|� �|�| z��|��wt%|��|kr t1|�}|S) NFr�rfr�rc� ��g|] }�|�� Sr\r\)rLrm�Bs �r2r`z#TR14.<locals>.f.<locals>.<listcomp>%����&:�&:�&:��q��t�&:�&:�&:r4c� ��g|] }�|�� Sr\r\)rLrm�As �r2r`z#TR14.<locals>.f.<locals>.<listcomp>)rGr4rF)rxryrr:�TR14r9rJrrRrSrgrrTrr r�r{rrVr�r^r��insertr8r )r1r_rcr�r�r;�processr<rtrXrr�r=�si�notherr|rr?�remrIrFr�s @@�r2r=zTR14.<locals>.f�sO������y� ��I� � ��$�$�&�&�D�A�q����~�~��A�U�+�+�+���A�U�+�+�+���1�9�9��� � ��d��B�� ������� :� :�A��x� ��}�}�����1�� ��� ���L�L��O�O�O������E���A���A�� ��!�� �#�s��3�3����:�:��L�L��O�O�O�O��L�L��A��&�&�&���H�A�q�"� �N�N�A�q�{�A�q�"�a�8� 9� 9� 9� 9��w�w�'�'�(�(���U����&*�%��(�(�^�^�3��"��1�a��B���, %�� � �A���A��' %��A�J���Q�4�>�$%�a��d�n�$%���t�q��t�|�|��R�5�A�b�E�>�>� '� � �A���A�#&�q��t�Q�q�T�?�?�D� !��t�t�|�|�&:�&:�&:�&:�T�&:�&:�&:�� #�A����$����� '���q�#� 6� 6� 6� 6�!"�1�����&:�&:�&:�&:�T�&:�&:�&:�� #�A����$����� '���q�#� 6� 6� 6�)�!�A�$��4�4�(�$'���$'��!�L�L�1�Q�4�%��!��*�Q�Q�q��t�y��|�_�_�a�5G�*G�$�)N�O�O�O�$���q�T�Q�q�T�\�\���t�q��t�|�|��R�5�A�b�E�>�>� '� � �A���A�#$�Q�4�D�)�!�A�$��4�4�(�$'���$'��!�L�L�1�Q�4�%��!��*�Q�Q�q��t�y��|�_�_�a�5G�*G�$�)N�O�O�O�$� �L�L��1��q��t�� $� $� $�Y�, %�\ �u�:�:�� � ��e��B�� r4rr�s ` r2rJrJ�s4���,^�^�^�^�^�@ �R�� � �r4c�2�����fd�}t||��S)aConvert sin(x)**-2 to 1 + cot(x)**2. See _TR56 docstring for advanced use of ``max`` and ``pow``. Examples ======== >>> from sympy.simplify.fu import TR15 >>> from sympy.abc import x >>> from sympy import sin >>> TR15(1 - 1/sin(x)**2) -cot(x)**2 c�(��t|t��rt|jt��s|S|j}|dzdkr"t |j|dzz��|jz Sd|z }t |ttd������}||kr|}|S)NrFrfc� �d|zSrer\r�s r2r�z!TR15.<locals>.f.<locals>.<lambda>e� ��!�a�%�r4r�)r8rrKr r��TR15r�r"�r1rX�iar<r�r�s ��r2r=zTR15.<locals>.f\�����2�s�#�#� � �2�7�C�(@�(@� ��I� �F�� �q�5�A�:�:����!�a�%�(�)�)�"�'�1� 1� �r�T�� �"�c�3���S�c� B� B� B�� ��7�7��B�� r4r�r1r�r�r=s `` r2rTrTL�4���� � � � � � � �R�� � �r4c�2�����fd�}t||��S)aConvert cos(x)**-2 to 1 + tan(x)**2. See _TR56 docstring for advanced use of ``max`` and ``pow``. Examples ======== >>> from sympy.simplify.fu import TR16 >>> from sympy.abc import x >>> from sympy import cos >>> TR16(1 - 1/cos(x)**2) -tan(x)**2 c�(��t|t��rt|jt��s|S|j}|dzdkr"t |j|dzz��|jz Sd|z }t |ttd������}||kr|}|S)NrFrfc� �d|zSrer\r�s r2r�z!TR16.<locals>.f.<locals>.<lambda>�rSr4r�)r8rrKrr�rTr�r!rUs ��r2r=zTR16.<locals>.f}rWr4rrXs `` r2�TR16r]mrYr4c�(�d�}t||��S)aDConvert f(x)**-i to g(x)**i where either ``i`` is an integer or the base is positive and f, g are: tan, cot; sin, csc; or cos, sec. Examples ======== >>> from sympy.simplify.fu import TR111 >>> from sympy.abc import x >>> from sympy import tan >>> TR111(1 - 1/tan(x)**2) 1 - cot(x)**2 c��t|t��r$|jjs|jjr |jjs|St|jt��r(t|jj d��|j zSt|jt��r(t|jj d��|j zSt|jt��r(t|jj d��|j zS|Sr7)r8rrKrSr�rR� is_negativer!r"r9r r$rr#r0s r2r=zTR111.<locals>.f�s��� �r�3� � � � �W� � �$&�F�$5� �:<�&�:L� ��I� �b�g�s� #� #� 1��r�w�|�A��'�'�"�&��0� 0� ���� %� %� 1��r�w�|�A��'�'�"�&��0� 0� ���� %� %� 1��r�w�|�A��'�'�"�&��0� 0�� r4rr>s r2�TR111ra�s#�� � � � �R�� � �r4c�2�����fd�}t||��S)ahConvert tan(x)**2 to sec(x)**2 - 1 and cot(x)**2 to csc(x)**2 - 1. See _TR56 docstring for advanced use of ``max`` and ``pow``. Examples ======== >>> from sympy.simplify.fu import TR22 >>> from sympy.abc import x >>> from sympy import tan, cot >>> TR22(1 + tan(x)**2) sec(x)**2 >>> TR22(1 + cot(x)**2) csc(x)**2 c����t|t��r|jjtt fvs|St |t td������}t |ttd������}|S)Nc� �|dz Srer\r�s r2r�z!TR22.<locals>.f.<locals>.<lambda>�� ��1�q�5�r4r�c� �|dz Srer\r�s r2r�z!TR22.<locals>.f.<locals>.<lambda>�rer4) r8rrKrTr"r!r�r#r$r�s ��r2r=zTR22.<locals>.f�si����2�s�#�#� ��� ��c� �(B�(B��I� �2�s�C���c�s� C� C� C�� �2�s�C���c�s� C� C� C��� r4rrXs `` r2�TR22rg�s4����$������ �R�� � �r4c�(�d�}t||��S)aConvert sin(x)**n and cos(x)**n with positive n to sums. Examples ======== >>> from sympy.simplify.fu import TRpower >>> from sympy.abc import x >>> from sympy import cos, sin >>> TRpower(sin(x)**6) -15*cos(2*x)/32 + 3*cos(4*x)/16 - cos(6*x)/32 + 5/16 >>> TRpower(sin(x)**3*cos(2*x)**4) (3*sin(x)/4 - sin(3*x)/4)*(cos(4*x)/2 + cos(8*x)/8 + 3/8) References ========== .. [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Power-reduction_formulae c�����t|t��r!t|jttf��s|S|���\}�|jd��j�r��j�r��j rIt|t��r4dd�z zt��fd�t�dzdz ��D���z}�n�j r^t|t��rIdd�z ztj �dz dz zzt��fd�t�dzdz ��D���z}n��jrEt|t��r0dd�z zt��fd�t�dz ��D���z}n^�jrWt|t��rBdd�z ztj �dz zzt��fd�t�dz ��D���z}�jr|d� zt��dz ��zz }|S)NrrFrfc�b��g|]+}t�|��t�d|zz �z��z��,S�rF�rr�rLrWr_r�s ��r2r`z&TRpower.<locals>.f.<locals>.<listcomp>�sJ���$/�$/�$/��%-�Q��N�N�3��A�a�C���{�3C�3C�$C�$/�$/�$/r4c���g|];}t�|��tj|zzt�d|zz �z��z��<Srk)rrrr rms ��r2r`z&TRpower.<locals>.f.<locals>.<listcomp>�sc���=Q�=Q�=Q�:;�>F�a��^�^��M�1�$�>%�%(�!�a��c�'�1��%5�%5�>6�=Q�=Q�=Qr4c�b��g|]+}t�|��t�d|zz �z��z��,Srkrlrms ��r2r`z&TRpower.<locals>.f.<locals>.<listcomp>�sJ���$)�$)�$)��%-�Q��N�N�3��A�a�C���{�3C�3C�$C�$)�$)�$)r4c���g|];}t�|��tj|zzt�d|zz �z��z��<Srk)rrrrrms ��r2r`z&TRpower.<locals>.f.<locals>.<listcomp>�sc���9K�9K�9K�:;�:B�!�Q����M�1�$�:%�%(�!�a��c�'�1��%5�%5�:6�9K�9K�9Kr4)r8rrKr rrr9r�rS�is_oddrr�rr�is_evenr)r1rtr_r�s @@r2r=zTRpower.<locals>.f�s������2�s�#�#� � �2�7�S�#�J�(G�(G� ��I��~�~�����1� �F�1�I�� �<� /�A�M� /��x� L�J�q�#�.�.� L���1��X�c�$/�$/�$/�$/�$/�"�A��E�1�9�-�-�$/�$/�$/�0�0����� L�j��C�0�0� L���1��X�a�m�q��s�A�g�6�6�s�=Q�=Q�=Q�=Q�=Q�?D�a�!�e�Q�Y�?O�?O�=Q�=Q�=Q�8R�R����� L�z�!�S�1�1� L���1��X�c�$)�$)�$)�$)�$)�"�1�Q�3�Z�Z�$)�$)�$)�*�*����� L�z�!�S�1�1� L���1��X�a�m�a��c�2�2�3�9K�9K�9K�9K�9K�?D�Q�q�S�z�z�9K�9K�9K�4L�L���y� /��a�1�"�g�h�q�!�A�#�.�.�.�.��� r4rr>s r2�TRpowerrs�s#��*���, �R�� � �r4c�P�t|�t����S)z�Return count of trigonometric functions in expression. Examples ======== >>> from sympy.simplify.fu import L >>> from sympy.abc import x >>> from sympy import cos, sin >>> L(cos(x)+sin(x)) 2 )r�countr&r0s r2�Lrv�s�� �R�X�X�+� ,� ,� -� -�-r4c�H�t|��|���fSrI)rv� count_opsr�s r2r�r�-s��a��d�d�A�K�K�M�M�2�r4c����tt���}tt���}|}t|��}t |t ��s|j�fd�|jD���St|��}|� tt��rT||��}�|���|��kr|}|� tt��rt|��}|� tt��r<||��}tt!|����}t#||||g����}t#t%|��|����S)a7Attempt to simplify expression by using transformation rules given in the algorithm by Fu et al. :func:`fu` will try to minimize the objective function ``measure``. By default this first minimizes the number of trig terms and then minimizes the number of total operations. Examples ======== >>> from sympy.simplify.fu import fu >>> from sympy import cos, sin, tan, pi, S, sqrt >>> from sympy.abc import x, y, a, b >>> fu(sin(50)**2 + cos(50)**2 + sin(pi/6)) 3/2 >>> fu(sqrt(6)*cos(x) + sqrt(2)*sin(x)) 2*sqrt(2)*sin(x + pi/3) CTR1 example >>> eq = sin(x)**4 - cos(y)**2 + sin(y)**2 + 2*cos(x)**2 >>> fu(eq) cos(x)**4 - 2*cos(y)**2 + 2 CTR2 example >>> fu(S.Half - cos(2*x)/2) sin(x)**2 CTR3 example >>> fu(sin(a)*(cos(b) - sin(b)) + cos(a)*(sin(b) + cos(b))) sqrt(2)*sin(a + b + pi/4) CTR4 example >>> fu(sqrt(3)*cos(x)/2 + sin(x)/2) sin(x + pi/3) Example 1 >>> fu(1-sin(2*x)**2/4-sin(y)**2-cos(x)**4) -cos(x)**2 + cos(y)**2 Example 2 >>> fu(cos(4*pi/9)) sin(pi/18) >>> fu(cos(pi/9)*cos(2*pi/9)*cos(3*pi/9)*cos(4*pi/9)) 1/16 Example 3 >>> fu(tan(7*pi/18)+tan(5*pi/18)-sqrt(3)*tan(5*pi/18)*tan(7*pi/18)) -sqrt(3) Objective function example >>> fu(sin(x)/cos(x)) # default objective function tan(x) >>> fu(sin(x)/cos(x), measure=lambda x: -x.count_ops()) # maximize op count sin(x)/cos(x) References ========== .. [1] https://www.sciencedirect.com/science/article/pii/S0895717706001609 c�2��g|]}t|������S))�measure)�fu)rLr<r{s �r2r`zfu.<locals>.<listcomp>ys&���A�A�A�A��A�w�/�/�/�A�A�Ar4)rA)r)�RL1�RL2rr8rrTr9r?�hasr!r"rBr rr�rBr�r�)r1r{�fRL1�fRL2r��rv1�rv2s ` r2r|r|-sH���L �#�w� � �D� �#�w� � �D� �C� ����B� �b�$� � �C��r�w�A�A�A�A���A�A�A�B�B� �R���B� �v�v�c�3�����d�2�h�h�� �G�C�L�L�7�7�2�;�;� &� &��B� �6�6�#�s� � � ��R���B� �v�v�c�3���3��d�2�h�h���(�3�-�-� � �� �#�r�3��$�'� 2� 2� 2�� �t�B�x�x��� )� )� )�)r4c��tt��}|rX|jD]O}|���\}}|dkr| }| }|||r ||��ndf�|���PnL|r;|jD]2}|t j||��f�|���3ntd���g}d}|D]z} || } | \}} t| ��dkr:t| ddi�} || ��} | | kr| } d}|�|| z���\|�|| dz���{|r t|�}|S)aApply ``do`` to addends of ``rv`` that (if ``key1=True``) share at least a common absolute value of their coefficient and the value of ``key2`` when applied to the argument. If ``key1`` is False ``key2`` must be supplied and will be the only key applied. rrfzmust have at least one keyFrqT) rr{r9r�rgrr:� ValueErrorrVr)r1r��key2�key1�abscr<r�r9r�rWrnr#rXr�s r2r�r��s��� �t� � �D� � 7��� 8� 8�A��>�>�#�#�D�A�q��1�u�u��B���B�� �!��+�T�T�!�W�W�W�!�,� -� 4� 4�Q� 7� 7� 7� 7�  8� �7��� -� -�A� �!�%���a���!� "� )� )�!� ,� ,� ,� ,� -��5�6�6�6� �D� �C� �  �  �� ��G�����1� �q�6�6�A�:�:��Q�'��'�'�A��"�Q�%�%�C��a�x�x����� �K�K��!�� � � � � �K�K��!�A�$�� � � � � �� �$�Z�� �Ir4z~ TR0 TR1 TR2 TR3 TR4 TR5 TR6 TR7 TR8 TR9 TR10 TR10i TR11 TR12 TR13 L TR2i TRmorrie TR12i TR14 TR15 TR16 TR111 TR22c�X�td��td��caadtz adS)NrFr�rf)r%r�r�r�r\r4r2r�r��s&���!�W�W�d�1�g�g�N�F�F��&��I�I�Ir4c ���t�t��d�||fD��\}}|�|��\}}|�|�����}dx}}t j|jvr#|�t j��}| }n5t j|jvr"|�t j��}| }d�||fD��\}}d�}|||��} | �dS| \} } } |||��} | �dS| \} }}| s|s| r(t| t��r| ||| | | f\} } } } }}||}}|sP| p| }|p|}t||j ��sdS||||j d|j dt|t��fS| s�| s�| r�|r�| r�|r�t| | j ��t||j ��urdSd�| | fD���t�fd�||fD����sdS|||| j d| j dt| | j ��fS| r| s|r|s |r | �| �|�|�dS| p| }|p|}|j |j krdS| s t j} | s t j} | | ur%|tz}||||j dt d z d fS| | z t"kr#|d | zz}||||j dt d z d fS| | z t$kr#|d | zz}||||j dt d z d fSdS)a)Return the gcd, s1, s2, a1, a2, bool where If two is False (default) then:: a + b = gcd*(s1*f(a1) + s2*f(a2)) where f = cos if bool else sin else: if bool, a + b was +/- cos(a1)*cos(a2) +/- sin(a1)*sin(a2) and equals n1*gcd*cos(a - b) if n1 == n2 else n1*gcd*cos(a + b) else a + b was +/- cos(a1)*sin(a2) +/- sin(a1)*cos(a2) and equals n1*gcd*sin(a + b) if n1 = n2 else n1*gcd*sin(b - a) Examples ======== >>> from sympy.simplify.fu import trig_split >>> from sympy.abc import x, y, z >>> from sympy import cos, sin, sqrt >>> trig_split(cos(x), cos(y)) (1, 1, 1, x, y, True) >>> trig_split(2*cos(x), -2*cos(y)) (2, 1, -1, x, y, True) >>> trig_split(cos(x)*sin(y), cos(y)*sin(y)) (sin(y), 1, 1, x, y, True) >>> trig_split(cos(x), -sqrt(3)*sin(x), two=True) (2, 1, -1, x, pi/6, False) >>> trig_split(cos(x), sin(x), two=True) (sqrt(2), 1, 1, x, pi/4, False) >>> trig_split(cos(x), -sin(x), two=True) (sqrt(2), 1, -1, x, pi/4, False) >>> trig_split(sqrt(2)*cos(x), -sqrt(6)*sin(x), two=True) (2*sqrt(2), 1, -1, x, pi/6, False) >>> trig_split(-sqrt(6)*cos(x), -sqrt(2)*sin(x), two=True) (-2*sqrt(2), 1, 1, x, pi/3, False) >>> trig_split(cos(x)/sqrt(6), sin(x)/sqrt(2), two=True) (sqrt(6)/3, 1, 1, x, pi/6, False) >>> trig_split(-sqrt(6)*cos(x)*sin(y), -sqrt(2)*sin(x)*sin(y), two=True) (-2*sqrt(2)*sin(y), 1, 1, x, pi/3, False) >>> trig_split(cos(x), sin(x)) >>> trig_split(cos(x), sin(z)) >>> trig_split(2*cos(x), -sin(x)) >>> trig_split(cos(x), -sqrt(3)*sin(x)) >>> trig_split(cos(x)*cos(y), sin(x)*sin(z)) >>> trig_split(cos(x)*cos(y), sin(x)*sin(y)) >>> trig_split(-sqrt(6)*cos(x), sqrt(2)*sin(x)*sin(y), two=True) Nc�,�g|]}t|����Sr\�rr�s r2r`ztrig_split.<locals>.<listcomp>���� '� '� '�1�G�A�J�J� '� '� 'r4rfc�6�g|]}|�����Sr\��as_exprr�s r2r`ztrig_split.<locals>.<listcomp>� �� *� *� *�A�A�I�I�K�K� *� *� *r4c�2�dx}}tj}|j�r4|���\}}t |j��dks|sdS|jrt |j��}n|g}|�d��}t|t��r|}n:t|t��r|}n"|j r|j tj ur||z}ndS|rd|d}t|t��r|r|}nB|}n?t|t��r|r|}n%|}n"|j r|j tj ur||z}ndS|tjur|nd||fSt|t��r|}nt|t��r|}|�|�dS|tjur|nd}|||fS)a�Return ``a`` as a tuple (r, c, s) such that ``a = (r or 1)*(c or 1)*(s or 1)``. Three arguments are returned (radical, c-factor, s-factor) as long as the conditions set by ``two`` are met; otherwise None is returned. If ``two`` is True there will be one or two non-None values in the tuple: c and s or c and r or s and r or s or c with c being a cosine function (if possible) else a sine, and s being a sine function (if possible) else oosine. If ``two`` is False then there will only be a c or s term in the tuple. ``two`` also require that either two cos and/or sin be present (with the condition that if the functions are the same the arguments are different or vice versa) or that a single cosine or a single sine be present with an optional radical. If the above conditions dictated by ``two`` are not met then None is returned. NrFr)rr:rxr�rVr9r{r^r8rr rJr�r�)r<r�r�r�r�r9rts r2� pow_cos_sinztrig_split.<locals>.pow_cos_sins���(� ��A� �U�� �8�% ��N�N�$�$�E�B���1�6�{�{�Q���c���t��x� ��A�F�|�|����s������ � �A��!�S�!�!� �����A�s�#�#� ������ �a�e�q�v�o�o��a�����t�� ���G���a��%�%�  �����������3�'�'� ����������X� �!�%�1�6�/�/��!�G�B�B��4��1�5���2�2�d�A�q�8� 8� ��3� � � ��A�A� ��3� � � ��A� �9��� �F��Q�U�?�?�R�R����1�a�x�r4rc��h|] }|j�� Sr\r!)rLr�s r2� <setcomp>ztrig_split.<locals>.<setcomp>\s��1�1�1�1���1�1�1r4c3�*�K�|] }|j�vV��dSrIr!)rLrmr9s �r2rNztrig_split.<locals>.<genexpr>]s)�����<�<�a�1�6�T�>�<�<�<�<�<�<r4r�FrFr�r�)r�r�r.r�r�rr�factors�quor8r rTr9rrr:r r�r�)r<rtr��ua�ubr�r�r�r�r�coa�ca�sa�cob�cb�sbr�r�r9s @r2r�r��s����f�~����� '� '��A�� '� '� '�D�A�q� �X�X�a�[�[�F�B�� �%�%��(�(� � � � �C��K�B���}�� �"�"� �V�V�A�M� "� "���S��� ��"�*� $� $� �V�V�A�M� "� "���S�� *� *�"�b�� *� *� *�D�A�q�?�?�?�D � �A�s���A��y����K�C��R�� �A�s���A��y����K�C��R� ��B��"���B��!4�!4��#&��B��R��#;� ��R��S�"�b��R�B�� �"7� �H�"�� �H�"���!�Q�V�$�$� ��4��B��A�F�1�I�q�v�a�y�*�Q��2D�2D�D�D�� T�3� T�� T�r� T�b� T�R� T��b�"�'�*�*�*�R���2I�2I�I�I��F�1�1��R��1�1�1���<�<�<�<�B��8�<�<�<�<�<���F��B��B�G�A�J���� �J�r�2�7�<S�<S�S�S� � �"� �� �r� � � ��Z�B�J�"�*��� �F� �H�"�� �H�"�� �6�Q�V� � � �F�� ��%�C�� ��%�C� �#�:�:� �6�M�C���B���q� �2�a�4��6� 6� ��W�� � � �1�S�5�L�C���B���q� �2�a�4��6� 6� ��W� � !� !� �1�S�5�L�C���B���q� �2�a�4��6� 6�"� !r4c�.�|jrt|j��dkrdS|j\}}|tjtjfvrDtj}|jr,|jdjr|jddkr | | }}| }|||fSd�|jD��\}}|�|��\}}|� |��� ��}tj|j vr$|� tj��}d}d}n;tj|j vr$|� tj��}d}d}ndx}}d�||fD��\}}|tjur||}}||}}|dkr| }| }|tjur|||fSdS)a�If ``e`` is a sum that can be written as ``g*(a + s)`` where ``s`` is ``+/-1``, return ``g``, ``a``, and ``s`` where ``a`` does not have a leading negative coefficient. Examples ======== >>> from sympy.simplify.fu import as_f_sign_1 >>> from sympy.abc import x >>> as_f_sign_1(x + 1) (1, x, 1) >>> as_f_sign_1(x - 1) (1, x, -1) >>> as_f_sign_1(-x + 1) (-1, x, -1) >>> as_f_sign_1(-x - 1) (-1, x, 1) >>> as_f_sign_1(2*x + 2) (2, x, 1) rFNrc�,�g|]}t|����Sr\r�r�s r2r`zas_f_sign_1.<locals>.<listcomp>�r�r4�����rfc�6�g|]}|�����Sr\r�r�s r2r`zas_f_sign_1.<locals>.<listcomp>�r�r4) rUrVr9rrr:rxr�r.r�r�r�r�) rXr<rtr�r�r�r�r�r�s r2rrvs���* �8��s�1�6�{�{�a�'�'��� �6�D�A�q��Q�]�A�E� "�"�"� �E�� �8� ���q� �+� ���q� �A� � ��2��r�q�A���A��!�Q�w�� '� '��� '� '� '�D�A�q� �X�X�a�[�[�F�B�� �%�%��(�(� � � � �C��}�� �"�"� �V�V�A�M� "� "�� �� ��� ��"�*� $� $� �V�V�A�M� "� "�� �� ���� ��R� *� *�"�b�� *� *� *�D�A�q��A�E�z�z��!�1���R�B�� �R�x�x��d���S���A�E�z�z��A�r�z���zr4c�.���fd�}t||��S)a2Replace all hyperbolic functions with trig functions using the Osborne rule. Notes ===== ``d`` is a dummy variable to prevent automatic evaluation of trigonometric/hyperbolic functions. References ========== .. [1] https://en.wikipedia.org/wiki/Hyperbolic_function c����t|t��s|S|jd}|js|�zn$t j�fd�|jD����}t|t ��rtt|��zSt|t��rt|��St|t��rtt|��zSt|t��rt|��tz St|t��rt!|��St|t"��rt%|��tz St'd|jz���)Nrc���g|]}|�z��Sr\r\)rLrmrcs �r2r`z'_osborne.<locals>.f.<locals>.<listcomp>�s���4I�4I�4I�Q�Q�q�S�4I�4I�4Ir4� unhandled %s)r8rr9rUrr�rrr rrrr!rr"rr#rr$�NotImplementedErrorrT)r1r<rcs �r2r=z_osborne.<locals>.f�s4����"�0�1�1� ��I� �G�A�J���x� J�A�a�C�C�S�^�4I�4I�4I�4I�!�&�4I�4I�4I�%J�%J�� �b�$� � � @��S��V�V�8�O� ��D� !� !� @��q�6�6�M� ��D� !� !� @��S��V�V�8�O� ��D� !� !� @��q�6�6�!�8�O� ��D� !� !� @��q�6�6�M� ��D� !� !� @��q�6�6�!�8�O�%�n�r�w�&>�?�?� ?r4r�rXrcr=s ` r2�_osborner��s1���"@�@�@�@�@�( �Q��?�?�r4c�.���fd�}t||��S)a1Replace all trig functions with hyperbolic functions using the Osborne rule. Notes ===== ``d`` is a dummy variable to prevent automatic evaluation of trigonometric/hyperbolic functions. References ========== .. [1] https://en.wikipedia.org/wiki/Hyperbolic_function c����t|t��s|S|jd��d���\}}|��t ji��|tzz}t|t��rt|��tz St|t��rt|��St|t��rt|��tz St|t��rt|��tzSt|t ��rt#|��St|t$��rt'|��tzSt)d|jz���)NrT)�as_Addr�)r8r&r9�as_independent�xreplacerr:rr rrrr!rr"rr#rr$rr�rT)r1�constr�r<rcs �r2r=z_osbornei.<locals>.f�s:����"�3�4�4� ��I��7�1�:�,�,�Q�t�,�<�<���q� �J�J��1�5�z� "� "�U�1�W� ,�� �b�#� � � @���7�7�1�9� � ��C� � � @���7�7�N� ��C� � � @���7�7�1�9� � ��C� � � @���7�7�1�9� � ��C� � � @���7�7�N� ��C� � � @���7�7�1�9� �%�n�r�w�&>�?�?� ?r4rr�s ` r2� _osborneir��s1��� @�@�@�@�@�( �Q��?�?�r4c������ddlm�ddlm�|�t ��}d�|D���|�t�����}d��D���t���t|�������fd�fS)a�Return an expression containing hyperbolic functions in terms of trigonometric functions. Any trigonometric functions initially present are replaced with Dummy symbols and the function to undo the masking and the conversion back to hyperbolics is also returned. It should always be true that:: t, f = hyper_as_trig(expr) expr == f(t) Examples ======== >>> from sympy.simplify.fu import hyper_as_trig, fu >>> from sympy.abc import x >>> from sympy import cosh, sinh >>> eq = sinh(x)**2 + cosh(x)**2 >>> t, f = hyper_as_trig(eq) >>> f(fu(t)) cosh(2*x) References ========== .. [1] https://en.wikipedia.org/wiki/Hyperbolic_function rr�)�collectc�.�g|]}|t��f��Sr\r)rLrs r2r`z!hyper_as_trig.<locals>.<listcomp>s �� (� (� (�Q�Q����L� (� (� (r4c��g|] \}}||f�� Sr\r\)rLrWrns r2r`z!hyper_as_trig.<locals>.<listcomp>!s �� $� $� $�t�q�!�Q��F� $� $� $r4c �����t|����t�������tj��SrI)r�r��dictr� ImaginaryUnit)r�r�rc�repsr�s ����r2r�zhyper_as_trig.<locals>.<lambda>%sG���'�'�(�(��!�Q��� � ��d���,�,�3.�3.�/0��+@�+@�r4) r�r��sympy.simplify.radsimpr��atomsr&r�r�rr�)r1�trigs�maskedr�rcr�r�s @@@@r2� hyper_as_trigr��s�������41�0�0�0�0�0�.�.�.�.�.�.� �H�H�*� +� +�E� (� (�%� (� (� (�D� �[�[��d��� $� $�F� %� $�t� $� $� $�D� ���A� �F�A� � �!@�!@�!@�!@�!@�!@�!@� @�@r4c��|�tt��s|Stt t |������S)a�Convert products and powers of sin and cos to sums. Explanation =========== Applied power reduction TRpower first, then expands products, and converts products to sums with TR8. Examples ======== >>> from sympy.simplify.fu import sincos_to_sum >>> from sympy.abc import x >>> from sympy import cos, sin >>> sincos_to_sum(16*sin(x)**3*cos(2*x)**2) 7*sin(x) - 5*sin(3*x) + 3*sin(5*x) - sin(7*x) )rrr r�r rs)�exprs r2� sincos_to_sumr�)s;��& �8�8�C�� � �.�� ��:�g�d�m�m�,�,�-�-�-r4)F)r�Fr�rI)NT)o� collectionsr�sympy.core.addr�sympy.core.exprr�sympy.core.exprtoolsrrr �sympy.core.functionr �sympy.core.mulr �sympy.core.numbersr r�sympy.core.powerr�sympy.core.singletonr�sympy.core.sortingr�sympy.core.symbolr�sympy.core.sympifyr�sympy.core.traversalr�(sympy.functions.combinatorial.factorialsr�%sympy.functions.elementary.hyperbolicrrrrrrr�(sympy.functions.elementary.trigonometricrr r!r"r#r$r%r&�sympy.ntheory.factor_r'�sympy.polys.polytoolsr(�sympy.strategies.treer)�sympy.strategies.corer*r+�sympyr,r3r?rBr�r�r�r�r�r�r�r�r�r�r�r�rrr0r5rBrJrTr]rargrsrvr{r�CTR1�CTR2�CTR3�CTR4r}r~r|r�r��fufuncsr��zip�locals�get�FUr�r�r�rr�r�r�r�r\r4r2�<module>r�s���#�#�#�#�#�#������� � � � � � �A�A�A�A�A�A�A�A�A�A�*�*�*�*�*�*�������$�$�$�$�$�$�$�$� � � � � � �"�"�"�"�"�"�&�&�&�&�&�&�#�#�#�#�#�#�&�&�&�&�&�&�*�*�*�*�*�*�=�=�=�=�=�=�<�<�<�<�<�<�<�<�<�<�<�<�<�<�<�<�<�<�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�/�/�/�/�/�/�(�(�(�(�(�(�(�(�(�(�(�(�1�1�1�1�1�1�1�1������� )�)�)����0���<q�q�q�q�h.�.�.�b5�5�5�@=�=�=�@B�B�B�B�*B�B�B�B�*���0H�H�H�H�V[�[�[�|-�-�-�-�`A�A�A�HM�M�M�M�`:�:�:�z � � � �Fx�x�x�v,�,�,�^t�t�t�nv�v�v�v�r����B����B���@����:+�+�+�\ .� .� .�"�<� �t�C�C���#�s�C��c�3��S�#�t�T�4���(�D�$��e�U�D�:� ;� ;�<�<��S�#�s�C��c�3��S�#�t�T�4���(�D�$��e�U�D� �c� �S�#�J��)�� ��S�z�C��:�s�+�,�� �3���x��e�S�9�8�D�� �e� �h��� �C��d�C��s�C�0�� �#�t�S�#�t�$��#�t�S�� �4��d�C��c�4�0� � ��3�2�X*�X*�X*�X*�v'�'�'�'�T !�"'����� �T�$�$�s�s�7�D�D���V�V�X�X�\�7�!;�!;�<�<�=�=� >� >�?�?����� ��s7�s7�s7�s7�l6�6�6�r%�%�%�P$�$�$�N(@�(@�(@�V.�.�.�.�.r4
Memory