Ticket #13442: trac_13442.patch

File trac_13442.patch, 5.5 KB (added by saraedum, 10 years ago)
  • sage/categories/fields.py

    # HG changeset patch
    # User Julian Rueth <julian.rueth@gmail.com>
    # Date 1350766197 0
    # Node ID 7ffe38b585130075a207b9069d9f0d72c0e231d6
    # Parent  698ec14c513f99fa5a3a7aeca982cc14fd249a89
    Trac #13442: rings can provide _gcd_univariate_polynomial for polynomial factorization
    
    diff --git a/sage/categories/fields.py b/sage/categories/fields.py
    a b class Fields(Category_singleton): 
    181181            """
    182182            return True
    183183
     184        def _gcd_univariate_polynomial(self, f, g):
     185            """
     186            Return the greatest common divisor of ``f`` and ``g``, as a
     187            monic polynomial.
     188
     189            INPUT:
     190
     191                - ``f``, ``g`` -- two polynomials defined over ``self``
     192
     193            .. NOTE::
     194
     195                This is a helper method for
     196                :meth:`sage.rings.polynomial.polynomial_element.Polynomial.gcd`.
     197
     198            EXAMPLES::
     199
     200                sage: R.<x> = QQbar[]
     201                sage: QQbar._gcd_univariate_polynomial(2*x,2*x^2)
     202                x
     203
     204            """
     205            ret = EuclideanDomains().ElementMethods().gcd(f,g)
     206            c = ret.leading_coefficient()
     207            if c.is_unit():
     208                return (1/c)*ret
     209            return ret
     210
    184211        def _test_characteristic_fields(self, **options):
    185212            """
    186213            Run generic tests on the method :meth:`.characteristic`.
  • sage/rings/polynomial/polynomial_element.pyx

    diff --git a/sage/rings/polynomial/polynomial_element.pyx b/sage/rings/polynomial/polynomial_element.pyx
    a b cdef class Polynomial(CommutativeAlgebraElement): 
    31013101        return Factorization(F, unit)
    31023102
    31033103    @coerce_binop
     3104    def gcd(self, other):
     3105        """
     3106        Compute a greatest common divisor of ``self`` and ``other``.
     3107
     3108        INPUT:
     3109
     3110            - ``other`` -- a polynomial in the same ring as ``self``
     3111
     3112        OUTPUT:
     3113
     3114            A greatest common divisor of ``self`` and ``other`` as a polynomial
     3115            in the same ring as ``self``. Over a field, the return value will
     3116            be a monic polynomial.
     3117
     3118        .. NOTE::
     3119
     3120            The actual algorithm for computing greatest common divisors depends
     3121            on the base ring underlying the polynomial ring. If the base ring
     3122            defines a method ``_gcd_univariate_polynomial``, then this method
     3123            will be called (see examples below).
     3124
     3125        EXAMPLES::
     3126
     3127            sage: R.<x> = QQ[]
     3128            sage: (2*x^2).gcd(2*x)
     3129            x
     3130            sage: R.zero().gcd(0)
     3131            0
     3132            sage: (2*x).gcd(0)
     3133            x
     3134
     3135            One can easily add gcd functionality to new rings by providing a
     3136            method ``_gcd_univariate_polynomial``::
     3137
     3138            sage: R.<x> = QQ[]
     3139            sage: S.<y> = R[]
     3140            sage: h1 = y*x
     3141            sage: h2 = y^2*x^2
     3142            sage: h1.gcd(h2)
     3143            Traceback (most recent call last):
     3144            ...
     3145            NotImplementedError: Univariate Polynomial Ring in x over Rational Field does not provide a gcd implementation for univariate polynomials
     3146            sage: T.<x,y> = QQ[]
     3147            sage: R._gcd_univariate_polynomial = lambda f,g: S(T(f).gcd(g))
     3148            sage: h1.gcd(h2)
     3149            x*y
     3150            sage: del R._gcd_univariate_polynomial
     3151
     3152        """
     3153        if hasattr(self.base_ring(), '_gcd_univariate_polynomial'):
     3154            return self.base_ring()._gcd_univariate_polynomial(self, other)
     3155        else:
     3156            raise NotImplementedError("%s does not provide a gcd implementation for univariate polynomials"%self.base_ring())
     3157
     3158    @coerce_binop
    31043159    def lcm(self, other):
    31053160        """
    31063161        Let f and g be two polynomials. Then this function returns the
    cdef class Polynomial(CommutativeAlgebraElement): 
    56895744            sage: (2*x*y).is_squarefree() # R does not provide a gcd implementation
    56905745            Traceback (most recent call last):
    56915746            ...
    5692             AttributeError: 'sage.rings.polynomial.polynomial_element.Polynomial_generic_dense' object has no attribute 'gcd'
     5747            NotImplementedError: Univariate Polynomial Ring in y over Rational Field does not provide a gcd implementation for univariate polynomials
    56935748            sage: (2*x*y^2).is_squarefree()
    56945749            False
    56955750
  • sage/rings/polynomial/polynomial_element_generic.py

    diff --git a/sage/rings/polynomial/polynomial_element_generic.py b/sage/rings/polynomial/polynomial_element_generic.py
    a b class Polynomial_generic_field(Polynomial_singular_repr, 
    620620            R = R[:R.degree()] - (aaa*B[:B.degree()]).shift(diff_deg)
    621621        return (Q, R)
    622622
    623     @coerce_binop
    624     def gcd(self, other):
    625         """
    626         Return the greatest common divisor of ``self`` and ``other``, as a
    627         monic polynomial.
    628 
    629         INPUT:
    630 
    631             - ``other`` -- a polynomial defined over the same ring as
    632               ``self``
    633 
    634         EXAMPLES::
    635 
    636             sage: R.<x> = QQbar[]
    637             sage: (2*x).gcd(2*x^2)
    638             x
    639 
    640         """
    641         from sage.categories.euclidean_domains import EuclideanDomains
    642         g = EuclideanDomains().ElementMethods().gcd(self, other)
    643         c = g.leading_coefficient()
    644         if c.is_unit():
    645             return (1/c)*g
    646         return g
    647        
    648623       
    649624class Polynomial_generic_sparse_field(Polynomial_generic_sparse, Polynomial_generic_field):
    650625    """