
    wgX                       d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z ddlmZmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZ ddlmZ ddlmZmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z% d5dZ&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d6dZ.d Z/d Z0d Z1d7dZ2d Z3d7dZ4d Z5d Z6d  Z7d! Z8d" Z9d# Z:d$ Z;d% Z<d& Z=d' Z>d( Z?d) Z@d* ZAd+ ZBd, ZCd- ZDd. ZEd/ ZFd0 ZGd1 ZHd7d2ZId3 ZJy4)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)Abssign)floor)eyeMatrixzeros)pretty_print)simplify)QQ)degreeLCPolypquoquopremrem)PolynomialErrorc                    t        t        | |      |      t        t        ||      |      }}||k(  r|dk  rt        g       S ||k(  r|dk(  rt        g       S |dk(  r|dk  rt        g       S |dk  r|dk(  rt        g       S |dk\  r|dk  rt        dg      S |dk  r|dk\  rt        dg      S t        | |      j                         }t        ||      j                         }|dk  rlt	        ||z         }d}	t        |      D ]  }
|	}|D ]  }|||
|f<   |dz   } |	dz   }	 d}	t        |||z         D ]  }
|	}|D ]  }|||
|f<   |dz   } |	dz   }	 |S |dk\  rt        |      t        |      k  r>g }t        t        |      t        |      z
        D ]  }
|j                  d        ||dd n=g }t        t        |      t        |      z
        D ]  }
|j                  d        ||dd t        ||      }d|z  }t	        |      }d}	t        |      D ]:  }
|	}|D ]  }||d|
z  |f<   |dz   } |	}|D ]  }||d|
z  dz   |f<   |dz   } |	dz   }	< |S y)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.

    r         N)	r   r   r   
all_coeffsr   rangelenappendmax)fgxmethodmnfpgpMkijcoeffhmxdims                   d/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/subresultants_qq_zz.py	sylvesterr2     s   < 41:q!641:q#9qA 	Av!a%bz 	Av!q&bz 	Av!a%bz	
Q16bz 	Av!a%qc{	
Q16qc{	a			 B	a			 B {!a%Lq 	AA !Q$E AA	 q!a% 	AA !Q$E AA	  {r7SWA3r7SW,- B!HA3r7SW,- B!HAYd3L 		AA !!A#q&	E A  %!A#'1*E AA		 3     c                    | dd }t        |d   |      }d}|t        |      k  rft        ||   |      }|dk  r|j                  ||          |dz
  }||k(  r|j                  ||          |dz
  }|dk\  r|}|dz   }|t        |      k  rf|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.

    Nr   r   r   )r   r   remove)poly_seqr#   Ldr+   d_is         r1   process_matrix_outputr:   n  s     	AqtQA	A
c!f*QqT1o7HHQqTNAA8HHQqTNAA!8AE c!f* Hr3   c                 6   | dk(  s|dk(  r| |gS t        | |      x}}t        ||      x}}|dk(  r	|dk(  r| |gS ||k  r|||||| f\  }}}}} }|dkD  r	|dk(  r| |gS | |g}t        | ||d      }|dz
  }	|	dkD  r|ddddf   }
t        ||z   |	z
  ||z         D ]  }|
j                  ||z   |	z
          t        ||	z
  |      D ]  }|
j                  ||	z
          g |
j                  d}}}||	k  rK|j                  |
ddd|f   j                                |
j                  |dz
  ||z          |dz  }||	k  rK|j                  t        ||      j                                |	dz  }	|	dkD  r|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.

    r   r   N)r   r2   r   row_delrowsr   detcol_swapr   as_exprr:   )r!   r"   r#   r&   degFr%   degGSR_Lr   r,   Spindcoeff_Lr*   ls                  r1   subresultants_sylvrH     s   2 	Ava1va|Aa|A 	Av!q&1v1u!"AtT1a!71dD!Q1ua1vq6D 	!Q1A 	
AA
a%q!tWQAE* 	"CJJq1uqy!	" Q? 	CJJq1u	 BGGQA1fNN2a1f:>>+,KKAq1u%FA 1f 	D!$,,./	Q% a%* 	KK q))r3   c                    | dk(  s|dk(  r| |gS t        | |      x}}t        ||      x}}|dk(  r	|dk(  r| |gS ||k  r|||||| f\  }}}}} }|dkD  r	|dk(  r| |gS | |g}t        | ||d      }|dz
  }	|	dkD  r|dd|z  d|	z  z
  ddf   }
g |
j                  d}}}||	k  rK|j                  |
ddd|f   j	                                |
j                  |dz
  ||z          |dz  }||	k  rK|j                  t        ||      j                                |	dz  }	|	dkD  r|j                   |j                                t        ||      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.

    r   r   r   N)	r   r2   r=   r   r>   r?   r   r@   r:   )r!   r"   r#   r&   rA   r%   rB   rC   r   r,   rD   rF   r*   rG   s                 r1   modified_subresultants_sylvrJ     s   2 	Ava1va|Aa|A 	Av!q&1v1u!"AtT1a!71dD!Q1ua1vq6D 	!Q1A 	
AA
a%q1qs{A~ BGGQA1fNN2a1f:>>+,KKAq1u%FA 1f 	D!$,,./	Q a%  	KK q))r3   c                 r    | dk(  s|dk(  rt        d| d|d      t        | ||d      j                         S )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.

    r   zThe resultant of z and z is not definedr   )r   r2   r>   )r!   r"   r#   s      r1   resrL     s<     	AvaaQRST	TAq!$((**r3   c                    t        | |      }t        ||      }||k  rd||z  z  t        || |      z  S |dk(  r||z  S t        | ||      }|dk(  ryt        ||      }t        ||      }d||z  z  |||z
  z  z  t        |||      z  S )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   )r   res_qr   r   )r!   r"   r#   r%   r&   rsrG   s           r1   rO   rO   (  s     	q!Aq!A1uac{U1a^++	
a!t1aL6q!A1aA!A#;QqS)%1a.88r3   c                 D   t        | |      }t        ||      }||k  rd||z  z  t        || |      z  S |dk(  r||z  S t        | ||      }|dk(  ry||z
  dz   }d||z  z  t        |||      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.
    rN   r   r   )r   res_zr   r   r   )r!   r"   r#   r%   r&   rP   deltawrQ   rG   r*   s              r1   rS   rS   D  s     	q!Aq!A1uac{U1a^++	
a!tAqM6EAIEqseAq!n,Aq!A1aA	A!Aq!Q$?"r3   c           	      |    t        t        |             D cg c]  }t        t        | |   |             c}S c c}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.

    )r   r   r	   r   )r6   r#   r+   s      r1   sign_seqrW   c  s1     /4CM.BCDHQK#$CCCs   9c                    t        t        | |      |      t        t        ||      |      }}||k(  r|dk  rt        g       S ||k(  r|dk(  rt        g       S |dk(  r|dk  rt        g       S |dk  r|dk(  rt        g       S |dk\  r|dk  rt        dg      S |dk  r|dk\  rt        dg      S t        d      }| |j	                  ||i      z  | j	                  ||i      |z  z
  }t        t        |||z
        ||      }t        ||      }	t        |	      }
t        |	      D ]R  }t        |	      D ]B  }|dk(  r$|j                  ||      |
|	dz
  |z
  |	dz
  |z
  f<   ,|j                  ||      |
||f<   D T |
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.

    r   r   yprs)
r   r   r   varsubsr   r    r   r   nth)pqr#   r$   r%   r&   rY   exprpolyr/   Br+   r,   s                r1   bezoutrc   l  s   J 41:q!641:q#9qA 	Av!a%bz 	Av!q&bz 	Av!a%bz	
Q16bz 	Av!a%qc{	
Q16qc{CA qvvqe}qvvqe}q00D T1Q3A&D
 
QBb	A2Y )r 	)A,0HHQN"q&1*b1fqj()((1a.!Q$		)) Hr3   c                     t        |       }t        t        |j                  dz              D ]'  }|j	                  d|z   |j                  dz
  |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').
    r   r   r   )r   r   intr=   row_swap)r&   r)   r+   s      r1   backward_eyerg     sR     	AA3qvvz?# *	

1q5!&&1*q.)* Hr3   c                    | dk(  s|dk(  r| |gS | |}}t        ||      x}}t        ||      x}}|dk(  r	|dk(  r||gS ||k  r||||||f\  }}}}}}|dkD  r	|dk(  r||gS ||g}	t        ||      ||z
  z  }
t        |||d      }||kD  rd}||k(  rd}|k  r|d|ddf   }|dz
  g }}||dz
  k  rV|j                  |ddd|f   j	                                ||dz
  k  r|j                  |dz
  |dz          |dz   }||dz
  k  rV|	j                  t        d||dz
  z  dz  z        t        ||      |
z  j                         z         |dz   }||k  rt        |	|      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.

    r   rZ   r   r   NrN   )
r   r   rc   r   r>   r?   re   r   r@   r:   )r^   r_   r#   r!   r"   r&   rA   r%   rB   rC   Frb   r,   r)   r*   rF   s                   r1   subresultants_bezoutrj     s   8 	Ava1vaqAa|Aa|A 	Av!q&1v1u!"AtT1a!71dD!Q1ua1vq6D
1a4$;A 	q!QA d{t|
t)ac1fIUB74!8mNN1Q!V9==?+4!8|

1q5!a%(AA	 4!8m 	Cq!A#wqy)*d7A.>.B-K-K-MMNE t) !q))r3   c                 h   | dk(  s|dk(  r| |gS | |}}t        ||      x}}t        ||      x}}|dk(  r	|dk(  r||gS ||k  r||||||f\  }}}}}}|dkD  r	|dk(  r||gS ||g}	t        |||d      }
||kD  rd}||k(  rd}|k  r|
d|ddf   }|dz
  g }}||dz
  k  rV|j                  |ddd|f   j                                ||dz
  k  r|j	                  |dz
  |dz          |dz   }||dz
  k  rV|	j                  t        ||      j                                |dz   }||k  rt        |	|      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.


    r   rZ   r   r   N)r   rc   r   r>   r?   r   r@   r:   )r^   r_   r#   r!   r"   r&   rA   r%   rB   rC   rb   r,   r)   r*   rF   s                  r1   modified_subresultants_bezoutrl     s   B 	Ava1vaqAa|Aa|A 	Av!q&1v1u!"AtT1a!71dD!Q1ua1vq6D 	q!QA d{t|
t)ac1fIUB74!8mNN1Q!V9==?+4!8|

1q5!a%(AA	 4!8m 	d7A&//12E t) !q))r3   c                    | dk(  s|dk(  r| |gS t        | |      }t        ||      }|dk(  r	|dk(  r| |gS ||kD  r||}}|| }} |dkD  r	|dk(  r| |gS d}t        | |      dk  rd}|  } | }t        | |      ||z
  z  }| |}	}||	g}
||z
  }t        |	|      }|dz
  }t        ||	t               }t        ||      }t        ||      }||z
  }||z
  }|||z   |z
  z  }|dk(  r*|
j	                  t        ||z  t        |      z               n&|
j	                  t        |t        |      z               |}|dkD  r|	|||f\  }}	}}|}|dz
  }t        ||	t               }t        ||      }t        ||      }||z
  }||z
  }|}||z   |z
  }||z  ||z  z  |z  }||}}||}}|dk(  r*|
j	                  t        ||z  t        |      z               n&|
j	                  t        |t        |      z               |dkD  r|r|
D cg c]  }|  }
}t        |
      }|
|dz
     t        k(  s|
|dz
     dk(  r|
j                  |dz
         |
S c c}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.

    r   r   domain)
r   r   r   r   r   r   r   r   r   pop)r^   r_   r#   r$   d0d1flaglcfa0a1	sturm_seqdel0rho1exp_dega2rho2d2deg_diff_newdel1mul_fac_olddeg_diff_oldrho3expo_oldexpo_newmul_fac_newr+   r%   s                              r1   sturm_pgr   a  s   ` 	Ava1v A,B
A,B	Qw271v	BwRB!1	Av"'!u D
1Q!BB Q(R"W
CBRI7Dr1ID1fGr2b!	!Br!HD
Q-BR<L7D |34K {(38s;/?#?@A(B#k*:$:;<  L
q&RRBBq&2r"%%2q	Rm|Bw  $;- X&)99KG$0+k4dQ;hsRx3{3C'CDEhc+.>(>?@- q&0 !*+AaR+	+ 	IAQ3)AE"2a"7a!e ,s   =
Ic                 r   | dk(  s|dk(  r| |gS t        | |      }t        ||      }|dk(  r	|dk(  r| |gS ||kD  r||}}|| }} |dkD  r	|dk(  r| |gS d}t        | |      dk  rd}|  } | }| |}}||g}t        ||t               }	t        |	|      }
|j	                  |	       |
dkD  rA||	||
f\  }}}}t        ||t               }	t        |	|      }
|j	                  |	       |
dkD  rA|r|D cg c]  }|  }}t        |      }||dz
     t        k(  s||dz
     dk(  r|j                  |dz
         |S c c}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.

    r   r   rn   r   r   r   r   r   r   r   rp   )r^   r_   r#   rq   rr   rs   ru   rv   rw   r{   r}   r+   r%   s                r1   sturm_qr     s   B 	Ava1v A,B
A,B	Qw271v	BwRB!1	Av"'!u D
1Q!BB BRI
b"R
 	 B
Q-Bb q&RRBB"b$$Rm"	 q& !*+AaR+	+ 	IAQ3)AE"2a"7a!e ,s   -
D4c                    t        | ||      }|g k(  st        |      dk(  r|S t        t        |d         t	        |d   |      t	        |d   |      z
  z        }|d   |d   g}d}t        |      }d}	|	|dz
  k  r|dk(  r=|j                  ||	           |	dz   }	|	|k(  rne|j                  ||	           |	dz   }	d}n?|dk(  r:|j                  ||	          |	dz   }	|	|k(  rn$|j                  ||	          |	dz   }	d}|	|dz
  k  r|dk(  rB|dkD  r=|d   |d   g}
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.

    r   r   r   )
euclid_amvr   r   r   r   r   r   r   )r^   r_   r#   r$   rZ   rt   rw   rs   r%   r+   aux_seqs              r1   	sturm_amvr   /  s   n Q1
C byCHM

 r#a&zVCFA.A1BBD
FC QQ I DCA	A
qs(AIAh(AAAvAh(AADQYc!f&AAAvc!f&AAD! qs(& {sQwQ<1.q! 	:ANN8IaL3$689	:	r3   c                    t        | ||d      }|g k(  st        |      dk(  r|S |d   |d   g}d}t        |      }d}||dz
  k  r|dk(  r?|j                  ||           |dz   }||k(  r	 |S |j                  ||           |dz   }d}nA|dk(  r<|j                  ||          |dz   }||k(  r	 |S |j                  ||          |dz   }d}||dz
  k  r|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.
    r   r   r   )r   r   r   )r^   r_   r#   rZ   
euclid_seqrs   r%   r+   s           r1   	euclid_pgr     s&   R 1aA
C byCHM
 a&#a&!J DCA	A
qs(AIAh(AAAv  Ah(AADQYc!f&AAAv
 	 c!f&AAD! qs($ r3   c                 n   | dk(  s|dk(  r| |gS t        | |      }t        ||      }|dk(  r	|dk(  r| |gS ||kD  r||}}|| }} |dkD  r	|dk(  r| |gS d}t        | |      dk  rd}|  } | }| |}}||g}t        ||t              }	t        |	|      }
|j	                  |	       |
dkD  r@||	||
f\  }}}}t        ||t              }	t        |	|      }
|j	                  |	       |
dkD  r@|r|D cg c]  }|  }}t        |      }||dz
     t        k(  s||dz
     dk(  r|j                  |dz
         |S c c}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.

    r   r   rn   r   )r^   r_   r#   rq   rr   rs   ru   rv   r   r{   r}   r+   r%   s                r1   euclid_qr     s   D 	Ava1v A,B
A,B	Qw271v	BwRB!1	Av"'!u D
1Q!BB BbJ	RB	B
Q-Br q&RRBBR#Rm2	 q& ",-Qqb-
- 	JA!a%C:a!e#4#9q1u .s   +
D2c                    | dk(  s|dk(  r| |gS t        | |      }t        ||      }|dk(  r	|dk(  r| |gS ||kD  r||}}|| }} |dkD  r	|dk(  r| |gS | }|}||g}t        ||      t        ||      z
  dz   d}	}d}
t        |||      t        d|z        z  }|j                  |       t        ||      }|dk\  r||||f\  }}}}|
dz  }
t	        |       }||dz
  z  |	|dz
  z  z  }	t        ||      |z
  dz   }t        |||      t        |	|dz
  z  |z        z  }|j                  |       t        ||      }|dk\  rt        |      }||dz
     t        k(  s||dz
     dk(  r|j                  |dz
         |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.

    r   r   rN   r   )r   rem_zr   r   r   r   r   rp   )r!   r"   r#   rq   rr   ru   rv   r   
deg_dif_p1cr+   r{   r}   sigma0r%   s                  r1   r   r   -  s   F 	Ava1v A,B
A,B	Qw271v	BwRB!1	Av"'1v 
B	
BbJ2qMF2qM1A5rJ 	
A	r2q	C"z!13	3Br
Q-B 'RRBB	QR&j1n%!j1n*=>B]R'!+
2r1a*q.&9V%C EE2Rm ' 	JA!a%C:a!e#4#9q1ur3   c           	      
   | dk(  s|dk(  r| |gS t        | |      }t        ||      }|dk(  r	|dk(  r| |gS ||kD  r||}}|| }} |dkD  r	|dk(  r| |gS t        d      }g }| |g}| |}	}||z
  }
|
}t        |      }t        t        ||            }t        |	|      }t        |      g}|
g}t	        ||d|d   f      }|j                  |       t        |      }|dz
  }t        ||	t               }t        ||      }t        ||      }||z
  }||z
  }||
|z   |z
  z  }|j                  d|z          d}|D ]
  }|d|z  z  } |d|z  z  }|dk(  r8d}t        t        |            D ]  }|||   ||   ||dz      z   z  z  } ||z  }ntd}t        t        |      dz
        D ]  }|||   ||   ||dz      z   z  z  } ||z  }|t        |      dz
     |t        |         z   |z
  }||t        |      dz
     |z  z  }t        ||z        dkD  r-|j                  t        ||z  |z  t        |      z               n-|j                  t        ||z  |z  t        |      z                t        d      }|j                  t        |             t	        ||d|t        |      dz
     f      }|j                  |       t        |      }|}|dkD  r|	|||f\  }}	}}|}
|dz
  }t        ||	t               }t        ||      }t        ||      }||z
  }||z
  }|}|
|z   |z
  } || z  ||z  z  |z  }!||!}}||}}|j                  d|z          d}|D ]
  }|d|z  z  } |d|z  z  }|dk(  r8d}t        t        |            D ]  }|||   ||   ||dz      z   z  z  } ||z  }ntd}t        t        |      dz
        D ]  }|||   ||   ||dz      z   z  z  } ||z  }|t        |      dz
     |t        |         z   |z
  }||t        |      dz
     |z  z  }t        ||z        dkD  r-|j                  t        ||z  |z  t        |      z               n-|j                  t        ||z  |z  t        |      z                t        d      }|j                  t        |             t	        ||d|t        |      dz
     f      }|j                  |       t        |      }|dkD  rt        |      }"||"dz
     t        k(  s||"dz
     dk(  r|j                  |"dz
         t        |      }"t        |       dk  r=|d   |d   g}#t        d|"      D ]"  }$|#j                  t        ||$   d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 ``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.

    r   r*   r   rn   rN   r   )r   r[   r   r	   r   r   sumr   r   r   r   r   r   r   rp   )%r^   r_   r#   rq   rr   r*   u_listsubres_lru   rv   rx   degdifrho_1rho_list_minus_1ry   rho_listp_listuvrz   r{   r|   r}   r~   r   r   numdenexpor   r   r   r   r   r%   r   r+   s%                                        r1   modified_subresultants_pgr   |  sn   < 	Ava1v 1+B
1+B	Qw271v	BwRB!1	Av"'!u 
SAF1vHB7DFrFE bQi(r1IDd}HVF	1q!VAY'	(A
MM!FA 1fGr2b!	!Br1ID
Q-BR<L7D |34K MM!l"# C Qw
a-C qs8}% 	<A8A;VAE]!:;;C	<$$s8}Q' 	<A8A;VAE]!:;;C	<$$s8}q()F3x=,AALPHS]Q./55 	S3Y!%-"2C4D"DFH(5&=#3S5E#EGGI 	CAOOT$Z 	1q!VCK!O45	6A
MM!FAL q&RRBBq&2r"%%2q	Rm|Bw  $;-X&)99KG %1+k4d 	a,&'  	AB7NC	R!Gm 1C3x=) @x{VAYA%>??@((CC3x=?+ @x{VAYA%>??@((C3x=1,-s8}0EETDX!23T99C sOaOOXeVmB&6K8H&HJLOOhuf}R'7[9I'IKKM Hd$q1aFa!89:aKo q&t 	HAA#!a%A!5QU 	HA	1w{A;,q! 	:ANN8HQK2$689	:r3   c           	         t        | ||      }|g k(  st        |      dk(  r|S t        |d         t        |d   |      t        |d   |      z
  z  }|d   |d   g}|D cg c]  }t        t	        ||      |       }}|d   }|dd }	|	D 
cg c]  }
|
dz
  	 }}
|D 
cg c]  }
||
z
  	 }}
|D cg c]  }d||dz
  z  t        d      z  z   }}|dd }t        |      }
t        |
      D ]C  }t        ||         dk(  r|j                  ||    |z         -|j                  ||   |z         E |S c c}w c c}
w c c}
w c c}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.

    r   r   r   rN   N)	r   r   r   r   r   r   r   r	   r   r^   r_   r#   lstrt   subr_seqra   deg_seqdeg	deg_seq_sr%   m_seqj_seqr,   factlst_sr*   s                    r1   subresultants_pgr   4  sy   6 $Aa
*C byCHM

 SV*s1vq)F3q61,==
?C AAH 588Dvd4mQ'8G8
!*C"I#$QQqS$E$#$S1W$E$ -22qRAqsGAaDL"2D2 GE 	D	A1X ,Q=BOOU1XIO,OOE!HsN+	, O+ 9 %$ 3s   D3D8D=0Ec           
         | dk(  s|dk(  r| |gS t        | |      }t        ||      }|dk(  r	|dk(  r| |gS ||kD  r||}}|| }} |dkD  r	|dk(  r| |gS d\  }}d}| |g}| |}
}	t        |
|      }||z
  }|dz  dk(  r|dz  }t        |dz   dz        }d}|}|dkD  r0|dz  }t        |	|
t              }|dk(  rt        ||      }nt        ||      }|}}t        ||      }||z
  }||z   |z   }||dz   z  |z  }d|z  }t        |      }t        ||z        dkD  r0|j                  t        t        |t        |      z                     n0|j                  t        t        |t        |      z                      |dz
  dkD  r|||dz
  z  z  }|
|||f\  }	}
}}|}|dz  dk(  r|dz  }t        |dz   dz        }|dz  dk(  r||z  }|dkD  r0t        |      }||dz
     t        k(  s||dz
     dk(  r|j                  |d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 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.

    r   r   r   r   r   rn   rN   )r   r   r
   r   r   r	   r   r   r   r   r   r   rp   )r^   r_   r#   rq   rr   r+   rQ   p_odd_index_sumr   ru   rv   sigma1p0phimul_facr}   r{   sigma2sigma3p1psir   r   r%   s                           r1   subresultants_amv_qr   u  s   < 	Ava1v A,B
A,B	Qw271v	BwRB!1	Av"'1v DAqO1vHBQiF	bB	Av{	Q
!a%1
CG	B q&	QR$6QiFQiF#VFFRm"W#g' 26"W, Ci7m sOaOOXfRW-=&>?@OOhvb#g,.>'?@@A 6A:a 00G RRBB6A:FAa!eq["Q3!8r!OM q&R 	HAA#!a%A!5QUr3   c                 B    t        |       }|dk(  ry|dz  }|dk(  r| S |S )z|
    base != 0 and expo >= 0 are integers;

    returns the sign of base**expo without
    evaluating the power itself!
    r   r   r   )r	   )baser   sbpes       r1   compute_signr     s3     
dB	Qw	B	Qws
	r3   c                 r   | j                         j                  r|j                         j                  rw| j                         j                  |j                         j                  k(  rBt        | |      t        ||      z
  dz   }t	        t        t        ||            |z  | z  ||      S t        | ||      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_univariategensr   r   r   r   r   r^   r_   r#   rT   s       r1   r   r     s    B 	
		!!aiik&?&?IIK		 0 001q!,q03r!Qx=%'A-q!44Aq!}r3   c                 r   | j                         j                  r|j                         j                  rw| j                         j                  |j                         j                  k(  rBt        | |      t        ||      z
  dz   }t	        t        t        ||            |z  | z  ||      S t        | ||      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   )r   r   r   r   r   r   r   r   r   s       r1   quo_zr     s    " 	
		!!aiik&?&?IIK		 0 001q!,q03r!Qx=%'A-q!44Aq!}r3   c                    | dk(  s|dk(  r| |gS t        | |      }t        ||      }|dk(  r	|dk(  r| |gS ||kD  r||}}|| }} |dkD  r	|dk(  r| |gS | }|}||g}t        ||      t        ||      z
  dz   d}	}t        ||      }
d\  }}d}|dz
  }|dz  dk(  r|dz  }t        |dz   dz        }|dz  }t        |||      t	        d|z        z  }t        ||      }t        ||      }||z
  }t        |
|dz         }||z   |z   }d|z  }|}t        ||z        dkD  r|j                  |       n|j                  |        |dz  dk(  r|dz  }|dz
  dkD  r|t        |
|dz
        z  }|dk\  r&t        |dz   dz        }|dz  dk(  r||z  }||||f\  }}}}|}|dz  }t        |       }||dz
  z  |	|dz
  z  z  }	t        ||      |z
  dz   }t        |||      t	        |	|dz
  z  |z        z  }t        ||      }t        ||      }||z
  }||z   |z   }||}}
t        |
|dz         |z  }d|z  }|}t        ||z        dkD  r|j                  |       n|j                  |        |dz  dk(  r|dz  }|dz
  dkD  r|t        |
|dz
        z  }|dk\  r&t        |      }||dz
     t        k(  s||dz
     dk(  r|j                  |dz
         |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.

    r   r   rN   r   r   )r   r   r
   r   r   r   r	   r   r   r   rp   )r!   r"   r#   rq   rr   ru   rv   r   r   r   r   r+   rQ   r   r   r   r{   r   r}   r   sgn_denr   r   r   r   r   r%   s                              r1   subresultants_amvr   .  s   > 	Ava1v A,B
A,B	Qw271v	BwRB!1	Av"'1v 
B	
BBxH2qMF2qM1A5rJ QiFDAqO	aB	Av{	Q
!a%1
C FA	r2q	C"z!13	3BQiF
Q-B	bB FBF,G c'O
#C)C
C 	S3Y!" 
Av{	Q 
AvzL&"q&:: 'a!eq["Q3!8r!ORRBB	QR&j1n%!j1n*=>B]R'!+
2r1a*q.&9V%C EER)Rm"W#g'   Q07: Ci sOaOOR!OObS" 6A:FA 6A:fb1f >>GQ 'V 	HAA#!a%A!5QUOr3   c           	         t        | ||      }|g k(  st        |      dk(  r|S t        |d         t        |d   |      t        |d   |      z
  z  }|d   |d   g}|D cg c]  }t        t	        ||      |       }}|d   }|dd }	|	D 
cg c]  }
|
dz
  	 }}
|D 
cg c]  }
||
z
  	 }}
|D cg c]  }d||dz
  z  t        d      z  z   }}|dd }t        |      }
t        |
      D ]U  }t        ||         dk(  r"|j                  t        ||    |z               6|j                  t        ||   |z               W |S c c}w c c}
w c c}
w c c}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.

    r   r   r   rN   N)
r   r   r   r   r   r   r   r	   r   r   r   s                    r1   modified_subresultants_amvr     s   4 Aa
"C byCHM

 SV*s1vq)F3q61,==
?C AAH 588Dvd4mQ'8G8
!*C"I#$QQqS$E$#$S1W$E$ -22qRAqsGAaDL"2D2 GE 	D	A1X 8Q=BOOXuQxi#o68OOXeAhn57	8 O+ 9 %$ 3s   EE
E0Ec                    |ddddf   }t        |j                  | z
  dz
  |j                  | z
  |z
  dz
  d      D ]  }|j                  |        t        |j                  dz
  |j                  |z
  dz
  d      D ]  }|j                  |        t        |      D ]   }|j                  |j                  dz
         " |ddd|j                  f   }|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.

    Nr   rN   r   )r   r=   r<   col_delr>   )deg_fdeg_gs1rdelcdelr)   r+   Mds           r1   correct_signr     s    4 	1a4A 166E>A%qvv~'<q'@"E 			! 166A:qvv}q0"5 			! 4[ 			!&&1* 
1ai<B668Or3   c                 D   | dk(  s|dk(  r| |gS | |}}t        ||      x}}t        ||      x}}|dk(  r	|dk(  r||gS ||k  r||||||f\  }}}}}}|dkD  r	|dk(  r||gS t        |||d      }	||g}
|dkD  rqt        | ||      }t        ||      }|dk  r|
S |dz
  }t        |||	|||z
        }t	        |t        ||      z  |z        }|
j                  |       ||}}||}} |dkD  rqt        |
      }|
|dz
     t        k(  s|
|dz
     dk(  r|
j                  |dz
         |
S )ai  
    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];
    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.

    r   r   )
r   r2   r   r   r   r   r   r   r   rp   )r^   r_   r#   r!   r"   r&   r   r%   r   r   sr_listrP   r8   rz   
sign_values                  r1   subresultants_remr   )  s   8 	Ava1v aqAq!Aq!AAv!q&1v1u#$aq!#; 1eUAq1ua1v 
1aA	B!fG !)1aL1aLq5N !)!!QGWq[A
a"Q(lj01 	q au!1! !)& 	GAq1u~A! 3AENr3   c                    | ddddf   }|j                   }|j                  }t        |dz   |      D ]L  }|||f   dk7  st        |dz   |      D ]&  }|||f   |||f   z  |||f   |||f   z  z
  |||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.

    Nr   r   )r=   colsr   )r)   r+   r,   marscsrP   r   s           r1   pivotr   q  s    & 
1a4B	B	B1Q3^ ad8q=1q5"% Ead8bAh.AqDBq!tH1DD1a4EBq!tH	
 Ir3   c                     t        |       }|g k(  rg S t        |      D ]1  }|j                  t        |      dz
        }|j	                  d|       3 t        | t               r|S t        |g      S )z@
    Rotates right by k. L is a row of a matrix or a list.

    r   r   )listr   rp   r   insert
isinstancer   r7   r*   llr+   els        r1   rotate_rr     sl    
 
aB	Rx	1X VVCGaK 
		!R At$26&",6r3   c                     t        |       }|g k(  rg S t        |      D ]1  }|j                  d      }|j                  t	        |      dz
  |       3 t        | t               r|S t        |g      S )z?
    Rotates left by k. L is a row of a matrix or a list.

    r   r   )r   r   rp   r   r   r   r   r   s        r1   rotate_lr     sk    
 
aB	Rx	1X #VVAY
		#b'A+r"# At$26&",6r3   c                     d}g }t        |       }| |   dk(  r|dz   }| |   dk(  rt        |dz         D ]"  }||z   |k  s|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.

    r   r   )r   r   r   r   )rowr   r#   r*   ra   lengr,   s          r1   row2polyr     s     	
ADs8D a&A+E a&A+ C!G_ $q5D=KKAE
#$ a=r3   c                     || z
  dk\  rt        d       yt        | |z
  dz   |      }t        | |z
  dz         D ]  }t        ||      ||ddf<    ||| |z
  dz   ddf<   |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.

    r   zReverse degreesNr   )printr   r   r   )r   r   row1row2col_numr%   r+   s          r1   	create_mar     s     u} eema)A55=1$% $4#!Q$$"AeemaHr3   c                     |}t        d| j                        D ]+  }| | j                  dz
  |f   dk(  r|dz
  } |dk\  r|c S dc S  y)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.

    r   r   N)r   r   r=   )r)   r   r,   r+   s       r1   find_degreer     sV     	A1aff &QVVaZ]q AAQ1%A%	&r3   c                 L   | j                  |dz
        }t        | j                        D ]  }|d|f   dk(  r n | j                  |z   dz   z
  }d}|dk7  rM||z   | j                  k  r;t	        ||dz         | ||z   ddf<   |dz  }|dz  }|dk7  r||z   | j                  k  r;| 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.

    r   r   N)r   r   r   r=   r   )s2rP   r   Rr+   mrs         r1   final_touchesr     s     	qsA 277^ QqS6Q;	 
AIM	"B 	
A
'a!ebggo AE*1q5!8	Q
a 'a!ebggo
 Ir3   c                    | dk(  s|dk(  r| |gS | |}}t        ||      x}}t        ||      x}}	|dk(  r	|dk(  r||gS ||k  r|||	|||f\  }}}}	}}|dkD  r	|dk(  r||gS t        |||d      }
t        |||d      }||g}d|z  }t        ||t              j	                         }t        |      }t        ||z
        D ]  }|j                  d        t        |g      }t        ||t              j	                         }t        |      }t        ||z
        D ]  }|j                  d        t        |g      }d}||	z
  dkD  rpd}t        ||	z
  dz
        D ]  }t        ||dz         |||z   ddf<    ||z   |	z
  dz
  }t        ||	z
        D ]  }t        |||z         |||z   ddf<    ||z   |	z
  }||	z
  dk(  rd}|	dkD  rZt        ||	|||      }t        ||	z
  dz         D ]  }t        |||      }|ddddf   } t        ||      }|n|	dz
  }t        |||
|||z
        }t        ||j                  dz
  ddf   ||      }t!        ||      }t#        ||z  |z        }|dddf   }t        |	|z
        D ]  }t        |||z         |||z   ddf<    ||	z   |z
  }t%        ||j                  dz
  ddf   ||z
        }||z  |z  }t        |	|z
        D ]  }t        |||z         |||z   ddf<    ||	z   |z
  }|	|}	}|j                  |       |	dkD  rZ|dk7  r)|j                  dkD  rt'        |||	      }t)        |       |S |dk7  r<|j                  dk(  r-t        |j+                  d      d      |dddf<   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.

    r   r   r   rn   N)r   r2   r   r   r   r   r   r   r   r   r   r   r   r   r   r=   r   r   r   r   pprintr   )r^   r_   r#   r$   r!   r"   r&   r   r%   r   r   r   r   r   row0leng0r+   r   leng1rP   r)   M1r8   rz   r   ra   temp2s                              r1   subresultants_vvr  		  s;   X 	Ava1v aqAq!Aq!AAv!q&1v1u#$aq!#; 1eUAq1ua1v 
1aA	B	1aA	B!fG!eG 1r"--/DIE7U?# A4&>D!b!,,.DIE7U?# A4&>D 	
A u}q uu}q() 	2A$T1q51Bq1uaxM	2I!uu}% 	2A$T1q51Bq1uaxM	2Iu} !)eUD$8uu}q() 	Aq!QB1a4A	
 5!9!) "!QGWq[A
!&&1*a-(!Q/434 Awuqy! 	1A#D!a%0Bq1uaxL	1IM !&&1*a-(%!)4u
*uqy! 	1A#D!a%0Bq1uaxL	1IM au 	tK !)P {rww{2q%(r

 N	 
1ABFF1Iq)1a4r
Nr3   c                    | dk(  s|dk(  r| |gS | |}}t        ||      x}}t        ||      x}}|dk(  r	|dk(  r||gS ||k  r||||||f\  }}}}}}|dkD  r	|dk(  r||gS t        |||d      }	||g}
d|z  }t        ||t              j	                         }t        |      }t        ||z
        D ]  }|j                  d        t        |g      }t        ||t              j	                         }t        |      }t        ||z
        D ]  }|j                  d        t        |g      }|dkD  rt        |||||      }t        ||z
  dz         D ]  }t        |||      }|ddddf   } t        ||      }||
S |dz
  }t        |||	|||z
        }t        ||j                  dz
  ddf   ||      }t        |t!        ||      z  |z        }|
j                  |       ||}}|}t        ||t              j	                         }t        |      }t        ||z
        D ]  }|j                  d        t        |g      }|dkD  r|
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.

    r   r   r   rn   N)r   r2   r   r   r   r   r   r   r   r   r   r   r   r   r=   r   r   )r^   r_   r#   r!   r"   r&   r   r%   r   r   r   r   r   r  r+   r   r  r)   r  r8   rz   r   ra   s                          r1   subresultants_vv_2r  	  s   L 	Ava1v aqAq!Aq!AAv!q&1v1u#$aq!#; 1eUAq1ua1v 
1aA	B!fG!eG 1r"--/DIE7U?# A4&>D!b!,,.DIE7U?# A4&>D !)eUD$8uu}q() 	Aq!QB1a4A	
 5!9N!) "!QGWq[A
!&&1*a-(!Q/4+z9: 	t auD!b)446D	w' 	AKKN	tf~9 !)< Nr3   N)r   )bz)r   )K__doc__sympy.concrete.summationsr   sympy.core.functionr   sympy.core.numbersr   sympy.core.singletonr   sympy.core.symbolr   r[   $sympy.functions.elementary.complexesr   r	   #sympy.functions.elementary.integersr
   sympy.matrices.denser   r   r   sympy.printing.pretty.prettyr   r   sympy.simplify.simplifyr   sympy.polys.domainsr   sympy.polys.polytoolsr   r   r   r   r   r   r   sympy.polys.polyerrorsr   r2   r:   rH   rJ   rL   rO   rS   rW   rc   rg   rj   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r3   r1   <module>r     s   rj 0 & " " * : 5 3 3 ? , " H H H 2gR6G*RB*H+$98#>DQfE*NJ*X~@L\cJHTM^M^vp?BgR &P0L\>@+ZFP:77,4& BObdr3   