Durand–Kerner复根迭代实战:每个根都像在做牛顿修正,为什么分母却要乘上其他候选根的距离
Durand–Kerner同时维护与多项式次数一样多的复数候选。每个候选用当前多项式值除以它到其他候选的差的乘积,再减去这份修正。分母不是原多项式导数的直接求值,却会在其他候选靠近真实根时逼近相应导数结构。本文实现有限次数的教学版本,并明确报告未收敛。

先把多项式变成首一形式
输入按最高次到常数项排列,先用首项系数除遍所有系数。之后分母只需候选差积,不再额外乘首项。若漏掉归一化,把同一多项式整体乘七就会改变修正尺度;测试对每份已知根样本都再乘七求解,要求得到同一组根。
差积来自因子化的局部结构
首一多项式可写成各个z减真实根的乘积。在第k个真实单根处,导数等于它与其他真实根之差的乘积。迭代用当前候选替代这些未知根,从而构造一个近似的分母。它利用所有候选之间的相对位置,不是给每个初始点各自运行互不联系的普通牛顿法。
本实现一轮只读旧候选快照
内层先在旧z列表上计算所有修正,把新值放进fresh,整轮结束才替换。这样每个分母使用相同时间层的候选。当前读取的mpmath实现采用逐项原地更新的变体,并带额外精度和输出清理;本文选择冻结快照的教学形式,不能把两份程序的迭代次数直接当作相同算法轨迹比较。
不同种子避免第一步直接除零
默认初值放在带相位偏移的复数圆周上,半径根据归一化系数给出简单尺度。手工初值必须数量正确、有限且互不相同;两个完全重合的候选会让差积为零。初值合法仍不保证收敛,坏尺度或后续碰撞都可能导致明确的ArithmeticError。
停止同时查看步子与残差
程序要求最大修正相对候选尺度足够小,并检查按系数规模与候选幅度缩放的残差。两者都通过才返回。即便如此,浮点残差很小也不是所有根位置都准确的普遍证明,尤其在重根与病态多项式附近,系数扰动可能对应很大的根位移。
已知三实根样本给出直观锚点
对三次多项式x立方减x平方减十四倍x加二十四,独立已知根为负四、二、三。程序保持内部复数,只在打印这份已核验样本时显示九位小数的实部,没有在迭代中强行删掉虚部。其他样本可以真正包含非实根,不能因为输入系数近似实数就把候选都限制在实轴。
全排列配对防止重复根蒙混过关
十五组测试从已知的平移缩放单位根构造二到六次多项式。独立裁判在所有根排列中寻找最大距离最小的配对,要求每个预期根都被一个候选覆盖。只检查每个候选的残差,可能让多个候选都挤在同一个真根附近而漏掉另一个,因此身份覆盖需要另验。
再由输出根重建全部系数
裁判把候选逐个作为线性因子相乘,重建首一多项式系数,再与输入比较。这条路径不用被测Horner残差,也不依赖候选排序,能补充检查整组根是否共同描述原问题。已知根配对与系数重建相互配合,比只打印一串看起来正确的小数更可靠。
超时和碰撞属于结果的一部分
测试把迭代预算设为一,要求三次样本报告预算耗尽,而不是返回初步近似并标作成功。首项为零、重复初值及布尔预算也被拒绝。教学入口限制一次到八次多项式和归一化系数幅度,不声称具备任意次数、任意动态范围的通用数值鲁棒性。
重根与高精度需要进一步工具
简单根附近的良好局部性质不等于全局收敛保证。遇到重根、很靠近的根或尺度差异巨大时,应重新检查工作精度、初值和独立误差证据。本文不安装mpmath,也不把标准库复数输出冒称其高精度结果;官方文档和源码只用于核对方法及数值边界。
完整程序与本次实际输出
保存为demo.py,执行python3 demo.py。本次使用Linux、CPython 3.12.14与标准库,退出码为0,标准错误为空。程序只处理内存教学样本,不访问网络或业务数据;不要使用会移除测试断言的优化启动选项。
import cmath
import math
from itertools import permutations
def evaluate(coefficients, z):
value = 0j
for coefficient in coefficients:
value = value*z + coefficient
return value
def roots(coefficients, initial=None, tolerance=1e-12, limit=200):
c = list(coefficients)
if not 2 <= len(c) <= 9 or any(type(x) not in (int, float, complex)
or not (math.isfinite(complex(x).real) and math.isfinite(complex(x).imag))
for x in c) or c[0] == 0:
raise ValueError('finite degree-one-to-eight polynomial required')
if type(limit) is not int or not 1 <= limit <= 1000:
raise ValueError('bounded positive iteration limit required')
if type(tolerance) not in (int, float) or not math.isfinite(tolerance) or tolerance <= 0:
raise ValueError('finite positive tolerance required')
c = [complex(x)/c[0] for x in c]
if any(not (math.isfinite(x.real) and math.isfinite(x.imag)) or abs(x) > 1e6 for x in c):
raise ValueError('normalized coefficient budget exceeded')
n = len(c) - 1
radius = 1 + max(abs(x) for x in c[1:])
z = ([radius*cmath.exp(2j*math.pi*(k+0.37)/n) for k in range(n)]
if initial is None else list(map(complex, initial)))
if len(z) != n or len(set(z)) != n or any(
not (math.isfinite(x.real) and math.isfinite(x.imag)) for x in z):
raise ValueError('distinct finite initial candidates required')
for iteration in range(1, limit + 1):
changes, fresh = [], []
for k, value in enumerate(z):
denominator = 1+0j
for j, other in enumerate(z):
if j != k:
denominator *= value - other
if denominator == 0:
raise ArithmeticError('candidate collision')
change = evaluate(c, value) / denominator
fresh.append(value - change)
changes.append(abs(change))
if any(not (math.isfinite(x.real) and math.isfinite(x.imag)) for x in fresh):
raise ArithmeticError('nonfinite iterate')
z = fresh # All corrections used the previous complete snapshot.
scale = max(1., max(map(abs, z)))
residual = max(abs(evaluate(c, x)) for x in z)
if max(changes) <= tolerance*scale and residual <= tolerance*sum(map(abs, c))*scale**n:
return z, iteration
raise ArithmeticError('iteration budget exhausted')
def coefficients_from_roots(values):
c = [1+0j]
for root in values:
out = [0j]*(len(c)+1)
for i, value in enumerate(c):
out[i] += value
out[i+1] -= value*root
c = out
return c
def matching_error(actual, expected):
return min(max(abs(a-b) for a,b in zip(actual, order))
for order in permutations(expected))
r, iterations = roots([1, -1, -14, 24])
assert matching_error(r, [-4, 2, 3]) < 1e-9
print('three known real roots:', sorted(round(x.real, 9) for x in r))
cases = 0
for n in range(2, 7):
for shift, radius in [(0j, 1.), (0.2+0.1j, .7), (-.3j, 1.3)]:
expected = [shift+radius*cmath.exp(2j*math.pi*k/n) for k in range(n)]
c = coefficients_from_roots(expected)
actual, _ = roots(c)
assert matching_error(actual, expected) < 1e-8
rebuilt = coefficients_from_roots(actual)
assert max(abs(a-b) for a,b in zip(rebuilt, c)) < 1e-8
scaled, _ = roots([7*x for x in c])
assert matching_error(scaled, expected) < 1e-8
cases += 1
for action in [lambda: roots([1, 0, 1], initial=[1, 1]),
lambda: roots([0, 1]), lambda: roots([1, 0, -2], limit=True),
lambda: roots([1, -1, -14, 24], limit=1)]:
try:
action()
except (ValueError, ArithmeticError):
pass
else:
raise AssertionError('invalid or unconverged request accepted')
print('known-root matching, coefficient rebuild and leading-scale cases:', cases)
print('duplicate seeds, leading zero and explicit nonconvergence: PASS')以下为上述完整程序本次实际标准输出,与源码分开列出。
three known real roots: [-4.0, 2.0, 3.0] known-root matching, coefficient rebuild and leading-scale cases: 15 duplicate seeds, leading zero and explicit nonconvergence: PASS
来源与复现范围
资料核对与本地运行日期:2026年10月8日。定义与格式对照上述一手资料;本文讲解、代码与教学测试独立编写。有限测试不代表完整协议、任意输入保证或生产性能评测。

