{"record":{"id":"fb094867662c0f5d","repo":"TheAlgorithms/Python","slug":"length-of-initial-roots-must-match-the-degree-of-t","errorCode":null,"errorMessage":"Length of initial roots must match the degree of the polynomial.","messagePattern":"Length of initial roots must match the degree of the polynomial\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/numerical_analysis/weierstrass_method.py","lineNumber":73,"sourceCode":"\n    if degree < 1:\n        raise ValueError(\"Degree of the polynomial must be at least 1.\")\n\n    if roots is None:\n        # Use perturbed complex roots of unity as initial guesses\n        rng = np.random.default_rng()\n        roots = np.array(\n            [\n                np.exp(2j * np.pi * i / degree) * (1 + 1e-3 * rng.random())\n                for i in range(degree)\n            ],\n            dtype=np.complex128,\n        )\n\n    else:\n        roots = np.asarray(roots, dtype=np.complex128)\n        if roots.shape[0] != degree:\n            raise ValueError(\n                \"Length of initial roots must match the degree of the polynomial.\"\n            )\n\n    for _ in range(max_iter):\n        # Construct the product denominator for each root\n        denominator = np.array([root - roots for root in roots], dtype=np.complex128)\n        np.fill_diagonal(denominator, 1.0)  # Avoid zero in diagonal\n        denominator = np.prod(denominator, axis=1)\n\n        # Evaluate polynomial at each root\n        numerator = polynomial(roots).astype(np.complex128)\n\n        # Compute update and clip to prevent overflow\n        delta = numerator / denominator\n        delta = np.clip(delta, -1e10, 1e10)\n        roots -= delta\n\n    return roots","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/numerical_analysis/weierstrass_method.py#L55-L91","documentation":"Error \"Length of initial roots must match the degree of the polynomial.\" thrown in TheAlgorithms/Python.","triggerScenarios":"Thrown at maths/numerical_analysis/weierstrass_method.py:73 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Provide exactly one initial root guess per polynomial degree."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}