
    Цiq3                         S r SSKrSSKJr  SSKJr  SSKJrJrJ	r	  SSK
Jr  SSKJrJrJr  SSKJr  SS	KJr  SS
KJr   " S S\5      rSS.S jrS rSS jrS rg)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                       \ rS rSrSrSrg)SolveFailed   z.Raised when solver's conditions were not met.  N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       T/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/sympy/solvers/polysys.pyr   r      s    8r   r   Fstrictc                    [        U /UQ70 UD6u  pE[        U5      [        UR                  5      s=:X  a  S:X  aJ  O  OGUu  px[        S UR                  5       UR                  5       -    5       5      (       a   [        XxU5      $ [        XEUS9$ ! [         a  n[        S[        U 5      U5      eSnAff = f! [         a     N>f = f)aH  
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   *   #    U  H	  oS :*  v   M     g7f)r   Nr   ).0is     r   	<genexpr>$solve_poly_system.<locals>.<genexpr>H   s     A@!Av@s   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@
 5zS]'a'Aq}}@AAA(s33 F33  D 3SXsCCD  s)   B :B8 
B5B00B58
CCc                    [        X/5      n[        U5      S:X  a  US   R                  (       a  g[        U5      S:w  a  [        eUR                  u  pEUu  pgUR                  U5      R                  (       d  [        e[        XdSS9n[        U5      R                  5        Vs/ s H  n[        X5      PM     n	nUR                  S5      n[        [        U5      R                  5       5      n
[        R                  " X5       VVs/ s H  u  pUR                  X[5      U4PM     nnn[        U[         S9$ s  snf s  snnf )	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   )r0   r1   r.   Gxypqexprp_rootsq_rootsq_rootp_root	solutionss                 r   r)   r)   Q   s   R 	!A
1v{qt~~
1v{88DADA558Q% A-21X]]_=_Tx _G=	A58==?#G ""7464 7Ef&++a(&14  6 )!122 >
6s   #D6D;c                    ^^^^ S mS mSUUUU4S jjm T" XR                   SS9nUb  [        U[        S9$ g! [         a    [        ef = f)	aN  
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                 \    U R                  5        H  n[        USS 5      (       d  M    g   g)z8Returns True if 'f' is univariate in its last variable. Nr5   FT)monomsany)r0   monoms     r   _is_univariate%solve_generic.<locals>._is_univariate   s)    XXZE5":   r   c                 r    U R                  X05      nU R                  U5      S:  a  UR                  SS9nU$ )z:Replace generator with a root so that the result is nice. r   F)deep)as_exprdegreer4   )r0   genzerorD   s       r   
_subs_root!solve_generic.<locals>._subs_root   s7    IIsk"88C=Ae$Ar   c                 R  > [        U 5      [        U5      s=:X  a  S:X  a>  O  O;[        [        U S   US   TS9R                  5       5      nU Vs/ s H  oD4PM     sn$ [	        XSS9n[        U5      S:X  a  US   R
                  (       a
  U(       d  / $ g[        [        TU5      5      n[        U5      [        U5      :  a  [        [        S5      5      e[        U5      S:X  a  UR                  5       nO[        [        S5      5      eUR                  nUS   n[        [        UR                  U5      TS9R                  5       5      nU(       d  / $ [        U5      S:X  a  U Vs/ s H  oD4PM     sn$ / n	U Hh  n/ n
USS nUSS  H2  nT" XU5      nU[        R                  Ld  M!  U
R                  U5        M4     T" X5       H  nU	R                  X4-   5        M     Mj     U	(       a/  [        U	S   5      [        U5      :w  a  [        [        S5      5      eU	$ s  snf s  snf )	z/Recursively solves reduced polynomial systems. r3   r   r5   r   Tr-   Nzv
                only zero-dimensional systems supported
                (finite number of solutions)
                )r%   r<   r   r:   r   r8   filterNotImplementedErrorr   popr&   r;   r   Zeroappend)systemr&   entryzerosrX   basis
univariater0   rW   rK   
new_systemnew_gensbeqsolutionrQ   _solve_reduced_systemrY   r   s                  r   rl   ,solve_generic.<locals>._solve_reduced_system	  s   v;#d)(q( vay$r(6BGGIJE(-.G..T2u:?uQx11	&78
u:D	!%j 2 '  
 z?a A%j 2 '  
 vv2h U1773<7<<>?Iu:?(-.G..	DJCRyH3BZ-QVV#%%b)	   2*G  G!34 H  Yq\*c$i7%j 2 '   w /J /s   H(H$T)rc   Nr6   F)r&   r   r^   r@   r   )r-   r.   r   resultrQ   rl   rY   s     ` @@@r   r*   r*      sb    HA AF"&uhhdC f"233   "!!"s	   : Ac           	      |   [        XSS9n[        [        U5      5      nUR                  S5      nUb'  [	        U5       H  u  pVUR                  U5      X5'   M     US   R                  S5      USS p7UR                  5       nUR                  5       n	U	 V
s1 s H  o4U4iM	     nn
[        USS 5      n[        USS 5      n[        X5       GH2  u  p[        5       nU GH  u  nn/ [        [        UU5      5      nnU H  nU4U-   nUR                  " U6 (       d  M  UR                  U5      S:w  d  M6  UR                  U5      R                  [        U5      5      nUR                  U5      UR                  5       :X  d  M  UR!                  U5        M     [#        US S	9nUR                  5       n	U	 H?  n
U
R$                  (       d  UR'                  U
5      nOUnUR)                  U
4U-   U45        MA     GM     UnGM5     [        U5      n[	        U5       H  u  nu  nnUX'   M     [+        U[,        S	9$ s  sn
f )
ad  
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\   domainNr   r5   r3   c                 "    U R                  5       $ )N)rV   )hs    r   <lambda>$solve_triangulated.<locals>.<lambda>  s
    QXXZr   r6   )r   r<   reversedget	enumerate
set_domainr;   
get_domainground_rootsr   zipsethas_only_gensrV   evaldictra   minis_Rationalalgebraic_fieldaddr@   r   )r-   r&   r,   rA   rq   r"   r1   r0   domrd   rX   rK   var_seqvars_seqvarvars
_solutionsvaluesHmapping_varsrs   rD   dom_zerork   _s                             r   solve_triangulatedr   U  s
   Z 	D)AXa[AXXhFaLDA<<'AD ! Q4::b>1QR5q
,,.CNNE,12ED'3EI2tCRy!Gab"H+	U
$KFCT#dF"34wA??E**qxx}/A))$w-8Axx}
2  A/0ANN$E''"2248H"H& 0(;<  %. 	5 ,8 YI%i0=Ha	 1 )!122M 3s   H9rn   )r   r=   
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   