忽略sympy中的假想根 [英] Ignore imaginary roots in sympy

查看:49
本文介绍了忽略sympy中的假想根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sympy求解多项式:

I'm using sympy to solve a polynomial:

x = Symbol('x')
y = solve(int(row["scaleA"])*x**3 + int(row["scaleB"])*x**2 + int(row["scaleC"])*x + int(row["scaleD"]), x)

y是可能的解决方案的列表.但是,我需要忽略那些虚构的问题,而只使用实际的解决方案.另外,我希望解决方案是值而不是表达式.现在看起来像:

y is a list of possible solutions. However, I need to ignore the imaginary ones and only use the real solutions. Also, I would like the solution as a value not an expression. Right now it looks like:

[-2/3 - 55**(1/3)*(-1/2 - sqrt(3)*I/2)/3, -2/3 - 55**(1/3)*(-1/2 + sqrt(3)*I/2)/3, -55**(1/3)/3 - 2/3]

我需要最后一个表达式的值(-2.22756).sympy中是否有功能可以简化此过程?

I need the last expression's value (-2.22756). Are there functions in sympy to simplify this?

推荐答案

正如Krastonov提到的mmpath提供了一种更简单的方法:

As Krastonov had mentioned mpmath provided an easier method:

y = polyroots([int(row["scaleA"]), int(row["scaleB"]), int(row["scaleC"]), int(row["scaleD"])-value])
for root in y:
   if "j" not in str(root):
       value = root

这篇关于忽略sympy中的假想根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆