【发布时间】:2015-10-28 22:02:57
【问题描述】:
我想用 Python 求解 6 阶多项式方程。 我试过“基本”版本:
avgIrms = 19.61
c_val = (0.000002324*avgIrms**6) - (0.0001527*avgIrms**5) + (0.003961843*avgIrms**4) - (0.052211292*avgIrms**3) + (0.379269091*avgIrms**2) -(0.404399274*avgIrms) + 0.000682896
print(c_val)
之后,我使用了带有以下代码的 numpy:
import numpy as np
avgIrms = 19.61
ppar = [0.000002324, -0.0001527, 0.003961843, -0.052211292, 0.379269091, -0.404399274, 0.000682896]
p = np.poly1d(ppar)
print(p(avgIrms))
在这两种方式中,覆盆子的处理时间都超过了 5 秒……太长了!对有效求解多项式方程有什么帮助吗? (不到一秒……)
提前致谢, 丹尼尔
【问题讨论】:
-
您的意思是“解决”多项式还是在特定参数上对其进行评估? “求解”多项式通常意味着找到根。我无法想象你会在使用 python 时胜过 numpy,但如果你想评估多项式,请在 Wikipedia 中查找“Horner 方法”。
-
当您说 « [...] 耗时超过五秒 [...] »,您是如何检查的?
-
我用这个解决了我的问题: def horner(coeffs, x): return reduce(lambda acc, c: acc * x + c, reversed(coeffs), 0) def poli(value): x = horner((0.000682896, -0.404399274, 0.379269091, -0.052211292, 0.003961843, -0.0001527, 0.000002324), 值) 返回 x
-
那你会把这个作为答案发布吗?与您的“基本”评估器相比,我仍然看不到这如何加快速度。在我的机器上
horner运行速度较慢。 -
六阶多项式可能导致过拟合。多项式将有六个根值。你可以使用 PolynomialFeatures(degree=6)
标签: python numpy polynomial-math