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


Python Poly.sturm方法代码示例

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


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

示例1: number_of_real_roots

# 需要导入模块: from sympy.polys import Poly [as 别名]
# 或者: from sympy.polys.Poly import sturm [as 别名]
def number_of_real_roots(f, *gens, **args):
    """Returns the number of distinct real roots of `f` in `(inf, sup]`.

       Examples
       ========

           >>> from sympy import Poly
           >>> from sympy.abc import x, y

           >>> from sympy.polys.polyroots import number_of_real_roots

           >>> f = Poly(x**2 - 1, x)

           Count real roots in the (-oo, oo) interval:

           >>> number_of_real_roots(f)
           2

           Count real roots in the (0, 2) interval:

           >>> number_of_real_roots(f, inf=0, sup=2)
           1

           Count real roots in the (2, oo) interval:

           >>> number_of_real_roots(f, inf=2)
           0

       References
       ==========

       .. [Davenport88] J.H. Davenport, Y. Siret, E. Tournier, Computer
           Algebra Systems and Algorithms for Algebraic Computation,
           Academic Press, London, 1988, pp. 124-128
    """

    def sign_changes(seq):
        count = 0

        for i in xrange(1, len(seq)):
            if (seq[i-1] < 0 and seq[i] >= 0) or \
               (seq[i-1] > 0 and seq[i] <= 0):
                count += 1

        return count

    F = Poly(f, *gens, **args)

    if not F.is_Poly:
        return 0

    if F.is_multivariate:
        raise ValueError('multivariate polynomials not supported')

    if F.degree() < 1:
        return 0

    inf = args.get('inf', None)

    if inf is not None:
        inf = sympify(inf)

        if not inf.is_number:
            raise ValueError("Not a number: %s" % inf)
        elif abs(inf) is S.Infinity:
            inf = None

    sup = args.get('sup', None)

    if sup is not None:
        sup = sympify(sup)

        if not sup.is_number:
            raise ValueError("Not a number: %s" % sup)
        elif abs(sup) is S.Infinity:
            sup = None

    sturm = F.sturm()

    if inf is None:
        signs_inf = sign_changes([ s.LC()*(-1)**s.degree() for s in sturm ])
    else:
        signs_inf = sign_changes([ s.eval(inf) for s in sturm ])

    if sup is None:
        signs_sup = sign_changes([ s.LC() for s in sturm ])
    else:
        signs_sup = sign_changes([ s.eval(sup) for s in sturm ])

    return abs(signs_inf - signs_sup)
开发者ID:Aang,项目名称:sympy,代码行数:92,代码来源:polyroots.py


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