� ��gq3���dZddlZddlmZddlmZddlmZmZm Z ddl m Z ddl m Z mZmZddlmZdd lmZdd lmZGd �d e��Zd d�d�Zd�Zdd�Zd�ZdS)z,Solvers of systems of polynomial equations. �N)�S)�default_sort_key)�Poly�groebner�roots)�parallel_poly_from_expr)�ComputationFailed�PolificationFailed�CoercionFailed��rcollect)� postfixes)� filldedentc��eZdZdZdS)� SolveFailedz.Raised when solver's conditions were not met. N)�__name__� __module__� __qualname__�__doc__���e/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/solvers/polysys.pyrrs������8�8�8�8rrF��strictc��� t|g|�Ri|��\}}n0#t$r#}tdt|��|���d}~wwxYwt|��t|j��cxkrdkrjnng|\}}t d�|���|���zD����r" t|||��S#t$rYnwxYwt|||���S)a� Return a list of solutions for the system of polynomial equations or else None. Parameters ========== seq: a list/tuple/set Listing all the equations that are needed to be solved gens: generators generators of the equations in seq for which we want the solutions strict: a boolean (default is False) if strict is True, NotImplementedError will be raised if the solution is known to be incomplete (which can occur if not all solutions are expressible in radicals) args: Keyword arguments Special options for solving the equations. Returns ======= List[Tuple] a list of tuples with elements being solutions for the symbols in the order they were passed as gens None None is returned when the computed basis contains only the ground. Examples ======== >>> from sympy import solve_poly_system >>> from sympy.abc import x, y >>> solve_poly_system([x*y - 2*y, 2*y**2 - x**2], x, y) [(0, 0), (2, -sqrt(2)), (2, sqrt(2))] >>> solve_poly_system([x**5 - x + y**3, y**2 - 1], x, y, strict=True) Traceback (most recent call last): ... UnsolvableFactorError �solve_poly_systemN�c3�"K�|] }|dkV�� dS)rNr)�.0�is r� <genexpr>z$solve_poly_system.<locals>.<genexpr>Hs&����A�A�!�q�A�v�A�A�A�A�A�Arr) rr r �len�gens�all� degree_list�solve_biquadraticr� solve_generic) �seqrr#�args�polys�opt�exc�f�gs rrrs%��ZD�,�S�@�4�@�@�@�4�@�@� ��s�s�� �D�D�D�� 3�S��X�X�s�C�C�C�����D���� �5�z�z�S���]�]�'�'�'�'�a�'�'�'�'�'����1� �A�A�q�}�}��������@�A�A�A� A� A� � �(��A�s�3�3�3��� � � ��� ���� ���F� 3� 3� 3�3s$�� A�>�A�;C � C�Cc�p� �t||g��}t|��dkr|djrdSt|��dkrt�|j\}� |\}}|�|��jst�t ||d���}� fd�t|�����D��}|� d��}tt|�������}� fd �tj ||��D��} t| t� ��S) a�Solve a system of two bivariate quadratic polynomial equations. Parameters ========== f: a single Expr or Poly First equation g: a single Expr or Poly Second Equation opt: an Options object For specifying keyword arguments and generators Returns ======= List[Tuple] a list of tuples with elements being solutions for the symbols in the order they were passed as gens None None is returned when the computed basis contains only the ground. Examples ======== >>> from sympy import Options, Poly >>> from sympy.abc import x, y >>> from sympy.solvers.polysys import solve_biquadratic >>> NewOption = Options((x, y), {'domain': 'ZZ'}) >>> a = Poly(y**2 - 4 + x, y, x, domain='ZZ') >>> b = Poly(y*2 + 3*x - 7, y, x, domain='ZZ') >>> solve_biquadratic(a, b, NewOption) [(1/3, 3), (41/27, 11/9)] >>> a = Poly(y + x**2 - 3, y, x, domain='ZZ') >>> b = Poly(-y + x - 4, y, x, domain='ZZ') >>> solve_biquadratic(a, b, NewOption) [(7/2 - sqrt(29)/2, -sqrt(29)/2 - 1/2), (sqrt(29)/2 + 7/2, -1/2 + sqrt(29)/2)] �rNrF)�expandc�0��g|]}t|�����Srr )r�expr�ys �r� <listcomp>z%solve_biquadratic.<locals>.<listcomp>�s#���=�=�=�T�x��a� � �=�=�=r�����c�F��g|]\}}|��|��|f��Sr)�subs)r�q_root�p_rootr4s �rr5z%solve_biquadratic.<locals>.<listcomp>�s<���6�6�6�n�f�f�&�+�+�a��(�(�&�1�6�6�6r��key)rr"� is_groundrr#�gcdrr�keys�ltrim�list� itertools�product�sortedr) r-r.r+�G�x�p�q�p_roots�q_roots� solutionsr4s @rr&r&Qs)���R �!�Q����A� �1�v�v��{�{�q��t�~�{��t� �1�v�v��{�{��� �8�D�A�q� �D�A�q� �5�5��8�8� ���� �Q��%� � � �A�=�=�=�=�U�1�X�X�]�]�_�_�=�=�=�G� ���� � �A��5��8�8�=�=�?�?�#�#�G�6�6�6�6��"�7�G�4�4�6�6�6�I� �)�!1� 2� 2� 2�2rc������d��d��d ����fd� � �||jd���}n#t$rt�wxYw|�t|t���SdS) a� Solve a generic system of polynomial equations. Returns all possible solutions over C[x_1, x_2, ..., x_m] of a set F = { f_1, f_2, ..., f_n } of polynomial equations, using Groebner basis approach. For now only zero-dimensional systems are supported, which means F can have at most a finite number of solutions. If the basis contains only the ground, None is returned. The algorithm works by the fact that, supposing G is the basis of F with respect to an elimination order (here lexicographic order is used), G and F generate the same ideal, they have the same set of solutions. By the elimination property, if G is a reduced, zero-dimensional Groebner basis, then there exists an univariate polynomial in G (in its last variable). This can be solved by computing its roots. Substituting all computed roots for the last (eliminated) variable in other elements of G, new polynomial system is generated. Applying the above procedure recursively, a finite number of solutions can be found. The ability of finding all solutions by this procedure depends on the root finding algorithms. If no solutions were found, it means only that roots() failed, but the system is solvable. To overcome this difficulty use numerical algorithms instead. Parameters ========== polys: a list/tuple/set Listing all the polynomial equations that are needed to be solved opt: an Options object For specifying keyword arguments and generators strict: a boolean If strict is True, NotImplementedError will be raised if the solution is known to be incomplete Returns ======= List[Tuple] a list of tuples with elements being solutions for the symbols in the order they were passed as gens None None is returned when the computed basis contains only the ground. References ========== .. [Buchberger01] B. Buchberger, Groebner Bases: A Short Introduction for Systems Theorists, In: R. Moreno-Diaz, B. Buchberger, J.L. Freire, Proceedings of EUROCAST'01, February, 2001 .. [Cox97] D. Cox, J. Little, D. O'Shea, Ideals, Varieties and Algorithms, Springer, Second Edition, 1997, pp. 112 Raises ======== NotImplementedError If the system is not zero-dimensional (does not have a finite number of solutions) UnsolvableFactorError If ``strict`` is True and not all solution components are expressible in radicals Examples ======== >>> from sympy import Poly, Options >>> from sympy.solvers.polysys import solve_generic >>> from sympy.abc import x, y >>> NewOption = Options((x, y), {'domain': 'ZZ'}) >>> a = Poly(x - y + 5, x, y, domain='ZZ') >>> b = Poly(x + y - 3, x, y, domain='ZZ') >>> solve_generic([a, b], NewOption) [(-1, 4)] >>> a = Poly(x - 2*y + 5, x, y, domain='ZZ') >>> b = Poly(2*x - y - 3, x, y, domain='ZZ') >>> solve_generic([a, b], NewOption) [(11/3, 13/3)] >>> a = Poly(x**2 + y, x, y, domain='ZZ') >>> b = Poly(x + y*4, x, y, domain='ZZ') >>> solve_generic([a, b], NewOption) [(0, 0), (1/4, -1/16)] >>> a = Poly(x**5 - x + y**3, x, y, domain='ZZ') >>> b = Poly(y**2 - 1, x, y, domain='ZZ') >>> solve_generic([a, b], NewOption, strict=True) Traceback (most recent call last): ... UnsolvableFactorError c�h�|���D]}t|dd���rdS�dS)z8Returns True if 'f' is univariate in its last variable. Nr6FT)�monoms�any)r-�monoms r�_is_univariatez%solve_generic.<locals>._is_univariate�sB���X�X�Z�Z� � �E��5��"��:��� ��u�u� ��trc��|�||i��}|�|��dkr|�d���}|S)z:Replace generator with a root so that the result is nice. rF)�deep)�as_expr�degreer1)r-�gen�zerorGs r� _subs_rootz!solve_generic.<locals>._subs_rootsD�� �I�I�s�D�k� "� "�� �8�8�C�=�=�A� � ����e��$�$�A��rFc����t|��t|��cxkrdkrLnnItt|d|d���������}d�|D��St ||d���}t|��dkr|djr|sgSdStt �|����}t|��t|��krttd �����t|��dkr|� ��}nttd �����|j }|d}tt|� |�����������}|sgSt|��dkr d �|D��Sg}|D]s} g} |dd�} |dd�D]2} �| || ��} | tj ur| �| ���3�| | ��D]}|�|| fz����t|rBt|d��t|��krttd �����|S) z/Recursively solves reduced polynomial systems. r0rr6rc��g|]}|f��Srr�rrWs rr5z@solve_generic.<locals>._solve_reduced_system.<locals>.<listcomp>���.�.�.��T�G�.�.�.rT�r*Nzv only zero-dimensional systems supported (finite number of solutions) c��g|]}|f��Srrr[s rr5z@solve_generic.<locals>._solve_reduced_system.<locals>.<listcomp>4r\r)r"rArr?rr=�filter�NotImplementedErrorr�popr#r@r�Zero�append)�systemr#�entry�zeros�basis� univariater-rVrKrW� new_system�new_gens�b�eq�solutionrQ�_solve_reduced_systemrXrs ����rrnz,solve_generic.<locals>._solve_reduced_system s���� �v�;�;�#�d�)�)� (� (� (� (�q� (� (� (� (� (���v�a�y�$�r�(�6�B�B�B�G�G�I�I�J�J�E�.�.��.�.�.� .����T�2�2�2�� �u�:�:��?�?�u�Q�x�1�?�� �� ��t��&���7�7�8�8� � �u�:�:��D� � � !� !�%�j�2�'�'��� � �z�?�?�a� � ���� � �A�A�%�j�2�'�'��� � �v���2�h���U�1�7�7�3�<�<��7�7�7�<�<�>�>�?�?��� ��I� �u�:�:��?�?�.�.��.�.�.� .�� �� 5� 5�D��J��C�R�C�y�H��3�B�3�Z� *� *���Z��3��-�-���Q�V�#�#��%�%�b�)�)�)��1�1�*�h�G�G� 5� 5��� � ��T�G�!3�4�4�4�4� 5� � ��Y�q�\�*�*�c�$�i�i�7�7�%�j�2�'�'��� ��rT)reNr;�F)r#r r`rDr)r*r+r�resultrQrnrXs ` @@@rr'r'�s�������H������A�A�A�A�A�A�A�A�A�F"�&�&�u�c�h�d�C�C�C���� �"�"�"�!�!�"�������f�"2�3�3�3�3��s�)�;c �0��t||d���}tt|����}|�d��}|�-t |��D]\}}|�|��||<�|d�d��|dd�}}|����|���}�fd�|D��} t|dd���} t|dd���} t| | ��D�]G\} } t��}| D�]-\}�gtt| |����}}|D]�}| f| z}|j |�r�|� | ��dkru|�| ���t|����}|� | ��|� ��kr|�|����t#|d �� ��}|���}|D]<}|js��|��}n�}|�|f|z|f���=��/|} ��It| ��} t | ��D] \}\}}|| |<�t+| t,� ��S) a� Solve a polynomial system using Gianni-Kalkbrenner algorithm. The algorithm proceeds by computing one Groebner basis in the ground domain and then by iteratively computing polynomial factorizations in appropriately constructed algebraic extensions of the ground domain. Parameters ========== polys: a list/tuple/set Listing all the equations that are needed to be solved gens: generators generators of the equations in polys for which we want the solutions args: Keyword arguments Special options for solving the equations Returns ======= List[Tuple] A List of tuples. Solutions for symbols that satisfy the equations listed in polys Examples ======== >>> from sympy import solve_triangulated >>> from sympy.abc import x, y, z >>> F = [x**2 + y + z - 1, x + y**2 + z - 1, x + y + z**2 - 1] >>> solve_triangulated(F, x, y, z) [(0, 0, 1), (0, 1, 0), (1, 0, 0)] References ========== 1. Patrizia Gianni, Teo Mora, Algebraic Solution of System of Polynomial Equations using Groebner Bases, AAECC-5 on Applied Algebra, Algebraic Algorithms and Error-Correcting Codes, LNCS 356 247--257, 1989 Tr]�domainNrr6r0c���h|]}|f�f��Srr)rrW�doms �r� <setcomp>z%solve_triangulated.<locals>.<setcomp>�s���2�2�2�D�4�'�3��2�2�2rc�*�|���S)N)rU)�hs r�<lambda>z$solve_triangulated.<locals>.<lambda>�s��Q�X�X�Z�Z�rr;)rrA�reversed�get� enumerate� set_domainr@� get_domain� ground_rootsr�zip�set� has_only_gensrU�eval�dictrc�min� is_Rational�algebraic_field�addrDr)r*r#r)rErrr r.r-rfrK�var_seq�vars_seq�var�vars� _solutions�values�H�mapping�_varsrwrGrW�dom_zerorm�_rts @r�solve_triangulatedr�Us����Z ���D�)�)�)�A� �X�a�[�[���A� �X�X�h� � �F� ���a�L�L� (� (�D�A�q��<�<��'�'�A�a�D�D� �Q�4�:�:�b�>�>�1�Q�R�R�5�q�A� �,�,�.�.�C� �N�N� � �E�2�2�2�2�E�2�2�2�I��t�C�R�C�y�!�!�G���a�b�b��"�"�H���(�+�+��� ��T��U�U� �$� =� =�K�F�C��T�#�d�F�"3�"3�4�4�w�A�� $� $����� ��"�1�?�E�*�$�q�x�x��}�}��/A�/A����� � �)�)�$�w�-�-�8�8�A��x�x��}�}���� � �2�2����� � � ���A�/�/�0�0�0�A��N�N�$�$�E�� =� =���'�#�"�2�2�4�8�8�H�H�"�H������&� 0�(�;�<�<�<�<�  =�� � ��Y���I�%�i�0�0� � ���=�H�a�� �!� � � �)�!1� 2� 2� 2�2rro)rrB� sympy.corer�sympy.core.sortingr� sympy.polysrrr�sympy.polys.polytoolsr�sympy.polys.polyerrorsr r r �sympy.simplifyr �sympy.utilitiesr�sympy.utilities.miscr� Exceptionrrr&r'r�rrr�<module>r�sf��2�2�����������/�/�/�/�/�/�-�-�-�-�-�-�-�-�-�-�9�9�9�9�9�9�(�(�(�(�(�(�(�(�(�(�#�#�#�#�#�#�%�%�%�%�%�%�+�+�+�+�+�+�9�9�9�9�9�)�9�9�9�*/�;4�;4�;4�;4�;4�|@3�@3�@3�F~4�~4�~4�~4�B`3�`3�`3�`3�`3r
Memory