U
    ZÊ¹jc!  ã                   @   sÎ   d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZmZmZ ed	d
„ ƒZedd„ ƒZedd„ ƒZeedƒfdd„ƒZeddd„ƒZdS )z/High-level polynomials manipulation functions. é    )ÚSÚBasicÚsymbolsÚDummy)ÚPolificationFailedÚComputationFailedÚMultivariatePolynomialErrorÚOptionError)Úallowed_flagsÚbuild_options)Úpoly_from_exprÚPoly)Úsymmetric_polyÚinterpolating_poly)Úsring)Únumbered_symbolsÚtakeÚpublicc                    s  t |ddgƒ d}t| dƒs&d}| g} t| f|ž|Ž\}} |j}t||ƒ}|j‰ ‡ fdd„tt|ƒƒD ƒ‰ g }| D ],}| ¡ \}}	}
| |j	ˆ Ž |	j	|Ž f¡ qrdd„ t
ˆ |
ƒD ƒ}|jsât|ƒD ]\}\}}| |¡|f||< qÂ|sì|\}|jsö|S |�r||fS ||f S d	S )
a²  
    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, \dots, x_n)`,
    then `f = f(x_{i_1}, x_{i_2}, \dots, x_{i_n})`, where
    `(i_1, i_2, \dots, i_n)` is a permutation of `(1, 2, \dots, 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)])

    Úformalr   TÚ__iter__Fc                    s   g | ]}t ˆ ƒ‘qS © )Únext©Ú.0Úi©r   r   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/polys/polyfuncs.pyÚ
<listcomp>=   s     zsymmetrize.<locals>.<listcomp>c                 S   s    g | ]\}\}}||  ¡ f‘qS r   )Úas_expr)r   ÚsÚ_Úgr   r   r   r   E   s    
 N)r
   Úhasattrr   r   r   ÚrangeÚlenÚ
symmetrizeÚappendr   Úzipr   Ú	enumerateÚsubs)ÚFÚgensÚargsÚiterableÚRÚoptÚresultÚfÚpÚrÚmZpolysr   ÚsymZnon_symr   r   r   r%      s2    !

r%   c           	   
   O   sÂ   t |g ƒ zt| f|ž|Ž\}}W n, tk
rN } z|j W Y ¢S d}~X Y nX tj|j }}|jr€| ¡ D ]}|| | }qln>t	||ƒ|dd…  }}| ¡ D ]}|| t
|f|ž|Ž }q |S )aê  
    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

    Né   )r
   r   r   Úexprr   ZZeroÚgenZis_univariateÚ
all_coeffsr   Úhorner)	r1   r+   r,   r*   r/   ÚexcÚformr8   Úcoeffr   r   r   r:   W   s    !
r:   c                 C   s  t | ƒ}t| tƒr<|| kr&t| | ƒS tt|  ¡ Ž ƒ\}}nvt| d tƒrvtt| Ž ƒ\}}||kr²t|| |¡ ƒS n<|t	d|d ƒkr˜t| |d  ƒS t| ƒ}tt	d|d ƒƒ}zt
||||ƒ ¡ W S  tk
� rü   tƒ }t
||||ƒ ¡  ||¡ Y S X dS )a)  
    Construct an interpolating polynomial for the data points
    evaluated at point x (which can be symbolic or numeric).

    Examples
    ========

    >>> from sympy.polys.polyfuncs import interpolate
    >>> from sympy.abc import a, b, 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

    If the interpolation is going to be used only once then the
    value of interest can be passed instead of passing a symbol:

    >>> interpolate([1, 4, 9], 5)
    25

    Symbolic coordinates are also supported:

    >>> [(i,interpolate((a, b), i)) for i in range(1, 4)]
    [(1, a), (2, b), (3, -a + 2*b)]
    r   r6   N)r$   Ú
isinstanceÚdictr   Úlistr'   ÚitemsÚtupleÚindexr#   r   ÚexpandÚ
ValueErrorr   r)   )ÚdataÚxÚnÚXÚYÚdr   r   r   Úinterpolate�   s$    *
rL   rG   c           
         sD  ddl m} tt| Ž ƒ\}}t|ƒˆ d }|dk r<tdƒ‚|ˆ| d ˆ| d ƒ}ttˆ|ƒƒD ]:}tˆ| d ƒD ]$}	||	|f ||	  ||	|d f< qxqdt|d ƒD ]H}tˆ| d ƒD ]2}	||	|| f  ||	  ||	ˆ| d | f< qÀq¬| ¡ d ‰t	‡ ‡fdd„tˆd ƒD ƒƒt	‡ ‡‡fdd„t|d ƒD ƒƒ S )	aŒ  
    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

    r   )Úonesr6   z'Too few values for the required degree.é   c                 3   s   | ]}ˆ| ˆ |  V  qd S )Nr   r   )rI   r3   r   r   Ú	<genexpr>  s     z'rational_interpolate.<locals>.<genexpr>c                 3   s&   | ]}ˆ|ˆ d   ˆ |  V  qdS )r6   Nr   r   ©rI   Údegnumr3   r   r   rO     s     )
Zsympy.matrices.denserM   r@   r'   r$   r	   r#   ÚmaxZ	nullspaceÚsum)
rF   rQ   rI   rM   ZxdataZydataÚkÚcÚjr   r   rP   r   Úrational_interpolateÏ   s     )$2 ÿrW   Nc              
   O   s:  t |g ƒ t|tƒr$|f| d }}zt| f|ž|Ž\} }W n. tk
rj } ztdd|ƒ‚W 5 d}~X Y nX | jrztdƒ‚|  ¡ }|dk r’t	dƒ‚|dkr¦t
ddd�}t||ƒ}|t|ƒkrÐt	d|t|ƒf ƒ‚|  ¡ |  ¡  }}g d	 }	}
t|dd… ƒD ]8\}}t|d |ƒ}|
||  }|	 ||f¡ |
 }
� qü|	S )
a#  
    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)]

    NÚvieter6   z(multivariate polynomials are not allowedz8Cannot derive Viete's formulas for a constant polynomialr3   )Ústartzrequired %s roots, got %séÿÿÿÿ)r
   r>   r   r   r   r   Zis_multivariater   ZdegreerE   r   r   r$   ZLCr9   r(   r   r&   )r1   Úrootsr+   r,   r/   r;   rH   ÚlcZcoeffsr0   Úsignr   r=   Zpolyr   r   r   rX     s:    

ÿÿ


rX   )N)Ú__doc__Z
sympy.corer   r   r   r   Zsympy.polys.polyerrorsr   r   r   r	   Zsympy.polys.polyoptionsr
   r   Zsympy.polys.polytoolsr   r   Zsympy.polys.specialpolysr   r   Zsympy.polys.ringsr   Zsympy.utilitiesr   r   r   r%   r:   rL   rW   rX   r   r   r   r   Ú<module>   s"   
G
5
A;