� ��g�.� ���UddlmZddlmZddlmZmZmZmZm Z m Z m Z ddl m Z ddlmZddlmZmZdd�Zd �Zd �Zd �Zd �Zd �Zd�Zd�Zd�Zd�Zeeeeeeeed�Zded<dS)�)� annotations)�Callable)�S�Add�Expr�Basic�Mul�Pow�Rational)� fuzzy_not)�Boolean)�ask�QTc���t|t��s|S|js�fd�|jD��}|j|�}t |d��r|����}|�|S|jj}t� |d��}|�|S||���}|�||kr|St|t��s|St|���S)a Simplify an expression using assumptions. Explanation =========== Unlike :func:`~.simplify` which performs structural simplification without any assumption, this function transforms the expression into the form which is only valid under certain assumptions. Note that ``simplify()`` is generally not done in refining process. Refining boolean expression involves reducing it to ``S.true`` or ``S.false``. Unlike :func:`~.ask`, the expression will not be reduced if the truth value cannot be determined. Examples ======== >>> from sympy import refine, sqrt, Q >>> from sympy.abc import x >>> refine(sqrt(x**2), Q.real(x)) Abs(x) >>> refine(sqrt(x**2), Q.positive(x)) x >>> refine(Q.real(x), Q.positive(x)) True >>> refine(Q.positive(x), Q.real(x)) Q.positive(x) See Also ======== sympy.simplify.simplify.simplify : Structural simplification without assumptions. sympy.assumptions.ask.ask : Query for boolean expressions using assumptions. c�0��g|]}t|�����S�)�refine)�.0�arg� assumptionss ��h/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/assumptions/refine.py� <listcomp>zrefine.<locals>.<listcomp>4s#���>�>�>�S��s�K�(�(�>�>�>�� _eval_refineN) � isinstancer�is_Atom�args�func�hasattrr� __class__�__name__� handlers_dict�getrr)�exprrr�ref_expr�name�handler�new_exprs ` rrr s����J �d�E� "� "��� � �<� �>�>�>�>�D�I�>�>�>���t�y�$����t�^�$�$���$�$�[�1�1�� � ��O� �>� "�D�����d�+�+�G���� ��w�t�[�)�)�H���d�h�.�.�� � �h�� %� %���� �(�K� (� (�(rc�D��ddlm}|jd}tt j|�����r1t tt j|�������r|Stt j|�����r| St|t��r~�fd�|jD��}g}g}|D]H}t||��r!|� |jd���3|� |���It|�|t|���zSdS)aF Handler for the absolute value. Examples ======== >>> from sympy import Q, Abs >>> from sympy.assumptions.refine import refine_abs >>> from sympy.abc import x >>> refine_abs(Abs(x), Q.real(x)) >>> refine_abs(Abs(x), Q.positive(x)) x >>> refine_abs(Abs(x), Q.negative(x)) -x r��Absc�J��g|]}tt|������� Sr)r�abs)r�ars �rrzrefine_abs.<locals>.<listcomp>bs)��� ;� ;� ;�Q�V�C��F�F�K� (� (� ;� ;� ;rN) �$sympy.functions.elementary.complexesr+rrr�realr �negativerr �append)r$rr+r�r�non_abs�in_abs�is ` r� refine_absr7Gs5���"9�8�8�8�8�8� �)�A�,�C� �1�6�#�;�;� �$�$�� �c�!�*�S�/�/�;�7�7� 8� 8��� � �1�:�c�?�?�K�(�(���t� ��#�s��� 1� ;� ;� ;� ;�#�(� ;� ;� ;������� "� "�A��!�S�!�!� "�� � �a�f�Q�i�(�(�(�(����q�!�!�!�!��G�}�s�s�3��<�0�0�0�0� 1� 1rc��ddlm}ddlm}t |j|��rst tj|jj d��|��rAt tj |j ��|��r|jj d|j zSt tj|j��|���r�|jj r�t tj |j ��|��rt|j��|j zSt tj|j ��|��r-||j��t|j��|j zzSt |j t��rHt |jt ��r.t|jj��|jj |j zzS|jt"ju�r�|j j�rx|}|j ���\}}t+|��}t+��}t+��}t-|��} |D]q} t tj | ��|��r|�| ���:t tj| ��|��r|�| ���r||z}t-|��dzr||z}|t"jzdz} n ||z}|dz} | |kst-|��| kr&|�| ��|jt3|�z}d|j z} t tj | ��|��r| ���r | |jz} | jr�| ���\} }|jr�|jt"jur�t tj|j ��|��r| dzdz } t tj | ��|��r|j|j zSt tj| ��|��r|j|j dzzS|j|j | zzS||kr|SdSdSdSdS)as Handler for instances of Pow. Examples ======== >>> from sympy import Q >>> from sympy.assumptions.refine import refine_Pow >>> from sympy.abc import x,y,z >>> refine_Pow((-1)**x, Q.real(x)) >>> refine_Pow((-1)**x, Q.even(x)) 1 >>> refine_Pow((-1)**x, Q.odd(x)) -1 For powers of -1, even parts of the exponent can be simplified: >>> refine_Pow((-1)**(x+y), Q.even(x)) (-1)**y >>> refine_Pow((-1)**(x+y+z), Q.odd(x) & Q.odd(z)) (-1)**y >>> refine_Pow((-1)**(x+y+2), Q.odd(x)) (-1)**(y + 1) >>> refine_Pow((-1)**(x+3), True) (-1)**(x + 1) rr*)�sign��N)r/r+�sympy.functionsr9r�baserrr0r�even�exp� is_numberr-�oddr r r� NegativeOne�is_Add� as_coeff_add�set�len�add�Oner�could_extract_minus_sign� as_two_terms�is_Pow�integer)r$rr+r9�old�coeff�terms� even_terms� odd_terms�initial_number_of_terms�t� new_coeff�e2r6�ps r� refine_PowrWms��89�8�8�8�8�8�$�$�$�$�$�$��$�)�S�!�!�1� �q�v�d�i�n�Q�'�(�(�+� 6� 6� 1��A�F�4�8�$�$�k�2�2� 1��9�>�!�$���0� 0� �1�6�$�)� � �k�*�*�? � �9� � D��1�6�$�(�#�#�[�1�1� 2��4�9�~�~���1�1��1�5���?�?�K�0�0� D��t�D�I����T�Y���4�8�)C�C�C� �d�h�� )� )� I��$�)�S�)�)� I��4�9�>�*�*�t�y�}�t�x�/G�H�H� �9�� � %� %��x��4 ��� $�x�4�4�6�6� ��u��E� � �� �U�U� ��E�E� �*-�e�*�*�'��)�)�A��1�6�!�9�9�k�2�2�)�"���q�)�)�)�)��Q�U�1�X�X�{�3�3�)�!� � �a�(�(�(����#���y�>�>�A�%�*��Y�&�E�!&����!� 3�I�I��Y�&�E� %�� �I���%�%��U���6M�)M�)M��I�I�i�(�(�(��9�s�E�{�3�D��t�x�Z���q�v�b�z�z�;�/�/�(��2�2�4�4�(��d�i����9� >��?�?�,�,�D�A�q��x�>�A�F�a�m�$;�$;��q�y���/�/��=�=�>�!"�Q��� �A�"�1�6�!�9�9�k�:�:�>�'+�y�!�%�'7� 7�!$�Q�U�1�X�X�{�!;�!;�>�'+�y�1�5�1�9�'=� =�'+�y�1�5�1�9�'=� =��$�;�;��K�? �? � &� %�4 �4 �f�;rc��ddlm}|j\}}tt j|��t j|��z|��r|||z ��Stt j|��t j|��z|��r|||z ��tj z Stt j|��t j|��z|��r|||z ��tj zStt j |��t j|��z|��r tj Stt j|��t j |��z|��rtj dz Stt j|��t j |��z|��rtj dz Stt j |��t j |��z|��r tj S|S)a� Handler for the atan2 function. Examples ======== >>> from sympy import Q, atan2 >>> from sympy.assumptions.refine import refine_atan2 >>> from sympy.abc import x, y >>> refine_atan2(atan2(y,x), Q.real(y) & Q.positive(x)) atan(y/x) >>> refine_atan2(atan2(y,x), Q.negative(y) & Q.negative(x)) atan(y/x) - pi >>> refine_atan2(atan2(y,x), Q.positive(y) & Q.negative(x)) atan(y/x) + pi >>> refine_atan2(atan2(y,x), Q.zero(y) & Q.negative(x)) pi >>> refine_atan2(atan2(y,x), Q.positive(y) & Q.zero(x)) pi/2 >>> refine_atan2(atan2(y,x), Q.negative(y) & Q.zero(x)) -pi/2 >>> refine_atan2(atan2(y,x), Q.zero(y) & Q.zero(x)) nan r)�atanr:) �(sympy.functions.elementary.trigonometricrYrrrr0�positiver1r�Pi�zero�NaN)r$rrY�y�xs r� refine_atan2ra�s���2>�=�=�=�=�=� �9�D�A�q� �1�6�!�9�9�q�z�!�}�}� $�k�2�2���t�A��E�{�{�� �Q�Z��]�]�Q�Z��]�]� *�K� 8� 8� ��t�A��E�{�{�Q�T�!�!� �Q�Z��]�]�Q�Z��]�]� *�K� 8� 8� ��t�A��E�{�{�Q�T�!�!� �Q�V�A�Y�Y���A��� &� � 4� 4� ��t� � �Q�Z��]�]�Q�V�A�Y�Y� &� � 4� 4���t�A�v� � �Q�Z��]�]�Q�V�A�Y�Y� &� � 4� 4����u�Q�w�� �Q�V�A�Y�Y������ "�K� 0� 0���u� �� rc���|jd}ttj|��|��r|Sttj|��|��r t jSt||��S)a  Handler for real part. Examples ======== >>> from sympy.assumptions.refine import refine_re >>> from sympy import Q, re >>> from sympy.abc import x >>> refine_re(re(x), Q.real(x)) x >>> refine_re(re(x), Q.imaginary(x)) 0 r)rrrr0� imaginaryr�Zero� _refine_reim�r$rrs r� refine_rerg�sa�� �)�A�,�C� �1�6�#�;�;� �$�$��� � �1�;�s� � �[�)�)���v� � ��k� *� *�*rc���|jd}ttj|��|��r tjSttj|��|��rtj |zSt||��S)a Handler for imaginary part. Explanation =========== >>> from sympy.assumptions.refine import refine_im >>> from sympy import Q, im >>> from sympy.abc import x >>> refine_im(im(x), Q.real(x)) 0 >>> refine_im(im(x), Q.imaginary(x)) -I*x r) rrrr0rrdrc� ImaginaryUnitrerfs r� refine_imrjsl�� �)�A�,�C� �1�6�#�;�;� �$�$���v� � �1�;�s� � �[�)�)�'��� �3�&�&� ��k� *� *�*rc���|jd}ttj|��|��r tjSttj|��|��r tjSdS)a" Handler for complex argument Explanation =========== >>> from sympy.assumptions.refine import refine_arg >>> from sympy import Q, arg >>> from sympy.abc import x >>> refine_arg(arg(x), Q.positive(x)) 0 >>> refine_arg(arg(x), Q.negative(x)) pi rN)rrrr[rrdr1r\)r$r�rgs r� refine_argrm+sV�� ��1��B� �1�:�b�>�>�;�'�'���v� � �1�:�b�>�>�;�'�'���t� � �4rc�n�|�d���}||krt||��}||kr|SdS)NT)�complex)�expandr)r$r�expanded�refineds rrereBsD���{�{�T�{�*�*�H��4�����;�/�/�� �h� � ��N� �4rc��|jd}ttj|��|��r tjSttj|����r\ttj|��|��r tjSttj |��|��r tj Sttj |����rt|� ��\}}ttj|��|��r tj Sttj |��|��r tj S|S)a* Handler for sign. Examples ======== >>> from sympy.assumptions.refine import refine_sign >>> from sympy import Symbol, Q, sign, im >>> x = Symbol('x', real = True) >>> expr = sign(x) >>> refine_sign(expr, Q.positive(x) & Q.nonzero(x)) 1 >>> refine_sign(expr, Q.negative(x) & Q.nonzero(x)) -1 >>> refine_sign(expr, Q.zero(x)) 0 >>> y = Symbol('y', imaginary = True) >>> expr = sign(y) >>> refine_sign(expr, Q.positive(im(y))) I >>> refine_sign(expr, Q.negative(im(y))) -I r)rrrr]rrdr0r[rHr1rBrc� as_real_imagri)r$rr�arg_re�arg_ims r� refine_signrwMs ��0 �)�A�,�C� �1�6�#�;�;� �$�$���v� � �1�6�#�;�;���!� �q�z�#��� � ,� ,� ��5�L� �q�z�#��� � ,� ,� !��=� � �1�;�s� � ���$��)�)�+�+���� �q�z�&�!�!�;� /� /� #��?� "� �q�z�&�!�!�;� /� /� $��O�#� #� �Krc��ddlm}|j\}}}tt j|��|��r&||z ���r|S||||��SdS)aU Handler for symmetric part. Examples ======== >>> from sympy.assumptions.refine import refine_matrixelement >>> from sympy import MatrixSymbol, Q >>> X = MatrixSymbol('X', 3, 3) >>> refine_matrixelement(X[0, 1], Q.symmetric(X)) X[0, 1] >>> refine_matrixelement(X[1, 0], Q.symmetric(X)) X[0, 1] r)� MatrixElementN)�"sympy.matrices.expressions.matexprryrrr� symmetricrI)r$rry�matrixr6�js r�refine_matrixelementr~vs{��A�@�@�@�@�@��9�L�F�A�q� �1�;�v� � � �,�,�+� ��E� +� +� -� -� ��K��}�V�Q��*�*�*�+�+r)r+r �atan2�re�imrr9ryz*dict[str, Callable[[Expr, Boolean], Expr]]r"N)T)� __future__r�typingr� sympy.corerrrrr r r �sympy.core.logicr �sympy.logic.boolalgr �sympy.assumptionsrrrr7rWrargrjrmrerwr~r"�__annotations__rrr�<module>r�s���"�"�"�"�"�"�"�������>�>�>�>�>�>�>�>�>�>�>�>�>�>�>�>�>�>�&�&�&�&�&�&�'�'�'�'�'�'�$�$�$�$�$�$�$�$�9)�9)�9)�9)�x#1�#1�#1�La �a �a �H*�*�*�Z+�+�+�.+�+�+�,���.���&�&�&�R+�+�+�. � � � � � � �)� =� =� � � � � � � r
Memory