📜  最小化给定多项式的根的总和(1)

📅  最后修改于: 2023-12-03 15:26:27.301000             🧑  作者: Mango

最小化给定多项式的根的总和

在数学和工程学中,多项式根问题是一个经典的问题。给定一个n次多项式,找到其所有的n个根是很困难的。为了寻找最小化给定多项式的根的总和而努力,我们可以使用二分法。

算法流程
  1. 首先确定多项式方程的初始值,例如采用二分法,我们可以将所有确信的最小值和最大值传递给解析器。
  2. 对于每个方程根,我们可以使用Newton动态迭代算法来找到根的估计值。Newton动态迭代算法可以通过依次执行重复的迭代来逐渐改进方程的根的估计值。方程的根可以通过简单地将初始值传递给Newton动态迭代算法来确定。
  3. 我们可以采用最优化算法,例如牛顿法或梯度下降法,来计算当前解(即多项式根)的梯度,然后将其与初始值相乘。这将创建一个新的多项式,我们将其暂存为当前的损失函数。
  4. 我们可以通过再次使用二分法来最小化损失函数,从而找到最小化给定多项式的根的总和的解。一旦我们找到了这个最小值,我们就可以确定每个多项式根的最优解。
代码实现
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动态迭代算法和最优化算法。这种方法可以帮助工程师在更短的时间内找到多项式的根。