def test_maijerg_ex_frompade(): """Test a Meijer-G approximant of a Meijer-G function.""" # e^x = 1 + x + 1/2 x^2 + 1/6 x^3 + 1/24 x^4 + ... tcoeffs = np.array([ 1, 1, 1. / 2., 1. / 6., 1. / 24. + 1. / 120., 1 / factorial(6.), 1 / factorial(7.) ]) p, q = misc.pade(tcoeffs, 3) points = np.array([0.8, 0.5, 0.2, -0.5, -0.8, -1.0]) y = np.exp(points) basic_pade = BasicPadeApproximant(points, y, tcoeffs, 3) tcoeffs_pade = taylor_coeffs(basic_pade(np.array([0.]), range(4)), 3) borel = borel_trans_coeffs(tcoeffs_pade) ratios = consecutive_ratios_odd(borel) ps, qs = rational_function_for_ratios(ratios) # Add the constant 1. to the qm coefficients ps = ps[::-1] qs = qs[::-1] qs = np.append(qs, np.array(1.)) rootsp = find_roots(ps) rootsq = find_roots(qs) xvector = np.append(np.array([1]), rootsp) yvector = rootsq pl = ps[0] ql = qs[0] #print tcoeffs_pade zs = meijerg_approx_low(xvector, yvector, pl, ql, points) #print "from pade ", p(points)/q(points) #print "result 0", y #print "result 1 ", zs assert (abs(y - np.real(zs)) < 1e-2).all()
def _pade_delay(p, q, c): """Numerically evaluated state-space using Pade approximants. This may have numerical issues for large values of p or q. """ i = np.arange(1, p+q+1) taylor = np.append([1.0], (-c)**i / factorial(i)) num, den = pade(taylor, q) return LinearFilter(num, den)
def test_pade_4term_exp(): # First four Taylor coefficients of exp(x). # Unlike poly1d, the first array element is the zero-order term. an = [1.0, 1.0, 0.5, 1.0 / 6] nump, denomp = pade(an, 0) assert_array_almost_equal(nump.c, [1.0 / 6, 0.5, 1.0, 1.0]) assert_array_almost_equal(denomp.c, [1.0]) nump, denomp = pade(an, 1) assert_array_almost_equal(nump.c, [1.0 / 6, 2.0 / 3, 1.0]) assert_array_almost_equal(denomp.c, [-1.0 / 3, 1.0]) nump, denomp = pade(an, 2) assert_array_almost_equal(nump.c, [1.0 / 3, 1.0]) assert_array_almost_equal(denomp.c, [1.0 / 6, -2.0 / 3, 1.0]) nump, denomp = pade(an, 3) assert_array_almost_equal(nump.c, [1.0]) assert_array_almost_equal(denomp.c, [-1.0 / 6, 0.5, -1.0, 1.0])
def test_pade_4term_exp(): # First four Taylor coefficients of exp(x). # Unlike poly1d, the first array element is the zero-order term. an = [1.0, 1.0, 0.5, 1.0/6] nump, denomp = pade(an, 0) assert_array_almost_equal(nump.c, [1.0/6, 0.5, 1.0, 1.0]) assert_array_almost_equal(denomp.c, [1.0]) nump, denomp = pade(an, 1) assert_array_almost_equal(nump.c, [1.0/6, 2.0/3, 1.0]) assert_array_almost_equal(denomp.c, [-1.0/3, 1.0]) nump, denomp = pade(an, 2) assert_array_almost_equal(nump.c, [1.0/3, 1.0]) assert_array_almost_equal(denomp.c, [1.0/6, -2.0/3, 1.0]) nump, denomp = pade(an, 3) assert_array_almost_equal(nump.c, [1.0]) assert_array_almost_equal(denomp.c, [-1.0/6, 0.5, -1.0, 1.0])
def lambert_delay(delay, sub_delay, tau, p, q): """Returns F = p/q s.t. F((tau*s+1)/e^(-sb)) = e^(-sa).""" a, b = delay, sub_delay r = a / b c = np.exp(a / tau) d = (b / tau) * np.exp(b / tau) i = np.arange(1, p + q + 1) taylor = np.append([1./r], (i+r)**(i-1) / factorial(i)) tf = pade(taylor, q) nds = np.poly1d([-d, 0]) # -ds return LinearSystem((c*r*tf[0](nds), tf[1](nds)), analog=True)
def series_evaluate(series, deta, order=1, series_type="taylor"): """ Return evaluated polynomial using the data frame """ assert order <= series_to_num_order(series) coeffs = series["b2"].values[:(order + 1)] if series_type == "pade" or series_type == "q" or series_type == "p": p_pade, q_pade = misc.pade(coeffs, 1) if series_type == "pade": return p_pade(deta) / q_pade(deta) if series_type == "q": return q_pade(deta) return p_pade(deta) poly = np.poly1d(coeffs[::-1]) return poly(deta)
def test_pade_trivial(): nump, denomp = pade([1.0], 0) assert_array_equal(nump.c, [1.0]) assert_array_equal(denomp.c, [1.0])
def test_pade(): # make sure scipy.misc.pade exists with suppress_warnings() as sup: sup.filter(DeprecationWarning, "`pade` is deprecated") pade([1, 2], 1)
def test_pade(): # make sure scipy.misc.pade exists with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) pade([1, 2], 1)