TheAlgorithms/Python · error · ValueError
Length of initial roots must match the degree of the polynom
Error message
Length of initial roots must match the degree of the polynomial.
What it means
Error "Length of initial roots must match the degree of the polynomial." thrown in TheAlgorithms/Python.
Source
Thrown at maths/numerical_analysis/weierstrass_method.py:73
if degree < 1:
raise ValueError("Degree of the polynomial must be at least 1.")
if roots is None:
# Use perturbed complex roots of unity as initial guesses
rng = np.random.default_rng()
roots = np.array(
[
np.exp(2j * np.pi * i / degree) * (1 + 1e-3 * rng.random())
for i in range(degree)
],
dtype=np.complex128,
)
else:
roots = np.asarray(roots, dtype=np.complex128)
if roots.shape[0] != degree:
raise ValueError(
"Length of initial roots must match the degree of the polynomial."
)
for _ in range(max_iter):
# Construct the product denominator for each root
denominator = np.array([root - roots for root in roots], dtype=np.complex128)
np.fill_diagonal(denominator, 1.0) # Avoid zero in diagonal
denominator = np.prod(denominator, axis=1)
# Evaluate polynomial at each root
numerator = polynomial(roots).astype(np.complex128)
# Compute update and clip to prevent overflow
delta = numerator / denominator
delta = np.clip(delta, -1e10, 1e10)
roots -= delta
return rootsView on GitHub (pinned to f5988cc097)
Solutions
- Provide exactly one initial root guess per polynomial degree.
When it happens
Trigger: Thrown at maths/numerical_analysis/weierstrass_method.py:73 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/fb094867662c0f5d.
Report an issue: GitHub.