� ��g`T����ddlmZmZmZmZmZddlmZmZm Z ddl m Z ddl m Z ddlmZmZmZddlmZmZd�Zdefd �Zd �Zd �Zdd �Zd�ZGd�de��Zd�Zd�Zdd�Z d�Z!d�Z"d�Z#d�Z$d�Z%dS)�)�Add�Expr�Mul�S�sympify)�_mexpand� count_ops� expand_mul)�default_sort_key)�Dummy)�root�sign�sqrt)�Poly�PolynomialErrorc�h�|jo+|jjot|j��tjuS)z/Return True if expr is a sqrt, otherwise False.)�is_Pow�exp� is_Rational�absr�Half)�exprs �i/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/simplify/sqrtdenest.py�is_sqrtr s*�� �;� K�4�8�/� K�C���M�M�Q�V�4K�K��returnc���|tjurdS|jrdS|js|jrt d�|jD����St|��rt|j ��dzSdS)a�Return the maximum depth of any square root argument of p. >>> from sympy.functions.elementary.miscellaneous import sqrt >>> from sympy.simplify.sqrtdenest import sqrt_depth Neither of these square roots contains any other square roots so the depth is 1: >>> sqrt_depth(1 + sqrt(2)*(1 + sqrt(3))) 1 The sqrt(3) is contained within a square root so the depth is 2: >>> sqrt_depth(1 + sqrt(2)*sqrt(1 + sqrt(3))) 2 �rc3�4K�|]}t|��V��dS�N�� sqrt_depth��.0�xs r� <genexpr>zsqrt_depth.<locals>.<genexpr>&s(����1�1�Q�:�a�=�=�1�1�1�1�1�1r) r� ImaginaryUnit�is_Atom�is_Add�is_Mul�max�argsrr"�base��ps rr"r"s��$ �A�O����q��y���q��x�2�1�8�2��1�1�!�&�1�1�1�1�1�1��q�z�z�&��!�&�!�!�A�%�%� �1rc���|jrdS|jrdSt|��s|jr |jjrt |j��S|js|j rtd�|j D����SdS)a�Return True if p is comprised of only Rationals or square roots of Rationals and algebraic operations. Examples ======== >>> from sympy.functions.elementary.miscellaneous import sqrt >>> from sympy.simplify.sqrtdenest import is_algebraic >>> from sympy import cos >>> is_algebraic(sqrt(2)*(3/(sqrt(7) + sqrt(5)*sqrt(2)))) True >>> is_algebraic(sqrt(2)*(3/(sqrt(7) + sqrt(5)*cos(2)))) False TFc3�4K�|]}t|��V��dSr )� is_algebraicr#s rr&zis_algebraic.<locals>.<genexpr>Cs(����3�3�q�<��?�?�3�3�3�3�3�3r) rr(rrr� is_Integerr2r-r)r*�allr,r.s rr2r2,s���  �}� ��t� ����u� �����q�x��A�E�$4���A�F�#�#�#� ���Q�X���3�3�A�F�3�3�3�3�3�3��urc���|dkrdgg}nn|dkr ddgddgddgg}n\|dkrgd�gd�gd�gd�gd �gd �gd �g}n>t|dz ��}d �|D��}d �|D��}|dg|dz zdgzgz|z}|S)ag Returns all possible subsets of the set (0, 1, ..., n-1) except the empty set, listed in reversed lexicographical order according to binary representation, so that the case of the fourth root is treated last. Examples ======== >>> from sympy.simplify.sqrtdenest import _subsets >>> _subsets(2) [[1, 0], [0, 1], [1, 1]] r�r�)rrr)rrr)rrr)rrr)rrr)rrr)rrrc��g|]}|dgz�� S)r�r#s r� <listcomp>z_subsets.<locals>.<listcomp>_��� !� !� !�!�a�1�#�g� !� !� !rc��g|]}|dgz�� S)rr9r#s rr:z_subsets.<locals>.<listcomp>`r;r)�_subsets)�n�a�b�a0�a1s rr=r=Hs��� �A�v�v��S�E��� �a�����V�a��V�a��V� $��� �a��� �Y�Y� � � �9�9�9� �Y�Y� � � �9�9�9�i�i�i� 9��� �Q��U�O�O�� !� !�q� !� !� !�� !� !�q� !� !� !�� �1�#�q�1�u�+���#�$� $�r� )�� �Hrr7c�~�t|��}t|��D]}t|��}||kr|cS|}�|S)a�Denests sqrts in an expression that contain other square roots if possible, otherwise returns the expr unchanged. This is based on the algorithms of [1]. Examples ======== >>> from sympy.simplify.sqrtdenest import sqrtdenest >>> from sympy import sqrt >>> sqrtdenest(sqrt(5 + 2 * sqrt(6))) sqrt(2) + sqrt(3) See Also ======== sympy.solvers.solvers.unrad References ========== .. [1] https://web.archive.org/web/20210806201615/https://researcher.watson.ibm.com/researcher/files/us-fagin/symb85.pdf .. [2] D. J. Jeffrey and A. D. Rich, 'Symplifying Square Roots of Square Roots by Denesting' (available at https://www.cybertester.com/data/denest.pdf) )r �range� _sqrtdenest0)r�max_iter�i�zs r� sqrtdenestrIesQ��6 �d� � �D� �8�_�_���� �� � �� �1�9�9��K�K�K���� �Krc��ddlm}t|��}|jr|tjtjf}�n�|j�rRt|jt���}d�|D��}td�|D����r#||��\}}}|||f}t|��Sd�t|��D��}t|t���} | ddkrg}�n�| \} } } |�| ��}|�| ��tj}|jrtg} g}|jD]@}t#|��| kr| �|���+|�|���At'j| ��}t'j|��}g}|g}|D]�}|d| kr|�|d���*|d}||kr|�d���N|jrft|j��}||vr2|�|��|�t'|�����|�|d����|�|d����t-|�}t-|�}|||dzf}n;|���\}}t1|��rtj||dzf}ng}t|��S) a�Return [a, b, r] for p.match(a + b*sqrt(r)) where, in addition to matching, sqrt(r) also has then maximal sqrt_depth among addends of p. Examples ======== >>> from sympy.functions.elementary.miscellaneous import sqrt >>> from sympy.simplify.sqrtdenest import _sqrt_match >>> _sqrt_match(1 + sqrt(2) + sqrt(2)*sqrt(3) + 2*sqrt(1+sqrt(5))) [1 + sqrt(2) + sqrt(6), 2, 1 + sqrt(5)] r)� split_surds��keyc��g|]}|dz��S)r6r9r#s rr:z_sqrt_match.<locals>.<listcomp>�s��&�&�&�1�!�Q�$�&�&�&rc3�2K�|]}|jo|jV��dSr )r� is_positive)r$�sqs rr&z_sqrt_match.<locals>.<genexpr>�s+����B�B�R�r�~�0�"�.�B�B�B�B�B�Brc�8�g|]\}}t|��||f��Sr9r!)r$rGr%s rr:z_sqrt_match.<locals>.<listcomp>�s)�� @� @� @�t�q�!�j��m�m�Q�� "� @� @� @rrr6)�sympy.simplify.radsimprKr� is_Numberr�Zeror)�sortedr,r r4�list� enumerater+�pop�Oner*r"�appendr� _from_args�remover� as_coeff_Mulr)r/rK�res�pargs�sqargs�rr@r?�v�nmax�depth�_rG�bv�rvr%rB�b1�x1�x1argss r� _sqrt_matchrl�s���3�2�2�2�2�2��� � �A��{�<��!�&�!�&�!��� ��:��q�v�#3�4�4�4��&�&��&�&�&�� �B�B�6�B�B�B� B� B� �!�k�!�n�n�G�A�q�!��Q��'�C���9�9� � A� @�y��/?�/?� @� @� @���1�*�+�+�+�� ��7�a�<�<��C�C��K�E�1�a�� � �!� � �A� �E�E�!�H�H�H���A��x� '�������%�%�A�!�!�}�}�u�,�,�� � �!� � � � �� � �!� � � � ��N�2�&�&���N�2�&�&���B���B�� ,� ,���Q�4�%�<�<��I�I�a��d�O�O�O�O��1��B��Q�w�w�� � �!� � � � ��9�,�%)�"�'�]�]�F� �F�{�{� &� � �a� 0� 0� 0� "� � �#�v�,� 7� 7� 7� 7� "� � �!�A�$������I�I�a��d�O�O�O�O��R��A��R��A��a��A��,�C�C��~�~�����1� �1�:�:� ��6�1�a��d�#�C�C��C� ��9�9�rc��eZdZdS)�SqrtdenestStopIterationN)�__name__� __module__� __qualname__r9rrrnrn�s�������Drrnc��t|��r�|���\}}|tjur�|jjr�t |jjt���}t|��dkr9td�|D����r t|��S#t$rYnwxYwtttd�|D�������}t!|��Sd�||fD��\}}||z St#|t��r�g}g}|jD]C}|���\}}|�|��|�|���Dtd�|D����r)td�|D����rt)||��St#|t*��r|j}|r|jd�|D���S|S) z+Returns expr after denesting its arguments.rLr6c3�*K�|]}|dzjV��dS)r6N)r3r#s rr&z_sqrtdenest0.<locals>.<genexpr>�s)����(I�(I�q�!�Q�$�):�(I�(I�(I�(I�(I�(Irc�,�g|]}t|����Sr9�rEr#s rr:z _sqrtdenest0.<locals>.<listcomp>�s��*I�*I�*I�q�<��?�?�*I�*I�*Irc�,�g|]}t|����Sr9ru)r$rGs rr:z _sqrtdenest0.<locals>.<listcomp>�s��4�4�4��L��O�O�4�4�4rc3�$K�|] }|jV�� dSr )r)r$�cs rr&z_sqrtdenest0.<locals>.<genexpr>�s$����)�)��q�}�)�)�)�)�)�)rc3�4K�|]}t|��V��dSr )r)r$�args rr&z_sqrtdenest0.<locals>.<genexpr>�s(����1O�1O�3�'�#�,�,�1O�1O�1O�1O�1O�1Orc�,�g|]}t|����Sr9ru)r$r?s rr:z _sqrtdenest0.<locals>.<listcomp>�s��=�=�=�1�|�A���=�=�=r)r�as_numer_denomrrZr-r)rVr,r �lenr4�_sqrtdenest_recrnrrr� _sqrtdenest1� isinstancer^r[� _sqrt_ratcombr�func)rr>�dr,�csrzrxr?s rrErE�s���t�}�}���"�"�$�$���1� ���:�:��v�}� M��a�f�k�/?�@�@�@���t�9�9�q�=�=�S�(I�(I�D�(I�(I�(I�%I�%I�=��.�q�1�1�1��2�����������H�S�*I�*I�D�*I�*I�*I�%J�K�K�L�L����%�%� %�4�4�a��V�4�4�4�D�A�q��Q�3�J��$���� +� �����9� � �C��#�#�%�%�D�A�q� �I�I�a�L�L�L� �K�K��N�N�N�N� �)�)�b�)�)�)� )� )� +�c�1O�1O�$�1O�1O�1O�.O�.O� +� ��T�*�*� *��$����?��y�� � ?��4�9�=�=��=�=�=�>� >� �Ks�B� B*�)B*c�2�ddlm}m}m}|jst |��S|jdkr2td��tt|j ����zS||j��\}}}|t|��z}||kr||}}t|dz|dzz ��}t|j ��dkr�||��\}}} |t|��z}|| kr| |} }t|dz| dzz ��} tt| ����} tt|| z����} || | ��\} }t| td��z | |td��zz z��}ntt|����}t|��dkrt�||z}t|j ��t|j ��kr,t|��t|j��krt�t t|����}t|��dkrt�|||��\} }|td��z | |td��zz z}||��}t|��S)alHelper that denests the square root of three or more surds. Explanation =========== It returns the denested expression; if it cannot be denested it throws SqrtdenestStopIteration Algorithm: expr.base is in the extension Q_m = Q(sqrt(r_1),..,sqrt(r_k)); split expr.base = a + b*sqrt(r_k), where `a` and `b` are on Q_(m-1) = Q(sqrt(r_1),..,sqrt(r_(k-1))); then a**2 - b**2*r_k is on Q_(m-1); denest sqrt(a**2 - b**2*r_k) and so on. See [1], section 6. Examples ======== >>> from sympy import sqrt >>> from sympy.simplify.sqrtdenest import _sqrtdenest_rec >>> _sqrtdenest_rec(sqrt(-72*sqrt(2) + 158*sqrt(5) + 498)) -sqrt(10) + sqrt(2) + 9 + 9*sqrt(5) >>> w=-6*sqrt(55)-6*sqrt(35)-2*sqrt(22)-2*sqrt(14)+2*sqrt(77)+6*sqrt(10)+65 >>> _sqrtdenest_rec(sqrt(w)) -sqrt(11) - sqrt(7) + sqrt(2) + 3*sqrt(5) r)�radsimp�rad_rationalizerK�����r6r)rSr�r�rKrrIr-rr~rr}r,rr"rnr )rr�r�rK�gr?r@�c2rBri�c2_1�c_1�d_1�num�denrx�acr�rbs rr~r~sr��4M�L�L�L�L�L�L�L�L�L� �;� ��$���� �y�1�}�}��B�x�x���d�i�Z�(8�(8�9�9�9�9��k�$�)�$�$�G�A�q�!� �$�q�'�'� �A��1�u�u��!�1�� �!�Q�$��A��+� � �B� �2�7�|�|�a����K��O�O� ��2�r� ��Q���Z�� ��7�7����B���A���A�� �&�&���d�4�j�j�)�)���d�2��8�n�n�-�-��"�?�2�s�+�+���S� �S��a���[�3��D��G�G� �#4�4� 5� 5��� ��b��� "� "���!�}�}�q���%�%� �Q��B� �2�7�|�|�s�4�9�~�~�%�%� �R�=�=�I�d�i�0�0� 0� 0�)� )��4��8�8���A��!�}�}�q���%�%���q�!�$�$�H�C�� �$�q�'�'� �C��T�!�W�W��%�%�A���� � �A� �A�;�;�rTc�V�ddlm}t|��s|S|j}|jr|St |��}|s|S|\}}}t |dz|dz|zz ��}|jr|jrt||||��}|�|Snvt | |z��} t| ��} | jr7tt ||z��||| ��}|�|t|d��z Snt|||��}|�|S|rt|��s|St|||||��} | r| S||||g} t||dz��g| dt!|����d}| d�|S|�Dt!|��t!|��kr"t#|��t#|��kr|S|S|S)zcReturn denested expr after denesting with simpler methods or, that failing, using the denester.r�r�r6N�r)�sympy.simplify.simplifyr�rr-r(rlrrrP�_sqrt_numeric_denestrr �_sqrt_symbolic_denestr2�sqrt_biquadratic_denest� _denesterr"r ) r�denesterr�r?�valr@rb�d2rH�dr2�drr_�av0s rrrAs���0�/�/�/�/�/� �4�=�=��� � � �A��y��� � �a�.�.�C� ��� ��G�A�q�!� �!�Q�$��A��a��-� � �B� �~�� �>� (�$�Q��1�b�1�1�A��}���� �B�3�q�5�/�/�C��c���B��~� (�(��!�A�#����1�c�B�B���=��T�!�Q�Z�Z�<�'�� "�!�Q�� *� *�� �=��H� ��<��-�-��� � !�$��1�a�� 4� 4�C� ��� � �a��B�-�C��7�7�4��7�#�#�$�c�1�j��.>�.>�?�?��B�A� �1�v�~�� ��}� �a�=�=�J�t�,�,� ,� ,��1��� �$���1O�1O��K��� �Krc���tt|||f��\}}}t|��}|sdS|\}}}|�r(tdd���} t |�t |��|dz|z |z ��|��}n#t$rYdSwxYw|���dkr�|� ��\} } } | |z } t| dzd| z| zz ��� d��rkt | t |��| d| zz zdzz��} | j r3ttj| �������} | SdSdSdS)a4Given an expression, sqrt(a + b*sqrt(b)), return the denested expression or None. Explanation =========== If r = ra + rb*sqrt(rr), try replacing sqrt(rr) in ``a`` with (y**2 - ra)/rb, and if the result is a quadratic, ca*y**2 + cb*y + cc, and (cb + b)**2 - 4*ca*cc is 0, then sqrt(a + b*sqrt(r)) can be rewritten as sqrt(ca*(sqrt(r) + (cb + b)/(2*ca))**2). Examples ======== >>> from sympy.simplify.sqrtdenest import _sqrt_symbolic_denest, sqrtdenest >>> from sympy import sqrt, Symbol >>> from sympy.abc import x >>> a, b, r = 16 - 2*sqrt(29), 2, -10*sqrt(29) + 55 >>> _sqrt_symbolic_denest(a, b, r) sqrt(11 - 2*sqrt(29)) + sqrt(5) If the expression is numeric, it will be simplified: >>> w = sqrt(sqrt(sqrt(3) + 1) + 1) + 1 + sqrt(2) >>> sqrtdenest(sqrt((w**2).expand())) 1 + sqrt(2) + sqrt(1 + sqrt(1 + sqrt(3))) Otherwise, it will only be simplified if assumptions allow: >>> w = w.subs(sqrt(3), sqrt(x + 3)) >>> sqrtdenest(sqrt((w**2).expand())) sqrt((sqrt(sqrt(sqrt(x + 3) + 1) + 1) + 1 + sqrt(2))**2) Notice that the argument of the sqrt is a square. If x is made positive then the sqrt of the square is resolved: >>> _.subs(x, Symbol('x', positive=True)) sqrt(sqrt(sqrt(x + 3) + 1) + 1) + 1 + sqrt(2) N�yT)�positiver6r�r)�maprrlr r�subsrr�degree� all_coeffsr�equals� is_numberrr\�as_content_primitive) r?r@rb�rval�ra�rb�rrr��newa�ca�cb�ccrHs rr�r�|s���T�'�A�q�!�9�%�%�G�A�q�!� �q�>�>�D� ���t��J�B��B� � � �#�� %� %� %�� �����t�B�x�x�!�Q�$��)�R��8�8�!�<�<�D�D��� � � ��4�4� ���� �;�;�=�=�A� � ����*�*�J�B��B� �!�G�B���A���"��R���(�(�/�/��2�2� ���T�!�W�W�r�1�R�4�y�0�1�4�4�5�5���;�K� ����0F�0F�0H�0H�!I�!I�J�J�A��� � � � � � s� :B� B�Bc��t|��}||z}t|��t|��dzks |dzjr�t|��t|��}}||cxkrdkrnndx}}|t||z��z|t||z ��zztd��zdz }|���SdS)z~Helper that denest $\sqrt{a + b \sqrt{r}}, d^2 = a^2 - b^2 r > 0$ If it cannot be denested, it returns ``None``. rr6r�N)rr"rr�expand) r?r@rbr�r��s�s1�s2r_s rr�r��s��� �R���A� �A��A� �!�}�}�z�!�}�}�q�(�(�(�Q��T�,>�(��a���$�q�'�'�B�� ��>�>�>�>�r�>�>�>�>�>��K�B���D��Q��K�K��"�t�A��E�{�{�"2�2�d�1�g�g�=��A���z�z�|�|�� )�(rc ��ddlm}m}|dks |dks|rt|j��dkrdS|||fD]#}|jD]}|dz} | jr| jsdS��$ttt||��������} t| ��dkrdS|dz | dz z|dz | dz z g\} } | | fD]�}tt|����} t| ��dkr�2||td| z����\}}||z }| |t|��zz}|dkr| }t|��cSdS)a�denest expr = sqrt(a + b*sqrt(r)) where a, b, r are linear combinations of square roots of positive rationals on the rationals (SQRR) and r > 0, b != 0, d2 = a**2 - b**2*r > 0 If it cannot denest it returns None. Explanation =========== Search for a solution A of type SQRR of the biquadratic equation 4*A**4 - 4*a*A**2 + b**2*r = 0 (1) sqd = sqrt(a**2 - b**2*r) Choosing the sqrt to be positive, the possible solutions are A = sqrt(a/2 +/- sqd/2) Since a, b, r are SQRR, then a**2 - b**2*r is a SQRR, so if sqd can be denested, it is done by _sqrtdenest_rec, and the result is a SQRR. Similarly for A. Examples of solutions (in both cases a and sqd are positive): Example of expr with solution sqrt(a/2 + sqd/2) but not solution sqrt(a/2 - sqd/2): expr = sqrt(-sqrt(15) - sqrt(2)*sqrt(-sqrt(5) + 5) - sqrt(3) + 8) a = -sqrt(15) - sqrt(3) + 8; sqd = -2*sqrt(5) - 2 + 4*sqrt(3) Example of expr with solution sqrt(a/2 - sqd/2) but not solution sqrt(a/2 + sqd/2): w = 2 + r2 + r3 + (1 + r3)*sqrt(2 + r2 + 5*r3) expr = sqrt((w**2).expand()) a = 4*sqrt(6) + 8*sqrt(2) + 47 + 28*sqrt(3) sqd = 29 + 20*sqrt(3) Define B = b/2*A; eq.(1) implies a = A**2 + B**2*r; then expr**2 = a + b*sqrt(r) = (A + B*sqrt(r))**2 Examples ======== >>> from sympy import sqrt >>> from sympy.simplify.sqrtdenest import _sqrt_match, sqrt_biquadratic_denest >>> z = sqrt((2*sqrt(2) + 4)*sqrt(2 + sqrt(2)) + 5*sqrt(2) + 8) >>> a, b, r = _sqrt_match(z**2) >>> d2 = a**2 - b**2*r >>> sqrt_biquadratic_denest(z, a, b, r, d2) sqrt(2) + sqrt(sqrt(2) + 2) + 2 r)r�r�r6Nr) rSr�r�r"r-r,r3rPrrIr)rr?r@rbr�r�r�r%r��y2�sqdrj�x2�A�Bn�Bd�BrHs rr�r��s���`@�?�?�?�?�?�?�?��A�v�v��a���q��J�t�y�$9�$9�A�$=�$=��t���A�Y������ � �A��A��B��=� ��� ��t�t�t� � � �:�d�7�7�2�;�;�/�/�0�0� 1� 1�C��#�������t���c�C��E�k�1�Q�3��Q��;� '�F�B���"�X� � �� �t�A�w�w� � �� �a�=�=�1� � � � ���H�Q�q�S�M�M�2�2���B� �r�E�� ��$�q�'�'� �M�� �q�5�5���A���{�{���� �4rc � ����ddlm}||krdS|d�dS|d��td��D����r�tt �����D]�t t ��fd�tt �����D�����}��d��dkr �dr| }t|��}|j r|�fcS��t�d��dgt ���zfSd�|d�#|dd �g}|d �|d �g}d|d<n�ttdd ��D������}|D])} | d r���| d krd|d<dS�!| d ��*��(t�d��dgt ���zfS�fd �|D���gz}t|||dz|��\} ��sdSt�fd �tt �����D����s8|d} t| dt | d| z��z���fSt ��fd�tt �����D���}t|��} d�vrY��d��t ���dz kr0�t ���dz r| d | d<| d | d<�t ����st | d| z��} | dkr(t�d��dgt ���zfSt#| ��t#���dzks| d zjsd|d<dSt't| ��d���} t#| ��t#���dzksd|d<dS|d| z ��} t | td ��z | dt���z| ztd ��z z��}|�fSt | d�z��| z}|dkr(t�d��dgt ���zfSt)t ���d��t|��}}t |td ��|zz | d|ztd ��|zz z���fS)a�Denests a list of expressions that contain nested square roots. Explanation =========== Algorithm based on <http://www.almaden.ibm.com/cs/people/fagin/symb85.pdf>. It is assumed that all of the elements of 'nested' share the same bottom-level radicand. (This is stated in the paper, on page 177, in the paragraph immediately preceding the algorithm.) When evaluating all of the arguments in parallel, the bottom-level radicand only needs to be denested once. This means that calling _denester with x arguments results in a recursive invocation with x+1 arguments; hence _denester has polynomial complexity. However, if the arguments were evaluated separately, each call would result in two recursive invocations, and the algorithm would have exponential complexity. This is discussed in the paper in the middle paragraph of page 179. rr�)NNrNc3�$K�|] }|jV�� dSr )rT)r$r>s rr&z_denester.<locals>.<genexpr>6s$����,�,��� �,�,�,�,�,�,rc�0��g|]}�|� �|��Sr9r9�r$rG�f�nesteds ��rr:z_denester.<locals>.<listcomp>8s&���H�H�H�Q�1�Q�4�H�v�a�y�H�H�Hrr�r6r7c�,�g|]}t|����Sr9)rl)r$rs rr:z_denester.<locals>.<listcomp>Hs ��'M�'M�'M�d� �D�(9�(9�'M�'M�'Mrc�x��g|]6}t|ddz��t�|ddzz��z ��7S)rr6r)r)r$rc�Rs �rr:z_denester.<locals>.<listcomp>TsU���<�<�<�/0� ��!��a��(�(���!�A�$��'� �*�*�+�<�<�<rc3�(�K�|] }�|V�� dSr r9)r$rGr�s �rr&z_denester.<locals>.<genexpr>Ys'�����4�4�A�1�Q�4�4�4�4�4�4�4rc�0��g|]}�|� �|��Sr9r9r�s ��rr:z_denester.<locals>.<listcomp>]s&���D�D�D�A�q��t�D�f�Q�i�D�D�DrF)r�r�)r�r�r4r=r}rrrD�countrrrW�filterr��anyrl�indexr"rTrr )r�r��h�max_depth_levelr�r/�sqp�values�nested2rcr��vad�sqvad�sqvad1r_r��FRr�r�r�s` @@rr�r�s�����.0�/�/�/�/�/��?����z� �1�v�~��z� �A��� �,�,�V�,�,�,� ,� ,� ��#�f�+�+�&�&� � �A���H�H�H�H�H�%��A���-�-�H�H�H�I�J�J�A��w�w�q�z�z�A�~�~�!�B�%�~��B���q�'�'�C��� ��A�v� � � � ��F�2�J���!��S��[�[��0�0� �� �q�6� ��"�1�"�g�Y�F��A��A��1�v�q�k�G��C��F�F��&��'M�'M�f�'M�'M�'M�N�N�O�O�F�� !� !���Q�4�!��}���!��9�9�%)�C��F�#-�:�:�%��a�D����y��F�2�J�'�'�!��S��[�[��8�8�<�<�<�<�4:�<�<�<�?@�c�B�G���#�q�1�u�o�>�>���1�� ��:��4�4�4�4��s�6�{�{�!3�!3�4�4�4�4�4�! I��r� �A���!��x��!��Q��/�/�/�0�0�!�3� 3��D�D�D�D�D��s�6�{�{�);�);�D�D�D�E�A��A���A��A�v�v�!�'�'�!�*�*�s�6�{�{�Q��6�6�1�S��[�[�1�_�;M�6��!��u��!���!��u��!���S��[�[�>� I��q��t�a�x�(�(���!�8�8���r� �+�+�a�S��V���_�<�<�!�#���*�Q�-�-�!�*;�;�;��Q��)�<�!�C��F�%�:�$�T�#�Y�Y��?�?�?��"�5�)�)�Z��]�]�Q�->�>�>�!�C��F�%�:� ���5��)�)���u�T�!�W�W�}��!��T�!�W�W� �V�0C�D��G�G�0K�L�M�M���A�v� ��a��d�1�f�%�%��)����7�7���r� �+�+�a�S��V���_�<�<��X�a�[�[�!�,�,�d�2�h�h�A����4��7�7�2�:���1��b��$�q�'�'�!�)�1D� D�E�E�q�H�Hrc�H�ddlm}d�}||��}|�!td�t||��D���S|\}}}|�|��}|�|��||} ||xx|||z| jz ��z cc<t ||��S)a#Denest rational combinations of radicals. Based on section 5 of [1]. Examples ======== >>> from sympy import sqrt >>> from sympy.simplify.sqrtdenest import sqrtdenest >>> z = sqrt(1+sqrt(3)) + sqrt(3+3*sqrt(3)) - sqrt(10+6*sqrt(3)) >>> sqrtdenest(z) 0 rr�c�B�t|��}t|dz ��D]|}t|dz|��D]f}||j}||j}t||z��}t t |����}|t |��kr |||fccS�g�}dS)Nr)r}rDr-rrIr)r?r>rG�jr�r�r/r�s r�findz_sqrt_ratcomb.<locals>.find�s��� ��F�F���q�1�u��� #� #�A��1�q�5�!�_�_� #� #���q�T�Y���q�T�Y���R�"�W�%�%���t�A�w�w�'�'����Q���<�<��a��7�N�N�N�N�N� �  #� #� #rNc��g|] \}}||z�� Sr9r9)r$rxrzs rr:z!_sqrt_ratcomb.<locals>.<listcomp>�s ��9�9�9���C�Q��W�9�9�9r)rSr�r�ziprYr-r�) r�r,r�r��indicesr��i1�i2r�rBs rr�r�}s���/�.�.�.�.�.� #� #� #��d�4�j�j�G����9�9�3�r�4�=�=�9�9�9�:�:��I�A�r�2� ������B��H�H�R�L�L�L� �b��B��r�F�F�F�g�g�b�1�f�r�w�&�'�'�'�F�F�F� ��T� "� "�"rN)r7)T)&� sympy.corerrrrr�sympy.core.functionrr r �sympy.core.sortingr �sympy.core.symbolr �sympy.functionsr rr� sympy.polysrrr�intr"r2r=rIrl� StopIterationrnrEr~rr�r�r�r�r�r9rr�<module>r�s���1�1�1�1�1�1�1�1�1�1�1�1�1�1�?�?�?�?�?�?�?�?�?�?�/�/�/�/�/�/�#�#�#�#�#�#�,�,�,�,�,�,�,�,�,�,�-�-�-�-�-�-�-�-�L�L�L�  �S� � � � �:���8 � � �:!�!�!�!�HL�L�L�^ � � � � �m� � � �"�"�"�J=�=�=�@8�8�8�8�v<�<�<�~���(G�G�G�TaI�aI�aI�H)#�)#�)#�)#�)#r
Memory