ó
¡¼™\c           @   sà   d  Z  d d l m Z m Z m Z m Z d d l m Z m Z 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 m Z d d l m Z d d	 l m Z d
 d d „  ƒ  YZ d d d „  ƒ  YZ d S(   s  
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.
iÿÿÿÿ(   t   IndexedBaset   Matrixt   Mult   Poly(   t   remt   prodt   degree_listt   diag(   t   range(   t   monomial_degt   itermonomials(   t   monomial_key(   t   poly_from_exprt   total_degree(   t   binomial(   t   combinations_with_replacementt   DixonResultantc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sL  
    A class for retrieving the Dixon's resultant of a multivariate
    system.

    Examples
    ========

    >>> from sympy.core 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            s­   | |  _  | |  _ t |  j ƒ |  _ t |  j  ƒ |  _ t d ƒ } g  t |  j ƒ D] ‰  | ˆ  ^ qR |  _ g  t |  j ƒ D]% ‰  t ‡  f d †  |  j  Dƒ ƒ ^ q{ |  _	 d S(   sV  
        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
        t   alphac         3   s   |  ] } t  | ƒ ˆ  Vq d  S(   N(   R   (   t   .0t   poly(   t   i(    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pys	   <genexpr>Z   s    N(
   t   polynomialst	   variablest   lent   nt   mR    R   t   dummy_variablest   maxt   max_degrees(   t   selfR   R   t   a(    (   R   sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyt   __init__B   s    		)c         C   s'  |  j  |  j d k r% t d ƒ ‚ n  |  j g } t |  j ƒ } xp t |  j ƒ D]_ } |  j | | | <d „  t |  j | ƒ Dƒ } | j	 g  |  j D] } | j
 | ƒ ^ q“ ƒ qP Wt | ƒ } t |  j |  j ƒ } t g  | D] \ } }	 | |	 ^ qÞ Œ  }
 | j ƒ  |
 j ƒ  } t | |  j ƒ d S(   s²  
        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)|
        i   s%   Method invalid for given combination.c         S   s   i  |  ] \ } } | | “ q S(    (    (   R   t   vart   t(    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pys
   <dictcomp>v   s   	 i    (   R   R   t
   ValueErrorR   t   listR   R   R   t   zipt   appendt   subsR   R   t   dett   factorR   (   R   t   rowst   tempt   idxt   substitutiont   ft   At   termsR   t   bt   product_of_differencest   dixon_polynomial(    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyt   get_dixon_polynomial]   s    0)c         C   s\   g  t  |  j ƒ D] } |  j | |  j | ^ q } t | ƒ } t | ƒ j ƒ  } t | Œ  S(   N(   R   R   R   R   R   R   t   monomsR	   (   R   R   t   list_of_productst   product(    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyt   get_upper_degree   s
    1c   
      C   s  | j  ƒ  } t t |  j |  j ƒ  ƒ ƒ } t | d t d t d |  j ƒ ƒ} t g  | D]4 } g  | D]! } t	 | |  j Œ j
 | ƒ ^ qe ^ qX ƒ } g  t | j d ƒ D]A } t g  | d d … | f D] } | d k ^ qÉ ƒ r© | ^ q© }	 | d d … |	 f S(   s¥   
        Construct the Dixon matrix from the coefficients of polynomial
        \alpha. Each coefficient is viewed as a polynomial of x_1, ...,
        x_n.
        t   reverset   keyt   lexiÿÿÿÿNi    (   t   coeffsR#   R
   R   R7   t   sortedt   TrueR   R   R   t   coeff_monomialR   t   shapet   any(
   R   t
   polynomialt   coefficientst	   monomialst   cR   t   dixon_matrixt   columnt   elementt   keep(    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyt   get_dixon_matrix‰   s    	A;(   t   __name__t
   __module__t   __doc__R   R3   R7   RI   (    (    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyR      s
   )		$	t   MacaulayResultantc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s2  
    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.core 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         C   sƒ   | |  _  | |  _ t | ƒ |  _ g  |  j  D] } t | |  j Œ ^ q+ |  _ |  j ƒ  |  _ |  j ƒ  |  _	 |  j
 |  j ƒ |  _ d S(   sÌ   
        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   t   degreest   _get_degree_mt   degree_mt   get_sizet   monomials_sizet   get_monomials_of_certain_degreet   monomial_set(   R   R   R   R   (    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyR   Ñ   s    
		(c         C   s   d t  d „  |  j Dƒ ƒ S(   s½   
        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
        i   c         s   s   |  ] } | d  Vq d S(   i   N(    (   R   t   d(    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pys	   <genexpr>ò   s    (   t   sumRN   (   R   (    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyRO   é   s    	c         C   s"   t  |  j |  j d |  j d ƒ S(   sÒ   
        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
        i   (   R   RP   R   (   R   (    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyRQ   ô   s    
c         C   sM   g  t  |  j | ƒ D] } t | Œ  ^ q } t | d t d t d |  j ƒ ƒS(   sw   
        Returns
        =======

        monomials: list
            A list of monomials of a certain degree.
        R8   R9   R:   (   R   R   R   R<   R=   R   (   R   t   degreet   monomialRC   (    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyRS      s
    	c   
      C   s  g  } g  } xt  |  j ƒ D]û } | d k ra |  j |  j | } |  j | ƒ } | j | ƒ q | j |  j | d |  j | d ƒ |  j |  j | } |  j | ƒ } x\ | D]T } xK | D]C } t | | ƒ d k r¿ g  | D] }	 |	 | k rá |	 ^ qá } q¿ q¿ Wq² W| j | ƒ q W| S(   s   
        Returns
        =======

        row_coefficients: list
            The row coefficients of Macaulay's matrix
        i    i   (   R   R   RP   RN   RS   R%   R   R   (
   R   t   row_coefficientst	   divisibleR   RW   RX   t	   poss_rowst   divt   pt   item(    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyt   get_row_coefficients  s$    #c   	      C   s«   g  } |  j  ƒ  } x† t |  j ƒ D]u } xl | | D]` } g  } t |  j | | |  j Œ } x' |  j D] } | j | j | ƒ ƒ qf W| j | ƒ q3 Wq" Wt	 | ƒ } | S(   st   
        Returns
        =======

        macaulay_matrix: Matrix
            The Macaulay numerator matrix
        (
   R_   R   R   R   R   R   RT   R%   R>   R   (	   R   R)   RY   R   t
   multiplierRB   R   t   monot   macaulay_matrix(    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyt
   get_matrix+  s    c   	      C   sü   g  } xm |  j  D]b } g  } xF t |  j ƒ D]5 \ } } | j t t | | ƒ |  j | k ƒ ƒ q, W| j | ƒ q Wg  t | ƒ D]+ \ } } t | ƒ |  j d k  rƒ | ^ qƒ } g  t | ƒ D]+ \ } } t | ƒ |  j d k rÁ | ^ qÁ } | | f S(   sÓ  
        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.
        i   (	   RT   t	   enumerateR   R%   t   boolR   RN   RV   R   (	   R   RZ   R   R*   R   t   vt   rt   reducedt   non_reduced(    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyt   get_reduced_nonreducedB  s    -%%c         C   s)  |  j  ƒ  \ } } | g  k r+ t d g ƒ Sg  t |  j ƒ D] \ } } | |  j | ^ q; } t g  t |  j ƒ D]  } |  j | j	 | | ƒ ^ qq ƒ } | d d … | f } g  }	 xb t | j
 ƒ D]Q }
 g  | D]" } | | |
 d d … f k ^ qÓ } t | k rÆ |	 j |
 ƒ qÆ qÆ W| |	 | f S(   s  
        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}.
        i   N(   Rj   R   Rd   R   RN   R#   R   R   R   t   coeffR)   R=   R%   (   R   t   matrixRh   Ri   R   Rf   t   reduction_sett   aist   reduced_matrixRH   t   rowt   ait   check(    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyt   get_submatrixa  s    
06/(   RJ   RK   RL   R   RO   RQ   RS   R_   Rc   Rj   Rs   (    (    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyRM   ¢   s   .							N(    (    (   RL   t   sympyR    R   R   R   R   R   R   R   t   sympy.core.compatibilityR   t   sympy.polys.monomialsR	   R
   t   sympy.polys.orderingsR   t   sympy.polys.polytoolsR   R   t(   sympy.functions.combinatorial.factorialsR   t	   itertoolsR   R   RM   (    (    (    sB   lib/python2.7/site-packages/sympy/polys/multivariate_resultants.pyt   <module>
   s   ""‹