
    wgq3                         d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ  G d de      ZdddZd ZddZd Zy)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                       e Zd ZdZy)SolveFailedz.Raised when solver's conditions were not met. N)__name__
__module____qualname____doc__     Z/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/solvers/polysys.pyr   r      s    8r   r   Fstrictc                   	 t        | g|i |\  }}t        |      t        |j                        cxk(  rdk(  rGn nD|\  }}t        d |j                         |j                         z   D              r	 t        |||      S t        |||      S # t        $ r}t        dt        |       |      d}~ww xY w# t        $ r Y @w xY w)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   c              3   &   K   | ]	  }|d k    yw)r   Nr   ).0is     r   	<genexpr>z$solve_poly_system.<locals>.<genexpr>H   s     A!qAvAs   r   )
r   r
   r	   lengensalldegree_listsolve_biquadraticr   solve_generic)	seqr   r"   argspolysoptexcfgs	            r   r   r      s    ZD,S@4@4@
s 5zS]'a'1Aq}}@AA(As33 F33  D 3SXsCCD  s)   B 5B7 	B4B//B47	CCc                 x   t        | |g      }t        |      dk(  r|d   j                  ryt        |      dk7  rt        |j                  \  }}|\  }}|j                  |      j                  st        t        ||d      }t        |      j                         D cg c]  }t        ||       }	}|j                  d      }t        t        |      j                               }
t        j                  |
|	      D cg c]  \  }}|j                  ||      |f }}}t        |t               S c c}w c c}}w )	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)]
       r   Nr   F)expandkey)r   r!   	is_groundr   r"   gcdr   r   keysr   ltrimlist	itertoolsproductsubssortedr   )r,   r-   r*   Gxypqexprp_rootsq_rootsq_rootp_root	solutionss                 r   r%   r%   Q   s   R 	!QA
1v{qt~~
1v{88DAqDAq558Q% A-21X]]_=Txa =G=	A58==?#G ""7G46nff&++a(&1 6I 6 )!122 >
6s   D1?D6c                     d d dfd		  | |j                   d      }|t        |t              S y# t        $ r t        w xY w)	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                 N    | j                         D ]  }t        |dd       s y y)z8Returns True if 'f' is univariate in its last variable. Nr1   FT)monomsany)r,   monoms     r   _is_univariatez%solve_generic.<locals>._is_univariate   s,    XXZ 	E5":	 r   c                 x    | j                  ||i      }| j                  |      dk\  r|j                  d      }|S )z:Replace generator with a root so that the result is nice. r   F)deep)as_exprdegreer0   )r,   genzeror@   s       r   
_subs_rootz!solve_generic.<locals>._subs_root   s9    IIsDk"88C=Ae$Ar   c                    t        |       t        |      cxk(  rdk(  r?n n<t        t        | d   |d         j                               }|D cg c]  }|f c}S t	        | |d      }t        |      dk(  r|d   j
                  r|sg S yt        t        |            }t        |      t        |      k  rt        t        d            t        |      dk(  r|j                         }nt        t        d            |j                  }|d   }t        t        |j                  |            j                               }|sg S t        |      dk(  r|D cg c]  }|f c}S g }	|D ]d  }g }
|dd }|dd D ]0  } |||      }|t        j                  us |
j                  |       2  |
|      D ]  }|	j                  ||fz           f |	r.t        |	d         t        |      k7  rt        t        d            |	S c c}w c c}w )	z/Recursively solves reduced polynomial systems. r/   r   r1   r   Tr)   Nzv
                only zero-dimensional systems supported
                (finite number of solutions)
                )r!   r8   r   r6   r   r4   filterNotImplementedErrorr   popr"   r7   r   Zeroappend)systemr"   entryzerosrS   basis
univariater,   rR   rG   
new_systemnew_gensbeqsolutionrM   _solve_reduced_systemrT   r   s                  r   rf   z,solve_generic.<locals>._solve_reduced_system	  s    v;#d)(q( vay$r(6BGGIJE(-.TG..T2u:?uQx11	&78
u:D	!%j 2 '  
 z?a A%j 2 '  
 vv2h U1773<7<<>?Iu:?(-.TG..	 	5DJCRyH3BZ *3-QVV#%%b)	* 2*hG 5  TG!345	5 Yq\*c$i7%j 2 '   w /J /s   
H
H
T)r]   Nr2   F)r"   r   rX   r<   r   )r)   r*   r   resultrM   rf   rT   s     ` @@@r   r&   r&      sd    HA AF"&uchhdC f"233   "!!"s	   > Ac           	      `   t        | |d      }t        t        |            }|j                  d      }|'t	        |      D ]  \  }}|j                  |      ||<    |d   j                  d      |dd }}|j                         }|j                         }	|	D 
ch c]  }
|
f|f	 }}
t        |dd       }t        |dd       }t        ||      D ]   \  }}t               }|D ]  \  }}g t        t        ||            }}|D ]  }|f|z   } |j                  | s|j                  |      dk7  s.|j                  |      j                  t        |            }|j                  |      |j                         k(  sz|j!                  |        t#        |d 	      }|j                         }	|	D ]9  }
|
j$                  s|j'                  |
      }n|}|j)                  |
f|z   |f       ;  |}# t        |      }t	        |      D ]  \  }\  }}|||<    t+        |t,        	      S c c}
w )
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

    TrV   domainNr   r1   r/   c                 "    | j                         S )N)rQ   )hs    r   <lambda>z$solve_triangulated.<locals>.<lambda>  s    QXXZ r   r2   )r   r8   reversedget	enumerate
set_domainr7   
get_domainground_rootsr   zipsethas_only_gensrQ   evaldictr[   minis_Rationalalgebraic_fieldaddr<   r   )r)   r"   r(   r=   rj   r   r-   r,   domr^   rS   rG   var_seqvars_seqvarvars
_solutionsvaluesHmapping_varsrl   r@   dom_zerore   _s                             r   solve_triangulatedr   U  s6   Z 	D)AXa[AXXhFaL 	(DAq<<'AaD	( Q4::b>1QR5qA
,,.CNNE,12D4'32I2tCRy!Gab"H(+ 	TU
$ 	=KFCT#dF"34wA $"1??E*qxx}/A))$w-8Axx}
2$ A/0ANN$E =''"2248H"H& 0(;<=	=. 	58 YI%i0  =Ha	!  )!122M 3s   H+rg   )r   r9   
sympy.corer   sympy.core.sortingr   sympy.polysr   r   r   sympy.polys.polytoolsr   sympy.polys.polyerrorsr	   r
   r   sympy.simplifyr   sympy.utilitiesr   sympy.utilities.miscr   	Exceptionr   r   r%   r&   r   r   r   r   <module>r      sX    2   / - - 9( ( # % +9) 9 */ ;4|@3F~4B`3r   