o
    8Va,                     @   s   d 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mZ ddlmZ G d	d
 d
eZdd Zdd Zdd Zdd ZdS )z,Solvers of systems of polynomial equations.     )S)Polygroebnerroots)parallel_poly_from_expr)ComputationFailedPolificationFailedCoercionFailedrcollect)default_sort_key	postfixes)
filldedentc                   @   s   e Zd ZdZdS )SolveFailedz-Raised when solver's conditions weren't met. N)__name__
__module____qualname____doc__ r   r   7/usr/lib/python3/dist-packages/sympy/solvers/polysys.pyr      s    r   c              
   O   s   zt | g|R i |\}}W n ty# } ztdt| |d}~ww t|t|j  kr3dkrYn n$|\}}tdd | |  D rYzt|||W S  tyX   Y nw t	||S )a  
    Solve a system of polynomial equations.

    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
    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 seq

    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_systemN   c                 s   s    | ]}|d kV  qdS )r   Nr   ).0ir   r   r   	<genexpr>9   s    z$solve_poly_system.<locals>.<genexpr>)
r   r   r   lengensallZdegree_listsolve_biquadraticr   solve_generic)seqr   argspolysoptexcfgr   r   r   r      s    "
r   c                    s   t | |g}t|dkr|d jrdS t|dkrt|j\} |\}}||js,tt||dd} fddt| D }|	d	}t
t| }g }	|D ]}
|D ]}| |
|
f}|	| qUqQt|	td
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. Solutions for symbols that satisfy the
        equations listed in seq.

    Examples
    ========

    >>> from sympy.polys 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)expandc                    s   g | ]}t | qS r   r
   )r   expryr   r   
<listcomp>x   s    z%solve_biquadratic.<locals>.<listcomp>key)r   r   	is_groundr   r   Zgcdr   r   keysltrimlistZsubsappendsortedr   )r%   r&   r#   GxpqZp_rootsZq_roots	solutionsZq_rootZp_rootsolutionr   r*   r   r   B   s(   '

r   c                    sb   dd  dd d fdd	z
| |j dd	}W n	 ty$   tw |d
ur/t|tdS d
S )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.

    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

    Returns
    =======

    List[Tuple]
        A List of tuples. Solutions for symbols that satisfy the
        equations listed in seq

    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

    Examples
    ========

    >>> from sympy.polys 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)]
    c                 S   s(   |   D ]}t|dd r dS qdS )z8Returns True if 'f' is univariate in its last variable. Nr-   FT)Zmonomsany)r%   Zmonomr   r   r   _is_univariate   s
   z%solve_generic.<locals>._is_univariatec                 S   s,   |  ||i}| |dkr|jdd}|S )z:Replace generator with a root so that the result is nice. r   F)Zdeep)Zas_exprdegreer(   )r%   genzeror8   r   r   r   
_subs_root   s   z!solve_generic.<locals>._subs_rootFc                    s  t | t |  krdkr$n ntt| d |d  }dd |D S t| |dd}t |dkr<|d jr<|s:g S dS tt |}t |dkrN| }ntt	d	|j
}|d }tt|| }|sjg S t |dkrwd
d |D S g }|D ]3}	g }
|dd }|dd D ]}|||	}|tjur|
| q|
|D ]
}|||	f  qq{|rt |d t |krtt	d	|S )z/Recursively solves reduced polynomial systems. r'   r   r-   c                 S      g | ]}|fqS r   r   r   r@   r   r   r   r,          z@solve_generic.<locals>._solve_reduced_system.<locals>.<listcomp>Tr"   Nzv
                only zero-dimensional systems supported
                (finite number of solutions)
                c                 S   rB   r   r   rC   r   r   r   r,     rD   )r   r3   r   r1   r   r0   filterpopNotImplementedErrorr   r   r2   r   ZZeror4   )systemr   entryzerosZbasisZ
univariater%   r?   r:   r@   Z
new_systemZnew_gensbeqr;   r=   _solve_reduced_systemrA   r   r   rO      sD    


z,solve_generic.<locals>._solve_reduced_systemT)rJ   Nr.   )F)r   r	   rH   r5   r   )r"   r#   resultr   rN   r   r      s   K	9r   c                 O   s  t | |dd}tt|}|d}|dur&t|D ]\}}||||< q|d d|dd }}| }| }	t	 }
|	D ]
}|

|f|f qAt|dd }t|dd }t||D ]n\}}t	 }|
D ]b\}}g tt||}}|D ]+}|f| }|j| r||dkr||t|}||| kr|| qzt|dd	 d
}| }	|	D ]}|js||}n|}|
|f| |f qqj|}
qat|
}
t|
D ]
\}\}}||
|< qt|
td
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.solvers.polysys 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

    TrE   domainNr   r-   r'   c                 S   s   |   S )N)r>   )hr   r   r   <lambda>x  s    z$solve_triangulated.<locals>.<lambda>r.   )r   r3   reversedget	enumerateZ
set_domainr2   Z
get_domainZground_rootssetaddr   zipZhas_only_gensr>   evaldictr4   minZis_RationalZalgebraic_fieldr5   r   )r"   r   r!   r6   rQ   r   r&   r%   ZdomrK   r:   r@   Zvar_seqZvars_seqvarvarsZ
_solutionsvaluesHmappingZ_varsrR   r8   Zdom_zeror;   _r   r   r   solve_triangulated'  sL   -



rc   N)r   Z
sympy.corer   Zsympy.polysr   r   r   Zsympy.polys.polytoolsr   Zsympy.polys.polyerrorsr   r   r	   Zsympy.simplifyr   Zsympy.utilitiesr   r   Zsympy.utilities.miscr   	Exceptionr   r   r   r   rc   r   r   r   r   <module>   s    1E !