Ticket #11593: trac_11593.patch

File trac_11593.patch, 5.9 KB (added by Kwankyu Lee, 10 years ago)

rebased for Sage 5.6

  • sage/rings/polynomial/polynomial_element.pyx

    # HG changeset patch
    # User Kwankyu Lee <ekwankyu@gmail.com>
    # Date 1327641330 28800
    # Node ID 7f9a1a4e58a7266539587cc52470520a200085e2
    # Parent  5e0cfff855cb60925f39018caa65ac50adba6e68
    Trac #11593: correct quo_rem for divisors with unit leading coefficient
    
    diff --git a/sage/rings/polynomial/polynomial_element.pyx b/sage/rings/polynomial/polynomial_element.pyx
    a b  
    161161    import sage.rings.complex_interval_field
    162162    is_ComplexIntervalField = sage.rings.complex_interval_field.is_ComplexIntervalField
    163163
    164 
    165164cdef class Polynomial(CommutativeAlgebraElement):
    166165    """
    167166    A polynomial.
     
    19761975        R = self.parent().base_ring()[var]
    19771976        return R(self.list())
    19781977       
    1979 
    19801978    def change_ring(self, R):
    19811979        """
    19821980        Return a copy of this polynomial but with coefficients in R, if at
     
    20412039                D[i + j] = a
    20422040               
    20432041        return D
    2044            
    20452042   
    20462043    def __copy__(self):
    20472044        """
     
    22922289            4*t^3*x^3 + 3*t^2*x^2
    22932290        """
    22942291        return multi_derivative(self, args)
    2295 
    22962292       
    22972293    def _derivative(self, var=None):
    22982294        r"""
     
    23682364            return self.parent().zero_element()
    23692365        coeffs = self.list()
    23702366        return self._parent([n*coeffs[n] for n from 1 <= n <= degree])
    2371    
    23722367
    23732368    def integral(self):
    23742369        """
     
    38073802        else:
    38083803            return a*self
    38093804
    3810 
    38113805    def coefficients(self):
    38123806        """
    38133807        Return the coefficients of the monomials appearing in self.
     
    40424036        v = list(f.newtonpoly(p))
    40434037        return [sage.rings.rational.Rational(x) for x in v]
    40444038
    4045 
    40464039    #####################################################################
    40474040    # Conversions to other systems
    40484041    #####################################################################   
     
    52765269
    52775270        return self.roots(ring=CC, multiplicities=False)
    52785271
    5279 
    52805272    def variable_name(self):
    52815273        """
    52825274        Return name of variable used in this polynomial as a string.
     
    54495441        deprecation(4522, "This function is deprecated. It will be removed in a future release of Sage. Please use the .variable_name() function instead.")
    54505442        return self.parent().variable_name()
    54515443
    5452 
    54535444    def _xgcd(self, other):
    54545445        r"""
    54555446        Extended gcd of self and polynomial other.
     
    59675958# ----------------- inner functions -------------
    59685959# Cython can't handle function definitions inside other function
    59695960   
    5970 
    59715961cdef _karatsuba_sum(v,w):
    59725962    if len(v)>=len(w):
    59735963        x = list(v)
     
    60256015    f.__coeffs = coeffs
    60266016    return f
    60276017
    6028 
    60296018cdef class Polynomial_generic_dense(Polynomial):
    60306019    """
    60316020    A generic dense polynomial.
     
    64546443            else:
    64556444                return self._new_c(self.__coeffs[-int(n):], self._parent)
    64566445   
     6446    @coerce_binop
     6447    def quo_rem(self, other):
     6448        """
     6449        Returns the quotient and remainder of the Euclidean division of
     6450        ``self`` and ``other``.
     6451
     6452        Raises ZerodivisionError if ``other`` is zero. Raises ArithmeticError if ``other`` has
     6453        a nonunit leading coefficient.
     6454
     6455        EXAMPLES::
     6456
     6457            sage: P.<x> = QQ[]
     6458            sage: R.<y> = P[]
     6459            sage: f = R.random_element(10)
     6460            sage: g = y^5+R.random_element(4)
     6461            sage: q,r = f.quo_rem(g)
     6462            sage: f == q*g + r
     6463            True
     6464            sage: g = x*y^5
     6465            sage: f.quo_rem(g)
     6466            Traceback (most recent call last):
     6467            ...
     6468            ArithmeticError: Nonunit leading coefficient
     6469            sage: g = 0
     6470            sage: f.quo_rem(g)
     6471            Traceback (most recent call last):
     6472            ...
     6473            ZeroDivisionError: Division by zero polynomial
     6474        """
     6475        if other.is_zero():
     6476            raise ZeroDivisionError("Division by zero polynomial")
     6477        if not other.leading_coefficient().is_unit():
     6478            raise ArithmeticError("Nonunit leading coefficient")
     6479        if self.is_zero():
     6480            return self, self
     6481
     6482        R = self.parent().base_ring()
     6483        x = (<Polynomial_generic_dense>self).__coeffs[:] # make a copy
     6484        y = (<Polynomial_generic_dense>other).__coeffs
     6485        m = len(x)  # deg(self)=m-1
     6486        n = len(y)  # deg(other)=n-1
     6487        if m < n:
     6488            return self.parent()(0), self
     6489
     6490        quo = list()
     6491        for k from m-n >= k >= 0:
     6492            q = x[n+k-1]/y[n-1]
     6493            x[n+k-1] = R.zero_element()
     6494            for j from n+k-2 >= j >= k:
     6495                x[j] -= q * y[j-k]
     6496            quo.insert(0,q)
     6497
     6498        return self._new_c(quo,self._parent), self._new_c(x,self._parent)._inplace_truncate(n-1)
     6499
    64576500    cpdef Polynomial truncate(self, long n):
    64586501        r"""
    64596502        Returns the polynomial of degree ` < n` which is equivalent
     
    64906533
    64916534    cdef _inplace_truncate(self, long n):
    64926535        if n < len(self.__coeffs):
    6493             while n > 0 and not self.__coeffs[n]:
     6536            while n > 0 and not self.__coeffs[n-1]:
    64946537                n -= 1
    64956538        self.__coeffs = self.__coeffs[:n]
    64966539        return self
    6497    
    64986540       
    64996541def make_generic_polynomial(parent, coeffs):
    65006542    return parent(coeffs)
    65016543
    6502 
    65036544cdef class ConstantPolynomialSection(Map):
    65046545    """
    65056546    This class is used for conversion from a polynomial ring to its base ring.
  • sage/rings/polynomial/polynomial_quotient_ring.py

    diff --git a/sage/rings/polynomial/polynomial_quotient_ring.py b/sage/rings/polynomial/polynomial_quotient_ring.py
    a b  
    484484            try:
    485485                if not self.__polynomial.divides(R.modulus()):
    486486                    return False
    487             except AttributeError:
     487            except (ZeroDivisionError,ArithmeticError):
    488488                return False
    489489            return self.__ring.has_coerce_map_from(R.polynomial_ring())
    490490