Beispiel #1
0
    def f(x):
        degree = 7
        n = 5
        u = x[0]
        v = x[1]
        B = x[2:]
        data = [
            (B[0], z(n)),
            (B[1], fsd(n, (u, 1))),
            (B[2], fsd(n, (v, 1))),
            (B[3], fsd(n, (u, 2))),
            (B[4], fsd(n, (v, 2))),
            (B[5], fsd(n, (u, 3))),
        ]
        points, weights = untangle(data)

        exponents = get_all_exponents(n, degree)
        # flatten list
        exponents = numpy.array([item for sublist in exponents for item in sublist])

        def evaluate_all_monomials(x):
            return numpy.prod(x[..., None] ** exponents.T[:, None], axis=0).T

        flt = numpy.vectorize(float)
        exact_vals = flt([integrate_monomial_over_unit_nball(k) for k in exponents])

        A = evaluate_all_monomials(points.T)

        out = numpy.dot(A, weights)
        out -= exact_vals

        norm_v = numpy.sqrt(numpy.dot(out, out))
        print(norm_v)
        return norm_v
Beispiel #2
0
def check_degree(quadrature, exact, dim, max_degree, tol):
    exponents = get_all_exponents(dim, max_degree)
    # flatten list
    exponents = numpy.array(
        [item for sublist in exponents for item in sublist])

    flt = numpy.vectorize(float)
    exact_vals = flt([exact(k) for k in exponents])

    def evaluate_all_monomials(x):
        # Evaluate monomials.
        # There's a more complex, faster implementation using matmul, exp, log.
        # However, this only works for strictly positive `x`, and requires some
        # tinkering. See below and
        # <https://stackoverflow.com/a/45421128/353337>.
        return numpy.prod(x[..., None]**exponents.T[:, None], axis=0).T

    vals = quadrature(evaluate_all_monomials)

    # check relative error
    err = abs(exact_vals - vals)
    is_smaller = err < (1 + abs(exact_vals)) * tol

    if numpy.all(is_smaller):
        return max_degree, numpy.max(err / (1 + abs(exact_vals)))

    k = numpy.where(~is_smaller)[0]
    # Return the max error for all exponents that are one smaller than the max_degree.
    # This is because this functions is usually called with target_degree + 1.
    idx = numpy.sum(exponents, axis=1) < max_degree
    return (
        numpy.sum(exponents[k[0]]) - 1,
        numpy.max(err[idx] / (1 + abs(exact_vals[idx]))),
    )
Beispiel #3
0
def check_degree(quadrature, exact, dim, max_degree, tol=1.0e-14):
    exponents = get_all_exponents(dim, max_degree)
    # flatten list
    exponents = numpy.array(
        [item for sublist in exponents for item in sublist])

    flt = numpy.vectorize(float)
    exact_vals = flt([exact(k) for k in exponents])

    def evaluate_all_monomials(x):
        # Evaluate monomials.
        # There's a more complex, faster implementation using matmul, exp, log.
        # However, this only works for strictly positive `x`, and requires some
        # tinkering. See below and
        # <https://stackoverflow.com/a/45421128/353337>.
        return numpy.prod(x[..., None]**exponents.T[:, None], axis=0).T

    vals = quadrature(evaluate_all_monomials)

    # print(exact_vals)
    # print(vals)

    # check relative error
    # The allowance is quite large here, 1e5 over machine precision.
    # Some tests fail if lowered, though.
    # TODO increase precision
    eps = numpy.finfo(float).eps
    mytol = abs(exact_vals) * tol + (1.0e5 + tol + exact_vals) * eps
    is_smaller = abs(exact_vals - vals) < mytol

    if numpy.all(is_smaller):
        return max_degree

    k = numpy.where(numpy.logical_not(is_smaller))[0]
    return numpy.sum(exponents[k[0]]) - 1  # = degree
Beispiel #4
0
    def f(x):
        degree = 11
        n = 3

        u = 0.871_740_148_509_601
        v = 0.591_700_181_433_148
        w = 0.209_299_217_902_484

        # u = x[0]
        # v = x[1]
        # w = x[2]
        # B = x[3:]
        B = x

        data = [
            (B[0], z(n)),
            (B[1], fsd(n, (u, 1))),
            (B[2], fsd(n, (v, 1))),
            (B[3], fsd(n, (w, 1))),
            (B[4], fsd(n, (u, 2))),
            (B[5], fsd(n, (v, 2))),
            (B[6], fsd(n, (w, 2))),
            (B[7], fsd(n, (u, 1), (v, 1))),
            (B[8], fsd(n, (u, 1), (w, 1))),
            (B[9], fsd(n, (u, 3))),
            (B[10], fsd(n, (v, 3))),
            (B[11], fsd(n, (w, 3))),
            (B[12], fsd(n, (u, 2), (v, 1))),
        ]

        points, weights = untangle(data)

        exponents = get_all_exponents(n, degree)
        # flatten list
        exponents = numpy.array(
            [item for sublist in exponents for item in sublist])

        def evaluate_all_monomials(x):
            return numpy.prod(x[..., None]**exponents.T[:, None], axis=0).T

        flt = numpy.vectorize(float)
        exact_vals = flt(
            [integrate_monomial_over_unit_nball(k) for k in exponents])

        A = evaluate_all_monomials(points.T)

        out = numpy.dot(A, weights)
        out -= exact_vals

        norm_v = numpy.sqrt(numpy.dot(out, out))
        # print()
        print(norm_v)
        # print()
        # for xx in x:
        #     print(f"{xx:.15e}")
        return norm_v
Beispiel #5
0
    def f(x):
        degree = 11
        n = 5
        u = x[0]
        v = x[1]
        w = x[2]
        B = x[3:]
        data = [
            (B[0], z(n)),
            (B[1], fsd(n, (u, 1))),
            (B[2], fsd(n, (v, 1))),
            (B[3], fsd(n, (w, 1))),
            (B[4], fsd(n, (u, 2))),
            (B[5], fsd(n, (v, 2))),
            (B[6], fsd(n, (w, 2))),
            (B[7], fsd(n, (u, 1), (v, 1))),
            (B[8], fsd(n, (u, 1), (w, 1))),
            (B[9], fsd(n, (u, 3))),
            (B[10], fsd(n, (v, 3))),
            (B[11], fsd(n, (w, 3))),
            (B[12], fsd(n, (u, 2), (v, 1))),
        ]
        if n > 3:
            data += [(B[13], fsd(n, (u, 4))), (B[14], fsd(n, (v, 4)))]
        if n > 4:
            data += [(B[15], fsd(n, (u, 5)))]
        points, weights = untangle(data)

        exponents = get_all_exponents(n, degree)
        # flatten list
        exponents = numpy.array(
            [item for sublist in exponents for item in sublist])

        def evaluate_all_monomials(x):
            return numpy.prod(x[..., None]**exponents.T[:, None], axis=0).T

        flt = numpy.vectorize(float)
        exact_vals = flt(
            [integrate_monomial_over_unit_nball(k) for k in exponents])

        A = evaluate_all_monomials(points.T)

        out = numpy.dot(A, weights)
        out -= exact_vals

        norm_v = numpy.sqrt(numpy.dot(out, out))
        print()
        print(norm_v)
        print()
        for xx in x:
            print(f"{xx:.15e}")
        return norm_v