K iXdZddlmZddlmZddlmZddlmZddl m Z ddl m Z mZddlmZdd lmZmZmZdd lmZdd lmZdd lmZdd lmZmZmZm Z m!Z!m"Z"m#Z#ddl$m%Z%d5dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-d6dZ.dZ/dZ0dZ1d7dZ2dZ3d7dZ4dZ5dZ6d Z7d!Z8d"Z9d#Z:d$Z;d%Zd(Z?d)Z@d*ZAd+ZBd,ZCd-ZDd.ZEd/ZFd0ZGd1ZHd7d2ZId3ZJy4)8a# This module contains functions for the computation of Euclidean, (generalized) Sturmian, (modified) subresultant polynomial remainder sequences (prs's) of two polynomials; included are also three functions for the computation of the resultant of two polynomials. Except for the function res_z(), which computes the resultant of two polynomials, the pseudo-remainder function prem() of sympy is _not_ used by any of the functions in the module. Instead of prem() we use the function rem_z(). Included is also the function quo_z(). An explanation of why we avoid prem() can be found in the references stated in the docstring of rem_z(). 1. Theoretical background: ========================== Consider the polynomials f, g in Z[x] of degrees deg(f) = n and deg(g) = m with n >= m. Definition 1: ============= The sign sequence of a polynomial remainder sequence (prs) is the sequence of signs of the leading coefficients of its polynomials. Sign sequences can be computed with the function: sign_seq(poly_seq, x) Definition 2: ============= A polynomial remainder sequence (prs) is called complete if the degree difference between any two consecutive polynomials is 1; otherwise, it called incomplete. It is understood that f, g belong to the sequences mentioned in the two definitions above. 1A. Euclidean and subresultant prs's: ===================================== The subresultant prs of f, g is a sequence of polynomials in Z[x] analogous to the Euclidean prs, the sequence obtained by applying on f, g Euclid's algorithm for polynomial greatest common divisors (gcd) in Q[x]. The subresultant prs differs from the Euclidean prs in that the coefficients of each polynomial in the former sequence are determinants --- also referred to as subresultants --- of appropriately selected sub-matrices of sylvester1(f, g, x), Sylvester's matrix of 1840 of dimensions (n + m) * (n + m). Recall that the determinant of sylvester1(f, g, x) itself is called the resultant of f, g and serves as a criterion of whether the two polynomials have common roots or not. In SymPy the resultant is computed with the function resultant(f, g, x). This function does _not_ evaluate the determinant of sylvester(f, g, x, 1); instead, it returns the last member of the subresultant prs of f, g, multiplied (if needed) by an appropriate power of -1; see the caveat below. In this module we use three functions to compute the resultant of f, g: a) res(f, g, x) computes the resultant by evaluating the determinant of sylvester(f, g, x, 1); b) res_q(f, g, x) computes the resultant recursively, by performing polynomial divisions in Q[x] with the function rem(); c) res_z(f, g, x) computes the resultant recursively, by performing polynomial divisions in Z[x] with the function prem(). Caveat: If Df = degree(f, x) and Dg = degree(g, x), then: resultant(f, g, x) = (-1)**(Df*Dg) * resultant(g, f, x). For complete prs's the sign sequence of the Euclidean prs of f, g is identical to the sign sequence of the subresultant prs of f, g and the coefficients of one sequence are easily computed from the coefficients of the other. For incomplete prs's the polynomials in the subresultant prs, generally differ in sign from those of the Euclidean prs, and --- unlike the case of complete prs's --- it is not at all obvious how to compute the coefficients of one sequence from the coefficients of the other. 1B. Sturmian and modified subresultant prs's: ============================================= For the same polynomials f, g in Z[x] mentioned above, their ``modified'' subresultant prs is a sequence of polynomials similar to the Sturmian prs, the sequence obtained by applying in Q[x] Sturm's algorithm on f, g. The two sequences differ in that the coefficients of each polynomial in the modified subresultant prs are the determinants --- also referred to as modified subresultants --- of appropriately selected sub-matrices of sylvester2(f, g, x), Sylvester's matrix of 1853 of dimensions 2n x 2n. The determinant of sylvester2 itself is called the modified resultant of f, g and it also can serve as a criterion of whether the two polynomials have common roots or not. For complete prs's the sign sequence of the Sturmian prs of f, g is identical to the sign sequence of the modified subresultant prs of f, g and the coefficients of one sequence are easily computed from the coefficients of the other. For incomplete prs's the polynomials in the modified subresultant prs, generally differ in sign from those of the Sturmian prs, and --- unlike the case of complete prs's --- it is not at all obvious how to compute the coefficients of one sequence from the coefficients of the other. As Sylvester pointed out, the coefficients of the polynomial remainders obtained as (modified) subresultants are the smallest possible without introducing rationals and without computing (integer) greatest common divisors. 1C. On terminology: =================== Whence the terminology? Well generalized Sturmian prs's are ``modifications'' of Euclidean prs's; the hint came from the title of the Pell-Gordon paper of 1917. In the literature one also encounters the name ``non signed'' and ``signed'' prs for Euclidean and Sturmian prs respectively. Likewise ``non signed'' and ``signed'' subresultant prs for subresultant and modified subresultant prs respectively. 2. Functions in the module: =========================== No function utilizes SymPy's function prem(). 2A. Matrices: ============= The functions sylvester(f, g, x, method=1) and sylvester(f, g, x, method=2) compute either Sylvester matrix. They can be used to compute (modified) subresultant prs's by direct determinant evaluation. The function bezout(f, g, x, method='prs') provides a matrix of smaller dimensions than either Sylvester matrix. It is the function of choice for computing (modified) subresultant prs's by direct determinant evaluation. sylvester(f, g, x, method=1) sylvester(f, g, x, method=2) bezout(f, g, x, method='prs') The following identity holds: bezout(f, g, x, method='prs') = backward_eye(deg(f))*bezout(f, g, x, method='bz')*backward_eye(deg(f)) 2B. Subresultant and modified subresultant prs's by =================================================== determinant evaluations: ======================= We use the Sylvester matrices of 1840 and 1853 to compute, respectively, subresultant and modified subresultant polynomial remainder sequences. However, for large matrices this approach takes a lot of time. Instead of utilizing the Sylvester matrices, we can employ the Bezout matrix which is of smaller dimensions. subresultants_sylv(f, g, x) modified_subresultants_sylv(f, g, x) subresultants_bezout(f, g, x) modified_subresultants_bezout(f, g, x) 2C. Subresultant prs's by ONE determinant evaluation: ===================================================== All three functions in this section evaluate one determinant per remainder polynomial; this is the determinant of an appropriately selected sub-matrix of sylvester1(f, g, x), Sylvester's matrix of 1840. To compute the remainder polynomials the function subresultants_rem(f, g, x) employs rem(f, g, x). By contrast, the other two functions implement Van Vleck's ideas of 1900 and compute the remainder polynomials by trinagularizing sylvester2(f, g, x), Sylvester's matrix of 1853. subresultants_rem(f, g, x) subresultants_vv(f, g, x) subresultants_vv_2(f, g, x). 2E. Euclidean, Sturmian prs's in Q[x]: ====================================== euclid_q(f, g, x) sturm_q(f, g, x) 2F. Euclidean, Sturmian and (modified) subresultant prs's P-G: ============================================================== All functions in this section are based on the Pell-Gordon (P-G) theorem of 1917. Computations are done in Q[x], employing the function rem(f, g, x) for the computation of the remainder polynomials. euclid_pg(f, g, x) sturm pg(f, g, x) subresultants_pg(f, g, x) modified_subresultants_pg(f, g, x) 2G. Euclidean, Sturmian and (modified) subresultant prs's A-M-V: ================================================================ All functions in this section are based on the Akritas-Malaschonok- Vigklas (A-M-V) theorem of 2015. Computations are done in Z[x], employing the function rem_z(f, g, x) for the computation of the remainder polynomials. euclid_amv(f, g, x) sturm_amv(f, g, x) subresultants_amv(f, g, x) modified_subresultants_amv(f, g, x) 2Ga. Exception: =============== subresultants_amv_q(f, g, x) This function employs rem(f, g, x) for the computation of the remainder polynomials, despite the fact that it implements the A-M-V Theorem. It is included in our module in order to show that theorems P-G and A-M-V can be implemented utilizing either the function rem(f, g, x) or the function rem_z(f, g, x). For clearly historical reasons --- since the Collins-Brown-Traub coefficients-reduction factor beta_i was not available in 1917 --- we have implemented the Pell-Gordon theorem with the function rem(f, g, x) and the A-M-V Theorem with the function rem_z(f, g, x). 2H. Resultants: =============== res(f, g, x) res_q(f, g, x) res_z(f, g, x) ) summation)expand)nan)S)Dummy)Abssign)floor)eyeMatrixzeros) pretty_print)simplify)QQ)degreeLCPolypquoquopremrem)PolynomialErrorctt|||tt|||}}||k(r|dkr tgS||k(r|dk(r tgS|dk(r|dkr tgS|dkr|dk(r tgS|dk\r|dkr tdgS|dkr|dk\r tdgSt||j}t||j}|dkrlt ||z}d} t |D]} | } |D]} | || | f<| dz} | dz} d} t |||zD]} | } |D]} | || | f<| dz} | dz} |St |t |kr>g} t t |t |z D]} | jd| |ddn=g} t t |t |z D]} | jd| |ddt||}d|z}t |}d} t |D]:} | } |D]} | |d| z| f<| dz} | } |D]} | |d| zdz| f<| dz} | dz} <|S)a The input polynomials f, g are in Z[x] or in Q[x]. Let m = degree(f, x), n = degree(g, x) and mx = max(m, n). a. If method = 1 (default), computes sylvester1, Sylvester's matrix of 1840 of dimension (m + n) x (m + n). The determinants of properly chosen submatrices of this matrix (a.k.a. subresultants) can be used to compute the coefficients of the Euclidean PRS of f, g. b. If method = 2, computes sylvester2, Sylvester's matrix of 1853 of dimension (2*mx) x (2*mx). The determinants of properly chosen submatrices of this matrix (a.k.a. ``modified'' subresultants) can be used to compute the coefficients of the Sturmian PRS of f, g. Applications of these Matrices can be found in the references below. Especially, for applications of sylvester2, see the first reference!! References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``On a Theorem by Van Vleck Regarding Sturm Sequences. Serdica Journal of Computing, Vol. 7, No 4, 101-134, 2013. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Sturm Sequences and Modified Subresultant Polynomial Remainder Sequences.'' Serdica Journal of Computing, Vol. 8, No 1, 29-46, 2014. rN) rrr all_coeffsr rangelenappendmax)fgxmethodmnfpgpMkijcoeffhmxdims e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/subresultants_qq_zz.py sylvesterr2s< 41:q !641:q#9qA Av!a%bz Av!q&bz Av!a%bz Q16bz Av!a%qc{ Q16qc{ a   B a   B{ !a%L q AA !Q$E AA   q!a% AA !Q$E AA    r7SW A3r7SW,-   B!HA3r7SW,-   B!H AYd 3L  AA !!A#q& E A  %!A#'1* E AA c|dd}t|d|}d}|t|krft|||}|dkr|j|||dz }||k(r|j|||dz }|dk\r|}|dz}|t|krf|S)aZ poly_seq is a polynomial remainder sequence computed either by (modified_)subresultants_bezout or by (modified_)subresultants_sylv. This function removes from poly_seq all zero polynomials as well as all those whose degree is equal to the degree of a preceding polynomial in poly_seq, as we scan it from left to right. Nrrr)rrremove)poly_seqr#Ldr+d_is r1process_matrix_outputr:ns  AqtQA A c!f*QqT1o 7 HHQqTNAA 8 HHQqTNAA !8A E c!f* Hr3c6|dk(s|dk(r||gSt||x}}t||x}}|dk(r |dk(r||gS||kr||||||f\}}}}}}|dkDr |dk(r||gS||g}t|||d}|dz } | dkDr|ddddf} t||z| z ||zD]} | j||z| z t|| z |D]} | j|| z g| jd}} } || krK| j | ddd| fj | j| dz | |z|dz }|| krK|j t| |j| dz} | dkDr|j |j t||S)a1 The input polynomials f, g are in Z[x] or in Q[x]. It is assumed that deg(f) >= deg(g). Computes the subresultant polynomial remainder sequence (prs) of f, g by evaluating determinants of appropriately selected submatrices of sylvester(f, g, x, 1). The dimensions of the latter are (deg(f) + deg(g)) x (deg(f) + deg(g)). Each coefficient is computed by evaluating the determinant of the corresponding submatrix of sylvester(f, g, x, 1). If the subresultant prs is complete, then the output coincides with the Euclidean sequence of the polynomials f, g. References: =========== 1. G.M.Diaz-Toca,L.Gonzalez-Vega: Various New Expressions for Subresultants and Their Applications. Appl. Algebra in Engin., Communic. and Comp., Vol. 15, 233-266, 2004. rrN) rr2rrow_delrowsrdetcol_swapras_exprr:)r!r"r#r&degFr%degGSR_Lrr,Spindcoeff_Lr*ls r1subresultants_sylvrHs2 Ava1v a|Aa|A Av!q&1v 1u!"AtT1a!71dD!Q1ua1v q6D !Q1A AA a% q!tWQAE* "C JJq1uqy ! "Q? C JJq1u  BGGQA1f NN2a1f:>>+ , KKAq1u % FA1f D!$,,./ Q% a%* KK q ))r3c|dk(s|dk(r||gSt||x}}t||x}}|dk(r |dk(r||gS||kr||||||f\}}}}}}|dkDr |dk(r||gS||g}t|||d}|dz } | dkDr|dd|zd| zz ddf} g| jd} } } | | krK| j| ddd| fj | j | dz | | z| dz } | | krK|jt | |j| dz} | dkDr|j|jt||S)aB The input polynomials f, g are in Z[x] or in Q[x]. It is assumed that deg(f) >= deg(g). Computes the modified subresultant polynomial remainder sequence (prs) of f, g by evaluating determinants of appropriately selected submatrices of sylvester(f, g, x, 2). The dimensions of the latter are (2*deg(f)) x (2*deg(f)). Each coefficient is computed by evaluating the determinant of the corresponding submatrix of sylvester(f, g, x, 2). If the modified subresultant prs is complete, then the output coincides with the Sturmian sequence of the polynomials f, g. References: =========== 1. A. G. Akritas,G.I. Malaschonok and P.S. Vigklas: Sturm Sequences and Modified Subresultant Polynomial Remainder Sequences. Serdica Journal of Computing, Vol. 8, No 1, 29--46, 2014. rrrN) rr2r=rr>r?rr@r:)r!r"r#r&rAr%rBrCrr,rDrFr*rGs r1modified_subresultants_sylvrJs2 Ava1v a|Aa|A Av!q&1v 1u!"AtT1a!71dD!Q1ua1v q6D !Q1A AA a% q1qs{A~ BGGQA1f NN2a1f:>>+ , KKAq1u % FA1f D!$,,./ Q a%  KK q ))r3cr|dk(s|dk(rtd|d|dt|||djS)aF The input polynomials f, g are in Z[x] or in Q[x]. The output is the resultant of f, g computed by evaluating the determinant of the matrix sylvester(f, g, x, 1). References: =========== 1. J. S. Cohen: Computer Algebra and Symbolic Computation - Mathematical Methods. A. K. Peters, 2003. rzThe resultant of z and z is not definedr)rr2r>)r!r"r#s r1resrLs< AvaQPQRSSAq!$((**r3ct||}t||}||krd||zzt|||zS|dk(r||zSt|||}|dk(ryt||}t||}d||zz|||z zzt|||zS)ad The input polynomials f, g are in Z[x] or in Q[x]. The output is the resultant of f, g computed recursively by polynomial divisions in Q[x], using the function rem. See Cohen's book p. 281. References: =========== 1. J. S. Cohen: Computer Algebra and Symbolic Computation - Mathematical Methods. A. K. Peters, 2003. r)rres_qrr)r!r"r#r%r&rsrGs r1rOrO(s q! Aq! A1uac{U1a^++ a!t 1aL 6q! A1aA!A#;QqS)%1a.8 8r3cDt||}t||}||krd||zzt|||zS|dk(r||zSt|||}|dk(ry||z dz}d||zzt|||z}t||}t||} ||z|z |z} t || | z|S)ag The input polynomials f, g are in Z[x] or in Q[x]. The output is the resultant of f, g computed recursively by polynomial divisions in Z[x], using the function prem(). See Cohen's book p. 283. References: =========== 1. J. S. Cohen: Computer Algebra and Symbolic Computation - Mathematical Methods. A. K. Peters, 2003. rNrr)rres_zrrr) r!r"r#r%r&rPdeltawrQrGr*s r1rSrSDs q! Aq! A1uac{U1a^++ a!t AqM 6EAIEqs eAq!n,Aq! A1aA A !Aq!Q$? "r3c |tt|Dcgc]}tt|||c}Scc}w)z Given a sequence of polynomials poly_seq, it returns the sequence of signs of the leading coefficients of the polynomials in poly_seq. )rrr r)r6r#r+s r1sign_seqrWcs1/4CM.B CDHQK# $ CC Cs9ctt|||tt|||}}||k(r|dkr tgS||k(r|dk(r tgS|dk(r|dkr tgS|dkr|dk(r tgS|dk\r|dkr tdgS|dkr|dk\r tdgStd}||j ||iz|j ||i|zz }tt |||z ||}t ||} t| } t| D]R} t| D]B} |dk(r$|j| | | | dz | z | dz | z f<,|j| | | | | f<DT| S)a The input polynomials p, q are in Z[x] or in Q[x]. Let mx = max(degree(p, x), degree(q, x)). The default option bezout(p, q, x, method='bz') returns Bezout's symmetric matrix of p and q, of dimensions (mx) x (mx). The determinant of this matrix is equal to the determinant of sylvester2, Sylvester's matrix of 1853, whose dimensions are (2*mx) x (2*mx); however the subresultants of these two matrices may differ. The other option, bezout(p, q, x, 'prs'), is of interest to us in this module because it returns a matrix equivalent to sylvester2. In this case all subresultants of the two matrices are identical. Both the subresultant polynomial remainder sequence (prs) and the modified subresultant prs of p and q can be computed by evaluating determinants of appropriately selected submatrices of bezout(p, q, x, 'prs') --- one determinant per coefficient of the remainder polynomials. The matrices bezout(p, q, x, 'bz') and bezout(p, q, x, 'prs') are related by the formula bezout(p, q, x, 'prs') = backward_eye(deg(p)) * bezout(p, q, x, 'bz') * backward_eye(deg(p)), where backward_eye() is the backward identity function. References ========== 1. G.M.Diaz-Toca,L.Gonzalez-Vega: Various New Expressions for Subresultants and Their Applications. Appl. Algebra in Engin., Communic. and Comp., Vol. 15, 233-266, 2004. rryprs) rrr varsubsrr r rnth) pqr#r$r%r&rYexprpolyr/Br+r,s r1bezoutrclsJ 41:q !641:q#9qA Av!a%bz Av!q&bz Av!a%bz Q16bz Av!a%qc{ Q16qc{ CA qvvqe} qvvqe}q0 0D T1Q3A &D QB b A 2Y)r )A,0HHQN"q&1*b1fqj()((1a.!Q$  )) Hr3ct|}tt|jdz D]'}|j d|z|jdz |z )|S)z Returns the backward identity matrix of dimensions n x n. Needed to "turn" the Bezout matrices so that the leading coefficients are first. See docstring of the function bezout(p, q, x, method='bz'). rrr)r rintr=row_swap)r&r)r+s r1 backward_eyergsR AA 3qvvz? #* 1q5!&&1*q.)* Hr3c|dk(s|dk(r||gS||}}t||x}}t||x}}|dk(r |dk(r||gS||kr||||||f\}}}}}}|dkDr |dk(r||gS||g} t||||z z} t|||d} ||kDrd} ||k(rd}  |kr| d| ddf} | dz g}}||dz krV|j| ddd| fj ||dz kr| j | dz |dz|dz}||dz krV| jt d| | dz zdz zt||| z jz| dz} | |krt| |S)a The input polynomials p, q are in Z[x] or in Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the subresultant polynomial remainder sequence of p, q by evaluating determinants of appropriately selected submatrices of bezout(p, q, x, 'prs'). The dimensions of the latter are deg(p) x deg(p). Each coefficient is computed by evaluating the determinant of the corresponding submatrix of bezout(p, q, x, 'prs'). bezout(p, q, x, 'prs) is used instead of sylvester(p, q, x, 1), Sylvester's matrix of 1840, because the dimensions of the latter are (deg(p) + deg(q)) x (deg(p) + deg(q)). If the subresultant prs is complete, then the output coincides with the Euclidean sequence of the polynomials p, q. References ========== 1. G.M.Diaz-Toca,L.Gonzalez-Vega: Various New Expressions for Subresultants and Their Applications. Appl. Algebra in Engin., Communic. and Comp., Vol. 15, 233-266, 2004. rrZrrNrN) rrrcrr>r?rerr@r:)r^r_r#r!r"r&rAr%rBrCFrbr,r)r*rFs r1subresultants_bezoutrjs8 Ava1v aqAa|Aa|A Av!q&1v 1u!"AtT1a!71dD!Q1ua1v q6D 1a4$;A q!QA d{  t|  t) ac1fIUB74!8m NN1Q!V9==? +4!8| 1q5!a%(AA 4!8m Cq!A#wqy)*d7A.>.B-K-K-MMN E t) !q ))r3ch|dk(s|dk(r||gS||}}t||x}}t||x}}|dk(r |dk(r||gS||kr||||||f\}}}}}}|dkDr |dk(r||gS||g} t|||d} ||kDrd} ||k(rd}  |kr| d| ddf} | dz g}} | |dz krV|j| ddd| fj| |dz kr| j | dz | dz| dz} | |dz krV| jt ||j | dz} | |krt| |S)a The input polynomials p, q are in Z[x] or in Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the modified subresultant polynomial remainder sequence of p, q by evaluating determinants of appropriately selected submatrices of bezout(p, q, x, 'prs'). The dimensions of the latter are deg(p) x deg(p). Each coefficient is computed by evaluating the determinant of the corresponding submatrix of bezout(p, q, x, 'prs'). bezout(p, q, x, 'prs') is used instead of sylvester(p, q, x, 2), Sylvester's matrix of 1853, because the dimensions of the latter are 2*deg(p) x 2*deg(p). If the modified subresultant prs is complete, and LC( p ) > 0, the output coincides with the (generalized) Sturm's sequence of the polynomials p, q. References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Sturm Sequences and Modified Subresultant Polynomial Remainder Sequences.'' Serdica Journal of Computing, Vol. 8, No 1, 29-46, 2014. 2. G.M.Diaz-Toca,L.Gonzalez-Vega: Various New Expressions for Subresultants and Their Applications. Appl. Algebra in Engin., Communic. and Comp., Vol. 15, 233-266, 2004. rrZrrN)rrcrr>r?rr@r:)r^r_r#r!r"r&rAr%rBrCrbr,r)r*rFs r1modified_subresultants_bezoutrlsB Ava1v aqAa|Aa|A Av!q&1v 1u!"AtT1a!71dD!Q1ua1v q6D q!QA d{  t|  t) ac1fIUB74!8m NN1Q!V9==? +4!8| 1q5!a%(AA 4!8m d7A&//12 E t) !q ))r3c|dk(s|dk(r||gSt||}t||}|dk(r |dk(r||gS||kDr||}}||}}|dkDr |dk(r||gSd}t||dkrd}| }| }t||||z z}||} }|| g} ||z } t| |} |dz } t|| t }t||}t||}| |z }||z }| | |z|z z}|dk(r*| j t ||zt |zn&| j t |t |z|}|dkDr| |||f\}} }}|} |dz } t|| t }t||}t||}| |z }||z }|}| |z|z }||z| |zz|z}||}}||}} |dk(r*| j t ||zt |zn&| j t |t |z|dkDr|r| Dcgc]}| } }t| }| |dz tk(s | |dz dk(r| j|dz | Scc}w)a p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the (generalized) Sturm sequence of p and q in Z[x] or Q[x]. If q = diff(p, x, 1) it is the usual Sturm sequence. A. If method == 0, default, the remainder coefficients of the sequence are (in absolute value) ``modified'' subresultants, which for non-monic polynomials are greater than the coefficients of the corresponding subresultants by the factor Abs(LC(p)**( deg(p)- deg(q))). B. If method == 1, the remainder coefficients of the sequence are (in absolute value) subresultants, which for non-monic polynomials are smaller than the coefficients of the corresponding ``modified'' subresultants by the factor Abs(LC(p)**( deg(p)- deg(q))). If the Sturm sequence is complete, method=0 and LC( p ) > 0, the coefficients of the polynomials in the sequence are ``modified'' subresultants. That is, they are determinants of appropriately selected submatrices of sylvester2, Sylvester's matrix of 1853. In this case the Sturm sequence coincides with the ``modified'' subresultant prs, of the polynomials p, q. If the Sturm sequence is incomplete and method=0 then the signs of the coefficients of the polynomials in the sequence may differ from the signs of the coefficients of the corresponding polynomials in the ``modified'' subresultant prs; however, the absolute values are the same. To compute the coefficients, no determinant evaluation takes place. Instead, polynomial divisions in Q[x] are performed, using the function rem(p, q, x); the coefficients of the remainders computed this way become (``modified'') subresultants with the help of the Pell-Gordon Theorem of 1917. See also the function euclid_pg(p, q, x). References ========== 1. Pell A. J., R. L. Gordon. The Modified Remainders Obtained in Finding the Highest Common Factor of Two Polynomials. Annals of MatheMatics, Second Series, 18 (1917), No. 4, 188-193. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Sturm Sequences and Modified Subresultant Polynomial Remainder Sequences.'' Serdica Journal of Computing, Vol. 8, No 1, 29-46, 2014. rrdomain) rrrrrrrrrpop)r^r_r#r$d0d1flaglcfa0a1 sturm_seqdel0rho1exp_dega2rho2d2 deg_diff_newdel1 mul_fac_old deg_diff_oldrho3expo_oldexpo_new mul_fac_newr+r%s r1sturm_pgras` Ava1v  A,B A,B Qw271v  BwRB!1 Av"'!u  D 1Q!  B B Q(R"W C BRI 7D r1ID1fG r2b ! !B r!HD Q-BR(>? @- q&0 !*+AaR+ + IAQ3)AE"2a"7 a!e ,s= Icr|dk(s|dk(r||gSt||}t||}|dk(r |dk(r||gS||kDr||}}||}}|dkDr |dk(r||gSd}t||dkrd}| }| }||}}||g}t||t } t| |} |j | | dkDrA|| || f\}}}}t||t } t| |} |j | | dkDrA|r|D cgc]} | }} t |} || dz t k(s || dz dk(r|j| dz |Scc} w)at p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the (generalized) Sturm sequence of p and q in Q[x]. Polynomial divisions in Q[x] are performed, using the function rem(p, q, x). The coefficients of the polynomials in the Sturm sequence can be uniquely determined from the corresponding coefficients of the polynomials found either in: (a) the ``modified'' subresultant prs, (references 1, 2) or in (b) the subresultant prs (reference 3). References ========== 1. Pell A. J., R. L. Gordon. The Modified Remainders Obtained in Finding the Highest Common Factor of Two Polynomials. Annals of MatheMatics, Second Series, 18 (1917), No. 4, 188-193. 2 Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Sturm Sequences and Modified Subresultant Polynomial Remainder Sequences.'' Serdica Journal of Computing, Vol. 8, No 1, 29-46, 2014. 3. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``A Basic Result on the Theory of Subresultants.'' Serdica Journal of Computing 10 (2016), No.1, 31-48. rrrnrrrrrrrrp) r^r_r#rqrrrsrurvrwr{r}r+r%s r1sturm_qrsB Ava1v  A,B A,B Qw271v  BwRB!1 Av"'!u  D 1Q!  B BBRI b"R B Q-B b q&RRBB"b$ $Rm" q&  !*+AaR+ + IAQ3)AE"2a"7 a!e ,s- D4ct|||}|gk(st|dk(r|Stt|dt |d|t |d|z z}|d|dg}d}t|}d} | |dz kr|dk(r=|j ||  | dz} | |k(rne|j ||  | dz} d}n?|dk(r:|j || | dz} | |k(rn$|j || | dz} d}| |dz kr|dk(rB|dkDr=|d|dg} t d|D]"} | j t|| |z$| }|S)a p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the (generalized) Sturm sequence of p and q in Z[x] or Q[x]. If q = diff(p, x, 1) it is the usual Sturm sequence. A. If method == 0, default, the remainder coefficients of the sequence are (in absolute value) ``modified'' subresultants, which for non-monic polynomials are greater than the coefficients of the corresponding subresultants by the factor Abs(LC(p)**( deg(p)- deg(q))). B. If method == 1, the remainder coefficients of the sequence are (in absolute value) subresultants, which for non-monic polynomials are smaller than the coefficients of the corresponding ``modified'' subresultants by the factor Abs( LC(p)**( deg(p)- deg(q)) ). If the Sturm sequence is complete, method=0 and LC( p ) > 0, then the coefficients of the polynomials in the sequence are ``modified'' subresultants. That is, they are determinants of appropriately selected submatrices of sylvester2, Sylvester's matrix of 1853. In this case the Sturm sequence coincides with the ``modified'' subresultant prs, of the polynomials p, q. If the Sturm sequence is incomplete and method=0 then the signs of the coefficients of the polynomials in the sequence may differ from the signs of the coefficients of the corresponding polynomials in the ``modified'' subresultant prs; however, the absolute values are the same. To compute the coefficients, no determinant evaluation takes place. Instead, we first compute the euclidean sequence of p and q using euclid_amv(p, q, x) and then: (a) change the signs of the remainders in the Euclidean sequence according to the pattern "-, -, +, +, -, -, +, +,..." (see Lemma 1 in the 1st reference or Theorem 3 in the 2nd reference) and (b) if method=0, assuming deg(p) > deg(q), we multiply the remainder coefficients of the Euclidean sequence times the factor Abs( LC(p)**( deg(p)- deg(q)) ) to make them modified subresultants. See also the function sturm_pg(p, q, x). References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``A Basic Result on the Theory of Subresultants.'' Serdica Journal of Computing 10 (2016), No.1, 31-48. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``On the Remainders Obtained in Finding the Greatest Common Divisor of Two Polynomials.'' Serdica Journal of Computing 9(2) (2015), 123-138. 3. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Subresultant Polynomial Remainder Sequences Obtained by Polynomial Divisions in Q[x] or in Z[x].'' Serdica Journal of Computing 10 (2016), No.3-4, 197-217. rrr) euclid_amvrrrrrrr) r^r_r#r$rZrtrwrsr%r+aux_seqs r1 sturm_amvr/sn Q1 C byCHM r#a&zVCFA.A1BBD FCQQ I D CA A qs( AI   Ah (AAAv   Ah (AAD QY   c!f &AAAv   c!f &AAD! qs(&{sQwQ<1.q! :A NN8IaL3$68 9 : r3ct|||d}|gk(st|dk(r|S|d|dg}d}t|}d}||dz kr|dk(r?|j|| |dz}||k(r |S|j|| |dz}d}nA|dk(r<|j|||dz}||k(r |S|j|||dz}d}||dz kr|S)as p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the Euclidean sequence of p and q in Z[x] or Q[x]. If the Euclidean sequence is complete the coefficients of the polynomials in the sequence are subresultants. That is, they are determinants of appropriately selected submatrices of sylvester1, Sylvester's matrix of 1840. In this case the Euclidean sequence coincides with the subresultant prs of the polynomials p, q. If the Euclidean sequence is incomplete the signs of the coefficients of the polynomials in the sequence may differ from the signs of the coefficients of the corresponding polynomials in the subresultant prs; however, the absolute values are the same. To compute the Euclidean sequence, no determinant evaluation takes place. We first compute the (generalized) Sturm sequence of p and q using sturm_pg(p, q, x, 1), in which case the coefficients are (in absolute value) equal to subresultants. Then we change the signs of the remainders in the Sturm sequence according to the pattern "-, -, +, +, -, -, +, +,..." ; see Lemma 1 in the 1st reference or Theorem 3 in the 2nd reference as well as the function sturm_pg(p, q, x). References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``A Basic Result on the Theory of Subresultants.'' Serdica Journal of Computing 10 (2016), No.1, 31-48. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``On the Remainders Obtained in Finding the Greatest Common Divisor of Two Polynomials.'' Serdica Journal of Computing 9(2) (2015), 123-138. 3. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Subresultant Polynomial Remainder Sequences Obtained by Polynomial Divisions in Q[x] or in Z[x].'' Serdica Journal of Computing 10 (2016), No.3-4, 197-217. rrr)rrr)r^r_r#rZ euclid_seqrsr%r+s r1 euclid_pgrs&R 1aA C byCHM a&#a&!J D CA A qs( AI   Ah (AAAv    Ah (AAD QY   c!f &AAAv    c!f &AAD! qs($ r3cn|dk(s|dk(r||gSt||}t||}|dk(r |dk(r||gS||kDr||}}||}}|dkDr |dk(r||gSd}t||dkrd}| }| }||}}||g}t||t} t| |} |j | | dkDr@|| || f\}}}}t||t} t| |} |j | | dkDr@|r|D cgc]} | }} t |} || dz t k(s || dz dk(r|j| dz |Scc} w)a p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the Euclidean sequence of p and q in Q[x]. Polynomial divisions in Q[x] are performed, using the function rem(p, q, x). The coefficients of the polynomials in the Euclidean sequence can be uniquely determined from the corresponding coefficients of the polynomials found either in: (a) the ``modified'' subresultant polynomial remainder sequence, (references 1, 2) or in (b) the subresultant polynomial remainder sequence (references 3). References ========== 1. Pell A. J., R. L. Gordon. The Modified Remainders Obtained in Finding the Highest Common Factor of Two Polynomials. Annals of MatheMatics, Second Series, 18 (1917), No. 4, 188-193. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Sturm Sequences and Modified Subresultant Polynomial Remainder Sequences.'' Serdica Journal of Computing, Vol. 8, No 1, 29-46, 2014. 3. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``A Basic Result on the Theory of Subresultants.'' Serdica Journal of Computing 10 (2016), No.1, 31-48. rrrnr) r^r_r#rqrrrsrurvrr{r}r+r%s r1euclid_qrsD Ava1v  A,B A,B Qw271v  BwRB!1 Av"'!u  D 1Q!  B BBbJ RB B Q-Br q&RRBB R #Rm2 q&  ",-Qqb- - JA!a%C:a!e#4#9q1u .s+ D2c|dk(s|dk(r||gSt||}t||}|dk(r |dk(r||gS||kDr||}}||}}|dkDr |dk(r||gS|}|}||g}t||t||z dzd} }d} t|||td|zz } |j| t| |} | dk\r|| || f\}}}}| dz } t | } | |dz z| |dz zz } t||| z dz}t|||t| |dz z| zz } |j| t| |} | dk\rt |}||dz t k(s ||dz dk(r|j|dz |S)a f, g are polynomials in Z[x] or Q[x]. It is assumed that degree(f, x) >= degree(g, x). Computes the Euclidean sequence of p and q in Z[x] or Q[x]. If the Euclidean sequence is complete the coefficients of the polynomials in the sequence are subresultants. That is, they are determinants of appropriately selected submatrices of sylvester1, Sylvester's matrix of 1840. In this case the Euclidean sequence coincides with the subresultant prs, of the polynomials p, q. If the Euclidean sequence is incomplete the signs of the coefficients of the polynomials in the sequence may differ from the signs of the coefficients of the corresponding polynomials in the subresultant prs; however, the absolute values are the same. To compute the coefficients, no determinant evaluation takes place. Instead, polynomial divisions in Z[x] or Q[x] are performed, using the function rem_z(f, g, x); the coefficients of the remainders computed this way become subresultants with the help of the Collins-Brown-Traub formula for coefficient reduction. References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``A Basic Result on the Theory of Subresultants.'' Serdica Journal of Computing 10 (2016), No.1, 31-48. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Subresultant Polynomial remainder Sequences Obtained by Polynomial Divisions in Q[x] or in Z[x].'' Serdica Journal of Computing 10 (2016), No.3-4, 197-217. rrrNr)rrem_zrrrrrrp)r!r"r#rqrrrurvr deg_dif_p1cr+r{r}sigma0r%s r1rr-sF Ava1v  A,B A,B Qw271v  BwRB!1 Av"'1v  B BbJ2qMF2qM1A5rJ A r2q C"z!13 3Br Q-B 'RRBB QR& j1n %!j1n*= >B]R'!+ 2r1 a*q.&9V%C E E2Rm ' JA!a%C:a!e#4#9q1u r3c  |dk(s|dk(r||gSt||}t||}|dk(r |dk(r||gS||kDr||}}||}}|dkDr |dk(r||gStd}g}||g}||} }||z } | } t|} tt||} t| |}t|g}| g}t ||d|df}|j |t |}|dz }t|| t }t||}t||}||z }||z }|| |z|z z}|j d|zd}|D] }|d|zz} |d|zz}|dk(r8d}tt|D]}|||||||dzzzz}|| z}ntd}tt|dz D]}|||||||dzzzz}|| z}|t|dz |t|z|z }||t|dz |zz}t||z dkDr-|j t| | z|zt|zn-|j t| | z|zt|z td}|j t|t ||d|t|dz f}|j |t |}|}|dkDr| |||f\}} }}|} |dz }t|| t }t||}t||}||z }||z }|}| |z|z } || z||zz|z}!||!}}||}}|j d|zd}|D] }|d|zz} |d|zz}|dk(r8d}tt|D]}|||||||dzzzz}|| z}ntd}tt|dz D]}|||||||dzzzz}|| z}|t|dz |t|z|z }||t|dz |zz}t||z dkDr-|j t| | z|zt|zn-|j t| | z|zt|z td}|j t|t ||d|t|dz f}|j |t |}|dkDrt|}"||"dz tk(s ||"dz dk(r|j|"dz t|}"t|dkr=|d|dg}#td|"D]"}$|#j t||$dz$|#}|S)a p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the ``modified'' subresultant prs of p and q in Z[x] or Q[x]; the coefficients of the polynomials in the sequence are ``modified'' subresultants. That is, they are determinants of appropriately selected submatrices of sylvester2, Sylvester's matrix of 1853. To compute the coefficients, no determinant evaluation takes place. Instead, polynomial divisions in Q[x] are performed, using the function rem(p, q, x); the coefficients of the remainders computed this way become ``modified'' subresultants with the help of the Pell-Gordon Theorem of 1917. If the ``modified'' subresultant prs is complete, and LC( p ) > 0, it coincides with the (generalized) Sturm sequence of the polynomials p, q. References ========== 1. Pell A. J., R. L. Gordon. The Modified Remainders Obtained in Finding the Highest Common Factor of Two Polynomials. Annals of MatheMatics, Second Series, 18 (1917), No. 4, 188-193. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Sturm Sequences and Modified Subresultant Polynomial Remainder Sequences.'' Serdica Journal of Computing, Vol. 8, No 1, 29-46, 2014. rr*rrnrNr)rr[rr rrsumrrrrrrrrp)%r^r_r#rqrrr*u_listsubres_lrurvrxdegdifrho_1rho_list_minus_1ryrho_listp_listuvrzr{r|r}r~rrnumdenexporrrrrr%rr+s% r1modified_subresultants_pgr|sn< Ava1v  1+B 1+B Qw271v  BwRB!1 Av"'!u  SA F1vH B 7D F rFEbQi( r1IDd}HVF 1q!VAY' (A MM! F A1fG r2b ! !B r1ID Q-BR?? @((CC3x=?+ @x{VAYA%>?? @((C3x=1,-s8}0EE TDX!23T99C sOa  OOXeVmB&6K8H&HJ L OOhuf}R'7[9I'IKK M Hd$ q1aF a!89 : a Ko q&t H AA#!a%A!5 QU H A 1w{A; ,q! :A NN8HQK2$68 9 : r3c t|||}|gk(st|dk(r|St|dt|d|t|d|z z}|d|dg}|Dcgc]}tt |||}}|d}|dd} | D cgc]} | dz  } } | D cgc]} || z  } } | D cgc]} d| | dz zt dz z}} |dd}t|} t | D]C}t||dk(r|j|| |z -|j|||z E|Scc}wcc} wcc} wcc} w)a p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the subresultant prs of p and q in Z[x] or Q[x], from the modified subresultant prs of p and q. The coefficients of the polynomials in these two sequences differ only in sign and the factor LC(p)**( deg(p)- deg(q)) as stated in Theorem 2 of the reference. The coefficients of the polynomials in the output sequence are subresultants. That is, they are determinants of appropriately selected submatrices of sylvester1, Sylvester's matrix of 1840. If the subresultant prs is complete, then it coincides with the Euclidean sequence of the polynomials p, q. References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: "On the Remainders Obtained in Finding the Greatest Common Divisor of Two Polynomials." Serdica Journal of Computing 9(2) (2015), 123-138. rrrrNN) rrrrrrrr rr^r_r#lstrtsubr_seqradeg_seqdeg deg_seq_sr%m_seqj_seqr,factlst_sr*s r1subresultants_pgr4sy6 $Aa *C byCHM SV*s1vq)F3q61,== ?CAAH588Dvd4mQ'8G8 !*C" I# $QQqS $E $# $S1W $E $-2 2qRAqsGAaDL " 2D 2 GE D A 1X, Q=B  OOU1XIO , OOE!HsN + , O+9 % $ 3sD3 D8 D=0Ec |dk(s|dk(r||gSt||}t||}|dk(r |dk(r||gS||kDr||}}||}}|dkDr |dk(r||gSd\}}d}||g}||} } t| |} ||z } | dzdk(r|dz }t|dzdz } d}|}|dkDr0|dz }t| | t}|dk(r t||}nt||}|}} t||}||z }|| z|z}| | dzz|z}d|z}t |}t ||z dkDr0|j tt|t|zn0|j tt|t|z |dz dkDr || |dz zz}| |||f\} } }}|} | dzdk(r|dz }t|dzdz } |dzdk(r|| z }|dkDr0t|}||dz tk(s ||dz dk(r|j|dz |S)a p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the subresultant prs of p and q in Q[x]; the coefficients of the polynomials in the sequence are subresultants. That is, they are determinants of appropriately selected submatrices of sylvester1, Sylvester's matrix of 1840. To compute the coefficients, no determinant evaluation takes place. Instead, polynomial divisions in Q[x] are performed, using the function rem(p, q, x); the coefficients of the remainders computed this way become subresultants with the help of the Akritas-Malaschonok-Vigklas Theorem of 2015. If the subresultant prs is complete, then it coincides with the Euclidean sequence of the polynomials p, q. References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``A Basic Result on the Theory of Subresultants.'' Serdica Journal of Computing 10 (2016), No.1, 31-48. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Subresultant Polynomial remainder Sequences Obtained by Polynomial Divisions in Q[x] or in Z[x].'' Serdica Journal of Computing 10 (2016), No.3-4, 197-217. rrrrrrnrN) rrr rrr rrrrrrrp)r^r_r#rqrrr+rQp_odd_index_sumrrurvsigma1p0phimul_facr}r{sigma2sigma3p1psirrr%s r1subresultants_amv_qrus< Ava1v  A,B A,B Qw271v  BwRB!1 Av"'1v  DAqO1vH BQiF bB Av{ Q !a%1 CG B q& Q R $ 6QiFQiF#VFFRm "W#g'26"W,Ci7m sOa  OOXfRW-=&>? @ OOhvb#g,.>'?@@ A 6A:a 00GRRBB  6A: FAa!eq[" Q3!8 r !OM q&R H AA#!a%A!5 QU r3cBt|}|dk(ry|dz}|dk(r| S|S)z| base != 0 and expo >= 0 are integers; returns the sign of base**expo without evaluating the power itself! rrr)r )basersbpes r1 compute_signrs3 dB Qw B Qws  r3cr|jjr|jjrw|jj|jjk(rBt||t||z dz}t t t |||z|z||St|||S)a Intended mainly for p, q polynomials in Z[x] so that, on dividing p by q, the remainder will also be in Z[x]. (However, it also works fine for polynomials in Q[x].) It is assumed that degree(p, x) >= degree(q, x). It premultiplies p by the _absolute_ value of the leading coefficient of q, raised to the power deg(p) - deg(q) + 1 and then performs polynomial division in Q[x], using the function rem(p, q, x). By contrast the function prem(p, q, x) does _not_ use the absolute value of the leading coefficient of q. This results not only in ``messing up the signs'' of the Euclidean and Sturmian prs's as mentioned in the second reference, but also in violation of the main results of the first and third references --- Theorem 4 and Theorem 1 respectively. Theorems 4 and 1 establish a one-to-one correspondence between the Euclidean and the Sturmian prs of p, q, on one hand, and the subresultant prs of p, q, on the other. References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``On the Remainders Obtained in Finding the Greatest Common Divisor of Two Polynomials.'' Serdica Journal of Computing, 9(2) (2015), 123-138. 2. https://planetMath.org/sturmstheorem 3. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``A Basic Result on the Theory of Subresultants.'' Serdica Journal of Computing 10 (2016), No.1, 31-48. r)as_poly is_univariategensrrrrrr^r_r#rTs r1rrsB !!aiik&?&? IIK   0 0 01q! ,q03r!Qx=%'A-q!44Aq!}r3cr|jjr|jjrw|jj|jjk(rBt||t||z dz}t t t |||z|z||St|||S)a} Intended mainly for p, q polynomials in Z[x] so that, on dividing p by q, the quotient will also be in Z[x]. (However, it also works fine for polynomials in Q[x].) It is assumed that degree(p, x) >= degree(q, x). It premultiplies p by the _absolute_ value of the leading coefficient of q, raised to the power deg(p) - deg(q) + 1 and then performs polynomial division in Q[x], using the function quo(p, q, x). By contrast the function pquo(p, q, x) does _not_ use the absolute value of the leading coefficient of q. See also function rem_z(p, q, x) for additional comments and references. r)rrrrrrrrrs r1quo_zrs" !!aiik&?&? IIK   0 0 01q! ,q03r!Qx=%'A-q!44Aq!}r3c|dk(s|dk(r||gSt||}t||}|dk(r |dk(r||gS||kDr||}}||}}|dkDr |dk(r||gS|}|}||g}t||t||z dzd} }t||} d\} } d} |dz }|dzdk(r| dz } t| dzdz }| dz } t|||t d|zz }t||}t||}||z }t | |dz}| |z| z}d|z}|}t ||z dkDr|j|n|j| |dzdk(r| dz } |dz dkDr|t | |dz z}|dk\r&t| dzdz }| dzdk(r| |z } ||||f\}}}}|}| dz } t| }||dz z| |dz zz } t|||z dz}t|||t | |dz z|zz }t||}t||}||z }| |z| z}||}} t | |dz|z}d|z}|}t ||z dkDr|j|n|j| |dzdk(r| dz } |dz dkDr|t | |dz z}|dk\r&t|}||dz tk(s ||dz dk(r|j|dz |S)a  p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(f, x) >= degree(g, x). Computes the subresultant prs of p and q in Z[x] or Q[x]; the coefficients of the polynomials in the sequence are subresultants. That is, they are determinants of appropriately selected submatrices of sylvester1, Sylvester's matrix of 1840. To compute the coefficients, no determinant evaluation takes place. Instead, polynomial divisions in Z[x] or Q[x] are performed, using the function rem_z(p, q, x); the coefficients of the remainders computed this way become subresultants with the help of the Akritas-Malaschonok-Vigklas Theorem of 2015 and the Collins-Brown- Traub formula for coefficient reduction. If the subresultant prs is complete, then it coincides with the Euclidean sequence of the polynomials p, q. References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``A Basic Result on the Theory of Subresultants.'' Serdica Journal of Computing 10 (2016), No.1, 31-48. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Subresultant Polynomial remainder Sequences Obtained by Polynomial Divisions in Q[x] or in Z[x].'' Serdica Journal of Computing 10 (2016), No.3-4, 197-217. rrrNrr) rrr rrrr rrrrp)r!r"r#rqrrrurvrrrrr+rQrrrr{rr}rsgn_denrrrrrr%s r1subresultants_amvr.s> Ava1v  A,B A,B Qw271v  BwRB!1 Av"'1v  B BBxH2qMF2qM1A5rJQiF DAqO aB Av{ Q !a%1 CFA r2q C"z!13 3BQiF Q-B bBFBF,G c'O #C )C C S3Y!" Av{ Q AvzL&"q&:: 'a!eq[" Q3!8 r !ORRBB  QR& j1n %!j1n*= >B]R'!+ 2r1 a*q.&9V%C E ER)Rm "W#g' Q07:Ci sOa  OOR ! OObS " 6A: FA 6A: fb1f >>GQ 'V H AA#!a%A!5 QU Or3c t|||}|gk(st|dk(r|St|dt|d|t|d|z z}|d|dg}|Dcgc]}tt |||}}|d}|dd} | D cgc]} | dz  } } | D cgc]} || z  } } | D cgc]} d| | dz zt dz z}} |dd}t|} t | D]U}t||dk(r"|jt|| |z6|jt|||zW|Scc}wcc} wcc} wcc} w)a p, q are polynomials in Z[x] or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the modified subresultant prs of p and q in Z[x] or Q[x], from the subresultant prs of p and q. The coefficients of the polynomials in the two sequences differ only in sign and the factor LC(p)**( deg(p)- deg(q)) as stated in Theorem 2 of the reference. The coefficients of the polynomials in the output sequence are modified subresultants. That is, they are determinants of appropriately selected submatrices of sylvester2, Sylvester's matrix of 1853. If the modified subresultant prs is complete, and LC( p ) > 0, it coincides with the (generalized) Sturm's sequence of the polynomials p, q. References ========== 1. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: "On the Remainders Obtained in Finding the Greatest Common Divisor of Two Polynomials." Serdica Journal of Computing, Serdica Journal of Computing, 9(2) (2015), 123-138. rrrrNN) rrrrrrrr rrrs r1modified_subresultants_amvrs4 Aa "C byCHM SV*s1vq)F3q61,== ?CAAH588Dvd4mQ'8G8 !*C" I# $QQqS $E $# $S1W $E $-2 2qRAqsGAaDL " 2D 2 GE D A 1X8 Q=B  OOXuQxi#o6 8 OOXeAhn5 7 8 O+9 % $ 3sE E  E0Ec|ddddf}t|j|z dz |j|z |z dz dD]}|j|t|jdz |j|z dz dD]}|j|t|D] }|j|jdz "|ddd|jf}|j S)a# Used in various subresultant prs algorithms. Evaluates the determinant, (a.k.a. subresultant) of a properly selected submatrix of s1, Sylvester's matrix of 1840, to get the correct sign and value of the leading coefficient of a given polynomial remainder. deg_f, deg_g are the degrees of the original polynomials p, q for which the matrix s1 = sylvester(p, q, x, 1) was constructed. rdel denotes the expected degree of the remainder; it is the number of rows to be deleted from each group of rows in s1 as described in the reference below. cdel denotes the expected degree minus the actual degree of the remainder; it is the number of columns to be deleted --- starting with the last column forming the square matrix --- from the matrix resulting after the row deletions. References ========== Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``Sturm Sequences and Modified Subresultant Polynomial Remainder Sequences.'' Serdica Journal of Computing, Vol. 8, No 1, 29-46, 2014. NrrNr)rr=r<col_delr>)deg_fdeg_gs1rdelcdelr)r+Mds r1 correct_signrs4 1a4A166E>A%qvv~'= degree(q, x). Computes the subresultant prs of p and q in Z[x] or Q[x]; the coefficients of the polynomials in the sequence are subresultants. That is, they are determinants of appropriately selected submatrices of sylvester1, Sylvester's matrix of 1840. To compute the coefficients polynomial divisions in Q[x] are performed, using the function rem(p, q, x). The coefficients of the remainders computed this way become subresultants by evaluating one subresultant per remainder --- that of the leading coefficient. This way we obtain the correct sign and value of the leading coefficient of the remainder and we easily ``force'' the rest of the coefficients to become subresultants. If the subresultant prs is complete, then it coincides with the Euclidean sequence of the polynomials p, q. References ========== 1. Akritas, A. G.:``Three New Methods for Computing Subresultant Polynomial Remainder Sequences (PRS's).'' Serdica Journal of Computing 9(1) (2015), 1-26. rr) rr2rrrrrrrrp)r^r_r#r!r"r&rr%rrsr_listrPr8rz sign_values r1subresultants_remr)s8 Ava1v  aqAq! Aq! AAv!q&1v 1u#$aq!#; 1eUAq1ua1v  1aA B!fG !) 1aL 1aL q5N!)!!QGWq[A a"Q(lj0 1 qau!1! !)& G Aq1u~A! 3 AE Nr3c|ddddf}|j}|j}t|dz|D]L}|||fdk7st|dz|D]&}|||f|||fz|||f|||fzz |||f<(d|||f<N|S)al M is a matrix, and M[i, j] specifies the pivot element. All elements below M[i, j], in the j-th column, will be zeroed, if they are not already 0, according to Dodgson-Bareiss' integer preserving transformations. References ========== 1. Akritas, A. G.: ``A new method for computing polynomial greatest common divisors and polynomial remainder sequences.'' Numerische MatheMatik 52, 119-127, 1988. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``On a Theorem by Van Vleck Regarding Sturm Sequences.'' Serdica Journal of Computing, 7, No 4, 101-134, 2013. Nrr)r=colsr)r)r+r,marscsrPrs r1pivotrqs& 1a4B B B 1Q3^ ad8q=1q5"% Ead8bAh.AqDBq!tH1DD1a4 EBq!tH  Ir3ct|}|gk(rgSt|D]1}|jt|dz }|j d|3t |tr|St |gS)z@ Rotates right by k. L is a row of a matrix or a list. rr)listrrprinsert isinstancer r7r*llr+els r1rotate_rrsl aB Rx 1X VVCGaK  !RAt$26&",6r3ct|}|gk(rgSt|D]1}|jd}|jt |dz |3t |tr|St |gS)z? Rotates left by k. L is a row of a matrix or a list. rr)rrrprrrr rs r1rotate_lrsk aB Rx 1X# VVAY #b'A+r"#At$26&",6r3cd}g}t|}||dk(r|dz}||dk(rt|dzD]"}||z|ks |j|||z$t||S)z Converts the row of a matrix to a poly of degree deg and variable x. Some entries at the beginning and/or at the end of the row may be zero. rr)rrrr)rowrr#r*ralengr,s r1row2polyrs A D s8D a&A+ E a&A+C!G_$ q5D= KKAE #$ a=r3c||z dk\r tdyt||z dz|}t||z dzD]}t||||ddf<||||z dzddf<|S)ao Creates a ``small'' matrix M to be triangularized. deg_f, deg_g are the degrees of the divident and of the divisor polynomials respectively, deg_g > deg_f. The coefficients of the divident poly are the elements in row2 and those of the divisor poly are the elements in row1. col_num defines the number of columns of the matrix M. rzReverse degreesNr)printr rr)rrrow1row2col_numr%r+s r1 create_mars u}   eema)A 55=1$ %$4#!Q$$"Aeema Hr3c|}td|jD],}||jdz |fdk(r|dz } t|dcSy)z Finds the degree of the poly corresponding (after triangularization) to the _last_ row of the ``small'' matrix M, created by create_ma(). deg_f is the degree of the divident poly. If _last_ row is all 0's returns None. rrN)rrr=r )r)rr,r+s r1 find_degreersQ A 1aff  QVVaZ] q AAq!9  r3cL|j|dz }t|jD]}|d|fdk(rn|j|zdzz }d}|dk7rM||z|jkr;t ||dz|||zddf<|dz }|dz}|dk7r||z|jkr;|S)a} s2 is sylvester2, r is the row pointer in s2, deg_g is the degree of the poly last inserted in s2. After a gcd of degree > 0 has been found with Van Vleck's method, and was inserted into s2, if its last term is not in the last column of s2, then it is inserted as many times as needed, rotated right by one each time, until the condition is met. rrN)rrrr=r)s2rPrRr+mrs r1 final_touchesrs qs A277^ QqS6Q;    AIM "B A 'a!ebggo AE*1q5!8  Q a 'a!ebggo Ir3c|dk(s|dk(r||gS||}}t||x}}t||x}} |dk(r |dk(r||gS||kr||| |||f\}}}} }}|dkDr |dk(r||gSt|||d} t|||d} ||g} d|z} t||tj }t |}t | |z D]}|jdt|g}t||tj }t |}t | |z D]}|jdt|g}d}|| z dkDrpd}t || z dz D]}t||dz| ||zddf<||z| z dz }t || z D]}t|||z| ||zddf<||z| z }|| z dk(rd}| dkDrZt|| ||| }t || z dzD]}t|||}|ddddf}t||}|n | dz }t||| |||z }t||jdz ddf||}t!||}t#||z |z}|dddf}t | |z D]}t|||z| ||zddf<|| z|z }t%||jdz ddf||z }||z |z}t | |z D]}t|||z| ||zddf<|| z|z }| |} }| j|| dkDrZ|dk7r)| jdkDrt'| || } t)| | S|dk7r<| jdk(r-t| j+dd| dddf<t)| | S)a p, q are polynomials in Z[x] (intended) or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the subresultant prs of p, q by triangularizing, in Z[x] or in Q[x], all the smaller matrices encountered in the process of triangularizing sylvester2, Sylvester's matrix of 1853; see references 1 and 2 for Van Vleck's method. With each remainder, sylvester2 gets updated and is prepared to be printed if requested. If sylvester2 has small dimensions and you want to see the final, triangularized matrix use this version with method=1; otherwise, use either this version with method=0 (default) or the faster version, subresultants_vv_2(p, q, x), where sylvester2 is used implicitly. Sylvester's matrix sylvester1 is also used to compute one subresultant per remainder; namely, that of the leading coefficient, in order to obtain the correct sign and to force the remainder coefficients to become subresultants. If the subresultant prs is complete, then it coincides with the Euclidean sequence of the polynomials p, q. If the final, triangularized matrix s2 is printed, then: (a) if deg(p) - deg(q) > 1 or deg( gcd(p, q) ) > 0, several of the last rows in s2 will remain unprocessed; (b) if deg(p) - deg(q) == 0, p will not appear in the final matrix. References ========== 1. Akritas, A. G.: ``A new method for computing polynomial greatest common divisors and polynomial remainder sequences.'' Numerische MatheMatik 52, 119-127, 1988. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``On a Theorem by Van Vleck Regarding Sturm Sequences.'' Serdica Journal of Computing, 7, No 4, 101-134, 2013. 3. Akritas, A. G.:``Three New Methods for Computing Subresultant Polynomial Remainder Sequences (PRS's).'' Serdica Journal of Computing 9(1) (2015), 1-26. rrrrnN)rr2rrrrrrr rrrrrrr=rrrrpprintr)r^r_r#r$r!r"r&rr%rrrrrrow0leng0r+rleng1rPr)M1r8rzrratemp2s r1subresultants_vvr s;X Ava1v  aqAq! Aq! AAv!q&1v 1u#$aq!#; 1eUAq1ua1v  1aA B 1aA B!fG!eG 1r " - - /D IE 7U? # A 4&>D !b ! , , .D IE 7U? # A 4&>D A u}q uu}q() 2A$T1q51Bq1uaxM 2 I  !uu}% 2A$T1q51Bq1uaxM 2 I  u}  !) eUD$ 8uu}q() Aq!QB1a4A  5 ! 9 !)"!QGWq[A !&&1*a-(!Q/4 34Awuqy! 1A#D!a%0Bq1uaxL 1 IM!&&1*a-(%!)4u *uqy! 1A#D!a%0Bq1uaxL 1 IMau tK !)P{rww{ 2q% (r N 1ABFF1Iq)1a4r Nr3c|dk(s|dk(r||gS||}}t||x}}t||x}}|dk(r |dk(r||gS||kr||||||f\}}}}}}|dkDr |dk(r||gSt|||d} ||g} d|z} t||tj } t | } t | | z D]}| jdt| g} t||tj }t |}t | |z D]}|jdt|g}|dkDrt|||| | }t ||z dzD]}t|||}|ddddf}t||}|| S|dz }t||| |||z }t||jdz ddf||}t|t!||z |z}| j|||}}|} t||tj }t |}t | |z D]}|jdt|g}|dkDr| S)a p, q are polynomials in Z[x] (intended) or Q[x]. It is assumed that degree(p, x) >= degree(q, x). Computes the subresultant prs of p, q by triangularizing, in Z[x] or in Q[x], all the smaller matrices encountered in the process of triangularizing sylvester2, Sylvester's matrix of 1853; see references 1 and 2 for Van Vleck's method. If the sylvester2 matrix has big dimensions use this version, where sylvester2 is used implicitly. If you want to see the final, triangularized matrix sylvester2, then use the first version, subresultants_vv(p, q, x, 1). sylvester1, Sylvester's matrix of 1840, is also used to compute one subresultant per remainder; namely, that of the leading coefficient, in order to obtain the correct sign and to ``force'' the remainder coefficients to become subresultants. If the subresultant prs is complete, then it coincides with the Euclidean sequence of the polynomials p, q. References ========== 1. Akritas, A. G.: ``A new method for computing polynomial greatest common divisors and polynomial remainder sequences.'' Numerische MatheMatik 52, 119-127, 1988. 2. Akritas, A. G., G.I. Malaschonok and P.S. Vigklas: ``On a Theorem by Van Vleck Regarding Sturm Sequences.'' Serdica Journal of Computing, 7, No 4, 101-134, 2013. 3. Akritas, A. G.:``Three New Methods for Computing Subresultant Polynomial Remainder Sequences (PRS's).'' Serdica Journal of Computing 9(1) (2015), 1-26. rrrrnN)rr2rrrrrrr rrrrrr=rr)r^r_r#r!r"r&rr%rrrrrrr+rrr)rr8rzrras r1subresultants_vv_2r sL Ava1v  aqAq! Aq! AAv!q&1v 1u#$aq!#; 1eUAq1ua1v  1aA B!fG!eG 1r " - - /D IE 7U? # A 4&>D !b ! , , .D IE 7U? # A 4&>D !) eUD$ 8uu}q() Aq!QB1a4A  5 ! 9N!)"!QGWq[A !&&1*a-(!Q/4 +z9: tauD!b)446D w' A KKN tf~9 !)< Nr3N)r)bz)r)K__doc__sympy.concrete.summationsrsympy.core.functionrsympy.core.numbersrsympy.core.singletonrsympy.core.symbolrr[$sympy.functions.elementary.complexesrr #sympy.functions.elementary.integersr sympy.matrices.denser r r sympy.printing.pretty.prettyrrsympy.simplify.simplifyrsympy.polys.domainsrsympy.polys.polytoolsrrrrrrrsympy.polys.polyerrorsrr2r:rHrJrLrOrSrWrcrgrjrlrrrrrrrrrrrrrrrrrrrrrrrrrr3r1rs rj0&""*:533?,"HHH2gR 6G*RB*H+$98#>DQ f  E*NJ*X~@L\cJHTM^M^vp?BgR &P0L\>@+ZFP: 7 7, 4 BObdr3