
    wg;                         d Z ddlmZ ddlmZ ddlmZ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mZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ  G d d      Z G d d      Zy)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                   V    e Zd ZdZd Zed        Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zy)DixonResultantaG  
    A class for retrieving the Dixon's resultant of a multivariate
    system.

    Examples
    ========

    >>> from sympy import symbols

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

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

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

    See Also
    ========

    Notebook in examples: sympy/example/notebooks.

    References
    ==========

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

    c                    || _         || _        t        | j                        | _        t        | j                         | _        t        d      }t        | j                        D cg c]  }||   	 c}| _        t        | j                        D cg c]   t        fd| j                   D              " c}| _	        yc c}w c c}w )aV  
        A class that takes two lists, a list of polynomials and list of
        variables. Returns the Dixon matrix of the multivariate system.

        Parameters
        ----------
        polynomials : list of polynomials
            A  list of m n-degree polynomials
        variables: list
            A list of all n variables
        alphac              3   :   K   | ]  }t        |           y w)N)r   ).0polyis     h/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/multivariate_resultants.py	<genexpr>z*DixonResultant.__init__.<locals>.<genexpr>[   s      S$T!21!5 Ss   N)
polynomials	variableslennmr   rangedummy_variablesmax_max_degrees)selfr   r   ar   s       `r   __init__zDixonResultant.__init__D   s     '"T^^$T%%& .3DFFm<!< 466]$ ! S$BRBR SS $  =$s   &C %Cc                 6    t        ddd       | j                  S )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_degreeszDixonResultant.max_degrees^   s%    ! &+'M	
        c           	         | j                   | j                  dz   k7  rt        d      | j                  g}t	        | j
                        }t        | j                        D ]k  }| j                  |   ||<   t        t        | j
                  |            }|j                  | j                  D cg c]  }|j                  |       c}       m t        |      }t        | j
                  | j                        }t        |D 	cg c]
  \  }}	||	z
   c}	} }
|j                         |
z  j                         }t!        || j                        d   S c c}w c c}	}w )a  
        Returns
        =======

        dixon_polynomial: polynomial
            Dixon's polynomial is calculated as:

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

            A =  |p_1(x_1,... x_n), ..., p_n(x_1,... x_n)|
                 |p_1(a_1,... x_n), ..., p_n(a_1,... x_n)|
                 |...             , ...,              ...|
                 |p_1(a_1,... a_n), ..., p_n(a_1,... a_n)|
           z%Method invalid for given combination.r   )r"   r!   
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_polynomialz#DixonResultant.get_dixon_polynomiali   s)    66dffqj!DEE   !DNN#= 	JC,,S1DIDNND 9:LKKt7G7GH!-HI	J
 4LDNND$8$89!$&?Aq1u&?!@EEG&<<DDF.0D0DEaHH I
 '@s   ,E
E
c                     t        ddd       t        | j                        D cg c]!  }| j                  |   | j                  |   z  # }}t        |      }t        |      j                         }t        | S c c}w )Nz
            The get_upper_degree() method of DixonResultant is deprecated. Use
            get_max_degrees() instead.
            r+   r,   r-   )	r   r#   r!   r   r&   r   r   monomsr   )r'   r   list_of_productsproducts       r   get_upper_degreezDixonResultant.get_upper_degree   s    ! &+'M	
 &+466]4 ! !NN1-1B1B11EE 4 4'(w-&&(W%%4s   &A:c           	          |j                         D cg c]!  }t        t        || j                              # }}t	        | D cg c]  }t        |       }}|S c c}w c c}w )z
        Returns a list of the maximum degree of each variable appearing
        in the coefficients of the Dixon polynomial. The coefficients are
        viewed as polys in $x_1, x_2, \dots, x_n$.
        )coeffsr   r   r   r8   r%   )r'   
polynomialr   	deg_listsdegsr1   s         r   get_max_degreeszDixonResultant.get_max_degrees   sg     ",!2!2!46 !dDNN!;< 6	 6 .1)_=Ts4y==6 >s   &AA c                 8   | j                  |      }t        | j                  |      }t        |dt	        d| j                              }t        |j                         D cg c]5  }|D cg c]'  }t        |g| j                   j                  |      ) c}7 c}}      }|j                  d   |j                  d   k7  rGt        |j                  d         D cg c]  }t        d |dd|f   D              r| }}|dd|f   }|S c c}w c c}}w c c}w )	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   r4   c              3   &   K   | ]	  }|d k7    ywr   N )r   elements     r   r   z2DixonResultant.get_dixon_matrix.<locals>.<genexpr>   s      4G7a< 4   N)rR   r   r   sortedr   r   rN   r   coeff_monomialshaper#   any)	r'   rO   r1   	monomialscr"   dixon_matrixcolumnkeeps	            r   get_dixon_matrixzDixonResultant.get_dixon_matrix   s)    **:6 "$..+>	9d+E4>>BD	 )3):):)<>$% *3 4$% !%Q 8 8 G G J  4 > ?
 a L$6$6q$99).|/A/A"/E)F 5v 4'6	24 4  5D 5 (40L 4 >5s   "	D
+,DD
"DD
c           	      L   j                   ryj                  \  }}t        j                         d         t	        |      D cg c]#  t        fdt	        |      D              s"% }}|ddf   t        dg|dz
  z  dgz   g      }dddf   |k(  ryyc c}w )a  
        Test for the validity of the Kapur-Saxena-Yang precondition.

        The precondition requires that the column corresponding to the
        monomial 1 = x_1 ^ 0 * x_2 ^ 0 * ... * x_n ^ 0 is not a linear
        combination of the remaining ones. In SymPy notation this is
        the last column. For the precondition to hold the last non-zero
        row of the rref matrix should be of the form [0, 0, ..., 1].
        Fr   c              3   2   K   | ]  }|f   d k7    ywrZ   r[   )r   jr   matrixs     r   r   z2DixonResultant.KSY_precondition.<locals>.<genexpr>   s     *O6!Q$<1+<*Os   Nr4   rX   T)is_zero_matrixr`   r
   rrefr#   ra   r   )r'   rk   r"   r!   r   r=   	conditions    `  `  r   KSY_preconditionzDixonResultant.KSY_precondition   s       ||1 &++-*+ 8Pas*OeAh*O'OPPQQC1IO,-	"Q$<9$ Qs   	#B!-B!c                    t        |j                        D cg c]   }|j                  |      j                  r|" }}t        |j                        D cg c]   }|j                  |      j                  r|" }}|||f   S c c}w c c}w )z/Remove the zero rows and columns of the matrix.)r#   r=   rowrl   colscol)r'   rk   r   r=   rj   rr   s         r   delete_zero_rows_and_columnsz+DixonResultant.delete_zero_rows_and_columns   s     V[[)OA1M1MAO O V[[)OA1M1MAO O dDj!!OOs    BB B	7B	c                     d}t        |j                        D ]$  }|j                  |      D ]  }|dk7  s	||z  } $ & |S )z;Calculate the product of the leading entries of the matrix.r4   r   )r#   r=   rq   )r'   rk   resrq   els        r   product_leading_entriesz&DixonResultant.product_leading_entries   sP    % 	Cjjo 7(C	
 
r2   c                     | j                  |      }|j                         \  }}}| j                  t        |            }| j                  |      S )z@Calculate the Kapur-Saxena-Yang approach to the Dixon Resultant.)rt   LUdecompositionr
   rx   )r'   rk   _Us       r   get_KSY_Dixon_resultantz&DixonResultant.get_KSY_Dixon_resultant   sK    226:((*1a228A;?++F33r2   N)__name__
__module____qualname____doc__r)   propertyr1   rG   rL   rR   rg   ro   rt   rx   r}   r[   r2   r   r   r      sK    (T$4 ! !"IH& 86"4r2   r   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)MacaulayResultanta-  
    A class for calculating the Macaulay resultant. Note that the
    polynomials must be homogenized and their coefficients must be
    given as symbols.

    Examples
    ========

    >>> from sympy import symbols

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

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

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

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

    See Also
    ========

    Notebook in examples: sympy/example/notebooks.

    References
    ==========

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

    c                 D   || _         || _        t        |      | _        | j                   D cg c]  }t	        |g| j                    c}| _        | j                         | _        | j                         | _	        | j                  | j                        | _        yc c}w )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)   zMacaulayResultant.__init__+  s     '"Y  ++-T;DNN; - **,"mmo !@@O-s   Bc                 @    dt        d | j                  D              z   S )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
        r4   c              3   &   K   | ]	  }|d z
    yw)r4   Nr[   )r   ds     r   r   z2MacaulayResultant._get_degree_m.<locals>.<genexpr>L  s     3q1u3r]   )sumr   r0   s    r   r   zMacaulayResultant._get_degree_mC  s     33dll3333r2   c                 h    t        | j                  | j                  z   dz
  | j                  dz
        S )z
        Returns
        =======

        size: int
            The size of set T. Set T is the set of all possible
            monomials of the n variables for degree equal to the
            degree_m
        r4   )r   r   r!   r0   s    r   r   zMacaulayResultant.get_sizeN  s+     .2DFFQJ??r2   c                     t        | j                  |      D cg c]
  }t        |  }}t        |dt	        d| j                              S c c}w )zw
        Returns
        =======

        monomials: list
            A list of monomials of a certain degree.
        TrT   rU   )r   r   r   r^   r   )r'   degreemonomialrb   s       r   r   z1MacaulayResultant.get_monomials_of_certain_degreeZ  sU     6dnn6<>?S(^ ?	 ? i&udnn=? 	?	?s   Ac                    g }g }t        | j                        D ]  }|dk(  r?| j                  | j                  |   z
  }| j	                  |      }|j                  |       G|j                  | j                  |dz
     | j                  |dz
     z         | j                  | j                  |   z
  }| j	                  |      }|D ]-  }|D ]&  }t        ||      dk(  s|D 	cg c]	  }	|	|k7  r|	 }}	( / |j                  |        |S c c}	w )z
        Returns
        =======

        row_coefficients: list
            The row coefficients of Macaulay's matrix
        r   r4   )r#   r!   r   r   r   r9   r   r	   )
r'   row_coefficients	divisibler   r   r   	poss_rowsdivpitems
             r   get_row_coefficientsz&MacaulayResultant.get_row_coefficientsi  s%    	tvv 	3AAva8??G ''1  A!6!%a!e!4"5 6a8 @@H	$ 7C& 7q#;!+:C )7$,0AI *. )7I )777
 !''	2	3   )7s    D	
c                 \   g }| j                         }t        | j                        D ]v  }||   D ]l  }g }t        | j                  |   |z  g| j
                   }| j                  D ]"  }|j                  |j                  |             $ |j                  |       n x t        |      }|S )zt
        Returns
        =======

        macaulay_matrix: Matrix
            The Macaulay numerator matrix
        )
r   r#   r!   r   r   r   r   r9   r_   r   )	r'   r=   r   r   
multipliercoefficientsr   monomacaulay_matrixs	            r   
get_matrixzMacaulayResultant.get_matrix  s     446tvv 	*A.q1 *
!D,,Q/*< -!^^- !-- CD ''(;(;D(ABCL)*	* !,r2   c           
         g }| j                   D ]f  }g }t        | j                        D ]9  \  }}|j                  t	        t        ||      | j                  |   k\               ; |j                  |       h t        |      D cg c]"  \  }}t        |      | j                  dz
  k  r|$ }}}t        |      D cg c]"  \  }}t        |      | j                  dz
  k\  r|$ }}}||fS c c}}w c c}}w )a  
        Returns
        =======

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

        Definition
        ==========

        A polynomial is said to be reduced in x_i, if its degree (the
        maximum degree of its monomials) in x_i is less than d_i. A
        polynomial that is reduced in all variables but one is said
        simply to be reduced.
        r4   )	r   	enumerater   r9   boolr   r   r   r!   )	r'   r   r"   r>   r   vrreducednon_reduceds	            r   get_reduced_nonreducedz(MacaulayResultant.get_reduced_nonreduced  s    $ 	"" 	#AD!$..1 I1Da!3t||A!FGHIT"		#
 "+9!5 +A!ftvvz)  + +%.y%9 /TQa&DFFAI-  / / ##+/s   'C,>'C2c           	         | j                         \  }}|g k(  rt        dg      S t        | j                        D cg c]  \  }}|| j                  |   z   }}}t        | j                        D cg c]#  }| j                  |   j                  ||         % }}|dd|f   }g }	t        |j                        D ]0  }
|D cg c]  }|||
ddf   v  }}d|vs |	j                  |
       2 ||	|f   S c c}}w c c}w c c}w )a  
        Returns
        =======

        macaulay_submatrix: Matrix
            The Macaulay denominator matrix. Columns that are non reduced are kept.
            The row which contains one of the a_{i}s is dropped. a_{i}s
            are the coefficients of x_i ^ {d_i}.
        r4   NT)r   r   r   r   r   r#   r!   r   coeffr=   r9   )r'   rk   r   r   r   r   reduction_setaisreduced_matrixrf   rq   aichecks                r   get_submatrixzMacaulayResultant.get_submatrix  s)     $::< b=9 &dnn57$!Qdll1o- 7 7 dff' "((q)9: ' '  7
+,,- 	!C<?@bR>#q&11@E@5 C 	!
 dK'((7' As   C73(C=DN)r~   r   r   r   r)   r   r   r   r   r   r   r   r[   r2   r   r   r      s2    -\P0	4
@? 8.$>)r2   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   r[   r2   r   <module>r      sL   	   / : : , , = . > = 3 @a4 a4F]) ])r2   