def test_maijerg_ex_recursive():
    """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 - ...
    # First construct Meijer-G from Taylor expansion
    tcoeffs = np.array([1, -1, 1. / 2., -1. / 6., 1. / 24.])
    borel = borel_trans_coeffs(tcoeffs)
    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]
    points = np.array(
        [0.01, 0.1, 0.15, 0.21, 0.23, 0.29, 0.31, 0.32, 0.3, 0.25])
    result0 = np.exp(-points)
    print result0
    zs = meijerg_approx_low(xvector, yvector, pl, ql, points)
    print "initial Meijer-G ", zs

    # Then make a Pade out of the Meijer-G results
    gen_pade = GeneralPadeApproximant(points, np.real(zs), 4, 6)
    tcoeffs_pade = taylor_coeffs(gen_pade(np.array([0.]), range(6)), 5)
    borel1 = borel_trans_coeffs(tcoeffs_pade)
    ratios1 = consecutive_ratios_odd(borel)
    ps1, qs1 = rational_function_for_ratios(ratios)
    # Add the constant 1. to the qm coefficients
    ps1 = ps1[::-1]
    qs1 = qs1[::-1]
    qs1 = np.append(qs, np.array(1.))
    rootsp1 = find_roots(ps1)
    rootsq1 = find_roots(qs1)
    xvector1 = np.append(np.array([1]), rootsp1)
    yvector1 = rootsq1
    pl1 = ps1[0]
    ql1 = qs1[0]
    #print tcoeffs_pade
    zs1 = meijerg_approx_low(xvector1, yvector1, pl1, ql1, points)
    print "True result", result0
    print "from pade ", gen_pade(points)
    print "result 1 ", np.real(zs1)
    #y, optz = optimize_pars_meijerg(xvector1, yvector1, pl1, ql1, points, result0)
    #print "result value", optz
    y = optimize_pars_meijerg(xvector1, yvector1, pl1, ql1, points, result0)
    print "result params", y
    print "True result", result0
def test_example2():
    """Test Meijer-G approximant for equation 37."""
    coeffs = np.array(
        [1 / 2., 3 / 4., -21 / 8., 333 / 16., -30885. / 128, 916731 / 256.])
    borel = borel_trans_coeffs(coeffs)
    print borel
    ratios = consecutive_ratios_odd(borel)
    print ratios
    ps, qs = rational_function_for_ratios(ratios)
    print "ps, qs", ps, qs
    # 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
    print "xvector", xvector
    print "yvector", yvector
    pl = ps[0]
    ql = qs[0]
    points = np.array([1., 2., 50.])
    zs = meijerg_approx_low(xvector, yvector, pl, ql, points)
    print zs / 2
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 test_rational_function_for_ratios():
    """Test the rational function fit for the consecutive ratios of Borel coefficients."""
    ratios = np.array([-1. / 8., -35. / 96., -11. / 24.])
    assert_raises(TypeError, rational_function_for_ratios, [1, 2, 3])
    ps, qs = rational_function_for_ratios(ratios)
    # Make polynomials to check prediction of ratios
    assert np.allclose(ps, [-1. / 8., -113. / 216])
    assert np.allclose(qs, [7. / 9])
def test_find_roots():
    """Test of the wrapper function to find the roots of a polynomial."""
    ratios = np.array([-1. / 8., -35. / 96., -11. / 24.])
    assert_raises(TypeError, rational_function_for_ratios, [1, 2, 3])
    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)
    assert np.allclose(rootsp, [-ps[1] / ps[0]])
    assert np.allclose(rootsq, [-1 / qs[0]])
def test_example3():
    coeffs = np.array([1, -1, 2.667, -4.667])
    borel = borel_trans_coeffs(coeffs)
    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]
    points = np.array([0.6736])
    zs = meijerg_approx_low(xvector, yvector, pl, ql, points)
    assert abs(1 - zs) < 1e-3
def test_meijerg():
    """Test Meijer-G approximant, comparing with results from the paper of Mera 2018."""
    ratios = np.array([-1. / 8., -35. / 96., -11. / 24.])
    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.array([1, rootsp[0]])
    yvector = np.array([rootsq[0]])
    pl = ps[0]
    ql = qs[0]
    points = np.array([-1 + 0j, -10 + 0j, -100 + 0j])
    zs = meijerg_approx_low(xvector, yvector, pl, ql, points)
    #print zs
    assert (zs -
            [1.133285 + 0.144952j, 0.744345 + 0.436317j, 0.386356 + 0.321210j]
            < 1e-6).all()
def test_meijerg_ex():
    """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.])
    borel = borel_trans_coeffs(tcoeffs)
    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]
    points = np.array([1.2, -1.4])
    result0 = np.exp(points)
    zs = meijerg_approx_low(xvector, yvector, pl, ql, points)
    assert (abs(result0 - np.real(zs)) < 1e-7).all()
def test_example5():
    coeffs = np.array([1, 6, 210, 13860])
    borel = borel_trans_coeffs(coeffs)
    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]
    points = np.array([1, 10, 100])
    zs = meijerg_approx_low(xvector, yvector, pl, ql, points)
    result0 = [
        0.473794 + 0.368724j, 0.255694 + 0.228610j, 0.144490 + 0.133539j
    ]
    assert (abs(zs - result0) < 1e-6).all()
def test_example4():
    coeffs = np.array([1, -0.667, 0.556, -2.056])
    borel = borel_trans_coeffs(coeffs)
    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]
    points = np.array([0.5381])
    zs = meijerg_approx_low(xvector, yvector, pl, ql, points)
    print "zs", zs
    v = 1 / (2. + zs)
    result0 = 0.5921
    print v, result0
def test_example6():
    coeffs = np.array([1, 1 / 2., 9 / 8., 75 / 16.])
    borel = borel_trans_coeffs(coeffs)
    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]
    points = np.array([1, 100])
    zs = meijerg_approx_low(xvector, yvector, pl, ql, points)
    result0 = [
        0.990312240887789089 + 0.481308237536857j,
        0.13677671640883210679 + 0.23483780883795517888j
    ]
    assert (abs(zs - result0) < 1e-7).all