a
    SG5d2                     @   s   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 deZddddZdd ZdddZdd ZdS )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                   @   s   e Zd ZdZdS )SolveFailedz.Raised when solver's conditions were not met. N)__name__
__module____qualname____doc__ r   r   Q/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/solvers/polysys.pyr      s   r   Fstrictc          	   
   O   s   zt | g|R i |\}}W n4 tyR } ztdt| |W Y d}~n
d}~0 0 t|t|j  krrdkrn nH|\}}tdd | |  D rzt|||W S  ty   Y n0 t	|||dS )a9  
    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
    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. 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_system([x**5 - x + y**3, y**2 - 1], x, y, strict=True)
    Traceback (most recent call last):
    ...
    UnsolvableFactorError

    solve_poly_systemN   c                 s   s   | ]}|d kV  qdS )r   Nr   ).0ir   r   r   	<genexpr>F       z$solve_poly_system.<locals>.<genexpr>r   )
r   r	   r   lengensalldegree_listsolve_biquadraticr   solve_generic)	seqr   r   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r6t|j\} |\}}||jsXtt||dd} fddt| D }|	d	}t
t| } fd
dt||D }	t|	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 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>   r   z%solve_biquadratic.<locals>.<listcomp>c                    s    g | ]\}}|  ||fqS r   )subs)r   Zq_rootZp_rootr.   r   r   r0      r   key)r   r   	is_groundr   r   gcdr   r   keysltrimlist	itertoolsproductsortedr   )
r)   r*   r'   GxpqZp_rootsZq_roots	solutionsr   r.   r   r"   O   s"    '



r"   c                    sl   dd  dd d fdd	z| |j dd	}W n tyN   tY n0 |d
urdt|tdS d
S d
S )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.

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

    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                 S   s(   |   D ]}t|dd r dS qdS )z8Returns True if 'f' is univariate in its last variable. Nr1   FT)monomsany)r)   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)deep)as_exprdegreer,   )r)   genzeror?   r   r   r   
_subs_root   s    z!solve_generic.<locals>._subs_rootFc                    s  t | t |  krdkrLn n,tt| d |d d }dd |D S t| |dd}t |dkr||d jr||sxg S d	S tt |}t |t |k rttd
t |dkr|	 }nttd
|j
}|d }tt||d }|sg S t |dkrdd |D S g }|D ]n}	g }
|d	d }|d	d D ](}|||	}|tjur<|
| q<|
|D ]}|||	f  qpq|rt |d t |krttd
|S )z/Recursively solves reduced polynomial systems. r+   r   r1   r   c                 S   s   g | ]
}|fqS r   r   r   rJ   r   r   r   r0     r   z@solve_generic.<locals>._solve_reduced_system.<locals>.<listcomp>Tr&   Nzv
                only zero-dimensional systems supported
                (finite number of solutions)
                c                 S   s   g | ]
}|fqS r   r   rL   r   r   r   r0   -  r   )r   r9   r   r7   r   r5   filterNotImplementedErrorr   popr   r8   r   Zeroappend)systemr   entryzerosbasisZ
univariater)   rI   rA   rJ   Z
new_systemnew_gensbeqsolutionrE   _solve_reduced_systemrK   r   r   r   r\     sD     
z,solve_generic.<locals>._solve_reduced_systemT)rT   Nr3   )F)r   r
   rO   r<   r   )r&   r'   r   resultr   r[   r   r#      s    a	C
r#   c                 O   s  t | |dd}tt|}|d}|durLt|D ]\}}||||< q4|d d|dd  }}| }| }	t	 }
|	D ]}|

|f|f qt|dd }t|dd }t||D ]\}}t	 }|
D ]\}}g tt|| }}|D ]V}|f| }|j| r||dkr||t|}||| kr|| qt|dd	 d
}| }	|	D ]2}|js||}n|}|
|f| |f qhq|}
qt|
}
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 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

    TrM   domainNr   r1   r+   c                 S   s   |   S )N)rH   )hr   r   r   <lambda>  r   z$solve_triangulated.<locals>.<lambda>r3   )r   r9   reversedget	enumerate
set_domainr8   
get_domainground_rootssetaddr   ziphas_only_gensrH   evaldictrR   minis_Rationalalgebraic_fieldr<   r   )r&   r   r%   r=   r^   r   r*   r)   domrU   rA   rJ   Zvar_seqZvars_seqvarvarsZ
_solutionsvaluesHmapping_varsr_   r?   Zdom_zerorZ   _r   r   r   solve_triangulatedP  sH    -

rx   )F)r   r:   
sympy.corer   sympy.core.sortingr   sympy.polysr   r   r   sympy.polys.polytoolsr   sympy.polys.polyerrorsr   r	   r
   sympy.simplifyr   Zsympy.utilitiesr   sympy.utilities.miscr   	Exceptionr   r   r"   r#   rx   r   r   r   r   <module>   s   <A
 A