Ticket #7881: 7585_6_gcd.patch

File 7585_6_gcd.patch, 8.5 KB (added by robertwb, 11 years ago)
  • sage/rings/polynomial/multi_polynomial.pyx

    # HG changeset patch
    # User David Roe <roed@math.harvard.edu>
    # Date 1260893171 18000
    # Node ID 7094350d64cda104d0a4ac0519d07a51f72ad37c
    # Parent  64dfca1b55f08a0eb1a986fe569e691cdb467298
    Makes polynomials respect the _gcd framework so that they can use coercion in gcds.
    
    diff -r 64dfca1b55f0 -r 7094350d64cd sage/rings/polynomial/multi_polynomial.pyx
    a b  
    99
    1010from sage.misc.derivative import multi_derivative
    1111from sage.rings.infinity import infinity
     12from sage.structure.element cimport Element
    1213
    1314def is_MPolynomial(x):
    1415    return isinstance(x, MPolynomial)
     
    387388        """
    388389        return multi_derivative(self, args)
    389390
     391    # gcd and xgcd are repeated from PrincipalIdealDomainElement since polynomials inherit from CommutativeRingElement
     392    def gcd(self, right):
     393        """
     394        Returns the gcd of self and right, or 0 if both are 0.
     395        """
     396        from sage.structure.element import get_coercion_model, gcd
     397        if not PY_TYPE_CHECK(right, Element) or not ((<Element>right)._parent is self._parent):
     398            return get_coercion_model().bin_op(self, right, gcd)
     399        return self._gcd(right)
     400
     401    def xgcd(self, right):
     402        r"""
     403        Return the extended gcd of self and other, i.e., elements `r, s, t` such that
     404        .. math::
     405       
     406           r = s \cdot self + t \cdot other.
     407       
     408        .. note::
     409
     410           There is no guarantee on minimality of the cofactors.  In
     411           the integer case, see documentation for Integer._xgcd() to
     412           obtain minimal cofactors.
     413        """
     414        from sage.structure.element import get_coercion_model, xgcd
     415        if not PY_TYPE_CHECK(right, Element) or not ((<Element>right)._parent is self._parent):
     416            return get_coercion_model().bin_op(self, right, xgcd)
     417        return self._xgcd(right)
    390418
    391419    def polynomial(self, var):
    392420        """
  • sage/rings/polynomial/multi_polynomial_libsingular.pyx

    diff -r 64dfca1b55f0 -r 7094350d64cd sage/rings/polynomial/multi_polynomial_libsingular.pyx
    a b  
    36813681        id_Delete(&_I,r)
    36823682        return new_MP(parent,res)
    36833683
    3684     def gcd(self, right, algorithm=None, **kwds):
     3684    def _gcd(self, right, algorithm=None, **kwds):
    36853685        """
    36863686        Return the greatest common divisor of self and right.
    36873687
  • sage/rings/polynomial/polynomial_element.pyx

    diff -r 64dfca1b55f0 -r 7094350d64cd sage/rings/polynomial/polynomial_element.pyx
    a b  
    6868
    6969from sage.misc.derivative import multi_derivative
    7070
    71 from sage.rings.arith import sort_complex_numbers_for_display
     71from sage.rings.arith import sort_complex_numbers_for_display, gcd
    7272
    7373import polynomial_fateman
    7474
     
    10961096        """
    10971097        return self * self
    10981098
     1099    # gcd and xgcd are repeated from PrincipalIdealDomainElement since polynomials inherit from CommutativeAlgebraElement
     1100    def gcd(self, right):
     1101        """
     1102        Returns the gcd of self and right, or 0 if both are 0.
     1103        """
     1104        from sage.structure.element import get_coercion_model, gcd
     1105        if not PY_TYPE_CHECK(right, Element) or not ((<Element>right)._parent is self._parent):
     1106            return get_coercion_model().bin_op(self, right, gcd)
     1107        return self._gcd(right)
     1108
     1109    def xgcd(self, right):
     1110        r"""
     1111        Return the extended gcd of self and other, i.e., elements `r, s, t` such that
     1112        .. math::
     1113       
     1114           r = s \cdot self + t \cdot other.
     1115       
     1116        .. note::
     1117
     1118           There is no guarantee on minimality of the cofactors.  In
     1119           the integer case, see documentation for Integer._xgcd() to
     1120           obtain minimal cofactors.
     1121        """
     1122        from sage.structure.element import get_coercion_model, xgcd
     1123        if not PY_TYPE_CHECK(right, Element) or not ((<Element>right)._parent is self._parent):
     1124            return get_coercion_model().bin_op(self, right, xgcd)
     1125        return self._xgcd(right)
     1126
    10991127    def squarefree_decomposition(self):
    11001128        """
    11011129        Return the square-free decomposition of self. This is a partial
  • sage/rings/polynomial/polynomial_integer_dense_flint.pyx

    diff -r 64dfca1b55f0 -r 7094350d64cd sage/rings/polynomial/polynomial_integer_dense_flint.pyx
    a b  
    554554        """
    555555        return not (fmpz_poly_degree(self.__poly) == -1)
    556556       
    557     def gcd(self, right):
     557    def _gcd(self, right):
    558558        r"""
    559559        Return the GCD of self and right.  The leading
    560560        coefficient need not be 1.
     
    603603        return (self//g)*right
    604604
    605605
    606     def xgcd(self, right):
     606    def _xgcd(self, right):
    607607        """
    608608        This function can't in general return ``(g,s,t)`` as above,
    609609        since they need not exist.  Instead, over the integers, we
  • sage/rings/polynomial/polynomial_integer_dense_ntl.pyx

    diff -r 64dfca1b55f0 -r 7094350d64cd sage/rings/polynomial/polynomial_integer_dense_ntl.pyx
    a b  
    533533
    534534
    535535       
    536     def gcd(self, right):
     536    def _gcd(self, right):
    537537        r"""
    538538        Return the GCD of self and right.  The leading
    539539        coefficient need not be 1.
     
    582582        return (self * right).quo_rem(g)[0]
    583583
    584584
    585     def xgcd(self, right):
     585    def _xgcd(self, right):
    586586        """
    587587        This function can't in general return ``(g,s,t)`` as above,
    588588        since they need not exist.  Instead, over the integers, we
  • sage/rings/polynomial/polynomial_modn_dense_ntl.pyx

    diff -r 64dfca1b55f0 -r 7094350d64cd sage/rings/polynomial/polynomial_modn_dense_ntl.pyx
    a b  
    16911691    """
    16921692    A dense polynomial over the integers modulo p, where p is prime.
    16931693    """
    1694 
    1695     def gcd(self, right):
    1696         return self._gcd(right)
    1697    
    16981694    def _gcd(self, right):
    16991695        """
    17001696        Return the GCD of self and other, as a monic polynomial.
     
    17061702        g = self.ntl_ZZ_pX().gcd(right.ntl_ZZ_pX())
    17071703        return self.parent()(g, construct=True)
    17081704
    1709     def xgcd(self, right):
    1710         r"""
    1711         Return the extended gcd of self and other, i.e., elements `r, s, t` such that
    1712        
    1713         .. math::
    1714 
    1715            r = s \cdot self + t \cdot other.
    1716        
    1717         """
    1718         # copied from sage.structure.element.PrincipalIdealDomainElement due to lack of mult inheritance
    1719         if not PY_TYPE_CHECK(right, Element) or not ((<Element>right)._parent is self._parent):
    1720             from sage.rings.arith import xgcd
    1721             return bin_op(self, right, xgcd)
    1722         return self._xgcd(right)
    1723 
    17241705    def _xgcd(self, right):
    17251706        """
    17261707        Return ``g, u, v`` such that ``g = u*self + v*right``.
  • sage/rings/polynomial/polynomial_real_mpfr_dense.pyx

    diff -r 64dfca1b55f0 -r 7094350d64cd sage/rings/polynomial/polynomial_real_mpfr_dense.pyx
    a b  
    547547        r._normalize()
    548548        return q, r * leading
    549549       
    550     def gcd(self, other):
     550    def _gcd(self, other):
    551551        """
    552552        Returns the gcd of self and other as a monic polynomial. Due to the
    553553        inherit instability of division in this inexact ring, the results may
  • sage/rings/polynomial/polynomial_template.pxi

    diff -r 64dfca1b55f0 -r 7094350d64cd sage/rings/polynomial/polynomial_template.pxi
    a b  
    316316        #assert(r._parent(pari(self) * pari(right)) == r)
    317317        return r
    318318
    319     def gcd(self, Polynomial_template other):
     319    def _gcd(self, Polynomial_template other):
    320320        """
    321321        Return the greatest common divisor of self and other.
    322322
     
    341341        #assert(r._parent(pari(self).gcd(pari(other))) == r)
    342342        return r
    343343
    344     def xgcd(self, Polynomial_template other):
     344    def _xgcd(self, Polynomial_template other):
    345345        """
    346346        Computes extended gcd of self and other.
    347347