Example #1
0
def test_j_roots():
    rf = lambda a, b: lambda n, mu: orth.j_roots(n, a, b, mu)
    ef = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x)
    wf = lambda a, b: lambda x: (1 - x) ** a * (1 + x) ** b

    vgq = verify_gauss_quad
    vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1.0, 1.0, 5)
    vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1.0, 1.0, 25, atol=1e-12)
    vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1.0, 1.0, 100, atol=1e-11)

    vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1.0, 1.0, 5)
    vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1.0, 1.0, 25, atol=1.5e-13)
    vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1.0, 1.0, 100, atol=1e-12)

    vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1.0, 1.0, 5, atol=2e-13)
    vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1.0, 1.0, 25, atol=2e-13)
    vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1.0, 1.0, 100, atol=1e-12)

    vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1.0, 1.0, 5)
    vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1.0, 1.0, 25, atol=1e-13)
    vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1.0, 1.0, 100, atol=2e-13)

    vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1.0, 1.0, 5)
    vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1.0, 1.0, 25)
    vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1.0, 1.0, 100, atol=1e-13)

    vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1.0, 1.0, 5, atol=1e-13)
    vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1.0, 1.0, 25, atol=2e-13)
    vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1.0, 1.0, 100, atol=1e-11)

    vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1.0, 1.0, 5)
    vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1.0, 1.0, 25, atol=1e-13)
    vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1.0, 1.0, 100, atol=1e-13)

    # when alpha == beta == 0, P_n^{a,b}(x) == P_n(x)
    xj, wj = orth.j_roots(6, 0.0, 0.0)
    xl, wl = orth.p_roots(6)
    assert_allclose(xj, xl, 1e-14, 1e-14)
    assert_allclose(wj, wl, 1e-14, 1e-14)

    # when alpha == beta != 0, P_n^{a,b}(x) == C_n^{alpha+0.5}(x)
    xj, wj = orth.j_roots(6, 4.0, 4.0)
    xc, wc = orth.cg_roots(6, 4.5)
    assert_allclose(xj, xc, 1e-14, 1e-14)
    assert_allclose(wj, wc, 1e-14, 1e-14)

    x, w = orth.j_roots(5, 2, 3, False)
    y, v, m = orth.j_roots(5, 2, 3, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    muI, muI_err = integrate.quad(wf(2, 3), -1, 1)
    assert_allclose(m, muI, rtol=muI_err)

    assert_raises(ValueError, orth.j_roots, 0, 1, 1)
    assert_raises(ValueError, orth.j_roots, 3.3, 1, 1)
    assert_raises(ValueError, orth.j_roots, 3, -2, 1)
    assert_raises(ValueError, orth.j_roots, 3, 1, -2)
    assert_raises(ValueError, orth.j_roots, 3, -2, -2)
Example #2
0
def test_j_roots():
    roots = lambda a, b: lambda n, mu: orth.j_roots(n, a, b, mu)
    evalf = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x)

    verify_gauss_quad(roots(-0.5, -0.75), evalf(-0.5, -0.75), 5)
    verify_gauss_quad(roots(-0.5, -0.75), evalf(-0.5, -0.75), 25, atol=1e-12)
    verify_gauss_quad(roots(-0.5, -0.75), evalf(-0.5, -0.75), 100, atol=1e-11)

    verify_gauss_quad(roots(0.5, -0.5), evalf(0.5, -0.5), 5)
    verify_gauss_quad(roots(0.5, -0.5), evalf(0.5, -0.5), 25, atol=1e-13)
    verify_gauss_quad(roots(0.5, -0.5), evalf(0.5, -0.5), 100, atol=1e-12)

    verify_gauss_quad(roots(1, 0.5), evalf(1, 0.5), 5, atol=2e-13)
    verify_gauss_quad(roots(1, 0.5), evalf(1, 0.5), 25, atol=2e-13)
    verify_gauss_quad(roots(1, 0.5), evalf(1, 0.5), 100, atol=1e-12)

    verify_gauss_quad(roots(0.9, 2), evalf(0.9, 2), 5)
    verify_gauss_quad(roots(0.9, 2), evalf(0.9, 2), 25, atol=1e-13)
    verify_gauss_quad(roots(0.9, 2), evalf(0.9, 2), 100, atol=2e-13)

    verify_gauss_quad(roots(18.24, 27.3), evalf(18.24, 27.3), 5)
    verify_gauss_quad(roots(18.24, 27.3), evalf(18.24, 27.3), 25)
    verify_gauss_quad(roots(18.24, 27.3), evalf(18.24, 27.3), 100, atol=1e-13)

    verify_gauss_quad(roots(47.1, -0.2), evalf(47.1, -0.2), 5, atol=1e-13)
    verify_gauss_quad(roots(47.1, -0.2), evalf(47.1, -0.2), 25, atol=1e-13)
    verify_gauss_quad(roots(47.1, -0.2), evalf(47.1, -0.2), 100, atol=1e-11)

    verify_gauss_quad(roots(2.25, 68.9), evalf(2.25, 68.9), 5)
    verify_gauss_quad(roots(2.25, 68.9), evalf(2.25, 68.9), 25, atol=1e-13)
    verify_gauss_quad(roots(2.25, 68.9), evalf(2.25, 68.9), 100, atol=1e-13)

    # when alpha == beta == 0, P_n^{a,b}(x) == P_n(x)
    xj, wj = orth.j_roots(6, 0.0, 0.0)
    xl, wl = orth.p_roots(6)
    assert_allclose(xj, xl, 1e-14, 1e-14)
    assert_allclose(wj, wl, 1e-14, 1e-14)

    # when alpha == beta != 0, P_n^{a,b}(x) == C_n^{alpha+0.5}(x)
    xj, wj = orth.j_roots(6, 4.0, 4.0)
    xc, wc = orth.cg_roots(6, 4.5)
    assert_allclose(xj, xc, 1e-14, 1e-14)
    assert_allclose(wj, wc, 1e-14, 1e-14)

    x, w = orth.j_roots(5, 2, 3, False)
    y, v, m = orth.j_roots(5, 2, 3, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    assert_raises(ValueError, orth.j_roots, 0, 1, 1)
    assert_raises(ValueError, orth.j_roots, 3.3, 1, 1)
    assert_raises(ValueError, orth.j_roots, 3, -2, 1)
    assert_raises(ValueError, orth.j_roots, 3, 1, -2)
    assert_raises(ValueError, orth.j_roots, 3, -2, -2)
Example #3
0
def test_j_roots():
    roots = lambda a, b: lambda n, mu: orth.j_roots(n, a, b, mu)
    evalf = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x)

    verify_gauss_quad(roots(-0.5, -0.75), evalf(-0.5, -0.75), 5)
    verify_gauss_quad(roots(-0.5, -0.75), evalf(-0.5, -0.75), 25, atol=1e-12)
    verify_gauss_quad(roots(-0.5, -0.75), evalf(-0.5, -0.75), 100, atol=1e-11)

    verify_gauss_quad(roots(0.5, -0.5), evalf(0.5, -0.5), 5)
    verify_gauss_quad(roots(0.5, -0.5), evalf(0.5, -0.5), 25, atol=1e-13)
    verify_gauss_quad(roots(0.5, -0.5), evalf(0.5, -0.5), 100, atol=1e-12)

    verify_gauss_quad(roots(1, 0.5), evalf(1, 0.5), 5, atol=2e-13)
    verify_gauss_quad(roots(1, 0.5), evalf(1, 0.5), 25, atol=2e-13)
    verify_gauss_quad(roots(1, 0.5), evalf(1, 0.5), 100, atol=1e-12)

    verify_gauss_quad(roots(0.9, 2), evalf(0.9, 2), 5)
    verify_gauss_quad(roots(0.9, 2), evalf(0.9, 2), 25, atol=1e-13)
    verify_gauss_quad(roots(0.9, 2), evalf(0.9, 2), 100, atol=2e-13)

    verify_gauss_quad(roots(18.24, 27.3), evalf(18.24, 27.3), 5)
    verify_gauss_quad(roots(18.24, 27.3), evalf(18.24, 27.3), 25)
    verify_gauss_quad(roots(18.24, 27.3), evalf(18.24, 27.3), 100, atol=1e-13)

    verify_gauss_quad(roots(47.1, -0.2), evalf(47.1, -0.2), 5, atol=1e-13)
    verify_gauss_quad(roots(47.1, -0.2), evalf(47.1, -0.2), 25, atol=1e-13)
    verify_gauss_quad(roots(47.1, -0.2), evalf(47.1, -0.2), 100, atol=1e-11)

    verify_gauss_quad(roots(2.25, 68.9), evalf(2.25, 68.9), 5)
    verify_gauss_quad(roots(2.25, 68.9), evalf(2.25, 68.9), 25, atol=1e-13)
    verify_gauss_quad(roots(2.25, 68.9), evalf(2.25, 68.9), 100, atol=1e-13)

    # when alpha == beta == 0, P_n^{a,b}(x) == P_n(x)
    xj, wj = orth.j_roots(6, 0.0, 0.0)
    xl, wl = orth.p_roots(6)
    assert_allclose(xj, xl, 1e-14, 1e-14)
    assert_allclose(wj, wl, 1e-14, 1e-14)

    # when alpha == beta != 0, P_n^{a,b}(x) == C_n^{alpha+0.5}(x)
    xj, wj = orth.j_roots(6, 4.0, 4.0)
    xc, wc = orth.cg_roots(6, 4.5)
    assert_allclose(xj, xc, 1e-14, 1e-14)
    assert_allclose(wj, wc, 1e-14, 1e-14)

    x, w = orth.j_roots(5, 2, 3, False)
    y, v, m = orth.j_roots(5, 2, 3, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    assert_raises(ValueError, orth.j_roots, 0, 1, 1)
    assert_raises(ValueError, orth.j_roots, 3.3, 1, 1)
    assert_raises(ValueError, orth.j_roots, 3, -2, 1)
    assert_raises(ValueError, orth.j_roots, 3, 1, -2)
    assert_raises(ValueError, orth.j_roots, 3, -2, -2)
Example #4
0
def test_j_roots():
    rf = lambda a, b: lambda n, mu: orth.j_roots(n, a, b, mu)
    ef = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x)
    wf = lambda a, b: lambda x: (1 - x)**a * (1 + x)**b

    vgq = verify_gauss_quad
    vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1., 5)
    vgq(rf(-0.5, -0.75),
        ef(-0.5, -0.75),
        wf(-0.5, -0.75),
        -1.,
        1.,
        25,
        atol=1e-12)
    vgq(rf(-0.5, -0.75),
        ef(-0.5, -0.75),
        wf(-0.5, -0.75),
        -1.,
        1.,
        100,
        atol=1e-11)

    vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 5)
    vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 25, atol=1.5e-13)
    vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 100, atol=1e-12)

    vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 5, atol=2e-13)
    vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 25, atol=2e-13)
    vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 100, atol=1e-12)

    vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 5)
    vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 25, atol=1e-13)
    vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 100, atol=2e-13)

    vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1., 5)
    vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1., 25)
    vgq(rf(18.24, 27.3),
        ef(18.24, 27.3),
        wf(18.24, 27.3),
        -1.,
        1.,
        100,
        atol=1e-13)

    vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1., 5, atol=1e-13)
    vgq(rf(47.1, -0.2),
        ef(47.1, -0.2),
        wf(47.1, -0.2),
        -1.,
        1.,
        25,
        atol=2e-13)
    vgq(rf(47.1, -0.2),
        ef(47.1, -0.2),
        wf(47.1, -0.2),
        -1.,
        1.,
        100,
        atol=1e-11)

    vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1., 5)
    vgq(rf(2.25, 68.9),
        ef(2.25, 68.9),
        wf(2.25, 68.9),
        -1.,
        1.,
        25,
        atol=1e-13)
    vgq(rf(2.25, 68.9),
        ef(2.25, 68.9),
        wf(2.25, 68.9),
        -1.,
        1.,
        100,
        atol=1e-13)

    # when alpha == beta == 0, P_n^{a,b}(x) == P_n(x)
    xj, wj = orth.j_roots(6, 0.0, 0.0)
    xl, wl = orth.p_roots(6)
    assert_allclose(xj, xl, 1e-14, 1e-14)
    assert_allclose(wj, wl, 1e-14, 1e-14)

    # when alpha == beta != 0, P_n^{a,b}(x) == C_n^{alpha+0.5}(x)
    xj, wj = orth.j_roots(6, 4.0, 4.0)
    xc, wc = orth.cg_roots(6, 4.5)
    assert_allclose(xj, xc, 1e-14, 1e-14)
    assert_allclose(wj, wc, 1e-14, 1e-14)

    x, w = orth.j_roots(5, 2, 3, False)
    y, v, m = orth.j_roots(5, 2, 3, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    muI, muI_err = integrate.quad(wf(2, 3), -1, 1)
    assert_allclose(m, muI, rtol=muI_err)

    assert_raises(ValueError, orth.j_roots, 0, 1, 1)
    assert_raises(ValueError, orth.j_roots, 3.3, 1, 1)
    assert_raises(ValueError, orth.j_roots, 3, -2, 1)
    assert_raises(ValueError, orth.j_roots, 3, 1, -2)
    assert_raises(ValueError, orth.j_roots, 3, -2, -2)
Example #5
0
import numpy as np
from numpy import sqrt
from numpy.testing import assert_allclose
import scipy.special as sc
import scipy.special.orthogonal as orth

rf = lambda a, b: lambda n, mu: sc.roots_jacobi(n, a, b, mu)
ef = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x)

rtol = 1e-15
atol = 1e-14
N = 25

root_func = rf(18.24, 27.3)
eval_func = ef(18.24, 27.3)
x, w, mu = root_func(N, True)

# test orthogonality. Note that the results need to be normalized,
# otherwise the huge values that can arise from fast growing
# functions like Laguerre can be very confusing.

n = np.arange(N)
v = eval_func(n[:, np.newaxis], x)
vv = np.dot(v * w, v.T)
vd = 1 / np.sqrt(vv.diagonal())
vv = vd[:, np.newaxis] * vv * vd
assert_allclose(vv, np.eye(N), rtol, atol)