ó
~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	 d d l
 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 m Z d d	 l m Z m Z d d
 l m Z m Z m Z e d „  ƒ Z  e d „  ƒ Z! e d „  ƒ Z" e e	 d ƒ d „ ƒ Z# e d d „ ƒ Z% d S(   s/   High-level polynomials manipulation functions. iÿÿÿÿ(   t   print_functiont   division(   t   St   Basict   Addt   Mult   symbols(   t   range(   t	   factorial(   t   PolificationFailedt   ComputationFailedt   MultivariatePolynomialErrort   OptionError(   t   allowed_flags(   t   poly_from_exprt   parallel_poly_from_exprt   Poly(   t   symmetric_polyt   interpolating_poly(   t   numbered_symbolst   taket   publicc   "        sž  t  | d d g ƒ t } t |  d ƒ s: t } |  g }  n  y t |  | | Ž \ }  } Wn¤ t k
 rü } g  } xŒ | j D]@ } | j r  | j | t	 j
 f ƒ qx t d t |  ƒ | ƒ ‚ qx W| sÎ | \ } n  | j j sÞ | S| rî | g  f S| g  f Sn Xg  | j } }	 | j | j } }
 xU t t | ƒ ƒ D]A } t | d | d t ƒ} | j t |	 ƒ | j |
 ƒ f ƒ q3Wt t t | ƒ d ƒ ƒ } t t t | ƒ d d ƒ ƒ } g  } x2|  D]*} g  } | j sú| j | j ƒ  ƒ | | j ƒ  8} n  xÍ| rÉd \ } } } xš t | j ƒ  ƒ D]† \ } \ ‰  } t ‡  f d	 †  | Dƒ ƒ r%t g  t | ˆ  ƒ D] \ } } | | ^ qfƒ } | | k r«| ˆ  | } } } q«q%q%W| d k rË| | ‰  } n Pg  } x6 t ˆ  ˆ  d d ƒ D] \ } } | j | | ƒ qêWg  t | | ƒ D] \ \ } } } | | ^ q} g  t | | ƒ D] \ \ } } } | | ^ qM} | j t | | Œ ƒ | d j  | ƒ } x! | d D] } | j  | ƒ } q£W| | 8} qýW| j t! | Œ  | j" ƒ  f ƒ q¿Wg  | D] \ } } | | j" ƒ  f ^ qô} | j s`x< t | ƒ D]+ \ } \ }  }! |  j# | ƒ |! f | | <q.Wn  | sr| \ } n  | j s| S| r| | f S| | f Sd
 S(   s´  
    Rewrite a polynomial in terms of elementary symmetric polynomials.

    A symmetric polynomial is a multivariate polynomial that remains invariant
    under any variable permutation, i.e., if ``f = f(x_1, x_2, ..., x_n)``,
    then ``f = f(x_{i_1}, x_{i_2}, ..., x_{i_n})``, where
    ``(i_1, i_2, ..., i_n)`` is a permutation of ``(1, 2, ..., n)`` (an
    element of the group ``S_n``).

    Returns a tuple of symmetric polynomials ``(f1, f2, ..., fn)`` such that
    ``f = f1 + f2 + ... + fn``.

    Examples
    ========

    >>> from sympy.polys.polyfuncs import symmetrize
    >>> from sympy.abc import x, y

    >>> symmetrize(x**2 + y**2)
    (-2*x*y + (x + y)**2, 0)

    >>> symmetrize(x**2 + y**2, formal=True)
    (s1**2 - 2*s2, 0, [(s1, x + y), (s2, x*y)])

    >>> symmetrize(x**2 - y**2)
    (-2*x*y + (x + y)**2, -2*y**2)

    >>> symmetrize(x**2 - y**2, formal=True)
    (s1**2 - 2*s2, -2*y**2, [(s1, x + y), (s2, x*y)])

    t   formalR   t   __iter__t
   symmetrizei   t   polysi    iÿÿÿÿc         3  s'   |  ] } ˆ  | ˆ  | d  k Vq d S(   i   N(    (   t   .0t   i(   t   monom(    s4   lib/python2.7/site-packages/sympy/polys/polyfuncs.pys	   <genexpr>h   s    N(   iÿÿÿÿNN(   i    ($   R   t   Truet   hasattrt   FalseR   R	   t   exprst	   is_Numbert   appendR   t   ZeroR
   t   lent   optR   R   t   genst   domainR   R   t   nextt
   set_domaint   listt   is_homogeneoust   TCt   Nonet	   enumeratet   termst   allt   maxt   zipR   t   mulR   t   as_exprt   subs("   t   FR&   t   argst   iterableR%   t   exct   resultt   exprR   R   t   domR   t   polyt   indicest   weightst   ft	   symmetrict   _heightt   _monomt   _coefft   coefft   nt   mt   heightt	   exponentst   m1t   m2t   st   _t   termt   pt   productt   symt   non_sym(    (   R   s4   lib/python2.7/site-packages/sympy/polys/polyfuncs.pyR      s‚    !	
&		%2$22#+	 	
c   	      O  sÖ   t  | g  ƒ y t |  | | Ž \ } } Wn t k
 rB } | j SXt j | j } } | j r‡ xp | j ƒ  D] } | | | } ql WnK t	 | | ƒ | d } } x. | j ƒ  D]  } | | t
 | | | Ž } q® W| S(   sê  
    Rewrite a polynomial in Horner form.

    Among other applications, evaluation of a polynomial at a point is optimal
    when it is applied using the Horner scheme ([1]).

    Examples
    ========

    >>> from sympy.polys.polyfuncs import horner
    >>> from sympy.abc import x, y, a, b, c, d, e

    >>> horner(9*x**4 + 8*x**3 + 7*x**2 + 6*x + 5)
    x*(x*(x*(9*x + 8) + 7) + 6) + 5

    >>> horner(a*x**4 + b*x**3 + c*x**2 + d*x + e)
    e + x*(d + x*(c + x*(a*x + b)))

    >>> f = 4*x**2*y**2 + 2*x**2*y + 2*x*y**2 + x*y

    >>> horner(f, wrt=x)
    x*(x*y*(4*y + 2) + y*(2*y + 1))

    >>> horner(f, wrt=y)
    y*(x*y*(4*x + 2) + x*(2*x + 1))

    References
    ==========
    [1] - https://en.wikipedia.org/wiki/Horner_scheme

    i   (   R   R   R	   R;   R   R#   t   gent   is_univariatet
   all_coeffsR   t   horner(	   R@   R&   R7   R6   R%   R9   t   formRS   RE   (    (    s4   lib/python2.7/site-packages/sympy/polys/polyfuncs.pyRV   —   s    !	c         C  s’  t  |  ƒ } d } t |  t ƒ rW t t |  j ƒ  Œ  ƒ \ } } t | | | | ƒ } n1t |  d t ƒ rš t t |  Œ  ƒ \ } } t | | | | ƒ } nî t |  ƒ } t	 g  t
 d | d ƒ D] } | | ^ q½ Œ  } | d d k r÷ t | d ƒ n t | d ƒ } g  }	 xF t
 d | d ƒ D]1 } |	 j | | | | ƒ | | | | } q!Wt g  t |	 | ƒ D] \ }
 } |
 | ^ qiŒ  } | j ƒ  S(   s¯  
    Construct an interpolating polynomial for the data points.

    Examples
    ========

    >>> from sympy.polys.polyfuncs import interpolate
    >>> from sympy.abc import x

    A list is interpreted as though it were paired with a range starting
    from 1:

    >>> interpolate([1, 4, 9, 16], x)
    x**2

    This can be made explicit by giving a list of coordinates:

    >>> interpolate([(1, 1), (2, 4), (3, 9)], x)
    x**2

    The (x, y) coordinates can also be given as keys and values of a
    dictionary (and the points need not be equispaced):

    >>> interpolate([(-1, 2), (1, 2), (2, 5)], x)
    x**2 + 1
    >>> interpolate({-1: 2, 1: 2, 2: 5}, x)
    x**2 + 1

    i    i   i   N(   R$   R-   t
   isinstancet   dictR*   R2   t   itemsR   t   tupleR   R   R   R"   R   t   expand(   t   datat   xRF   R=   t   Xt   YR   t   numertt   denomt   coeffsRE   t   y(    (    s4   lib/python2.7/site-packages/sympy/polys/polyfuncs.pyt   interpolateÍ   s"    012R^   c   
        s©  d d l  m } t t |  Œ  ƒ \ } } t | ƒ ˆ d } | d k  rW t d ƒ ‚ n  | ˆ | d ˆ | d ƒ } xb t t ˆ | ƒ ƒ D]K } xB t ˆ | d ƒ D], }	 | |	 | f | |	 | |	 | d f <q§ WqŒ Wxj t | d ƒ D]X } xO t ˆ | d ƒ D]9 }	 | |	 | | f | |	 | |	 ˆ | d | f <qWqì W| j ƒ  d ‰ t	 ‡  ‡ f d †  t ˆ d ƒ Dƒ ƒ t	 ‡  ‡ ‡ f d †  t | d ƒ Dƒ ƒ S(	   sŒ  
    Returns a rational interpolation, where the data points are element of
    any integral domain.

    The first argument  contains the data (as a list of coordinates). The
    ``degnum`` argument is the degree in the numerator of the rational
    function. Setting it too high will decrease the maximal degree in the
    denominator for the same amount of data.

    Examples
    ========

    >>> from sympy.polys.polyfuncs import rational_interpolate

    >>> data = [(1, -210), (2, -35), (3, 105), (4, 231), (5, 350), (6, 465)]
    >>> rational_interpolate(data, 2)
    (105*x**2 - 525)/(x + 1)

    Values do not need to be integers:

    >>> from sympy import sympify
    >>> x = [1, 2, 3, 4, 5, 6]
    >>> y = sympify("[-1, 0, 2, 22/5, 7, 68/7]")
    >>> rational_interpolate(zip(x, y), 2)
    (3*x**2 - 7*x + 2)/(x + 1)

    The symbol for the variable can be changed if needed:
    >>> from sympy import symbols
    >>> z = symbols('z')
    >>> rational_interpolate(data, 2, X=z)
    (105*z**2 - 525)/(z + 1)

    References
    ==========

    .. [1] Algorithm is adapted from:
           http://axiom-wiki.newsynthesis.org/RationalInterpolation

    iÿÿÿÿ(   t   onesi   i    s'   Too few values for the required degree.i   c         3  s!   |  ] } ˆ | ˆ  | Vq d  S(   N(    (   R   R   (   R_   t   r(    s4   lib/python2.7/site-packages/sympy/polys/polyfuncs.pys	   <genexpr>=  s    c         3  s)   |  ] } ˆ | ˆ d  ˆ  | Vq d S(   i   N(    (   R   R   (   R_   t   degnumRg   (    s4   lib/python2.7/site-packages/sympy/polys/polyfuncs.pys	   <genexpr>>  s    (
   t   sympy.matrices.denseRf   R*   R2   R$   R   R   R1   t	   nullspacet   sum(
   R]   Rh   R_   Rf   t   xdatat   ydatat   kt   ct   jR   (    (   R_   Rh   Rg   s4   lib/python2.7/site-packages/sympy/polys/polyfuncs.pyt   rational_interpolate  s    ).;&c         O  s  t  | g  ƒ t | t ƒ r3 | f | d	 } } n  y t |  | | Ž \ }  } Wn% t k
 rv } t d d | ƒ ‚ n X|  j r t d ƒ ‚ n  |  j	 ƒ  } | d k  r¶ t
 d ƒ ‚ n  | d	 k r× t d d d ƒ} n  t | | ƒ } | t | ƒ k rt
 d | t | ƒ f ƒ ‚ n  |  j ƒ  |  j ƒ  } } g  d }	 }
 xY t | d ƒ D]G \ } } t | d | ƒ } |
 | | } |	 j | | f ƒ |
 }
 qNW|	 S(
   s#  
    Generate Viete's formulas for ``f``.

    Examples
    ========

    >>> from sympy.polys.polyfuncs import viete
    >>> from sympy import symbols

    >>> x, a, b, c, r1, r2 = symbols('x,a:c,r1:3')

    >>> viete(a*x**2 + b*x + c, [r1, r2], x)
    [(r1 + r2, -b/a), (r1*r2, c/a)]

    t   vietei   s(   multivariate polynomials are not alloweds7   can't derive Viete's formulas for a constant polynomialRg   t   starts   required %s roots, got %siÿÿÿÿN(   R   RX   R   R-   R   R	   R
   t   is_multivariateR   t   degreet
   ValueErrorR   R   R$   t   LCRU   R.   R   R"   (   R@   t   rootsR&   R7   R%   R9   RF   t   lcRc   R:   t   signR   RE   R=   (    (    s4   lib/python2.7/site-packages/sympy/polys/polyfuncs.pyRr   A  s6    	N(&   t   __doc__t
   __future__R    R   t
   sympy.coreR   R   R   R   R   t   sympy.core.compatibilityR   t(   sympy.functions.combinatorial.factorialsR   t   sympy.polys.polyerrorsR	   R
   R   R   t   sympy.polys.polyoptionsR   t   sympy.polys.polytoolsR   R   R   t   sympy.polys.specialpolysR   R   t   sympy.utilitiesR   R   R   R   RV   Re   Rq   R-   Rr   (    (    (    s4   lib/python2.7/site-packages/sympy/polys/polyfuncs.pyt   <module>   s    ("…68;