K i;dZddlmZddlmZddlmZmZddlm Z m Z m Z ddl m Z ddlmZddlmZmZdd lmZdd lmZmZdd lmZdd lmZdd lmZGddZGddZy)a This module contains functions for two multivariate resultants. These are: - Dixon's resultant. - Macaulay's resultant. Multivariate resultants are used to identify whether a multivariate system has common roots. That is when the resultant is equal to zero. )prod)Mul)Matrixdiag)Poly degree_listrem)simplify) IndexedBase) itermonomials monomial_deg) monomial_key)poly_from_expr total_degree)binomial)combinations_with_replacement)sympy_deprecation_warningcVeZdZdZdZedZdZdZdZ dZ dZ d Z d Z d Zy ) DixonResultantaG A class for retrieving the Dixon's resultant of a multivariate system. Examples ======== >>> from sympy import symbols >>> from sympy.polys.multivariate_resultants import DixonResultant >>> x, y = symbols('x, y') >>> p = x + y >>> q = x ** 2 + y ** 3 >>> h = x ** 2 + y >>> dixon = DixonResultant(variables=[x, y], polynomials=[p, q, h]) >>> poly = dixon.get_dixon_polynomial() >>> matrix = dixon.get_dixon_matrix(polynomial=poly) >>> matrix Matrix([ [ 0, 0, -1, 0, -1], [ 0, -1, 0, -1, 0], [-1, 0, 1, 0, 0], [ 0, -1, 0, 0, 1], [-1, 0, 0, 1, 0]]) >>> matrix.det() 0 See Also ======== Notebook in examples: sympy/example/notebooks. References ========== .. [1] [Kapur1994]_ .. [2] [Palancz08]_ c||_||_t|j|_t|j|_t d}t |jDcgc]}|| c}|_t |jDcgc] tfd|jD"c}|_ ycc}wcc}w)aV A class that takes two lists, a list of polynomials and list of variables. Returns the Dixon matrix of the multivariate system. Parameters ---------- polynomials : list of polynomials A list of m n-degree polynomials variables: list A list of all n variables alphac3:K|]}t|yw)N)r).0polyis i/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/multivariate_resultants.py z*DixonResultant.__init__..[s S$T!21!5 SsN) polynomials variableslennmr rangedummy_variablesmax _max_degrees)selfrrars `r__init__zDixonResultant.__init__Ds'"T^^$T%%&  .3DFFm<!<466]$! S$BRBR SS$ =$s & C%Cc6tddd|jS)NzS The max_degrees property of DixonResultant is deprecated. 1.5$deprecated-dixonresultant-propertiesdeprecated_since_versionactive_deprecations_target)rr&r's r max_degreeszDixonResultant.max_degrees^s%! &+'M     c |j|jdzk7r td|jg}t |j }t |jD]k}|j|||<tt|j |}|j|jDcgc]}|j|c}mt|}t|j |j}t|D cgc] \}} || z  c} }} |j| z j} t!| |jdScc}wcc} }w)a Returns ======= dixon_polynomial: polynomial Dixon's polynomial is calculated as: delta = Delta(A) / ((x_1 - a_1) ... (x_n - a_n)) where, A = |p_1(x_1,... x_n), ..., p_n(x_1,... x_n)| |p_1(a_1,... x_n), ..., p_n(a_1,... x_n)| |... , ..., ...| |p_1(a_1,... a_n), ..., p_n(a_1,... a_n)| z%Method invalid for given combination.r)r"r! ValueErrorrlistrr#r$dictzipappendsubsrrdetfactorr) r'rowstempidx substitutionfAtermsr(bproduct_of_differencesdixon_polynomials rget_dixon_polynomialz#DixonResultant.get_dixon_polynomialis) 66dffqj !DE E  !DNN#= JC,,S1DIDNND 9:L KKt7G7GH! -H I J 4LDNND$8$89!$&?Aq1u&?!@EEG&<<DDF.0D0DEaHHI '@s ,E E ctdddt|jDcgc]!}|j||j|z#}}t |}t |j}t|Scc}w)Nz The get_upper_degree() method of DixonResultant is deprecated. Use get_max_degrees() instead. r+r,r-) rr#r!rr&rrmonomsr )r'rlist_of_productsproducts rget_upper_degreezDixonResultant.get_upper_degrees! &+'M  &+466]4 !!NN1-1B1B11EE44'(w-&&(W%% 4s&A:c |jDcgc]!}tt||j#}}t |Dcgc] }t |}}|Scc}wcc}w)z Returns a list of the maximum degree of each variable appearing in the coefficients of the Dixon polynomial. The coefficients are viewed as polys in $x_1, x_2, \dots, x_n$. )coeffsrrrr8r%)r' polynomialr deg_listsdegsr1s rget_max_degreeszDixonResultant.get_max_degreessg",!2!2!46!dDNN!;<6 6.1)_=Ts4y= = 6>s &AA c 8|j|}t|j|}t|dt d|j}t |j Dcgc]5}|Dcgc]'}t|g|jj|)c}7c}}}|jd|jdk7rGt|jdDcgc]}td|dd|fDr|}}|dd|f}|Scc}wcc}}wcc}w) z Construct the Dixon matrix from the coefficients of polynomial \alpha. Each coefficient is viewed as a polynomial of x_1, ..., x_n. Tlexreversekeyrr4c3&K|] }|dk7 ywrN)relements rrz2DixonResultant.get_dixon_matrix..s4G7a<4N) rRr rsortedrrrNrcoeff_monomialshaper#any) r'rOr1 monomialscr" dixon_matrixcolumnkeeps rget_dixon_matrixzDixonResultant.get_dixon_matrixs)**:6 "$..+> 9d+E4>>BD )3):):)<>$%*3 4$%!%Q 8 8 G G J 4>?   a L$6$6q$9 9).|/A/A"/E)F5v4'6 2445D5(40L 4> 5s" D +,D D "D D c Ljryj\}}tjdt |Dcgc]#t fdt |Ds"%}}|ddft dg|dz zdgzg}dddf|k(ryycc}w)a Test for the validity of the Kapur-Saxena-Yang precondition. The precondition requires that the column corresponding to the monomial 1 = x_1 ^ 0 * x_2 ^ 0 * ... * x_n ^ 0 is not a linear combination of the remaining ones. In SymPy notation this is the last column. For the precondition to hold the last non-zero row of the rref matrix should be of the form [0, 0, ..., 1]. Frc32K|]}|fdk7ywrZr[)rjrmatrixs rrz2DixonResultant.KSY_precondition..s*O6!Q$<1+<*OsNr4rXT)is_zero_matrixr`r rrefr#rar)r'rkr"r!rr= conditions ` ` rKSY_preconditionzDixonResultant.KSY_preconditions  ||1&++-*+ 8Pas*OeAh*O'OPPQQC1IO,- "Q$<9 $Qs #B!-B!ct|jDcgc] }|j|jr|"}}t|jDcgc] }|j |jr|"}}|||fScc}wcc}w)z/Remove the zero rows and columns of the matrix.)r#r=rowrlcolscol)r'rkrr=rjrrs rdelete_zero_rows_and_columnsz+DixonResultant.delete_zero_rows_and_columnssV[[)OA1M1MAOOV[[)OA1M1MAOOdDj!! OOs BB B 7B cd}t|jD]$}|j|D]}|dk7s ||z}$&|S)z;Calculate the product of the leading entries of the matrix.r4r)r#r=rq)r'rkresrqels rproduct_leading_entriesz&DixonResultant.product_leading_entriessP% Cjjo 7(C    r2c|j|}|j\}}}|jt|}|j|S)z@Calculate the Kapur-Saxena-Yang approach to the Dixon Resultant.)rtLUdecompositionr rx)r'rk_Us rget_KSY_Dixon_resultantz&DixonResultant.get_KSY_Dixon_resultantsK226:((*1a228A;?++F33r2N)__name__ __module__ __qualname____doc__r)propertyr1rGrLrRrgrortrxr}r[r2rrrsK(T$4!!"IH& 86"4r2rc@eZdZdZdZdZdZdZdZdZ dZ d Z y ) MacaulayResultanta- A class for calculating the Macaulay resultant. Note that the polynomials must be homogenized and their coefficients must be given as symbols. Examples ======== >>> from sympy import symbols >>> from sympy.polys.multivariate_resultants import MacaulayResultant >>> x, y, z = symbols('x, y, z') >>> a_0, a_1, a_2 = symbols('a_0, a_1, a_2') >>> b_0, b_1, b_2 = symbols('b_0, b_1, b_2') >>> c_0, c_1, c_2,c_3, c_4 = symbols('c_0, c_1, c_2, c_3, c_4') >>> f = a_0 * y - a_1 * x + a_2 * z >>> g = b_1 * x ** 2 + b_0 * y ** 2 - b_2 * z ** 2 >>> h = c_0 * y * z ** 2 - c_1 * x ** 3 + c_2 * x ** 2 * z - c_3 * x * z ** 2 + c_4 * z ** 3 >>> mac = MacaulayResultant(polynomials=[f, g, h], variables=[x, y, z]) >>> mac.monomial_set [x**4, x**3*y, x**3*z, x**2*y**2, x**2*y*z, x**2*z**2, x*y**3, x*y**2*z, x*y*z**2, x*z**3, y**4, y**3*z, y**2*z**2, y*z**3, z**4] >>> matrix = mac.get_matrix() >>> submatrix = mac.get_submatrix(matrix) >>> submatrix Matrix([ [-a_1, a_0, a_2, 0], [ 0, -a_1, 0, 0], [ 0, 0, -a_1, 0], [ 0, 0, 0, -a_1]]) See Also ======== Notebook in examples: sympy/example/notebooks. References ========== .. [1] [Bruce97]_ .. [2] [Stiller96]_ cD||_||_t||_|jDcgc]}t |g|jc}|_|j |_|j|_ |j|j|_ ycc}w)z Parameters ========== variables: list A list of all n variables polynomials : list of SymPy polynomials A list of m n-degree polynomials N) rrr r!rdegrees _get_degree_mdegree_mget_sizemonomials_sizeget_monomials_of_certain_degree monomial_set)r'rrrs rr)zMacaulayResultant.__init__+s'"Y ++- T;DNN;- **, "mmo!@@O-sBc@dtd|jDzS)z Returns ======= degree_m: int The degree_m is calculated as 1 + \sum_1 ^ n (d_i - 1), where d_i is the degree of the i polynomial r4c3&K|] }|dz  yw)r4Nr[)rds rrz2MacaulayResultant._get_degree_m..Ls3q1u3r])sumrr0s rrzMacaulayResultant._get_degree_mCs33dll3333r2cht|j|jzdz |jdz S)z Returns ======= size: int The size of set T. Set T is the set of all possible monomials of the n variables for degree equal to the degree_m r4)rrr!r0s rrzMacaulayResultant.get_sizeNs+ .2DFFQJ??r2ct|j|Dcgc] }t| }}t|dt d|jScc}w)zw Returns ======= monomials: list A list of monomials of a certain degree. TrTrU)rrrr^r)r'degreemonomialrbs rrz1MacaulayResultant.get_monomials_of_certain_degreeZsU6dnn6<>?S(^? ?i&udnn=? ? ?sA cg}g}t|jD]}|dk(r?|j|j|z }|j |}|j |G|j |j |dz |j|dz z|j|j|z }|j |}|D]-}|D]&}t||dk(s|D cgc] } | |k7r|  }} (/|j ||Scc} w)z Returns ======= row_coefficients: list The row coefficients of Macaulay's matrix rr4)r#r!rrrr9rr ) r'row_coefficients divisiblerrr poss_rowsdivpitems rget_row_coefficientsz&MacaulayResultant.get_row_coefficientsis% tvv 3AAva8??G ''1  A!6!%a!e!4"56a8 @@H $7C&7q#;!+:C)7$,0AI*.)7I)777 !'' 2 3  )7s D c\g}|j}t|jD]v}||D]l}g}t|j||zg|j }|j D]"}|j|j|$|j|nxt|}|S)zt Returns ======= macaulay_matrix: Matrix The Macaulay numerator matrix ) rr#r!rrrrr9r_r) r'r=rr multiplier coefficientsrmonomacaulay_matrixs r get_matrixzMacaulayResultant.get_matrixs446tvv *A.q1 * ! D,,Q/*<-!^^-!--CD ''(;(;D(ABC L) * *!,r2c g}|jD]f}g}t|jD]9\}}|jt t |||j |k\;|j|ht|Dcgc]"\}}t||jdz kr|$}}}t|Dcgc]"\}}t||jdz k\r|$}}}||fScc}}wcc}}w)a Returns ======= reduced: list A list of the reduced monomials non_reduced: list A list of the monomials that are not reduced Definition ========== A polynomial is said to be reduced in x_i, if its degree (the maximum degree of its monomials) in x_i is less than d_i. A polynomial that is reduced in all variables but one is said simply to be reduced. r4) r enumeraterr9boolrrrr!) r'rr"r>rvrreduced non_reduceds rget_reduced_nonreducedz(MacaulayResultant.get_reduced_nonreduceds$ "" #AD!$..1 I1 Da!3t||A!FGH I   T "  # "+9!5+A!ftvvz)++%.y%9/TQa&DFFAI-/ / ## +/s 'C,>'C2c |j\}}|gk(r tdgSt|jDcgc]\}}||j|z}}}t |j Dcgc]#}|j|j||%}}|dd|f}g} t |jD]0} |D cgc] } | || ddfv} } d| vs | j| 2|| |fScc}}wcc}wcc} w)a Returns ======= macaulay_submatrix: Matrix The Macaulay denominator matrix. Columns that are non reduced are kept. The row which contains one of the a_{i}s is dropped. a_{i}s are the coefficients of x_i ^ {d_i}. r4NT) rrrrrr#r!rcoeffr=r9) r'rkrrrr reduction_setaisreduced_matrixrfrqaichecks r get_submatrixzMacaulayResultant.get_submatrixs) $::< b=9 &dnn57$!Qdll1o-7 7dff '"((q)9:'' 7 +,,- !C#q&11@E@5  C  ! dK'((7' AsC73(C=DN) r~rrrr)rrrrrrrr[r2rrrs2-\P0 4 @ ? 8.$>)r2rN)rmathrsympy.core.mulrsympy.matrices.denserrsympy.polys.polytoolsrrr sympy.simplify.simplifyr sympy.tensor.indexedr sympy.polys.monomialsr r sympy.polys.orderingsrrr(sympy.functions.combinatorial.factorialsr itertoolsrsympy.utilities.exceptionsrrrr[r2rrsL /::,,=.>=3@a4a4F])])r2