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


Python Poly.to_field方法代码示例

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


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

示例1: roots

# 需要导入模块: from sympy.polys.polytools import Poly [as 别名]
# 或者: from sympy.polys.polytools.Poly import to_field [as 别名]

#.........这里部分代码省略.........

        for i in [-1, 1]:
            if not f.eval(i):
                f = f.quo(Poly(f.gen - i, f.gen))
                result.append(i)
                break

        n = f.degree()

        if n == 1:
            result += map(cancel, roots_linear(f))
        elif n == 2:
            result += map(cancel, roots_quadratic(f))
        elif f.is_cyclotomic:
            result += roots_cyclotomic(f)
        elif n == 3 and cubics:
            result += roots_cubic(f)
        elif n == 4 and quartics:
            result += roots_quartic(f)
        elif n == 5 and quintics:
            result += roots_quintic(f)

        return result

    (k,), f = f.terms_gcd()

    if not k:
        zeros = {}
    else:
        zeros = {S(0): k}

    coeff, f = preprocess_roots(f)

    if auto and f.get_domain().has_Ring:
        f = f.to_field()

    result = {}

    if not f.is_ground:
        if not f.get_domain().is_Exact:
            for r in f.nroots():
                _update_dict(result, r, 1)
        elif f.degree() == 1:
            result[roots_linear(f)[0]] = 1
        elif f.degree() == 2:
            for r in roots_quadratic(f):
                _update_dict(result, r, 1)
        elif f.length() == 2:
            for r in roots_binomial(f):
                _update_dict(result, r, 1)
        else:
            _, factors = Poly(f.as_expr()).factor_list()

            if len(factors) == 1 and factors[0][1] == 1:
                for root in _try_decompose(f):
                    _update_dict(result, root, 1)
            else:
                for factor, k in factors:
                    for r in _try_heuristics(Poly(factor, f.gen, field=True)):
                        _update_dict(result, r, k)

    if coeff is not S.One:
        _result, result, = result, {}

        for root, k in _result.iteritems():
            result[coeff*root] = k

    result.update(zeros)

    if filter not in [None, 'C']:
        handlers = {
            'Z': lambda r: r.is_Integer,
            'Q': lambda r: r.is_Rational,
            'R': lambda r: r.is_real,
            'I': lambda r: r.is_imaginary,
        }

        try:
            query = handlers[filter]
        except KeyError:
            raise ValueError("Invalid filter: %s" % filter)

        for zero in dict(result).iterkeys():
            if not query(zero):
                del result[zero]

    if predicate is not None:
        for zero in dict(result).iterkeys():
            if not predicate(zero):
                del result[zero]

    if not multiple:
        return result
    else:
        zeros = []

        for zero, k in result.iteritems():
            zeros.extend([zero]*k)

        return sorted(zeros, key=default_sort_key)
开发者ID:yuriy-demidov,项目名称:sympy,代码行数:104,代码来源:polyroots.py

示例2: roots

# 需要导入模块: from sympy.polys.polytools import Poly [as 别名]
# 或者: from sympy.polys.polytools.Poly import to_field [as 别名]

#.........这里部分代码省略.........
        for i in [-1, 1]:
            if not f.eval(i):
                f = f.quo(Poly(f.gen - i, f.gen))
                result.append(i)
                break

        n = f.degree()

        if n == 1:
            result += list(map(cancel, roots_linear(f)))
        elif n == 2:
            result += list(map(cancel, roots_quadratic(f)))
        elif f.is_cyclotomic:
            result += roots_cyclotomic(f)
        elif n == 3 and cubics:
            result += roots_cubic(f, trig=trig)
        elif n == 4 and quartics:
            result += roots_quartic(f)
        elif n == 5 and quintics:
            result += roots_quintic(f)

        return result

    (k,), f = f.terms_gcd()

    if not k:
        zeros = {}
    else:
        zeros = {S(0): k}

    coeff, f = preprocess_roots(f)

    if auto and f.get_domain().is_Ring:
        f = f.to_field()

    rescale_x = None
    translate_x = None

    result = {}

    if not f.is_ground:
        dom = f.get_domain()
        if not dom.is_Exact and dom.is_Numerical:
            for r in f.nroots():
                _update_dict(result, r, 1)
        elif f.degree() == 1:
            result[roots_linear(f)[0]] = 1
        elif f.length() == 2:
            roots_fun = roots_quadratic if f.degree() == 2 else roots_binomial
            for r in roots_fun(f):
                _update_dict(result, r, 1)
        else:
            _, factors = Poly(f.as_expr()).factor_list()
            if len(factors) == 1 and f.degree() == 2:
                for r in roots_quadratic(f):
                    _update_dict(result, r, 1)
            else:
                if len(factors) == 1 and factors[0][1] == 1:
                    if f.get_domain().is_EX:
                        res = to_rational_coeffs(f)
                        if res:
                            if res[0] is None:
                                translate_x, f = res[2:]
                            else:
                                rescale_x, f = res[1], res[-1]
                            result = roots(f)
开发者ID:bjodah,项目名称:sympy,代码行数:70,代码来源:polyroots.py

示例3: roots

# 需要导入模块: from sympy.polys.polytools import Poly [as 别名]
# 或者: from sympy.polys.polytools.Poly import to_field [as 别名]

#.........这里部分代码省略.........
    tmp_ = f
    (k,), f = f.terms_gcd()
    if (tmp_ != f):
        add_comment("Rewrite the equation as")
        add_eq(Mul(f.gen**k, f.as_expr(), evaluate=False), 0)
    if not k:
        zeros = {}
    else:
        zeros = {S(0): k}
        if not f.is_ground:
            add_comment("Solve the equation")
            add_eq(f.as_expr(), 0)
        else:
            add_comment("The roots are")
            for z in zeros:
                add_eq(f.gen, 0)
            return {S(0): k}

    coeff, fp = preprocess_roots(f)
    if coeff.is_Rational or f.degree() <= 2:
        coeff = S.One
    else:
        f = fp

    if (coeff is not S.One):
        add_comment("Use the substitution")
        t = Dummy("t")
        add_eq(f.gen, t*coeff)
        add_comment("We have")
        f = Poly(f.as_expr().subs(f.gen, t), t)
        add_eq(f.as_expr(), 0)

    if auto and f.get_domain().has_Ring:
        f = f.to_field()

    rescale_x = None
    translate_x = None

    result = {}
    print_all_roots = False
    if not f.is_ground:
        if not f.get_domain().is_Exact:
            add_comment("Use numerical methods")
            for r in f.nroots():
                add_eq(f.gen, r)
                _update_dict(result, r, 1)
        elif f.degree() == 1:
            tmp = roots_linear(f)[0]
            result[tmp] = 1
        elif f.degree() == 2:
            for r in roots_quadratic(f):
                _update_dict(result, r, 1)
        elif f.length() == 2:
            for r in roots_binomial(f):
                _update_dict(result, r, 1)
        else:
            rr = find_rational_roots(f)
            if len(rr) > 0:
                print_all_roots = True
                g = f
                h = 1
                for r in rr:
                    while g(r) == 0:
                        g = g.quo(Poly(f.gen - r, f.gen))
                        h = Mul(h, f.gen - r, evaluate=False)
                        _update_dict(result, r, 1)
开发者ID:hrashk,项目名称:sympy,代码行数:70,代码来源:polyroots.py


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