
    Ed;                         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dS )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                   ^    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dS )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          fdt           j                  D              _         fdt           j                  D              _        dS )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                      g | ]
}|         S  r   ).0ias     Clib/python3.11/site-packages/sympy/polys/multivariate_resultants.py
<listcomp>z+DixonResultant.__init__.<locals>.<listcomp>X   s    <<<!<<<    c                 R    g | ]"t          fd j        D                       #S )c              3   B   K   | ]}t          |                   V  d S N)r	   )r   polyr   s     r   	<genexpr>z5DixonResultant.__init__.<locals>.<listcomp>.<genexpr>[   s0       S S$T!2!21!5 S S S S S Sr    )maxpolynomialsr   r   selfs    @r   r   z+DixonResultant.__init__.<locals>.<listcomp>[   sJ     $ $ $ ! S S S S$BR S S SSS $ $ $r    N)	r'   	variableslennmr   rangedummy_variables_max_degrees)r)   r'   r*   r   s   `  @r   __init__zDixonResultant.__init__D   s     '"T^$$T%&&  <<<<eDFmm<<<$ $ $ $46]]$ $ $r    c                 4    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   r0   r)   s    r   max_degreeszDixonResultant.max_degrees^   s1    ! &+'M	
 	
 	
 	
   r    c                 X   | j         | j        dz   k    rt          d          | j        g}t	          | j                  }t          | j                  D ]W}| j        |         ||<   d t          | j        |          D             |	                    fd| j        D                        Xt          |          }t          | j        | j                  }t          d |D              }|                                |z                                  }t          || j                  d         S )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.c                     i | ]\  }}||	S r   r   )r   varts      r   
<dictcomp>z7DixonResultant.get_dixon_polynomial.<locals>.<dictcomp>   s    KKKvsACKKKr    c                 :    g | ]}|                               S r   )subs)r   fsubstitutions     r   r   z7DixonResultant.get_dixon_polynomial.<locals>.<listcomp>   s%    HHH!--HHHr    c                     g | ]
\  }}||z
  S r   r   )r   r   bs      r   r   z7DixonResultant.get_dixon_polynomial.<locals>.<listcomp>   s     &?&?&?Aq1u&?&?&?r    r   )r-   r,   
ValueErrorr'   listr*   r.   r/   zipappendr   r   detfactorr   )	r)   rowstempidxAtermsproduct_of_differencesdixon_polynomialrC   s	           @r   get_dixon_polynomialz#DixonResultant.get_dixon_polynomiali   s.    6dfqj! 	FDEEE  !DN##== 	J 	JC,S1DIKKT^T1J1JKKKLKKHHHHt7GHHHIIII4LLDND$899!$&?&?&?&?&?!@EEGG&<<DDFF.0DEEaHHr    c                      t          ddd            fdt           j                  D             }t          |          }t	          |                                          }t          | S )Nz
            The get_upper_degree() method of DixonResultant is deprecated. Use
            get_max_degrees() instead.
            r3   r4   r5   c                 F    g | ]}j         |         j        |         z  S r   )r*   r0   r(   s     r   r   z3DixonResultant.get_upper_degree.<locals>.<listcomp>   s=     4 4 4 ! !N1-1B11EE 4 4 4r    )r   r.   r,   r   r   monomsr   )r)   list_of_productsproducts   `  r   get_upper_degreezDixonResultant.get_upper_degree   s    ! &+'M	
 	
 	
 	
4 4 4 4%*46]]4 4 4'((w--&&((W%%r    c                 n      fd|                                 D             }d t          | D             }|S )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$.
        c                 T    g | ]$}t          t          |j                            %S r   )r	   r   r*   r   r$   r)   s     r   r   z2DixonResultant.get_max_degrees.<locals>.<listcomp>   s=     6 6 6 !dDN!;!;<< 6 6 6r    c                 ,    g | ]}t          |          S r   )r&   )r   degss     r   r   z2DixonResultant.get_max_degrees.<locals>.<listcomp>   s    ===Ts4yy===r    )coeffsrH   )r)   
polynomial	deg_listsr9   s   `   r   get_max_degreeszDixonResultant.get_max_degrees   sV    6 6 6 6!+!2!2!4!46 6 6	 >=S)_===r    c                                           |          }t           j        |          t          dt	          d j                            t           fd|                                D                       j        d         j        d         k    r2fdt          j        d                   D             }d	d	|f         S )
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keyc                 0    g | ]fd D             S )c                 Z    g | ]'}t          gj        R                      |          (S r   )r   r*   coeff_monomial)r   r-   cr)   s     r   r   z>DixonResultant.get_dixon_matrix.<locals>.<listcomp>.<listcomp>   sI      4  4  4$% !%Q 8 8 8 8 G G J J  4  4  4r    r   )r   rk   	monomialsr)   s    @r   r   z3DixonResultant.get_dixon_matrix.<locals>.<listcomp>   sP     > > >$% 4  4  4  4  4)2 4  4  4 > > >r    r   r;   c                 Z    g | ]'}t          d  dd|f         D                       %|(S )c              3   "   K   | ]
}|d k    V  dS r   Nr   )r   elements     r   r%   z=DixonResultant.get_dixon_matrix.<locals>.<listcomp>.<genexpr>   s6       4 4G7a< 4 4 4 4 4 4r    N)any)r   columndixon_matrixs     r   r   z3DixonResultant.get_dixon_matrix.<locals>.<listcomp>   s`     5 5 5v 4 4'6	24 4 4 4 45F 5 5 5r    N)	rb   r   r*   sortedr   r   r_   shaper.   )r)   r`   r9   keeprs   rl   s   `   @@r   get_dixon_matrixzDixonResultant.get_dixon_matrix   s    **:66 "$.+>>	9d+E4>BBD D D	  > > > > >)3):):)<)<> > > ? ?
 a L$6q$99 	15 5 5 5|/A"/E)F)F 5 5 5D (40Lr    c                 (   j         rdS j        \  }t                                          d                   fdt	          |          D             }|ddf         t          dgdz
  z  dgz   g          }dddf         |k    rdS dS )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                 h    g | ]-t          fd t                    D                       +.S )c              3   4   K   | ]}|f         d k    V  dS ro   r   )r   jr   matrixs     r   r%   z=DixonResultant.KSY_precondition.<locals>.<listcomp>.<genexpr>   s0      *O*O6!Q$<1+<*O*O*O*O*O*Or    )rq   r.   )r   r   r}   r,   s    @r   r   z3DixonResultant.KSY_precondition.<locals>.<listcomp>   sE    PPPas*O*O*O*O*OeAhh*O*O*O'O'OPPPPr    Nr;   rt   T)is_zero_matrixrv   r   rrefr.   r   )r)   r}   r-   rL   	conditionr,   s    `   @r   KSY_preconditionzDixonResultant.KSY_precondition   s       	5|1 &++--*++PPPPP588PPPQQQQC1IO,--	"QQQ$<9$ 	45r    c                     fdt          j                  D             }fdt          j                  D             }||f         S )z/Remove the zero rows and columns of the matrix.c                 H    g | ]}                     |          j        |S r   )rowr~   )r   r   r}   s     r   r   z?DixonResultant.delete_zero_rows_and_columns.<locals>.<listcomp>   B     O O OA1MOO O Or    c                 H    g | ]}                     |          j        |S r   )colr~   )r   r|   r}   s     r   r   z?DixonResultant.delete_zero_rows_and_columns.<locals>.<listcomp>   r   r    )r.   rL   cols)r)   r}   rL   r   s    `  r   delete_zero_rows_and_columnsz+DixonResultant.delete_zero_rows_and_columns   s    O O O OV[))O O OO O O OV[))O O O dDj!!r    c                     d}t          |j                  D ]'}|                    |          D ]}|dk    r||z  } n(|S )z;Calculate the product of the leading entries of the matrix.r;   r   )r.   rL   r   )r)   r}   resr   els        r   product_leading_entriesz&DixonResultant.product_leading_entries   s`    %% 	 	Cjjoo  7 (CE 
r    c                     |                      |          }|                                \  }}}|                      t          |                    }|                     |          S )z@Calculate the Kapur-Saxena-Yang approach to the Dixon Resultant.)r   LUdecompositionr   r   )r)   r}   _Us       r   get_KSY_Dixon_resultantz&DixonResultant.get_KSY_Dixon_resultant   sY    226::((**1a228A;;??++F333r    N)__name__
__module____qualname____doc__r1   propertyr9   rS   rY   rb   rx   r   r   r   r   r   r    r   r   r      s        ( (T$ $ $4 ! ! X!"I "I "IH& & &     8  6" " "  4 4 4 4 4r    r   c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )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                     | _         | _        t          |           _         fd j         D              _                                          _                                          _         	                     j                   _
        dS )z
        Parameters
        ==========

        variables: list
            A list of all n variables
        polynomials : list of SymPy polynomials
            A  list of m n-degree polynomials
        c                 4    g | ]}t          |gj        R  S r   )r   r*   r\   s     r   r   z.MacaulayResultant.__init__.<locals>.<listcomp>:  s7     - - -T;DN;;; - - -r    N)r'   r*   r+   r,   degrees_get_degree_mdegree_mget_sizemonomials_sizeget_monomials_of_certain_degreemonomial_set)r)   r'   r*   s   `  r   r1   zMacaulayResultant.__init__+  s     '"Y- - - -+- - - **,,"mmoo !@@OOr    c                 D    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
        r;   c              3       K   | ]	}|d z
  V  
dS )r;   Nr   )r   ds     r   r%   z2MacaulayResultant._get_degree_m.<locals>.<genexpr>L  s&      33q1u333333r    )sumr   r8   s    r   r   zMacaulayResultant._get_degree_mC  s(     333dl3333333r    c                 R    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
        r;   )r   r   r,   r8   s    r   r   zMacaulayResultant.get_sizeN  s(     .2DFQJ???r    c                     d t          | j        |          D             }t          |dt          d| j                            S )zw
        Returns
        =======

        monomials: list
            A list of monomials of a certain degree.
        c                      g | ]}t          | S r   r   )r   monomials     r   r   zEMacaulayResultant.get_monomials_of_certain_degree.<locals>.<listcomp>b  s(     ? ? ?S(^ ? ? ?r    Trd   re   )r   r*   ru   r   )r)   degreerl   s      r   r   z1MacaulayResultant.get_monomials_of_certain_degreeZ  s]    ? ?5dn6<> >? ? ?	 i&udn==? ? ? 	?r    c                    g }g }t          | j                  D ]}|dk    r@| j        | j        |         z
  }|                     |          }|                    |           H|                    | j        |dz
           | j        |dz
           z             | j        | j        |         z
  }|                     |          }|D ])}|D ]$t          |          dk    rfd|D             }%*|                    |           |S )z
        Returns
        =======

        row_coefficients: list
            The row coefficients of Macaulay's matrix
        r   r;   c                      g | ]
}|k    |S r   r   )r   itemps     r   r   z:MacaulayResultant.get_row_coefficients.<locals>.<listcomp>  s1     )7 )7 )7$,0AI)7 )7 )7 )7r    )r.   r,   r   r   r   rI   r*   r
   )	r)   row_coefficients	divisibler   r   r   	poss_rowsdivr   s	           @r   get_row_coefficientsz&MacaulayResultant.get_row_coefficientsi  sK    	tv 	3 	3AAv 3a8??GG ''1111  A!6!%a!e!4"5 6 6 6a8 @@HH	$ 7 7C& 7 7q#;;!+ 7)7 )7 )7 )7) )7 )7 )7I7 !''	2222r    c                 f   g }|                                  }t          | j                  D ]v}||         D ]k}g }t          | j        |         |z  g| j        R  }| j        D ]*}|                    |                    |                     +|                    |           lwt          |          }|S )zt
        Returns
        =======

        macaulay_matrix: Matrix
            The Macaulay numerator matrix
        )
r   r.   r,   r   r'   r*   r   rI   rj   r   )	r)   rL   r   r   
multipliercoefficientsr$   monomacaulay_matrixs	            r   
get_matrixzMacaulayResultant.get_matrix  s     4466tv 	* 	*A.q1 * *
!D,Q/*< -!^- - - !- C CD ''(;(;D(A(ABBBBL))))* !,,r    c           
      p    g } j         D ]r}g }t           j                  D ]D\  }}|                    t	          t          ||           j        |         k                         E|                    |           s fdt          |          D             } fdt          |          D             }||fS )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.
        c                 P    g | ]"\  }}t          |          j        d z
  k      |#S r;   r   r,   r   r   rr)   s      r   r   z<MacaulayResultant.get_reduced_nonreduced.<locals>.<listcomp>  sB     + + +A!fftvz)+1 + + +r    c                 P    g | ]"\  }}t          |          j        d z
  k     |#S r   r   r   s      r   r   z<MacaulayResultant.get_reduced_nonreduced.<locals>.<listcomp>  sB     / / /TQa&&DFAI-/q / / /r    )r   	enumerater*   rI   boolr   r   )r)   r   r-   rM   r   vreducednon_reduceds   `       r   get_reduced_nonreducedz(MacaulayResultant.get_reduced_nonreduced  s    $ 	" 	# 	#AD!$.11 I I1Da!3!3t|A!FGGHHHHT""""+ + + +9!5!5 + + +/ / / /Yy%9%9 / / / ##r    c                    	                                   \  }}|g k    rt          dg          S  fdt           j                  D              fdt	           j                  D             }|dd|f         g }t	          j                  D ]*		fd|D             }d|vr|                    	           +|||f         S )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}.
        r;   c                 6    g | ]\  }}|j         |         z  S r   )r   )r   r   r   r)   s      r   r   z3MacaulayResultant.get_submatrix.<locals>.<listcomp>  s4     7 7 7$!Qdl1o- 7 7 7r    c                 \    g | ](}j         |                             |                   )S r   )r'   coeff)r   r   reduction_setr)   s     r   r   z3MacaulayResultant.get_submatrix.<locals>.<listcomp>  sD     ' ' ' "((q)9:: ' ' 'r    Nc                 ,    g | ]}|d d f         v S r#   r   )r   aireduced_matrixr   s     r   r   z3MacaulayResultant.get_submatrix.<locals>.<listcomp>  s+    @@@bR>#qqq&11@@@r    T)r   r   r   r*   r.   r,   rL   rI   )
r)   r}   r   r   aisrw   checkr   r   r   s
   `      @@@r   get_submatrixzMacaulayResultant.get_submatrix  s      $::<< b= 	997 7 7 7%dn557 7 7' ' ' ' 'df' ' '  7
+,-- 	! 	!C@@@@@C@@@E5  !C   dK'((r    N)r   r   r   r   r1   r   r   r   r   r   r   r   r   r    r   r   r      s        - -\P P P0	4 	4 	4
@ 
@ 
@? ? ?     8  .$ $ $>) ) ) ) )r    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   r    r   <module>r      st  	 	             / / / / / / / / : : : : : : : : : : , , , , , , , , , , , , = = = = = = = = . . . . . . > > > > > > > > = = = = = = 3 3 3 3 3 3 @ @ @ @ @ @a4 a4 a4 a4 a4 a4 a4 a4F]) ]) ]) ]) ]) ]) ]) ]) ]) ])r    