Пример #1
0
def test_reproduction():
    p = [-1.0, -2.0, -3.0]

    def f(z):
        return (z**3 - 2 * z**2 + 4 * z - 7) / ((z - p[0]) * (z - p[1]) *
                                                (z - p[2]))

    nodes = np.arange(1, 8, dtype=float)
    r = baryrat.aaa(nodes, f(nodes))
    assert np.allclose(f(nodes), r(nodes))
    z = np.linspace(0, 1, 100)
    assert np.allclose(f(z), r(z))

    pol, res = r.polres()
    assert np.allclose(sorted(p), sorted(pol))
    ratfun = sum(res[j] / (z - pol[j]) for j in range(len(pol)))
    bias = ratfun - f(z)
    assert np.allclose(bias, bias[0])  # should be constant

    mp.dps = 100
    pol, res = r.polres(use_mp=True)
    pol = np.real_if_close(np.array(pol, complex))
    assert np.allclose(sorted(p), sorted(pol))
    ratfun = sum(res[j] / (z - pol[j]) for j in range(len(pol)))
    bias = np.array(ratfun - f(z), complex)
    assert np.allclose(bias, bias[0])  # should be constant
Пример #2
0
def test_approx():
    Z = np.linspace(0.0, 1.0, 101)
    def f(z): return np.exp(z)*np.sin(2*np.pi*z)
    F = f(Z)

    r = baryrat.aaa(Z, F, mmax=10)

    assert np.linalg.norm(r(Z) - F, np.inf) < 1e-10, 'insufficient approximation'

    # check invoking with functions
    r2 = baryrat.aaa(Z, f, mmax=10)
    assert np.linalg.norm(r(Z) - r2(Z), np.inf) < 1e-15

    # check that calling r works for scalars, vectors, matrices
    assert np.isscalar(r(0.45))
    assert r(np.ones(7)).shape == (7,)
    assert r(np.ones((3,2))).shape == (3,2)
Пример #3
0
def test_aaa_complex():
    Z = np.linspace(0.0, 1.0, 101)

    def f(z):
        return np.exp(2j * np.pi * z)

    F = f(Z)
    r = baryrat.aaa(Z, F, mmax=8)
    assert np.linalg.norm(r(Z) - F,
                          np.inf) < 1e-10, 'insufficient approximation'
Пример #4
0
def test_reproduction():
    p = [-1.0, -2.0, -3.0]
    def f(z):
        return (z**3 - 2*z**2 + 4*z - 7) / ((z - p[0])*(z - p[1])*(z - p[2]))
    nodes = np.arange(1, 8, dtype=float)
    r = baryrat.aaa(nodes, f(nodes))
    assert np.allclose(f(nodes), r(nodes))
    z = np.linspace(0, 1, 100)
    assert np.allclose(f(z), r(z))
    pol, res = r.polres()
    assert np.allclose(sorted(p), sorted(pol))
Пример #5
0
def test_zeros():
    Z = np.linspace(0.0, 1.0, 101)
    F = np.exp(Z) * np.sin(2 * np.pi * Z)
    r = baryrat.aaa(Z, F, mmax=6)

    zer = r.zeros()
    assert np.allclose(zer,
                       np.array([-0.38621461, 1.43052691, 0.49999907, 1., 0.]))
    assert np.allclose(r(zer), 0.0)

    zer2 = r.zeros(use_mp=True)
    assert np.allclose(sorted(zer), sorted(zer2))
Пример #6
0
def test_polres():
    Z = np.linspace(0.0, 1.0, 101)
    F = np.exp(Z) * np.sin(2*np.pi*Z)
    r = baryrat.aaa(Z, F, mmax=6)
    pol, res = r.polres()

    assert np.allclose(pol,
            np.array([2.26333482+0.j, 0.2338428+0.90087977j,
                0.2338428-0.90087977j, 0.96472415+0.85470621j,
                0.96472415-0.85470621j]))
    assert np.allclose(res,
            np.array([69.08984183+0.j, 20.50747913-9.24908921j,
                20.50747913+9.24908921j, 23.24692682+23.94602455j,
                23.24692682-23.94602455j]))
    polvals = r(pol)
    assert np.min(np.abs(polvals)) > 1e13