Esempio n. 1
0
def stieltjes_bounded(a, b, weight, N, singularity_list, Nquad=10):


    assert a < b

    subintervals = compute_subintervals(a, b, singularity_list)

    ab = np.zeros([N, 2])
    ab[0, 1] = np.sqrt(gq_modification_composite(weight, a, b, Nquad,
                                                 subintervals=subintervals))

    peval = lambda x, n: eval_driver(x, np.array([n]), 0, ab)

    for n in range(1, N):
        integrand = lambda x: weight(x) * x * peval(x, n-1).flatten()**2
        ab[n, 0] = gq_modification_composite(integrand, a, b, n+1+Nquad,
                                             subintervals)
        if n == 1:
            integrand = lambda x: weight(x) * ((x - ab[n, 0]) *
                                  peval(x, n-1).flatten())**2
        else:
            integrand = lambda x: weight(x) * ((x - ab[n, 0]) *
                                  peval(x, n-1).flatten() - ab[n-1, 1] *
                                  peval(x, n-2).flatten())**2
        ab[n, 1] = np.sqrt(gq_modification_composite(integrand, a, b,
                                                      n+1+Nquad, subintervals))

    return ab
Esempio n. 2
0
def predict_correct_bounded_composite(a, b, weight, N, singularity_list,
                                      Nquad=10):
    """ Three-term recurrence coefficients from composite quadrature

    Computes the first N three-term recurrence coefficient pairs associated to
    weight on the bounded interval [a, b]. 

    Performs composite quadrature on [a, b] using
    utils.quad.gq_modification_composite.
    """

    assert a < b

    # First divide [a, b] into subintervals based on singularity locations.
    global_subintervals = compute_subintervals(a, b, singularity_list)

    ab = np.zeros([N, 2])
    ab[0, 1] = np.sqrt(gq_modification_composite(weight, a, b, Nquad, 
                        subintervals=global_subintervals))

    integrand = weight

    for n in range(0, N-1):
        # Guess next coefficients
        ab[n+1, 0], ab[n+1, 1] = ab[n, 0], ab[n, 1]

        # Set up linear modification roots and subintervals
        breaks = singularity_list.copy()
        pn_zeros = gauss_quadrature_driver(ab, n)[0]
        pn1_zeros = gauss_quadrature_driver(ab, n+1)[0]

        roots = np.hstack([pn_zeros, pn1_zeros])
        breaks += [[z, 0, 0] for z in roots]
        subintervals = compute_subintervals(a, b, breaks)

        # Leading coefficient
        qlc = np.prod(leading_coefficient_driver(n+2, ab)[-2:])

        ab[n+1, 0] += ab[n, 1] * gq_modification_composite(integrand, a, b,
                                                           n+1+Nquad,
                                                           subintervals,
                                                           roots=roots,
                                                           leading_coefficient=qlc)

        # Here subintervals are the global ones
        pn1_zeros = gauss_quadrature_driver(ab, n+1)[0]
        qlc = (leading_coefficient_driver(n+2, ab)[-1])**2

        ab[n+1, 1] *= np.sqrt(gq_modification_composite(integrand, a, b,
                                n+1+Nquad, global_subintervals,
                                quadroots=pn1_zeros, leading_coefficient=qlc))

    return ab
Esempio n. 3
0
def predict_correct_bounded(a, b, weight, N, singularity_list, Nquad=10):
    """ Three-term recurrence coefficients from quadrature

    Computes the first N three-term recurrence coefficient pairs associated to
    weight on the bounded interval [a, b].

    Performs global integration on [a, b] using
    utils.quad.gq_modification_composite.
    """

    assert a < b

    # First divide [a, b] into subintervals based on singularity locations.
    subintervals = compute_subintervals(a, b, singularity_list)

    ab = np.zeros([N, 2])
    ab[0, 1] = np.sqrt(gq_modification_composite(weight, a, b, Nquad,
                       subintervals=subintervals))

    peval = lambda x, n: eval_driver(x, np.array([n]), 0, ab)

    for n in range(0, N-1):
        # Guess next coefficients
        ab[n+1, 0], ab[n+1, 1] = ab[n, 0], ab[n, 1]

        integrand = lambda x: weight(x) * peval(x, n).flatten() *\
                      peval(x, n+1).flatten()
        ab[n+1, 0] += ab[n, 1] * gq_modification_composite(integrand, a, b,
                                                           n+1+Nquad,
                                                           subintervals)

        integrand = lambda x: weight(x) * peval(x, n+1).flatten()**2
        ab[n+1, 1] *= np.sqrt(gq_modification_composite(integrand, a, b,
                                                        n+1+Nquad,
                                                        subintervals))

    return ab
Esempio n. 4
0
def compute_moment_bounded(a, b, weight, n, singularity_list, Nquad=10):
    """
    Return the first 2n+1 (finite) moments, {mu_i}_{i=0}^2n
    """
    assert a < b

    subintervals = compute_subintervals(a, b, singularity_list)

    m = np.zeros(2 * n + 1, )
    for i in range(len(m)):

        def integrand(x):
            return weight(x) * x**i

        # integrand = lambda x: weight(x) * x**i
        m[i] = gq_modification_composite(integrand, a, b, i + 1 + Nquad,
                                         subintervals)

    return m
Esempio n. 5
0
def stieltjes_bounded_composite(a, b, weight, N, singularity_list, Nquad=10):

    assert a < b

    # First divide [a, b] into subintervals based on singularity locations.
    global_subintervals = compute_subintervals(a, b, singularity_list)

    ab = np.zeros([N, 2])
    ab[0, 1] = np.sqrt(gq_modification_composite(weight, a, b, Nquad, 
                                             subintervals=global_subintervals))

    integrand = weight

    for n in range(1, N):

        breaks = singularity_list.copy()
        pnminus1_zeros = gauss_quadrature_driver(ab, n-1)[0]
        roots = np.hstack([0, pnminus1_zeros])
        breaks += [[z, 0, 0] for z in roots]
        subintervals = compute_subintervals(a, b, breaks)

        qlc = np.prod(leading_coefficient_driver(n, ab)[-1])**2

        ab[n, 0] = gq_modification_composite(integrand, a, b, n+1+Nquad,
                                             subintervals, roots=np.zeros(1,),
                                             quadroots=pnminus1_zeros,
                                             leading_coefficient=qlc)

        if n == 1:
            pnminus1_zeros = np.hstack([ab[n, 0], pnminus1_zeros])
            s = gq_modification_composite(integrand, a, b, n+1+Nquad,
                                          global_subintervals,
                                          quadroots=pnminus1_zeros,
                                          leading_coefficient=qlc)
        else:
            pnminus1_zeros = np.hstack([ab[n, 0], pnminus1_zeros])
            s_1 = gq_modification_composite(integrand, a, b, n+1+Nquad,
                                            global_subintervals,
                                            quadroots=pnminus1_zeros,
                                            leading_coefficient=qlc)

            pnminus2_zeros = gauss_quadrature_driver(ab, n-2)[0]
            qlc = np.prod(leading_coefficient_driver(n-1, ab)[-1])**2
            s_2 = gq_modification_composite(integrand, a, b, n+1+Nquad,
                                            global_subintervals,
                                            quadroots=pnminus2_zeros,
                                            leading_coefficient=qlc)

            # below pnminus1_zeros already includes ab[n, 0]
            roots = np.hstack([pnminus1_zeros, pnminus2_zeros]) 
            breaks_new = singularity_list.copy()
            breaks_new += [[z, 0, 0] for z in roots]
            subintervals = compute_subintervals(a, b, breaks_new)
            qlc = np.prod(leading_coefficient_driver(n, ab)[-2:])
            s_3 = gq_modification_composite(integrand, a, b, n+1+Nquad,
                                            subintervals, roots=roots,
                                            leading_coefficient=qlc)

            s = s_1 + ab[n-1, 1]**2 * s_2 - 2 * ab[n-1, 1] * s_3

        ab[n, 1] = np.sqrt(s)

    return ab
Esempio n. 6
0
    def test_gq_modification_composite(self):
        """ gq_modification using a composite strategy
        Testing of gq_modification on an interval [-1,1] using a composite
        quadrature rule over a partition of [-1,1].
        """

        alpha = -1. + 6 * np.random.rand()
        beta = -1. + 6 * np.random.rand()
        J = JacobiPolynomials(alpha=alpha, beta=beta)

        delta = 5e-6
        N = 10

        G = np.zeros([N, N])

        # Integrate just the weight function. We'll use modifications for the polynomial part
        integrand = lambda x: jacobi_weight_normalized(x, alpha, beta)

        for n in range(N):
            for m in range(N):

                coeffs = J.leading_coefficient(max(n, m) + 1)
                if n != m:
                    zeros = np.sort(
                        np.hstack((J.gauss_quadrature(n)[0],
                                   J.gauss_quadrature(m)[0])))
                    quadzeros = np.zeros(0)
                    leading_coefficient = coeffs[n] * coeffs[m]
                else:
                    zeros = np.zeros(0)
                    quadzeros = J.gauss_quadrature(n)[0]
                    leading_coefficient = coeffs[n]**2

                demarcations = np.sort(zeros)
                D = demarcations.size

                subintervals = np.zeros([D + 1, 4])
                if D == 0:
                    subintervals[0, :] = [-1, 1, beta, alpha]
                else:
                    subintervals[0, :] = [-1, demarcations[0], beta, 0]
                    for q in range(D - 1):
                        subintervals[q + 1, :] = [
                            demarcations[q], demarcations[q + 1], 0, 0
                        ]

                    subintervals[D, :] = [demarcations[-1], 1, 0, alpha]

                G[n, m] = quad.gq_modification_composite(
                    integrand,
                    -1,
                    1,
                    20,
                    subintervals=subintervals,
                    roots=zeros,
                    quadroots=quadzeros,
                    leading_coefficient=leading_coefficient)

        errstr = 'Failed for (N,alpha,beta) = ({0:d}, {1:1.6f}, {2:1.6f})'.format(
            N, alpha, beta)

        self.assertAlmostEqual(np.linalg.norm(G - np.eye(N), ord=np.inf),
                               0,
                               delta=delta,
                               msg=errstr)