Hi all,
Are there any python modules which contain functions for getting the roots of a quartic (degree 4) polynomial? I don't think NumPy has anything like it, and I really don't want to enter the equations in by hand.
Thanks.
Hi all,
Are there any python modules which contain functions for getting the roots of a quartic (degree 4) polynomial? I don't think NumPy has anything like it, and I really don't want to enter the equations in by hand.
Thanks.
Brief answer for : there are ready-made options. NumPy provides polynomial root routines (e.g. numpy.roots and the numpy.polynomial classes) that compute all roots numerically from a coefficient list. For symbolic or high-precision work, SymPy can return exact factors or high-precision numeric roots. 's Newton-method idea is valid for targeted single-root refinement, but companion-matrix/eigenvalue methods (what libraries typically use) are a simpler way to get all four roots at once.
A minimal NumPy workflow (coefficients highest-degree first):
import numpy as np
coeffs = [a4, a3, a2, a1, a0] # degree-4 down to constant
roots = np.roots(coeffs)
# filter near-real roots if needed
real_roots = [r.real for r in roots if abs(r.imag) < 1e-12] Notes: the leading coefficient must be nonzero; roots may be complex; scaling very large/small coefficients helps numerical stability; repeated or nearly repeated roots can be inaccurate with standard floating-point methods.
For exact algebraic answers or higher precision, SymPy's polynomial routines are useful:
from sympy import symbols, Poly
x = symbols('x')
p = Poly(a4*x**4 + a3*x**3 + a2*x**2 + a1*x + a0, x)
p.nroots() # numeric; or p.factor() for symbolic factoring Further reading on the closed-form quartic and the companion-matrix approach is available (quartic formula and companion matrix). Official docs: NumPy docs (https://numpy.org/doc/stable/) and SymPy docs (https://docs.sympy.org/latest/).
Couldn't find anything.
Is there a reason you wouldn't want to use Newton's Method to approximate a solution?
Jeff
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.