
~9\c           @  s}  d  Z  d d l m Z m Z d d l m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z d d l m Z m Z d d l m Z d d l m Z d d l m Z d d l m Z m Z d d	 l m Z d d
 l m Z d d l 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* m+ Z+ m, Z, m- Z- m. Z. m/ Z/ m0 Z0 m1 Z1 m2 Z2 m3 Z3 d d l4 m5 Z5 m6 Z6 d d l7 m8 Z8 d d l9 m: Z: d d l; m< Z< d d l= m> Z> d d l? m@ Z@ d d lA mB ZB d d lC mD ZD d d lE mF ZF mG ZG mH ZH mI ZI mJ ZJ d d lK mL ZL mM ZM e! d d d  ZN d   ZO d   ZP d6 d6 d  ZR d   ZS d    ZT d6 d!  ZU d"   ZV d#   ZW d$   ZX d%   ZY d&   ZZ d'   Z[ d(   Z\ eI d6 e] e^ d6 d)   Z_ d*   Z` e_ Za eb jc d+  d,   Zd eI d6 d-   Ze d.   Zf d/   Zg d0   Zh eI d1    Zi eI d6 d2   Zj d3 e@ f d4     YZk eI d6 e^ d5   Zl d6 S(7   s&   Computational algebraic field theory. i(   t   print_functiont   division(
   t   St   Rationalt   AlgebraicNumbert   Addt   Mult   sympifyt   Dummyt
   expand_mult   It   pi(   t   reducet   range(   t   Factors(   t   _mexpand(   t   exp(   t   cost   sin(   t   sieve(   t   divisors(   t   ZZt   QQ(   t   dup_chebyshevt(   t   IsomorphismFailedt   CoercionFailedt   NotAlgebraict   GeneratorsError(
   t   Polyt   PurePolyt   invertt   factor_listt   groebnert	   resultantt   degreet   poly_from_exprt   parallel_poly_from_exprt   lcm(   t   dict_from_exprt   expr_from_dict(   t   rs_compose_add(   t   ring(   t   CRootOf(   t   cyclotomic_poly(   t   LambdaPrinter(   t
   _split_gcd(   t   _is_sum_surds(   t   numbered_symbolst
   variationst   lambdifyt   publict   sift(   t   pslqt   mpi   i   c         C  s  t  |  d t  r3 g  |  D] } | d ^ q }  n  t |   d k rM |  d Si | | 6} t | d  rr | j n g  } t d d  }	 x t | t |   D] }
 d } |
 } x& | D] } | | | | <| | } q Wx t rg  } |	 | d } xB |  D]: } t | j	   j
 | |   | k  r | j |  q q W| rJ| }  n  t |   d k rd|  d S| | k rtPn  | d 9} q Wq Wt d |   d S(   se   
    Return a factor having root ``v``
    It is assumed that one of the factors has root ``v``.
    i    i   t   symbolsi
   i   s4   multiple candidates for the minimal polynomial of %sN(   t
   isinstancet   tuplet   lent   hasattrR6   R   R   t   Truet   abst   as_exprt   evalft   appendt   NotImplementedError(   t   factorst   xt   vt   domt   prect   boundt   ft   pointsR6   t   tt   nt   prec1t   n_tempt   st
   candidatest   eps(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _choose_factor,   s6     	$	c         C  s5  d d l  m } d   } g  } x |  j D] } | j s | |  ra | j t j | d f  q) | j r | j | t j f  q) | j r | j	 j
 r | j | t j f  q) t  q) n  | | j | d t \ } } | j t |   t |   d f  q) W| j d d    | d d t j k r0|  Sg  | D] \ } } | ^ q7} x. t t |   D] }	 | |	 d k rbPqbqbWt | |	   \ }
 } } g  } g  } xS | D]K \ } } | | k r| j | | t j  q| j | | t j  qWt |   } t |   } t | d  t | d  }  |  S(	   s7  
    helper function for ``_minimal_polynomial_sq``

    It selects a rational ``g`` such that the polynomial ``p``
    consists of a sum of terms whose surds squared have gcd equal to ``g``
    and a sum of terms with surds squared prime with ``g``;
    then it takes the field norm to eliminate ``sqrt(g)``

    See simplify.simplify.split_surds and polytools.sqf_norm.

    Examples
    ========

    >>> from sympy import sqrt
    >>> from sympy.abc import x
    >>> from sympy.polys.numberfields import _separate_sq
    >>> p= -x + sqrt(2) + sqrt(3) + sqrt(7)
    >>> p = _separate_sq(p); p
    -x**2 + 2*sqrt(3)*x + 2*sqrt(7)*x - 2*sqrt(21) - 8
    >>> p = _separate_sq(p); p
    -x**4 + 4*sqrt(7)*x**3 - 32*x**2 + 8*sqrt(7)*x + 20
    >>> p = _separate_sq(p); p
    -x**8 + 48*x**6 - 536*x**4 + 1728*x**2 - 400

    i(   R3   c         S  s   |  j  o |  j t j k S(   N(   t   is_PowR   R   t   Half(   t   expr(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   is_sqrtm   s    i   t   binaryt   keyc         S  s   |  d S(   Ni   (    (   t   z(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   <lambda>~   t    i   (   t   sympy.utilities.iterablesR3   t   argst   is_MulR?   R   t   Onet   is_AtomRQ   R   t
   is_integerR@   R;   R   t   sortR   R9   R-   RR   R   R   (   t   pR3   RT   t   at   yt   Tt   FRW   t   surdst   it   gt   b1t   b2t   a1t   a2t   p1t   p2(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _separate_sqR   sB    			'c         C  s.  d d l  m } t |   }  t |  } | j sL | d k sL | |   rP d S|  t d |  } |  | 8}  x@ t |   } | |  k r | j i | | | 6 }  Pqp | }  qp W| d k rt |   } |  j	 | | j
 |   d k  r |  }  n  |  j   d }  |  St |   d } t | | |  } | S(   s  
    Returns the minimal polynomial for the ``nth-root`` of a sum of surds
    or ``None`` if it fails.

    Parameters
    ==========

    p : sum of surds
    n : positive integer
    x : variable of the returned polynomial

    Examples
    ========

    >>> from sympy.polys.numberfields import _minimal_polynomial_sq
    >>> from sympy import sqrt
    >>> from sympy.abc import x
    >>> q = 1 + sqrt(2) + sqrt(3)
    >>> _minimal_polynomial_sq(q, 3, x)
    x**12 - 4*x**9 - 4*x**6 + 16*x**3 - 8

    i(   R.   i    i   N(   t   sympy.simplify.simplifyR.   R   t
   is_Integert   NoneR   Ro   t   subsR   t   coeffR"   t	   primitiveR   RP   (   Ra   RJ   RB   R.   t   pnRm   RA   t   result(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _minimal_polynomial_sq   s,    $

"
c         C  s(  t  t |   } | d k r3 t | | |  } n  | d k rT t | | |  } n | j i | | 6 } |  t k r| t k r t d t  \ } }	 | t |  d  }
 | t |  d  } q9t	 | | | f | |  \ \ }
 } } |
 j
 |  } | j   } n- |  t k r-t | | |  } n t d   |  t k sQ| t k rot | | d | | g } n$ t |
 |  } t | j   |  } t | |  } t | |  } |  t k r| d k s| d k r| St | | d | } | j   \ } } t | | |  | |  |  } | j   S(   s  
    return the minimal polynomial for ``op(ex1, ex2)``

    Parameters
    ==========

    op : operation ``Add`` or ``Mul``
    ex1, ex2 : expressions for the algebraic elements
    x : indeterminate of the polynomials
    dom: ground domain
    mp1, mp2 : minimal polynomials for ``ex1`` and ``ex2`` or None

    Examples
    ========

    >>> from sympy import sqrt, Add, Mul, QQ
    >>> from sympy.polys.numberfields import _minpoly_op_algebraic_element
    >>> from sympy.abc import x, y
    >>> p1 = sqrt(sqrt(2) + 1)
    >>> p2 = sqrt(sqrt(2) - 1)
    >>> _minpoly_op_algebraic_element(Mul, p1, p2, x, QQ)
    x - 1
    >>> q1 = sqrt(y)
    >>> q2 = 1 / y
    >>> _minpoly_op_algebraic_element(Add, q1, q2, x, QQ.frac_field(y))
    x**2*y**2 - 2*x*y - y**3 + 1

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Resultant
    .. [2] I.M. Isaacs, Proc. Amer. Math. Soc. 25 (1970), 638
           "Degrees of sums in a separable field extension".

    t   Xi    s   option not availablet   gensi   t   domainN(   R   t   strRr   t   _minpoly_composeRs   R   R   R)   R&   R$   t   composeR=   R   t   _mulyR@   R!   R(   R'   t   as_expr_dictR"   R   R   RP   (   t   opt   ex1t   ex2RB   RD   t   mp1t   mp2Rc   t   RRy   Rm   Rn   t   _t   rt   mp1at   deg1t   deg2RA   t   res(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _minpoly_op_algebraic_element   s:    $($c         C  s]   t  |  |  d } t |  } g  | j   D]! \ \ } } | | | | ^ q, } t |   S(   s@   
    Returns ``expand_mul(x**degree(p, x)*p.subs(x, 1/x))``
    i    (   R#   R"   t   termsR   (   Ra   RB   Rm   RJ   Rg   t   cRb   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _invertx  s    4c         C  se   t  |  |  d } t |  } g  | j   D]) \ \ } } | | | | | | ^ q, } t |   S(   s8   
    Returns ``_mexpand(y**deg*p.subs({x:x / y}))``
    i    (   R#   R"   R   R   (   Ra   RB   Rc   Rm   RJ   Rg   R   Rb   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR   %  s    <c         C  sC  t  |  } | s' t |  | |  } n  | j sC t d |    n  | d k  r | | k rn t d |    n  t | |  } | d k r | S| } d |  }  n  t t |   } | j i | | 6 } | j	   \ } } t
 t | | | | | d | g | d | } | j   \ }	 }
 t |
 | |  | |  } | j   S(   s  
    Returns ``minpoly(ex**pw, x)``

    Parameters
    ==========

    ex : algebraic element
    pw : rational number
    x : indeterminate of the polynomial
    dom: ground domain
    mp : minimal polynomial of ``p``

    Examples
    ========

    >>> from sympy import sqrt, QQ, Rational
    >>> from sympy.polys.numberfields import _minpoly_pow, minpoly
    >>> from sympy.abc import x, y
    >>> p = sqrt(1 + sqrt(2))
    >>> _minpoly_pow(p, 2, x, QQ)
    x**2 - 2*x - 1
    >>> minpoly(p**2, x)
    x**2 - 2*x - 1
    >>> _minpoly_pow(y, Rational(1, 3), x, QQ.frac_field(y))
    x**3 - y
    >>> minpoly(y**Rational(1, 3), x)
    x**3 - y

    s*   %s doesn't seem to be an algebraic elementi    s
   %s is zeroii   Rz   R{   (   R   R}   t   is_rationalR   t   ZeroDivisionErrorR   R   R|   Rs   t   as_numer_denomR   R!   R   RP   R=   (   t   ext   pwRB   RD   R5   Rc   RJ   t   dR   R   RA   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _minpoly_pow0  s(    	3c      	   G  ss   t  t | d | d |  |  } | d | d } x: | d D]. } t  t | | |  | d | } | | } q= W| S(   s.   
    returns ``minpoly(Add(*a), dom, x)``
    i    i   i   R   (   R   R   (   RB   RD   Rb   R5   Ra   t   px(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _minpoly_adde  s     c      	   G  ss   t  t | d | d |  |  } | d | d } x: | d D]. } t  t | | |  | d | } | | } q= W| S(   s.   
    returns ``minpoly(Mul(*a), dom, x)``
    i    i   i   R   (   R   R   (   RB   RD   Rb   R5   Ra   R   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _minpoly_mulq  s     c         C  s  |  j  d j   \ } } | t k r| j r| j } t |  } | j r t | t  } t	 g  t
 |  D]  } | | | d | | ^ qk   S| j d k r | d k r d | d d | d d | d	 d
 Sn  | d	 d k r^t | t  } g  t
 | d  D] } | | | | | ^ q} t	 |   } t |  \ } }	 t |	 | |   }
 |
 Sd t d	 | t  d	 t j } t | | t  }
 |
 Sn  t d |    d S(   st   
    Returns the minimal polynomial of ``sin(ex)``
    see http://mathworld.wolfram.com/TrigonometryAngles.html
    i    i   i	   i@   i   i`   i   i$   i   i   s*   %s doesn't seem to be an algebraic elementN(   R[   t   as_coeff_MulR   R   t   qR   t   is_primeR   R   R   R   Ra   R   RP   R   R   RR   R}   R   R   (   R   RB   R   Rb   RJ   R   Rg   R   R   RA   R   RS   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _minpoly_sin}  s,    			7+3#c         C  s  d d l  m } |  j d j   \ } } | t k r| j r| j d k r | j d k r d | d d | d	 d | d S| j d
 k rd | d d | d Sna | j d	 k rt | j  } | j	 rt
 |  |  } t | j i | d | d	  | 6  Sn  t | j  } t | t  } g  t | d  D] } | | | | | ^ q:} t |   d | j }	 t |	  \ }
 } t | | |   } | Sn  t d |    d S(   st   
    Returns the minimal polynomial of ``cos(ex)``
    see http://mathworld.wolfram.com/TrigonometryAngles.html
    i(   t   sqrti    i   i   i   i   i   i   i	   i   s*   %s doesn't seem to be an algebraic elementN(   t   sympyR   R[   R   R   R   Ra   R   R   R   R   R   Rs   t   intR   R   R   R   R   RP   R   (   R   RB   R   R   Rb   R   RM   RJ   Rg   R   R   RA   R   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _minpoly_cos  s,    	$	+3c   	      C  s  |  j  d j   \ } } t | j  } | t t k r| j r| j d k s_ | j d k rW| d k r{ | d | d S| d k r | d d S| d k r | d | d d S| d k r | d d S| d	 k r | d | d d S| d
 k r| d | d | d | d d S| j rWd } x# t	 |  D] } | | | 7} q7W| Sn  g  t
 d |  D] } t | |  ^ qh} t | | |   } | St d |    n  t d |    d S(   s7   
    Returns the minimal polynomial of ``exp(ex)``
    i    i   ii   i   i   i   i   i	   i
   s*   %s doesn't seem to be an algebraic elementN(   R[   R   R   R   R
   R   R   Ra   R   R   R   R+   RP   R   (	   R   RB   R   Rb   R   RM   Rg   RA   R5   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _minpoly_exp  s6    	$	,c         C  sT   |  j  } | j i | |  j j d 6 } t | |  \ } } t | | |   } | S(   sA   
    Returns the minimal polynomial of a ``CRootOf`` object.
    i    (   RS   Rs   t   polyRz   R   RP   (   R   RB   Ra   R   RA   Rw   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _minpoly_rootof  s
    	 c      
   C  sn  |  j  r |  j | |  j S|  t k rp t | d d | d | \ } } t |  d k rh | d d S| t St | d  r |  | j k r | |  S| j r t	 |   r |  | 8}  x) t
 |   } | |  k r |  S| }  q Wn  |  j rt | | |  j  } nh|  j rt |   j } t | j   d    } | t r| t k rt g  | t | d	 D] \ }	 }  |	 |  ^ q^  } t | t  }
 g  |
 j   D] } | j ^ q} t t | d  } t j } |
 j | t j  } g  |
 j   D]$ \ } } | | j | | j ^ q} t |   } t  | |  } | j | | | j | | | } | | | t! d |  } t" t | | | | d | d | } qjt# | | |  j  } n |  j$ rt% |  j& |  j' | |  } n |  j( t) k rt* |  |  } ns |  j( t+ k rt, |  |  } nR |  j( t' k r9t- |  |  } n1 |  j( t. k rZt/ |  |  } n t0 d |    | S(
   s  
    Computes the minimal polynomial of an algebraic element
    using operations on minimal polynomials

    Examples
    ========

    >>> from sympy import minimal_polynomial, sqrt, Rational
    >>> from sympy.abc import x, y
    >>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=True)
    x**2 - 2*x - 1
    >>> minimal_polynomial(sqrt(y) + 1/y, x, compose=True)
    x**2*y**2 - 2*x*y - y**3 + 1

    i   i   R{   R6   c         S  s   |  d j  o |  d j  S(   Ni    i   (   t   is_Rational(   t   itx(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyRX     RY   R   R   s*   %s doesn't seem to be an algebraic elementN(1   R   R   Ra   R
   R   R9   R:   R6   t   is_QQR.   Ro   t   is_AddR   R[   R\   R   RA   R3   t   itemsR;   R   R   t   FalseRr   t   dictt   valuesR   R%   R   t   NegativeOnet   popt   Zerot   minimal_polynomialR   R   R   RQ   R   t   baseR   t	   __class__R   R   R   R   R   R*   R   R   (   R   RB   RD   R   RA   R   R   RG   R   t   bxt   r1Rc   t   denst   lcmdenst   neg1t   expn1R   t   numsR   R   R   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR}     sZ    	#&
		5"	7$'	c         C  s  d d l  m } d d l m } d d l m } t |   }  |  j rZ t |  d t	 }  n  x' | |   D] } | j
 rg t } Pqg qg W| d k	 r t |  t } }	 n t d  t } }	 | s |  j r | t t |  j   } q t } n  t | d  r#| | j k r#t d | | f   n  | rt |  | |  }
 |
 j   d	 }
 |
 j | | |
 |   } | j rt |
  }
 n  | r|	 |
 | d
 t	 S|
 j |  S| j st d   n  t |  | |	  }
 | r|	 |
 | d
 t	 S|
 j |  S(   s-  
    Computes the minimal polynomial of an algebraic element.

    Parameters
    ==========

    ex : Expr
        Element or expression whose minimal polynomial is to be calculated.

    x : Symbol, optional
        Independent variable of the minimal polynomial

    compose : boolean, optional (default=True)
        Method to use for computing minimal polynomial. If ``compose=True``
        (default) then ``_minpoly_compose`` is used, if ``compose=False`` then
        groebner bases are used.

    polys : boolean, optional (default=False)
        If ``True`` returns a ``Poly`` object else an ``Expr`` object.

    domain : Domain, optional
        Ground domain

    Notes
    =====

    By default ``compose=True``, the minimal polynomial of the subexpressions of ``ex``
    are computed, then the arithmetic operations on them are performed using the resultant
    and factorization.
    If ``compose=False``, a bottom-up algorithm is used with ``groebner``.
    The default algorithm stalls less frequently.

    If no ground domain is given, it will be generated automatically from the expression.

    Examples
    ========

    >>> from sympy import minimal_polynomial, sqrt, solve, QQ
    >>> from sympy.abc import x, y

    >>> minimal_polynomial(sqrt(2), x)
    x**2 - 2
    >>> minimal_polynomial(sqrt(2), x, domain=QQ.algebraic_field(sqrt(2)))
    x - sqrt(2)
    >>> minimal_polynomial(sqrt(2) + sqrt(3), x)
    x**4 - 10*x**2 + 1
    >>> minimal_polynomial(solve(x**3 + x + 3)[0], x)
    x**3 + x + 3
    >>> minimal_polynomial(sqrt(y), x)
    x**2 - y

    i(   R"   (   t   FractionField(   t   preorder_traversalt	   recursiveRB   R6   s5   the variable %s is an element of the ground domain %si   t   fields!   groebner method only works for QQN(   t   sympy.polys.polytoolsR"   t   sympy.polys.domainsR   t   sympy.core.basicR   R   t	   is_numberR   R;   t   is_AlgebraicNumberR   Rr   R   R   R   t   free_symbolsR   t   listR:   R6   R   R}   Ru   Rt   t   is_negativeR	   t   collectR   R@   t   _minpoly_groebner(   R   RB   R~   t   polysR{   R"   R   R   RS   t   clsRw   R   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR   <  s>    6					&	c           s5  d d l  m } d d l m } t d d t  i  i    d    f d        f d     d   } t } | |   }  |  j r |  j	 j
 |  S|  j r |  j | |  j } n$| |   } | r |  d }  n  d } |  j r&d	 |  j j r&d	 |  j }	 t |  j |	 |  } n$ t |   rJt |  t j |  } n  | d k	 r_| } n  | d k r  |   }
 | |
 g t  j    } t | t  j    | g d
 d } t | d  \ } } t | | |   } n  | r1t | |  } | j | | | |   d k  r1t |  } q1n  | S(   s/  
    Computes the minimal polynomial of an algebraic number
    using Groebner bases

    Examples
    ========

    >>> from sympy import minimal_polynomial, sqrt, Rational
    >>> from sympy.abc import x
    >>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=False)
    x**2 - 2*x - 1

    i(   R"   (   t   expand_multinomialRb   R   c           sN   t     } |  |  <| d  k	 r7 | | |  |  <n | j |   |  <| S(   N(   t   nextRr   R=   (   R   R   R   Rb   (   t	   generatort   mappingR6   (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   update_mapping  s    
c           s[  |  j  rO |  t j k r? |   k r4  |  d d  S |  SqG|  j rG|  Sn|  j r~ t g  |  j D] }   |  ^ qe   S|  j r t g  |  j D] }   |  ^ q   S|  j	 r|  j
 j rG|  j
 d k  rx|  j j rx|  j j   \ } } t | d t \ } } |  j | } t | j | |  j   } | j | j |  j   } |  j
 d k rg  |  S| |  j
 }  n  |  j
 j s|  j |  j
 j j   t d |  j
 j  } }	 n |  j |  j
 } }	   |  } | |	 }
 |
  k r  |
 d |	 |  S |
 SqGn9 |  j rG|  j  k r9 |  j |  j  S |  j Sn  t d |    d  S(   Ni   i   i    R   is)   %s doesn't seem to be an algebraic number(   R^   R   t   ImaginaryUnitR   R   R   R[   R\   R   RQ   R   R   t   as_coeff_addt   primitive_elementR;   R   t   genR=   Rs   t   expandRq   Ra   R   R   R   t   roott   minpolyR   (   R   Rh   Rt   R   t   eltR   t   algt   inverseR   R   RS   (   t   bottom_up_scanR   R6   R   (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR     sF    			&	&	
2
	c         S  s   |  j  r> d |  j j r> |  j d k  r> |  j j r; t Sq> n  |  j r t } xI |  j D]> } | j rj t S| j  rW | j j r | j d k r t SqW qW W| r t Sn  t S(   s   
        Returns True if it is more likely that the minimal polynomial
        algorithm works better with the inverse
        i   i    (	   RQ   R   R_   R   R   R;   R\   R[   R   (   R   t   hitRa   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   simpler_inverse  s    	
			i   t   ordert   lexi    N(   R   R"   t   sympy.core.functionR   R/   R   Rr   R   R   R   R=   R   R   Ra   RQ   R   Rq   Rx   R   R.   R   R]   R   R   R    R   RP   R   Rt   R	   (   R   RB   R   R"   R   R   t   invertedRw   R   RJ   t   busRe   t   GR   RA   (    (   R   R   R   R6   R   s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR     sF    1				("R   c         c  sT   xM t  d d d d d d g |  d t D]$ } | d d k r( t |  Vq( q( Wd	 S(
   s1   Generate coefficients for `primitive_element()`. i   ii   ii   it
   repetitioni    N(   R0   R;   R   (   RJ   t   coeffs(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   _coeffs_generator*  s    .c         K  sX  |  s t  d   n  | d k	 r7 t |  t } } n t d  t } } | j d t  sM|  d d g } } t | t	  r | j
 j |  } n t | | d t } xl |  d D]` } t | d | \ } }	 t |	 | |  } | j   \ }
 } } | |
 | 7} | j |
  q W| j d t  s:| j   | f S| |  | f Sn  t d d	 t } g  g  } } xz |  D]r } t |  } | j r| j r| j |  } qt  d
 |   n t
 | |  } | j |  | j |  qsW| j d t  } x| t |   D] } | t g  t | |  D] \ } } | | ^ q* } t | | g | | g d d d t } | d  | | d | d d } } xf t t | |   D]N \ } \ } } y' t | | | d d j   | | <Wqt k
 rPqXqWPqWt d   | j    \ } } | j d t  sG| j   | | f S| | | f Sd S(   s4   Construct a common number field for all extensions. s3   can't compute primitive element for empty extensionRB   R   i    i   R   t	   extensionRc   R   s#   expected minimal polynomial, got %sR   R   R   R   iR{   R   s%   run out of coefficient configurationsN(!   t
   ValueErrorRr   R   R   R   R   t   getR   R7   R   R   t   replaceR   R;   R   RP   t   sqf_normR?   R=   R/   R   t   is_Polyt   is_univariateR   R9   t   sumt   zipR    t	   enumeratet
   all_coeffsR   t   RuntimeErrort   clear_denoms(   R   RB   R[   R   R   R   Rh   t   extR   RA   RM   R   Re   t   YRc   RG   t   coeffs_generatorR   R   t   HRg   t   h(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR   3  s^    		6)$(	c         C  s   |  j  j   } | j  j   } | | d k r2 t S| | k rB t S|  j  j   } | j  j   } d | | | d } } } xU t r t | }	 |	 | }
 |
 | k r Pn  | |	 d r | |
 r t S| d 7} q Wt S(   s5   Returns `True` if there is a chance for isomorphism. i    i   i   (   R   R"   R   R;   t   discriminantR   (   Rb   t   bRJ   t   mt   dat   dbRg   t   kt   halfRa   t   P(    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   is_isomorphism_possible  s$    	

c         C  s  |  j  j s | j  j r) t d   n  |  j } | j j | j  } d | j j   d } } } xt d d  D]} |  j  j	 |  } | j  j	 |  }	 d |	 g g  t d |  D] } |	 | ^ q | g }
 t
 j | } t
 _ t |
 d t d  d d	 } | t
 _ | d k rPn  | | k r4| } n Pg  | d
  D] } t |  | d
 ^ q@} x | d
 sz| j   qcWt t |   } t | | j d d } | j |  j |  j r9t |  d d } } x0 t |  D]" \ } } | | |	 | | 7} qW| | d k  r2g  | D] } | ^ q!S| Sqt | j |  j |  j rmg  | D] } | ^ q\S| d 9} qt Wd S(   s2   Construct field isomorphism using PSLQ algorithm. s)   PSLQ doesn't support complex coefficientsid   i   i   i   t   maxcoeffg    _Bt   maxstepsi  iR{   R   i    N(   R   t   is_realR@   R   R   R   R"   Rr   R   R>   R5   t   dpsR4   R   R   R   R   t   reversedR   R~   t   remt   is_zeroR9   R   (   Rb   R   RG   Rh   RJ   R   t   prevRg   t   At   Bt   basisR  R   R   R   R   t   approxRt   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   field_isomorphism_pslq  sB    	7		+c         C  s  t  |  j d | \ } } x | D] \ } } | j   d k r" | j j   j   } t |  d g  } } x6 t |  D]( \ } }	 | j |	 | j	 | |  qy Wt
 |   }
 |  j	 |
 j d t  d k r | S|  j	 |
 j d t  d k rg  | D] } | ^ q Sq" q" Wd Sd S(   s/   Construct field isomorphism via factorization. R   i   t   chopi    N(   R   R   R"   t   rept   TCt   to_sympy_listR9   R   R?   R   R   R>   R;   Rr   (   Rb   R   R   RA   RG   R   R   R   Rg   Rt   R   R   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   field_isomorphism_factor  s     c         K  s   t  |   t  |  }  } |  j s1 t |   }  n  | j sI t |  } n  |  | k r_ |  j   S|  j j   } | j j   } | d k r |  j g S| | d k r d S| j d t	  r y# t
 |  |  } | d k	 r | SWq t k
 r q Xn  t |  |  S(   s4   Construct an isomorphism between two number fields. i   i    t   fastN(   R   R   R   R   R   R"   R   Rr   R   R;   R  R@   R  (   Rb   R   R[   RJ   R   Rw   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   field_isomorphism  s*    		

c   	      K  sD  | j  d  } t |  d  r- t |   }  n	 |  g }  t |   d k rl t |  d  t k rl t |  d  St |  | d t \ } } t	 g  t
 | |   D] \ } } | | ^ q  } | d k r t | | f  St |  } | j s t | d | } n  t | |  } | d k	 r't | |  St d | | j f   d S(   s7   Express `extension` in the field generated by `theta`. R   t   __iter__i   i    R   s   %s is not in a subfield of %sN(   R   R:   R   R9   t   typeR8   R   R   R;   R   R   Rr   R   R   R  R   R   (	   R   t   thetaR[   R   R   R   Rt   R   R   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   to_number_field  s$    	(2	t   IntervalPrinterc           B  s)   e  Z d  Z d   Z d   Z d   Z RS(   s?   Use ``lambda`` printer but print numbers as ``mpi`` intervals. c         C  s   d t  t |   j |  S(   Ns	   mpi('%s')(   t   superR  t   _print_Integer(   t   selfRS   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR  2  s    c         C  s   d t  t |   j |  S(   Ns	   mpi('%s')(   R  R  t   _print_Rational(   R  RS   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR  5  s    c         C  s   t  t |   j | d t S(   Nt   rational(   R  R  t
   _print_PowR;   (   R  RS   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR  8  s    (   t   __name__t
   __module__t   __doc__R  R  R  (    (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyR  /  s   		c   
      C  s;  t  |   }  |  j r |  |  f S|  j s7 t d   n  t d |  d d d t   } t |  d t } | j d t  } t	 j
 t } } zh xa | s |   }  xK | D]4 \ } }	 | |  j k r |  j |	 k r t } Pq q Wt	 j
 d 9_
 q WWd | t	 _
 X| d k	 r1| j | |	 d	 | d
 | \ } }	 n  | |	 f S(   s<   Give a rational isolating interval for an algebraic number. s+   complex algebraic numbers are not supportedt   modulest   mpmatht   printerR   t   sqfi   NRO   R  (    (   R   R   R  R@   R1   R  R   R;   t	   intervalsR5   R  R   Rb   R   Rr   t   refine_root(
   R   RO   R  t   funcR   R'  R  t   doneRb   R   (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   isolate<  s,    	
			
'N(m   R"  t
   __future__R    R   R   R   R   R   R   R   R   R   R	   R
   R   t   sympy.core.compatibilityR   R   t   sympy.core.exprtoolsR   R   R   t&   sympy.functions.elementary.exponentialR   t(   sympy.functions.elementary.trigonometricR   R   t   sympy.ntheoryR   t   sympy.ntheory.factor_R   R   R   R   t   sympy.polys.orthopolysR   t   sympy.polys.polyerrorsR   R   R   R   R   R   R   R   R   R    R!   R"   R#   R$   R%   t   sympy.polys.polyutilsR&   R'   t   sympy.polys.ring_seriesR(   t   sympy.polys.ringsR)   t   sympy.polys.rootoftoolsR*   t   sympy.polys.specialpolysR+   t   sympy.printing.lambdareprR,   t   sympy.simplify.radsimpR-   Rp   R.   t   sympy.utilitiesR/   R0   R1   R2   R3   R$  R4   R5   RP   Ro   Rx   Rr   R   R   R   R   R   R   R   R   R   R   R}   R;   R   R   R   R   t   __all__R?   R   R   R   R  R  R  R  R  R+  (    (    (    s7   lib/python2.7/site-packages/sympy/polys/numberfields.pyt   <module>   sl   F"F(&	A	8O		5			&	 	$		J_			K		6	# 