当前位置: 首页>>代码示例>>Python>>正文


Python Polynomial.gcd方法代码示例

本文整理汇总了Python中sage.rings.polynomial.polynomial_element.Polynomial.gcd方法的典型用法代码示例。如果您正苦于以下问题:Python Polynomial.gcd方法的具体用法?Python Polynomial.gcd怎么用?Python Polynomial.gcd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sage.rings.polynomial.polynomial_element.Polynomial的用法示例。


在下文中一共展示了Polynomial.gcd方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: gcd

# 需要导入模块: from sage.rings.polynomial.polynomial_element import Polynomial [as 别名]
# 或者: from sage.rings.polynomial.polynomial_element.Polynomial import gcd [as 别名]
    def gcd(self,other,algorithm=None):
        """
        Return the gcd of this polynomial and ``other``

        INPUT:

        - ``other`` -- a polynomial defined over the same ring as this
          polynomial.

        ALGORITHM:

        Two algorithms are provided:

        - ``generic``: Uses the generic implementation, which depends on the
          base ring being a UFD or a field.
        - ``dense``: The polynomials are converted to the dense representation,
          their gcd is computed and is converted back to the sparse
          representation.

        Default is ``dense`` for polynomials over ZZ and ``generic`` in the
        other cases.

        EXAMPLES::

            sage: R.<x> = PolynomialRing(ZZ,sparse=True)
            sage: p = x^6 + 7*x^5 + 8*x^4 + 6*x^3 + 2*x^2 + x + 2
            sage: q = 2*x^4 - x^3 - 2*x^2 - 4*x - 1
            sage: gcd(p,q)
            x^2 + x + 1
            sage: gcd(p, q, algorithm = "dense")
            x^2 + x + 1
            sage: gcd(p, q, algorithm = "generic")
            x^2 + x + 1
            sage: gcd(p, q, algorithm = "foobar")
            Traceback (most recent call last):
            ...
            ValueError: Unknown algorithm 'foobar'

        TESTS:

        Check that :trac:`19676` is fixed::

            sage: S.<y> = R[]
            sage: x.gcd(y)
            1
            sage: (6*x).gcd(9)
            3
        """

        from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
        from sage.arith.all import lcm

        if algorithm is None:
            if self.base_ring() == ZZ:
                algorithm = "dense"
            else:
                algorithm = "generic"
        if algorithm=="dense":
            S = self.parent()
            # FLINT is faster but a bug makes the conversion extremely slow,
            # so NTL is used in those cases where the conversion is too slow. Cf
            # <https://groups.google.com/d/msg/sage-devel/6qhW90dgd1k/Hoq3N7fWe4QJ>
            sd = self.degree()
            od = other.degree()
            if max(sd,od)<100 or \
               min(len(self.__coeffs)/sd, len(other.__coeffs)/od)>.06:
                implementation="FLINT"
            else:
                implementation="NTL"
            D = PolynomialRing(S.base_ring(),'x',implementation=implementation)
            g = D(self).gcd(D(other))
            return S(g)
        elif algorithm=="generic":
            return Polynomial.gcd(self,other)
        else:
            raise ValueError("Unknown algorithm '%s'" % algorithm)
开发者ID:anuragwaliya,项目名称:sage,代码行数:78,代码来源:polynomial_element_generic.py


注:本文中的sage.rings.polynomial.polynomial_element.Polynomial.gcd方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。