def char_coefficients(poles):
"""
Calculate the coefficients of a characteristic polynomial.
Args:
poles (list or :obj:`numpy.ndarray`): pol configuration
Return:
:obj:`numpy.ndarray`: coefficients
"""
poles = np.array(poles) # convert to numpy array
poles = np.ravel(poles) # transform to 1d array
s = sp.symbols("s")
poly = 1
for s_i in poles:
poly = (s - s_i) * poly
poly = sp.expand(poly)
# calculate the coefficient of characteristic polynomial
n = len(poles)
p = []
for i in range(n):
p.append(poly.subs([(s, 0)]))
poly = poly - p[i]
poly = poly / s
poly = sp.expand(poly)
# convert numbers and complex objects from multiplication to a complex
# number
p = [complex(x) for x in p]
# if imaginary part is greater than the boundary, set imaginary part to zero
boundary = 1e-12
for idx, val in enumerate(p):
if abs(val.imag) > boundary:
msg = "Imaginary Part of the coefficient p[{}] "
"is unequal zero ({})) for a given tolerance of {}".format(
str(idx), str(boundary), str(val.imag))
warnings.warn(msg)
p[idx] = val.real
return np.array(p, dtype=float) # [a_0, a_1, ... , a_n-1]
评论列表
文章目录