如何使 sympy 简化为零的部首表达式

编程入门 行业动态 更新时间:2024-10-26 23:39:50
本文介绍了如何使 sympy 简化为零的部首表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

多项式 x^3 - 3x + 1 的三个(实数)根相加为 0.但是 sympy 似乎不能简化这个根的总和:

>>>从 sympy 导入 *>>>从 sympy.abc 导入 x>>>rr = real_roots(x**3 -3*x + 1)>>>总和(rr)CRootOf(x**3 - 3*x + 1, 0) + CRootOf(x**3 - 3*x + 1, 1) + CRootOf(x**3 - 3*x + 1, 2)

函数simplify 和radsimp 不能简化这个表达式.然而,最小多项式计算正确:

>>>最小多项式(总和(rr))_X

由此我们可以得出结论,总和为0.有没有一种直接的方法可以让 sympy 简化这个根的总和?

解决方案

如果可能,以下函数计算等于代数项的有理数:

import sympy as sp# 尝试将代数项简化为有理数def try_simplify_to_rational(expr):尝试:float(expr) # 表达式计算结果为实数吗?minPoly = sp.poly(sp.minimal_polynomial(expr))print('最小多项式:', minPoly)如果 len(minPoly.monoms()) == 1: # minPoly == x返回 0如果 minPoly.degree() == 1: # minPoly == a*x + ba,b = minPoly.coeffs()返回 sp.Rational(-b, a)除了类型错误:pass # 表达式不计算为实数除了 sp.polys.polyerrors.NotAlgebraic:pass # 表达式不计算为代数数除了例外作为 exc:打印(意外异常:",str(exc))print('化简为有理数不成功')return expr # 化简不成功

查看工作示例:

x = sp.symbols('x')rr = sp.real_roots(x**3 - 3*x + 1)# 根之和等于 (-1)* x^2 的系数,这里为 0打印(sp.simplify(sum(rr)))打印(try_simplify_to_rational(sum(rr)))# ->0

在sympy issue #19726.

The three (real) roots of the polynomial x^3 - 3x + 1 sum up to 0. But sympy does not seem to be able to simplify this sum of roots:

>>> from sympy import * >>> from sympy.abc import x >>> rr = real_roots(x**3 -3*x + 1) >>> sum(rr) CRootOf(x**3 - 3*x + 1, 0) + CRootOf(x**3 - 3*x + 1, 1) + CRootOf(x**3 - 3*x + 1, 2)

The functions simplify and radsimp cannot simplify this expression. The minimal polynomial, however, is computed correctly:

>>> minimal_polymial(sum(rr)) _x

From this we can conclude that the sum is 0. Is there a direct way of making sympy simplify this sum of roots?

解决方案

The following function computes the rational number equal to an algebraic term if possible:

import sympy as sp # try to simplify an algebraic term to a rational number def try_simplify_to_rational(expr): try: float(expr) # does the expression evaluate to a real number? minPoly = sp.poly(sp.minimal_polynomial(expr)) print('minimal polynomial:', minPoly) if len(minPoly.monoms()) == 1: # minPoly == x return 0 if minPoly.degree() == 1: # minPoly == a*x + b a,b = minPoly.coeffs() return sp.Rational(-b, a) except TypeError: pass # expression does not evaluate to a real number except sp.polys.polyerrors.NotAlgebraic: pass # expression does not evaluate to an algebraic number except Exception as exc: print("unexpected exception:", str(exc)) print('simplification to rational number not successful') return expr # simplification not successful

See the working example:

x = sp.symbols('x') rr = sp.real_roots(x**3 - 3*x + 1) # sum of roots equals (-1)*coefficient of x^2, here 0 print(sp.simplify(sum(rr))) print(try_simplify_to_rational(sum(rr))) # -> 0

A more elaborate function computing also simple radical expressions is proposed in sympy issue #19726.

更多推荐

如何使 sympy 简化为零的部首表达式

本文发布于:2023-06-05 01:23:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/508972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部首   表达式   为零   sympy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!