Beispiel #1
0
def stieltjes_unbounded(a, b, weight, N, singularity_list, Nquad=10):

    assert a < b

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

    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_unbounded_composite(integrand, a, b,
                                                       n+1+Nquad,
                                                       singularity_list)
        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_unbounded_composite(integrand, a,
                                b, n+1+Nquad, singularity_list))

    return ab
Beispiel #2
0
def fidistinv_setup_helper2(ug, idistinv, exponents, M, alpha, beta):

    # vgrid = np.cos( np.linspace(np.pi, 0, M) )
    # Or
    # vgrid = np.cos( np.linspace(np.pi, 0, M+1)[:-1] + M/(2*np.pi) )
    # Or
    vgrid = np.cos(np.linspace(np.pi, 0, M+2))
    vgrid = vgrid[1:-1]

    ab = jacobi_recurrence_values(M, -1/2, -1/2)
    V = eval_driver(vgrid, np.arange(M), 0, ab)

    iV = np.linalg.inv(V)

    ugrid = np.zeros((M, ug.size - 1))
    xgrid = np.zeros((M, ug.size - 1))
    xcoeffs = np.zeros((M, ug.size - 1))

    for q in range(ug.size - 1):
        ugrid[:, q] = (vgrid + 1) / 2 * (ug[q+1] - ug[q]) + ug[q]

        if ug.size == 3:
            xgrid[:, q] = 2 * bbeta.ppf(ugrid[:, q], beta+1, alpha+1) - 1
        else:
            xgrid[:, q] = idistinv(ugrid[:, q])

        temp = xgrid[:, q]

        # temp = temp(ugrid)
        # for ugrid near 0, then temp behaves like a certain rational function (1-ugrid)**???
        if exponents[0, q] != 0:
            temp = (temp - xgrid[0, q]) / (xgrid[-1, q] - xgrid[0, q])
        else:
            temp = (temp - xgrid[-1, q]) / (xgrid[-1, q] - xgrid[0, q])

        with np.errstate(divide='ignore', invalid='ignore'):
            temp = temp * (1 + vgrid)**exponents[0, q] * (1 - vgrid)**exponents[1, q]
            temp[~np.isfinite(temp)] = 0

        xcoeffs[:, q] = np.dot(iV, temp)

    data = np.zeros((M+6, ug.size - 1))
    for q in range(ug.size - 1):
        data[:, q] = np.hstack((ug[q], ug[q+1], xgrid[0, q], xgrid[-1, q], exponents[:, q], xcoeffs[:, q]))

    return data
Beispiel #3
0
def driver_helper(u, data):

    tol = 1e-12

    M = data.shape[0] - 6

    ab = jacobi_recurrence_values(M, -1/2, -1/2)

    app = np.append(data[0, :], data[1, -1])
    edges = np.insert(app, [0, app.size], [-np.inf, np.inf])
    j = np.digitize(u, edges, right=False)
    B = edges.size - 1

    x = np.zeros(u.size)
    x[np.where(j == 1)] = data[2, 0]  # Boundary bins
    x[np.where(j == B)] = data[3, -1]

    for qb in range(2, B):
        umask = (j == qb)
        if not any(umask):
            continue

        q = qb - 1
        vgrid = (u[umask] - data[0, q-1]) / (data[1, q-1] - data[0, q-1]) * 2 - 1
        V = eval_driver(vgrid, np.arange(M), 0, ab)
        temp = np.dot(V, data[6:, q-1])
        with np.errstate(divide='ignore', invalid='ignore'):
            temp = temp / ((1 + vgrid)**data[4, q-1] * (1 - vgrid)**data[5, q-1])

        if data[4, q-1] != 0:
            flags = abs(u[umask] - data[0, q-1]) < tol
            temp[flags] = 0
            temp = temp * (data[3, q-1] - data[2, q-1]) + data[2, q-1]
        else:
            flags = abs(u[umask] - data[1, q-1]) < tol
            temp[flags] = 0
            temp = temp * (data[3, q-1] - data[2, q-1]) + data[3, q-1]

        x[umask] = temp

    return x
Beispiel #4
0
def predict_correct_discrete(xg, wg, N):

    assert all(i >= 0 for i in wg)
    assert N <= len(xg)

    ab = np.zeros([N, 2])
    ab[0, 1] = np.sqrt(np.sum(wg))

    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: peval(x, n).flatten() * peval(x, n+1).flatten()
        ab[n+1, 0] += ab[n, 1] * np.sum(integrand(xg) * wg)

        integrand = lambda x: peval(x, n+1).flatten()**2
        ab[n+1, 1] *= np.sqrt(np.sum(integrand(xg) * wg))

    return ab
Beispiel #5
0
def opolynd_eval(x, lambdas, ab):
    # Evaluates tensorial orthonormal polynomials associated with the
    # univariate recurrence coefficients ab.

    try:
        M, d = x.shape
    except Exception:
        d = x.size
        M = 1
        x = np.reshape(x, (M, d))

    N, d2 = lambdas.shape

    assert d == d2, "Dimension 1 of x and lambdas must be equal"

    p = np.ones([M, N])

    for qd in range(d):
        p = p * opoly1d.eval_driver(x[:, qd], lambdas[:, qd], 0, ab)

    return p
Beispiel #6
0
def predict_correct_unbounded(a, b, weight, N, singularity_list, Nquad=10):
    assert a < b
    ab = np.zeros([N, 2])
    ab[0, 1] = np.sqrt(gq_modification_unbounded_composite(weight, a, b, Nquad,
                                                           singularity_list))

    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_unbounded_composite(integrand,
                                    a, b, n+1+Nquad, singularity_list)

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

    return ab
Beispiel #7
0
def stieltjes_discrete(xg, wg, N):

    assert all(i >=0 for i in wg)
    assert N <= len(xg)

    ab = np.zeros([N, 2])
    ab[0, 1] = np.sqrt(np.sum(wg))

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

    for n in range(1, N):
        integrand = lambda x: x * peval(x, n-1).flatten()**2
        ab[n, 0] = np.sum(integrand(xg) * wg)
        if n == 1:
            integrand = lambda x: ((x - ab[n, 0]) *\
                                    peval(x, n-1).flatten())**2
        else:
            integrand = lambda 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(np.sum(integrand(xg) * wg))

    return ab
Beispiel #8
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
def verify_orthonormal(ab, n, xg, wg):
    nmax = np.max(n)
    assert nmax <= len(ab) - 1
    P = eval_driver(xg, n, 0, ab)
    return np.dot(wg * P.T, P)