当前位置: 首页>>代码示例>>Python>>正文


Python polynomial.polyroots方法代码示例

本文整理汇总了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)) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:9,代码来源:test_polynomial.py

示例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)) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:9,代码来源:test_polynomial.py

示例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 
开发者ID:mancellin,项目名称:capytaine,代码行数:51,代码来源:prony_decomposition.py


注:本文中的numpy.polynomial.polynomial.polyroots方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。