a
    RG5d(                     @   s   d Z ddlmZmZmZmZmZ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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AddMulsymbolsDummy)PolificationFailedComputationFailedMultivariatePolynomialErrorOptionError)allowed_flags)poly_from_exprparallel_poly_from_exprPoly)symmetric_polyinterpolating_poly)numbered_symbolstakepublicc              
      sv  t |ddg d}t| ds&d}| g} zt| g|R i |\} }W n ty } zg }|jD ],}|jrz||tjf q^t	dt
| |q^|s|\}|jjs|W  Y d}~S |r|g fW  Y d}~S |g f W  Y d}~S W Y d}~n
d}~0 0 g |j }}	|j|j }}
tt
|D ]0}t|d |dd	}|t|	||
f qttt
|d }ttt
|d
d}g }| D ]}g }|js||  || |j8 }|rd\}}}t| D ]X\}\ }t fdd|D rtdd t| D }||kr| |  }}}q|dkr||  }nqg }t  dd d D ]\}}|||  qXdd t||D }dd t||D }|t|g|R   |d
 |}|dd D ]}||}q||8 }q|t| |  f qzdd |D }|jsBt|D ] \}\}}|!||f||< q |sN|\}|jsZ|S |rh||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__F
symmetrizeN   )polysr   )r   NNc                 3   s"   | ]} |  |d   kV  qdS r   N .0imonomr   Q/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/polys/polyfuncs.py	<genexpr>e       zsymmetrize.<locals>.<genexpr>c                 S   s   g | ]\}}|| qS r   r   )r   nmr   r   r"   
<listcomp>f   r$   zsymmetrize.<locals>.<listcomp>)r   c                 S   s   g | ]\\}}}|| qS r   r   )r   s_r%   r   r   r"   r'   u   r$   c                 S   s   g | ]\\}}}|| qS r   r   )r   r)   pr%   r   r   r"   r'   v   r$   c                 S   s   g | ]\}}||  fqS r   )as_expr)r   r(   r*   r   r   r"   r'      r$   )"r   hasattrr   r   exprs	is_Numberappendr   Zeror	   lenoptr   r   gensdomainranger   next
set_domainlistis_homogeneousTCas_poly	enumeratetermsallmaxzipr   mulr   r+   subs)Fr3   argsiterabler2   excresultexprr   r   domr   polyindicesweightsf	symmetricZ_heightZ_monom_coeffcoeffheight	exponentsm1m2termproductr*   symZnon_symr   r    r"   r      s    !

,



r   c           	   
   O   s   t |g  zt| g|R i |\}}W n* tyR } z|jW  Y d}~S d}~0 0 tj|j }}|jr| D ]}|| | }qpnDt	|||dd  }}| D ]"}|| t
|g|R i | }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

    Nr   )r   r   r   rH   r   r0   genis_univariate
all_coeffsr   horner)	rM   r3   rD   rC   r2   rF   formrX   rP   r   r   r"   r[      s    !
 r[   c                 C   s   t | }t| tr<|| v r&t| | S tt|   \}}nvt| d trvtt|  \}}||v rt||| S n<|t	d|d v rt| |d  S t| }tt	d|d }zt
|||| W S  t y   t }t
|||| || Y S 0 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   r   N)r1   
isinstancedictr   r8   r@   itemstupleindexr5   r   expand
ValueErrorr   rB   )dataxr%   XYdr   r   r"   interpolate   s$    *
ri   re   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< qq| 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   )onesr   z'Too few values for the required degree.   c                 3   s   | ]}|  |  V  qd S )Nr   r   )rf   rr   r"   r#   D  r$   z'rational_interpolate.<locals>.<genexpr>c                 3   s&   | ]}| d    |  V  qdS r   r   r   rf   degnumrl   r   r"   r#   E  r$   )
Zsympy.matrices.denserj   r8   r@   r1   r   r5   r?   Z	nullspacesum)
rd   rn   rf   rj   ZxdataZydatakcjr   r   rm   r"   rational_interpolate  s     )$2 rs   Nc              
   O   sB  t |g  t|tr$|f| d }}zt| g|R i |\} }W n0 tyr } ztdd|W Y d}~n
d}~0 0 | jrtd|  }|dk rt	d|du rt
ddd}t||}|t|krt	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vieter   z(multivariate polynomials are not allowedz8Cannot derive Viete's formulas for a constant polynomialrl   )startzrequired %s roots, got %sr   )r   r]   r   r   r   r	   is_multivariater
   degreerc   r   r   r1   LCrZ   r<   r   r/   )rM   rootsr3   rD   r2   rF   r%   lccoeffsrG   signr   rP   rJ   r   r   r"   rt   H  s:    

"


rt   )N)__doc__
sympy.corer   r   r   r   r   r   sympy.polys.polyerrorsr   r	   r
   r   Zsympy.polys.polyoptionsr   sympy.polys.polytoolsr   r   r   sympy.polys.specialpolysr   r   Zsympy.utilitiesr   r   r   r   r[   ri   rs   rt   r   r   r   r"   <module>   s"    
 
5
A;