ó
9­\c           @  sî   d  Z  d d l 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 m Z m Z d d l m Z d d l m Z m Z d d	 l m Z d
 e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d S(   s,   Solvers of systems of polynomial equations. iÿÿÿÿ(   t   print_functiont   division(   t   S(   t   Polyt   groebnert   roots(   t   parallel_poly_from_expr(   t   ComputationFailedt   PolificationFailedt   CoercionFailedt   PolynomialError(   t   rcollect(   t   default_sort_keyt	   postfixes(   t
   filldedentt   SolveFailedc           B  s   e  Z d  Z RS(   s-   Raised when solver's conditions weren't met. (   t   __name__t
   __module__t   __doc__(    (    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyR      s   c         O  sâ   y t  |  | | Ž \ } } Wn+ t k
 rI } t d t |  ƒ | ƒ ‚ n Xt | ƒ t | j ƒ k op d k n rÕ | \ } } t d „  | j ƒ  | j ƒ  Dƒ ƒ rÕ y t | | | ƒ SWqÒ t k
 rÎ qÒ XqÕ n  t	 | | ƒ S(   s  
    Solve a system of polynomial equations.

    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))]

    t   solve_poly_systemi   c         s  s   |  ] } | d  k Vq d S(   i   N(    (   t   .0t   i(    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pys	   <genexpr>)   s    (
   R   R   R   t   lent   genst   allt   degree_listt   solve_biquadraticR   t   solve_generic(   t   seqR   t   argst   polyst   optt   exct   ft   g(    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyR      s    +&
c         C  sM  t  |  | g ƒ } t | ƒ d k r5 | d j r5 d St | ƒ d k rP t ‚ n  | j \ } } | \ } } | j | ƒ j s† t ‚ n  t | | d t ƒ} g  t	 | ƒ j
 ƒ  D] } t | | ƒ ^ q® }	 | j d ƒ } t t	 | ƒ j
 ƒ  ƒ }
 g  } xD |
 D]< } x3 |	 D]+ } | j | | ƒ | f } | j | ƒ q
Wqý Wt | d t ƒS(   s¹  Solve a system of two bivariate quadratic polynomial equations.

    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)]
    i   i    i   t   expandiÿÿÿÿt   keyN(   R   R   t	   is_groundt   NoneR   R   t   gcdR   t   FalseR   t   keysR   t   ltrimt   listt   subst   appendt   sortedR   (   R!   R"   R   t   Gt   xt   yt   pt   qt   exprt   p_rootst   q_rootst	   solutionst   q_roott   p_roott   solution(    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyR   2   s&    		.c           s„   d „  ‰  d „  ‰ t  ‡  ‡ ‡ f d † ‰ y ˆ |  | j d t ƒ} Wn t k
 r_ t ‚ n X| d k	 r| t | d t ƒSd Sd S(   s
	  
    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.

    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  s9   x2 |  j  ƒ  D]$ } t d „  | d  Dƒ ƒ r t Sq Wt S(   s8   Returns True if 'f' is univariate in its last variable. c         s  s   |  ] } | Vq d  S(   N(    (   R   t   m(    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pys	   <genexpr>¥   s    iÿÿÿÿ(   t   monomst   anyR(   t   True(   R!   t   monom(    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyt   _is_univariate¢   s    c         S  sD   |  j  i | | 6ƒ } |  j | ƒ d k r@ | j d t ƒ } n  | S(   s:   Replace generator with a root so that the result is nice. i   t   deep(   t   as_exprt   degreeR#   R(   (   R!   t   gent   zeroR2   (    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyt
   _subs_rootª   s    c           s2  t  |  ƒ t  | ƒ k o# d k n re t t |  d | d ƒ j ƒ  ƒ } g  | D] } | f ^ qR St |  | d t ƒ} t  | ƒ d k rª | d j rª | s£ g  Sd Sn  t t ˆ  | ƒ ƒ } t  | ƒ d k rà | j	 ƒ  } n t
 t d ƒ ƒ ‚ | j } | d } t t | j | ƒ ƒ j ƒ  ƒ } | s0g  St  | ƒ d k r\g  | D] } | f ^ qISg  }	 x’ | D]Š } g  }
 | d  } xC | d  D]7 } ˆ | | | ƒ } | t j k	 rŠ|
 j | ƒ qŠqŠWx+ ˆ |
 | ƒ D] } |	 j | | f ƒ qÕWqiW|	 r.t  |	 d ƒ t  | ƒ k r.t
 t d ƒ ƒ ‚ n  |	 S(   s/   Recursively solves reduced polynomial systems. i   i    iÿÿÿÿR   sv   
                only zero-dimensional systems supported
                (finite number of solutions)
                N(   R   R+   R   R)   R   R>   R%   R&   t   filtert   popt   NotImplementedErrorR   R   R*   R   t   ZeroR-   (   t   systemR   t   entryt   zerosRE   t   basist
   univariateR!   RD   R7   t
   new_systemt   new_genst   bt   eqR:   (   R@   t   _solve_reduced_systemRF   (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyRT   ³   sD    (#	
!
"RL   R$   N(   R(   R   R>   R	   RI   R&   R.   R   (   R   R   t   result(    (   R@   RT   RF   s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyR   f   s    <			9
c         O  s§  t  |  | d t ƒ} t t | ƒ ƒ } | j d ƒ } | d k	 ru x0 t | ƒ D] \ } } | j | ƒ | | <qO Wn  | d j d ƒ | d } } | j	 ƒ  } | j
 ƒ  }	 t g  ƒ }
 x$ |	 D] } |
 j | f | f ƒ q¾ Wt | d  ƒ } t | d ƒ } x]t | | ƒ D]L\ } } t g  ƒ } x+|
 D]#\ } } g  t t | | ƒ ƒ } } x‹ | D]ƒ } | f | } | j | Œ  r\| j | ƒ d k r\| j | ƒ j t | ƒ ƒ } | j | ƒ | j ƒ  k rß| j | ƒ qßq\q\Wt | d d „  ƒ} | j
 ƒ  }	 xI |	 D]A } | j s,| j | ƒ } n | } | j | f | | f ƒ qWq-W| }
 qWt |
 ƒ }
 x* t |
 ƒ D] \ } \ } } | |
 | <qwWt |
 d t ƒS(	   s0  
    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.

    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

    R   t   domaini    iÿÿÿÿi   R$   c         S  s
   |  j  ƒ  S(   N(   RC   (   t   h(    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyt   <lambda>6  t    N(   R   R>   R+   t   reversedt   getR&   t	   enumeratet
   set_domainR*   t
   get_domaint   ground_rootst   sett   addR   t   zipt   has_only_gensRC   t   evalt   dictR-   t   mint   is_Rationalt   algebraic_fieldR.   R   (   R   R   R   R/   RV   R   R"   R!   t   domRM   R7   RE   t   var_seqt   vars_seqt   vart   varst
   _solutionst   valuest   Ht   mappingt   _varsRW   R2   t   dom_zeroR:   t   _(    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyt   solve_triangulated÷   sH    $	"
N(   R   t
   __future__R    R   t
   sympy.coreR   t   sympy.polysR   R   R   t   sympy.polys.polytoolsR   t   sympy.polys.polyerrorsR   R   R	   R
   t   sympy.simplifyR   t   sympy.utilitiesR   R   t   sympy.utilities.miscR   t	   ExceptionR   R   R   R   Ru   (    (    (    s4   lib/python2.7/site-packages/sympy/solvers/polysys.pyt   <module>   s   "		4	‘