
    \h;                         S r SSKJr  SSKJr  SSKJrJr  SSKJ	r	J
r
Jr  SSKJr  SSKJr  SSKJrJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  SSKJr   " S S5      r " S S5      rg)a  
This module contains functions for two multivariate resultants. These
are:

- Dixon's resultant.
- Macaulay's resultant.

Multivariate resultants are used to identify whether a multivariate
system has common roots. That is when the resultant is equal to zero.
    )prod)Mul)Matrixdiag)Polydegree_listrem)simplify)IndexedBase)itermonomialsmonomial_deg)monomial_key)poly_from_exprtotal_degree)binomial)combinations_with_replacement)sympy_deprecation_warningc                   ^    \ rS rSrSrS r\S 5       rS rS r	S r
S rS	 rS
 rS rS rSrg)DixonResultant   a  
A class for retrieving the Dixon's resultant of a multivariate
system.

Examples
========

>>> from sympy import symbols

>>> from sympy.polys.multivariate_resultants import DixonResultant
>>> x, y = symbols('x, y')

>>> p = x + y
>>> q = x ** 2 + y ** 3
>>> h = x ** 2 + y

>>> dixon = DixonResultant(variables=[x, y], polynomials=[p, q, h])
>>> poly = dixon.get_dixon_polynomial()
>>> matrix = dixon.get_dixon_matrix(polynomial=poly)
>>> matrix
Matrix([
[ 0,  0, -1,  0, -1],
[ 0, -1,  0, -1,  0],
[-1,  0,  1,  0,  0],
[ 0, -1,  0,  0,  1],
[-1,  0,  0,  1,  0]])
>>> matrix.det()
0

See Also
========

Notebook in examples: sympy/example/notebooks.

References
==========

.. [1] [Kapur1994]_
.. [2] [Palancz08]_

c                   ^ Xl         X l        [        U R                  5      U l        [        U R                   5      U l        [        S5      n[        U R                  5       Vs/ s H  oCU   PM	     snU l        [        U R                  5       V^s/ s H"  m[        U4S jU R                    5       5      PM$     snU l	        gs  snf s  snf )a  
A class that takes two lists, a list of polynomials and list of
variables. Returns the Dixon matrix of the multivariate system.

Parameters
----------
polynomials : list of polynomials
    A  list of m n-degree polynomials
variables: list
    A list of all n variables
alphac              3   @   >#    U  H  n[        U5      T   v   M     g 7f)N)r   ).0polyis     [/var/www/auris/envauris/lib/python3.13/site-packages/sympy/polys/multivariate_resultants.py	<genexpr>*DixonResultant.__init__.<locals>.<genexpr>[   s      SBR$T!21!5BRs   N)
polynomials	variableslennmr   rangedummy_variablesmax_max_degrees)selfr    r!   ar   s       `r   __init__DixonResultant.__init__D   s     '"T^^$T%%& .3DFFm<m!m< 466]$" ! S$BRBR SS"$  =$s   $C)C	c                 0    [        SSSS9  U R                  $ )NzS
            The max_degrees property of DixonResultant is deprecated.
            1.5$deprecated-dixonresultant-propertiesdeprecated_since_versionactive_deprecations_target)r   r(   r)   s    r   max_degreesDixonResultant.max_degrees^   s%    ! &+'M	
        c           	         U R                   U R                  S-   :w  a  [        S5      eU R                  /n[	        U R
                  5      n[        U R                  5       Hm  nU R                  U   X#'   [        [        U R
                  U5      5      nUR                  U R                   Vs/ s H  oUR                  U5      PM     sn5        Mo     [        U5      n[        U R
                  U R                  5      n[        U VV	s/ s H	  u  pX-
  PM     sn	n6 n
UR                  5       U
-  R                  5       n[!        XR                  5      S   $ s  snf s  sn	nf )ab  
Returns
=======

dixon_polynomial: polynomial
    Dixon's polynomial is calculated as:

    delta = Delta(A) / ((x_1 - a_1) ... (x_n - a_n)) where,

    A =  |p_1(x_1,... x_n), ..., p_n(x_1,... x_n)|
         |p_1(a_1,... x_n), ..., p_n(a_1,... x_n)|
         |...             , ...,              ...|
         |p_1(a_1,... a_n), ..., p_n(a_1,... a_n)|
   z%Method invalid for given combination.r   )r$   r#   
ValueErrorr    listr!   r%   r&   dictzipappendsubsr   r   detfactorr   )r)   rowstempidxsubstitutionfAtermsr*   bproduct_of_differencesdixon_polynomials               r   get_dixon_polynomial#DixonResultant.get_dixon_polynomiali   s"    66dffqj!DEE   !DNN#=C,,S1DIDNND 9:LKKt7G7GH7G!-7GHI !
 4LDNND$8$89!$&?qu&?!@EEG&<<DDF.0D0DEaHH I
 '@s   ,E
E
c                     [        SSSS9  [        U R                  5       Vs/ s H"  nU R                  U   U R                  U   -  PM$     nn[        U5      n[        U5      R                  5       n[        U6 $ s  snf )Nz
            The get_upper_degree() method of DixonResultant is deprecated. Use
            get_max_degrees() instead.
            r.   r/   r0   )	r   r%   r#   r!   r(   r   r   monomsr   )r)   r   list_of_productsproducts       r   get_upper_degreeDixonResultant.get_upper_degree   s    ! &+'M	
 &+466]4%2 !NN1-1B1B11EE%2 	 4'(w-&&(W%%4s   )A:c           	          UR                  5        Vs/ s H!  n[        [        X R                  5      5      PM#     nn[	        U6  Vs/ s H  n[        U5      PM     nnU$ s  snf s  snf )z
Returns a list of the maximum degree of each variable appearing
in the coefficients of the Dixon polynomial. The coefficients are
viewed as polys in $x_1, x_2, \dots, x_n$.
)coeffsr   r   r!   r<   r'   )r)   
polynomialr   	deg_listsdegsr4   s         r   get_max_degreesDixonResultant.get_max_degrees   si     ",!2!2!46!4 !dNN!;<!4 	 6 .1)_=_Ts4y_=6 >s   (A A%c                 T   U R                  U5      n[        U R                  U5      n[        US[	        SU R                  5      S9n[        UR                  5        VVs/ s H9  nU Vs/ s H(  n[        U/U R                  Q76 R                  U5      PM*     snPM;     snn5      nUR                  S   UR                  S   :w  aQ  [        UR                  S   5       Vs/ s H%  n[        S USS2U4    5       5      (       d  M#  UPM'     nnUSS2U4   nU$ s  snf s  snnf s  snf )	z
Construct the Dixon matrix from the coefficients of polynomial
\alpha. Each coefficient is viewed as a polynomial of x_1, ...,
x_n.
Tlexreversekeyr   r8   c              3   *   #    U  H	  oS :g  v   M     g7fr   N )r   elements     r   r   2DixonResultant.get_dixon_matrix.<locals>.<genexpr>   s      42 -4a<2   N)rX   r   r!   sortedr   r   rT   r   coeff_monomialshaper%   any)	r)   rU   r4   	monomialscr$   dixon_matrixcolumnkeeps	            r   get_dixon_matrixDixonResultant.get_dixon_matrix   s4    **:6 "$..+>	9d+E4>>BD	 )3):):)<>)<A *3 4)2A !%Q 8 8 G G J)2 4)<> ?
 a L$6$6q$99).|/A/A"/E)F 5)Fv 4'6	24 4 )FD 5 (40L 4 >5s$   
D
)/DD
!"D%D%D
c           	      j  ^^ TR                   (       a  gTR                  u  p#[        TR                  5       S   5      m[	        U5       V^s/ s H+  m[        UU4S j[	        U5       5       5      (       d  M)  TPM-     nnTUSS24   m[        S/US-
  -  S/-   /5      nTSSS24   U:X  a  ggs  snf )aw  
Test for the validity of the Kapur-Saxena-Yang precondition.

The precondition requires that the column corresponding to the
monomial 1 = x_1 ^ 0 * x_2 ^ 0 * ... * x_n ^ 0 is not a linear
combination of the remaining ones. In SymPy notation this is
the last column. For the precondition to hold the last non-zero
row of the rref matrix should be of the form [0, 0, ..., 1].
Fr   c              3   8   >#    U  H  nTTU4   S :g  v   M     g7fra   rb   )r   jr   matrixs     r   r   2DixonResultant.KSY_precondition.<locals>.<genexpr>   s     *Oh6!Q$<1+<hs   Nr8   r_   T)is_zero_matrixrh   r
   rrefr%   ri   r   )r)   rt   r$   r#   r   rA   	conditions    `  `  r   KSY_preconditionDixonResultant.KSY_precondition   s       || &++-*+ 8P8as*OeAh*O'O8PQQC1IO,-	"Q$<9$ Qs   (B09B0c                 :   [        UR                  5       Vs/ s H&  o!R                  U5      R                  (       a  M$  UPM(     nn[        UR                  5       Vs/ s H&  oAR                  U5      R                  (       a  M$  UPM(     nnXU4   $ s  snf s  snf )z/Remove the zero rows and columns of the matrix.)r%   rA   rowrv   colscol)r)   rt   r   rA   rs   r}   s         r   delete_zero_rows_and_columns+DixonResultant.delete_zero_rows_and_columns   s     V[[)O)!A1M1MA) 	 O V[[)O)!A1M1MA) 	 O Dj!!OOs   #BB#BBc                     Sn[        UR                  5       H(  nUR                  U5       H  nUS:w  d  M  X$-  n  M&     M*     U$ )z;Calculate the product of the leading entries of the matrix.r8   r   )r%   rA   r|   )r)   rt   resr|   els        r   product_leading_entries&DixonResultant.product_leading_entries   sD    %Cjjo7(C & &
 
r6   c                     U R                  U5      nUR                  5       u  p#nU R                  [        U5      5      nU R                  U5      $ )z@Calculate the Kapur-Saxena-Yang approach to the Dixon Resultant.)r   LUdecompositionr
   r   )r)   rt   _Us       r   get_KSY_Dixon_resultant&DixonResultant.get_KSY_Dixon_resultant   sI    226:((*a228A;?++F33r6   )r(   r&   r$   r#   r    r!   N)__name__
__module____qualname____firstlineno____doc__r+   propertyr4   rK   rQ   rX   ro   ry   r   r   r   __static_attributes__rb   r6   r   r   r      sK    (T$4 ! !"IH& 86"4r6   r   c                   H    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSrg)MacaulayResultant   a  
A class for calculating the Macaulay resultant. Note that the
polynomials must be homogenized and their coefficients must be
given as symbols.

Examples
========

>>> from sympy import symbols

>>> from sympy.polys.multivariate_resultants import MacaulayResultant
>>> x, y, z = symbols('x, y, z')

>>> a_0, a_1, a_2 = symbols('a_0, a_1, a_2')
>>> b_0, b_1, b_2 = symbols('b_0, b_1, b_2')
>>> c_0, c_1, c_2,c_3, c_4 = symbols('c_0, c_1, c_2, c_3, c_4')

>>> f = a_0 * y -  a_1 * x + a_2 * z
>>> g = b_1 * x ** 2 + b_0 * y ** 2 - b_2 * z ** 2
>>> h = c_0 * y * z ** 2 - c_1 * x ** 3 + c_2 * x ** 2 * z - c_3 * x * z ** 2 + c_4 * z ** 3

>>> mac = MacaulayResultant(polynomials=[f, g, h], variables=[x, y, z])
>>> mac.monomial_set
[x**4, x**3*y, x**3*z, x**2*y**2, x**2*y*z, x**2*z**2, x*y**3,
x*y**2*z, x*y*z**2, x*z**3, y**4, y**3*z, y**2*z**2, y*z**3, z**4]
>>> matrix = mac.get_matrix()
>>> submatrix = mac.get_submatrix(matrix)
>>> submatrix
Matrix([
[-a_1,  a_0,  a_2,    0],
[   0, -a_1,    0,    0],
[   0,    0, -a_1,    0],
[   0,    0,    0, -a_1]])

See Also
========

Notebook in examples: sympy/example/notebooks.

References
==========

.. [1] [Bruce97]_
.. [2] [Stiller96]_

c                 F   Xl         X l        [        U5      U l        U R                    Vs/ s H  n[	        U/U R                  Q76 PM     snU l        U R                  5       U l        U R                  5       U l	        U R                  U R                  5      U l        gs  snf )z
Parameters
==========

variables: list
    A list of all n variables
polynomials : list of SymPy polynomials
    A  list of m n-degree polynomials
N)r    r!   r"   r#   r   degrees_get_degree_mdegree_mget_sizemonomials_sizeget_monomials_of_certain_degreemonomial_set)r)   r    r!   r   s       r   r+   MacaulayResultant.__init__+  s     '"Y  ++-+ AET;DNN;+- **,"mmo !@@O-s    Bc                 @    S[        S U R                   5       5      -   $ )z
Returns
=======

degree_m: int
    The degree_m is calculated as  1 + \sum_1 ^ n (d_i - 1),
    where d_i is the degree of the i polynomial
r8   c              3   *   #    U  H	  oS -
  v   M     g7f)r8   Nrb   )r   ds     r   r   2MacaulayResultant._get_degree_m.<locals>.<genexpr>L  s     3l1ulre   )sumr   r3   s    r   r   MacaulayResultant._get_degree_mC  s     33dll3333r6   c                 h    [        U R                  U R                  -   S-
  U R                  S-
  5      $ )z
Returns
=======

size: int
    The size of set T. Set T is the set of all possible
    monomials of the n variables for degree equal to the
    degree_m
r8   )r   r   r#   r3   s    r   r   MacaulayResultant.get_sizeN  s+     .2DFFQJ??r6   c                     [        U R                  U5       Vs/ s H  n[        U6 PM     nn[        US[	        SU R                  5      S9$ s  snf )zO
Returns
=======

monomials: list
    A list of monomials of a certain degree.
Tr[   r\   )r   r!   r   rf   r   )r)   degreemonomialrj   s       r   r   1MacaulayResultant.get_monomials_of_certain_degreeZ  s`     6dnn6<>?> )1S(^> 	 ? i&udnn=? 	?	?s   Ac                 8   / n/ n[        U R                  5       H  nUS:X  a@  U R                  U R                  U   -
  nU R	                  U5      nUR                  U5        MI  UR                  U R                  US-
     U R                  US-
     -  5        U R                  U R                  U   -
  nU R	                  U5      nU H6  nU H-  n[        X5      S:X  d  M  U V	s/ s H  n	X:w  d  M
  U	PM     nn	M/     M8     UR                  U5        M     U$ s  sn	f )zW
Returns
=======

row_coefficients: list
    The row coefficients of Macaulay's matrix
r   r8   )r%   r#   r   r   r   r=   r!   r	   )
r)   row_coefficients	divisibler   r   r   	poss_rowsdivpitems
             r   get_row_coefficients&MacaulayResultant.get_row_coefficientsi  s    	tvvAAva8??G ''1  A!6!%a!e!4"5 6a8 @@H	$C&q;!+:C )7)$,0I *.)I )7I ' %
 !''	2    )7s   #	D
0D
c                 f   / nU R                  5       n[        U R                  5       Hz  nX#    Ho  n/ n[        U R                  U   U-  /U R
                  Q76 nU R                   H#  nUR                  UR                  U5      5        M%     UR                  U5        Mq     M|     [        U5      nU$ )zL
Returns
=======

macaulay_matrix: Matrix
    The Macaulay numerator matrix
)
r   r%   r#   r   r    r!   r   r=   rg   r   )	r)   rA   r   r   
multipliercoefficientsr   monomacaulay_matrixs	            r   
get_matrixMacaulayResultant.get_matrix  s     446tvvA.1
!D,,Q/*< -!^^- !--D ''(;(;D(AB .L) 2  !,r6   c           
         / nU R                    Hg  n/ n[        U R                  5       H8  u  pEUR                  [	        [        X%5      U R                  U   :  5      5        M:     UR                  U5        Mi     [        U5       VVs/ s H%  u  pF[        U5      U R                  S-
  :  d  M#  UPM'     nnn[        U5       VVs/ s H%  u  pF[        U5      U R                  S-
  :  d  M#  UPM'     nnnXx4$ s  snnf s  snnf )ak  
Returns
=======

reduced: list
    A list of the reduced monomials
non_reduced: list
    A list of the monomials that are not reduced

Definition
==========

A polynomial is said to be reduced in x_i, if its degree (the
maximum degree of its monomials) in x_i is less than d_i. A
polynomial that is reduced in all variables but one is said
simply to be reduced.
r8   )	r   	enumerater!   r=   boolr   r   r   r#   )	r)   r   r$   rB   r   vrreducednon_reduceds	            r   get_reduced_nonreduced(MacaulayResultant.get_reduced_nonreduced  s    $ 	""AD!$..1Da!3t||A!FGH 2T"	 #
 "+9!5 +!5!ftvvz) !5 +%.y%9 /%9TQa&DFFAI- %9 / ##+/s   "C7.C7"C=+C=c           	          U R                  5       u  p#U/ :X  a  [        S/5      $ [        U R                  5       VVs/ s H  u  pEXPR                  U   -  PM     nnn[        U R                  5       Vs/ s H#  nU R                  U   R                  Xd   5      PM%     nnUSS2U4   n/ n	[        UR                  5       H6  n
U Vs/ s H  oXSS24   ;   PM     nnSU;  d  M%  U	R                  U
5        M8     XU4   $ s  snnf s  snf s  snf )z
Returns
=======

macaulay_submatrix: Matrix
    The Macaulay denominator matrix. Columns that are non reduced are kept.
    The row which contains one of the a_{i}s is dropped. a_{i}s
    are the coefficients of x_i ^ {d_i}.
r8   NT)r   r   r   r!   r   r%   r#   r    coeffrA   r=   )r)   rt   r   r   r   r   reduction_setaisreduced_matrixrn   r|   aichecks                r   get_submatrixMacaulayResultant.get_submatrix  s!     $::< b=9 &dnn575 37!ll1o-5 	 7 dff'%1 "(()9:% 	 '  7
+,,-C<?@Cb>q&11CE@5 C  .
 K'((7' As   D 4*DD)r   r   r   r   r#   r    r!   N)r   r   r   r   r   r+   r   r   r   r   r   r   r   r   rb   r6   r   r   r      s2    -\P0	4
@? 8.$>)r6   r   N)r   mathr   sympy.core.mulr   sympy.matrices.denser   r   sympy.polys.polytoolsr   r   r	   sympy.simplify.simplifyr
   sympy.tensor.indexedr   sympy.polys.monomialsr   r   sympy.polys.orderingsr   r   r   (sympy.functions.combinatorial.factorialsr   	itertoolsr   sympy.utilities.exceptionsr   r   r   rb   r6   r   <module>r      sL   	   / : : , , = . > = 3 @a4 a4F]) ])r6   