📅  最后修改于: 2023-12-03 15:26:27.301000             🧑  作者: Mango
在数学和工程学中,多项式根问题是一个经典的问题。给定一个n次多项式,找到其所有的n个根是很困难的。为了寻找最小化给定多项式的根的总和而努力,我们可以使用二分法。
def minimize_poly_roots(poly, lower_bound, upper_bound):
eps = 1e-16
def newton(poly, x):
return x - poly(x) / poly.deriv()(x)
def root_estimate(poly, lb, ub, eps):
static_window_size = 5000
max_iterations = 10000
prec = abs(eps * (1 if eps > 0 else max_iterations))
if poly(lb) > 0:
poly, lb, ub = poly.reverse(), -ub, -lb
if poly.deriv().range_contains_zero(lb, ub):
return None
if tmp0 := poly(lb):
return lb
if tmp1 := poly(ub):
return ub
iters = 0
while lb < ub:
iters += 1
if iters % static_window_size == 0:
mid = (lb + ub) / 2
if abs(poly(mid)) < eps:
return mid
mid = newton(poly, (lb + ub) / 2)
if lb < mid < ub and abs(poly(mid)) < prec:
return mid
if tmp := poly(mid):
if tmp0 * tmp < 0:
ub = mid
tmp1 = tmp
elif tmp1 * tmp < 0:
lb = mid
tmp0 = tmp
else:
if abs(tmp0) < abs(tmp1):
tmp1 = tmp
ub = mid
else:
tmp0 = tmp
lb = mid
else:
return mid
return ub if abs(poly(ub)) < abs(poly(lb)) else lb
roots = []
while True:
root = root_estimate(poly, lower_bound, upper_bound, eps)
if root is None:
break
roots.append(root)
lower_bound = root + eps
return roots
最小化给定多项式的根的总和是一个有趣和复杂的问题。我们可以采用一种高效的算法来解决这个问题。本文介绍了解决这个问题的方法,包括采用二分法、Newton动态迭代算法和最优化算法。这种方法可以帮助工程师在更短的时间内找到多项式的根。