K ic!dZddlmZmZmZmZddlmZmZm Z m Z ddl m Z m Z ddlmZmZddlmZmZddlmZddlmZmZmZed Zed Zed Zeed fd ZeddZy)z/High-level polynomials manipulation functions. )SBasicsymbolsDummy)PolificationFailedComputationFailedMultivariatePolynomialError OptionError) allowed_flags build_options)poly_from_exprPoly)symmetric_polyinterpolating_poly)sring)numbered_symbolstakepublicct|ddgd}t|dsd}|g}t|g|i|\}}|j}t ||}|j}t t |Dcgc] }t|}}g}|D]C} | j\} } } |j| j|| j|fEt| D cgc]\} \}}| |jf}}} }|js,t|D]\}\}}|j||f||< |s|\}|js|S|r||fS||fzScc}wcc}}} w)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)]) formalrT__iter__F)r hasattrrrr rangelennext symmetrizeappendas_exprzipr enumeratesubs)FgensargsiterableRoptriresultfprms_gpolyssymnon_syms [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/polyfuncs.pyrrsyB$9-.H 1j ! C  "T "T "DAq 99D d #CkkG&+CI&67tG}7G7 F ?,,.1a yqyy'*IAIIt,<=>?037A ? ?)!Vaa  ?E ? ::!*6!2 3 A~W%'2F1I 3  :: 5= UH$ $/8 @s .EEct|g t|g|i|\}}tj |j }}|jr|jD] }||z|z} |St|||dd}}|jD]}||zt|g|i|z}|S#t$r}|jcYd}~Sd}~wwxYw)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 rexprrZerogen is_univariate all_coeffsrhorner) r*r#r$r"r'excformr9coeffs r4r<r<WsB$1D1D13#D\\^ $E8e#D $ K q#,QR4\\^ ;E8fU:T:T::D ; K xxsB$$ C- B>8C>Cct|}t|tr5||vrt||St t |j \}}nt|dtr5t t |\}}||vrbt||j|S|td|dzvrt||dz St |}t td|dz} t||||jS#t$r9t}t||||jj||cYSwxYw)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)] rr6)r isinstancedictrlistritemstupleindexrrexpand ValueErrorrr!)dataxnXYds r4 interpolaterOs$T D A$ 9T!W: C&'1 d1gu %T #DAqAv1771:''E!QUO#a!e~%T AU1a!e_%AB!!Q1-4466 B G!!Q1-446;;AqAABsC;;?D=<D=rJcp ddlm}tt|\}}t |z dz }|dkr t d||zdz|zdz}t t|D]-}t |zdzD]} || |f|| z|| |dzf</t |dzD]7}t |zdzD]!} || ||z f || z|| |zdz|z f<#9|jd t fdt dzDt fdt |dzDz 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)onesr6z'Too few values for the required degree.c34K|]}||zzywN).0r(rLr,s r4 z'rational_interpolate..s7!q!t 7sc3@K|]}|zdz|zzyw)r6NrU)rVr(rLdegnumr,s r4rWz'rational_interpolate..s'Aq!AJN#ad*As) sympy.matrices.denserQrCrrr rmax nullspacesum) rIrYrLrQxdataydatakcjr(r,s `` @r4rational_interpolatercshR*T #LE5 E VaA1uCDD VaZ!^VaZ!^,A 3vq> "+vzA~& +AAqD'%(*AaQhK ++1q5\=vzA~& =A()!QU( |E!H'>> 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)] Nvieter6z(multivariate polynomials are not allowedz8Cannot derive Viete's formulas for a constant polynomialr,)startz required z roots, got )r rArr rris_multivariater degreerHrrrLCr;r rr)r*rootsr#r$r'r=rKlccoeffsr)signr(r?polys r4rfrf sZ"$%hote11D1D13 ) 68 8  A1u FH H } A. NECJ3u:FGGBrDFfQRj)5a!eU+eBh tUm$u  M= 1C001sD D6$ D11D6rT)__doc__ sympy.corerrrrsympy.polys.polyerrorsrrr r sympy.polys.polyoptionsr r sympy.polys.polytoolsr rsympy.polys.specialpolysrrsympy.polys.ringsrsympy.utilitiesrrrrr<rOrcrfrUrdr4rys50/..A6(#::D%D%N22j>B>BB)08C8Cv55rd