
    Ed(                        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             Zed	             Zed
             Ze ed          fd            Ze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           	         t          |ddg           d}t          | d          sd}| g} 	 t          | g|R i |\  } }n# t          $ r}g }|j        D ]H}|j        r"|                    |t          j        f           +t          dt          |           |          |s|\  }|j        j        s|cY d}~S |r	|g fcY d}~S |g fz   cY d}~S d}~ww xY wg |j        }	}|j        |j        }
}t!          t          |                    D ]N}t#          |dz   |d	          }|                    t%          |	          |                    |
          f           Ot)          t!          t          |          dz
                      }t)          t!          t          |          d
d                    }g }| D ]}g }|j        sV|                    |                                           ||                                                    |j                  z  }|r_d\  }}}t1          |                                          D ]V\  }\  }t5          fd|D                       r3t7          d t9          |          D                       }||k    r||}}}W|dk    r||c}nng }t9          dd         dz             D ]\  }}|                    ||z
             d t9          ||          D             }d t9          ||          D             }|                    t;          |g|R             |d
                             |          }|dd         D ]}|                    |          }||z  }|_|                    t?          | |                                 f           d |D             }|j        s2t1          |          D ]"\  }\  }}|!                    |          |f||<   #|s|\  }|j        s|S |r||fS ||fz   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   B   K   | ]}|         |d z            k    V  dS r   N ).0imonoms     5lib/python3.11/site-packages/sympy/polys/polyfuncs.py	<genexpr>zsymmetrize.<locals>.<genexpr>e   s4      AAAuQx5Q</AAAAAA    c                     g | ]
\  }}||z  S r   r   )r    nms      r#   
<listcomp>zsymmetrize.<locals>.<listcomp>f   s     !F!F!F$!Q!A#!F!F!Fr%   )r   c                 $    g | ]\  \  }}}||z  S r   r   )r    s_r'   s       r#   r)   zsymmetrize.<locals>.<listcomp>u   $    @@@YVaQAqD@@@r%   c                 $    g | ]\  \  }}}||z  S r   r   )r    r,   pr'   s       r#   r)   zsymmetrize.<locals>.<listcomp>v   r-   r%   c                 @    g | ]\  }}||                                 fS r   )as_expr)r    r+   r/   s      r#   r)   zsymmetrize.<locals>.<listcomp>   s)    000$!Qa000r%   )"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   r1   subs)Fr9   argsiterabler8   excresultexprr   r   domr!   polyindicesweightsf	symmetric_height_monom_coeffcoeffheight	exponentsm1m2termproductr/   symnon_symr"   s                                 @r#   r   r      s   B $9-...H1j!! C&(:T:::T::33 & & &I 	C 	CD~ CtQVn----'c!ffcBBB 	GFw~ 	&MMMMMM &rz!!!!!!~%%%%%%%&( 7E#*#D3t99 < <a!eT666d7mmT__S%9%9:;;;;5TQ''((G5TAr**++GF &6 &6	 	(QTTVV$$$'''A 	&4#GVV%.qwwyy%9%9 G G!>E5AAAAAAAAA G !F!F#gu2E2E!F!F!FGGF' G28%"} %vuuIeU122Y%566 * *B  b))))@@#eY*?*?@@@D@@#eY*?*?@@@DS....///1gkk%((G!""X ) )!++a..LA;  	> 	sI		4555500%000E: 3!*6!2!2 	3 	3A~W%'2F1II : % 	%5= UH$$s6   A   
C
A%C/C5C:C CCCc                    t          |g            	 t          | g|R i |\  }}n# t          $ r}|j        cY d}~S d}~ww xY wt          j        |j        }}|j        r |                                D ]
}||z  |z   }nGt          ||          |dd         }}|                                D ]}||z  t          |g|R i |z   }|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	   rN   r   r6   genis_univariate
all_coeffsr   horner)	rS   r9   rJ   rI   r8   rL   formrb   rX   s	            r#   re   re      s   B $1D111D1133   x #D ;\\^^ 	$ 	$E8e#DD	$ q#,,QRR4\\^^ 	; 	;E8fU:T:::T:::DDKs   & 
A<AAc                 D   t          |           }t          | t                    rE|| v rt          | |                   S t	          t          |                                            \  }}nt          | d         t                    rFt	          t          |            \  }}||v r(t          ||                    |                             S n\|t          d|dz             v rt          | |dz
                     S t	          |           }t	          t          d|dz                       }	 t          ||||                                          S # t          $ rI t                      }t          ||||                                                              ||          cY S w xY w)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   )r7   
isinstancedictr   r>   rF   itemstupleindexr;   r   expand
ValueErrorr   rH   )dataxr'   XYds         r#   interpolatert      s   T 	D		A$ &9 	T!W::C&''11d1gu%% 	&T
##DAqAv (1771::'''( E!QUOO# &a!e~~%T

AU1a!e__%%AB!!Q1--44666 B B BGG!!Q1--4466;;AqAAAAABs   (#E AFFrp   c                   
 ddl m} t          t          |            \  }}t	          |          z
  dz
  }|dk     rt          d           ||z   dz   |z   dz             }t          t          |                    D ]5}t          |z   dz             D ]}	||	|f         ||	         z  ||	|dz   f<   6t          |dz             D ]?}t          |z   dz             D ]'}	||	||z
  f          ||	         z  ||	|z   dz   |z
  f<   (@|                                d         
t          
fdt          dz             D                       t          
fdt          |dz             D                       z  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   4   K   | ]}|         |z  z  V  d S Nr   )r    r!   rq   rs     r#   r$   z'rational_interpolate.<locals>.<genexpr>D  s/      77!q!t777777r%   c              3   @   K   | ]}|z   d z            |z  z  V  dS r   r   )r    r!   rq   degnumrz   s     r#   r$   z'rational_interpolate.<locals>.<genexpr>E  s9      AAq!AJN#ad*AAAAAAr%   )
sympy.matrices.denserv   r>   rF   r7   r   r;   rE   	nullspacesum)ro   r|   rq   rv   xdataydatakcjr!   rz   s    ``       @r#   rational_interpolater     s   R *)))))T
##LE5E

VaA1u ECDDDVaZ!^VaZ!^,,A3vq>>"" + +vzA~&& 	+ 	+AAqD'%(*AaQhKK	+1q5\\ = =vzA~&& 	= 	=A()!QU(|E!H'<Aa!a!##$$	=	aA77777U6A:%6%677777AAAAAAE!a%LLAAAAAB Cr%   Nc                    t          |g            t          |t                    r|f|z   d}}	 t          | g|R i |\  } }n## t          $ r}t          dd|          d}~ww xY w| j        rt          d          |                                 }|dk     rt          d          |t          dd          }t          ||          }|t          |          k    r"t          d|d	t          |                    |                                 |                                 }}g d
}
}	t          |dd                   D ]:\  }}t!          |dz   |          }|
||z  z  }|	                    ||f           |
 }
;|	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 polynomialrz   )startz	required z roots, got r   )r   rh   r   r   r	   r
   is_multivariater   degreern   r   r   r7   LCrd   rB   r   r5   )rS   rootsr9   rJ   r8   rL   r'   lccoeffsrM   signr!   rX   rP   s                 r#   r   r   H  s   " $% ,hote11D111D1133 1 1 1C0001 	 8)68 8 	8 	


A1u HFH H 	H  / A...NNECJJ Hj3u:::FGGGBrDFfQRRj))  5a!eU++eBhtUm$$$uMs   A 
A#AA#ry   )__doc__
sympy.corer   r   r   r   r   r   sympy.polys.polyerrorsr	   r
   r   r   sympy.polys.polyoptionsr   sympy.polys.polytoolsr   r   r   sympy.polys.specialpolysr   r   sympy.utilitiesr   r   r   r   re   rt   r   r   r   r%   r#   <module>r      s   5 5 : 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9. . . . . . . . . . . . 2 1 1 1 1 13 3 3 3 3 3 3 3 3 3( ( ( ( ( ( ( ( : : : : : : : : : :A% A% A%H 2 2 2j >B >B >BB )0 8C 8C 8C 8Cv 5 5 5 5 5 5r%   