本文整理汇总了Python中numpy.polynomial.polynomial.polyroots方法的典型用法代码示例。如果您正苦于以下问题:Python polynomial.polyroots方法的具体用法?Python polynomial.polyroots怎么用?Python polynomial.polyroots使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.polynomial.polynomial
的用法示例。
在下文中一共展示了polynomial.polyroots方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_polyroots
# 需要导入模块: from numpy.polynomial import polynomial [as 别名]
# 或者: from numpy.polynomial.polynomial import polyroots [as 别名]
def test_polyroots(self):
assert_almost_equal(poly.polyroots([1]), [])
assert_almost_equal(poly.polyroots([1, 2]), [-.5])
for i in range(2, 5):
tgt = np.linspace(-1, 1, i)
res = poly.polyroots(poly.polyfromroots(tgt))
assert_almost_equal(trim(res), trim(tgt))
示例2: test_polyroots
# 需要导入模块: from numpy.polynomial import polynomial [as 别名]
# 或者: from numpy.polynomial.polynomial import polyroots [as 别名]
def test_polyroots(self) :
assert_almost_equal(poly.polyroots([1]), [])
assert_almost_equal(poly.polyroots([1, 2]), [-.5])
for i in range(2, 5) :
tgt = np.linspace(-1, 1, i)
res = poly.polyroots(poly.polyfromroots(tgt))
assert_almost_equal(trim(res), trim(tgt))
示例3: exponential_decomposition
# 需要导入模块: from numpy.polynomial import polynomial [as 别名]
# 或者: from numpy.polynomial.polynomial import polyroots [as 别名]
def exponential_decomposition(X, F, m):
"""Use Prony's method to approximate the sampled real function F=f(X) as a sum of m
exponential functions x → Σ a_i exp(lamda_i x).
Parameters
----------
X: 1D array
sampling points.
F: 1D array (same size as X)
values of the function to approximate at the points of x.
m: integer
number of exponential functions
Return
------
a: 1D array (size m)
coefficients of the exponentials
lamda: 1D array (size m)
growth rate of the exponentials
"""
assert X.shape == F.shape
# Compute the coefficients of the polynomials of Prony's method
A = toeplitz(c=F[m-1:-1], r=F[:m][::-1])
P, *_ = np.linalg.lstsq(A, F[m:], rcond=None)
# Build and solve polynomial function
coeffs = np.ones(m+1)
# coeffs[:m] = -P[::-1]
for i in range(m):
coeffs[m-i-1] = -P[i]
roots = polynomial.polyroots(coeffs)
# Discard values where log is undefined
roots = roots[np.logical_or(np.imag(roots) != 0.0, np.real(roots) >= 0.0)]
# Deduce lamda and keep only interesting values
lamda = np.real(np.log(roots)/(X[1] - X[0]))
lamda = np.unique(lamda)
lamda = lamda[np.logical_and(-20.0 < lamda, lamda < 0.0)]
# Fit the values of 'a' on the curve
def f(x, *ar):
ar = np.asarray(ar)[:, np.newaxis]
la = lamda[:, np.newaxis]
return np.sum(ar * np.exp(la * x), axis=0)
a, *_ = curve_fit(f, X, F, p0=np.zeros(lamda.shape))
return a, lamda