def realpolyroots(*cs):
"""returns the roots of a polynom with given coefficients
polynomial with coefficients given in cs:
0 = \sum_i cs[i] * x^(len(cs)-i-1)
"""
if not cs:
return [0]
try:
f = 1.0/cs[0]
cs = [f*c for c in cs[1:]]
except ArithmeticError:
return realpolyroots(*cs[1:])
else:
n = len(cs)
if n == 0:
return []
elif n == 1:
return [-cs[0]]
elif n == 2:
return _realroots_quadratic(*cs)
elif n == 3:
return _realroots_cubic(*cs)
elif n == 4:
return _realroots_quartic(*cs)
else:
raise RuntimeError("realpolyroots solver currently limited to polynoms up to the power of 4")
# def realpolyroots_eigenvalue(*cs):
# # as realpolyroots but using an equivalent eigenvalue problem
# # (this code is currently used for functional tests only)
# if not _has_numeric:
# raise RuntimeError("realpolyroots_eigenvalue depends on Numeric")
# if not cs:
# return [0]
# try:
# f = 1.0/cs[0]
# cs = [f*c for c in cs[1:]]
# except ArithmeticError:
# return realpolyroots_eigenvalue(*cs[1:])
# else:
# if not cs:
# return []
# n = len(cs)
# a = Numeric.zeros((n, n), Numeric.Float)
# for i in range(n-1):
# a[i+1][i] = 1
# for i in range(n):
# a[0][i] = -cs[i]
# rs = []
# for r in LinearAlgebra.eigenvalues(a):
# if type(r) == types.ComplexType:
# if not r.imag:
# rs.append(r.real)
# else:
# rs.append(r)
# return rs
#
评论列表
文章目录