예제 #1
0
def error6(N):
    cN = F(1, 6) + F(10, 36 * N)
    coeffs = (1, -5, 25, -125, 625, -3125)
    p_exact = de_casteljau.basic(cN, coeffs)
    p_computed = de_casteljau.basic(float(cN), coeffs)
    err_exact = abs((F(p_computed) - p_exact) / p_exact)
    return float(err_exact)
예제 #2
0
def error5(N):
    bN = F(1, 5) + F(8, 25 * N)
    coeffs = (1, -4, 16, -64, 256, -1024)
    p_exact = de_casteljau.basic(bN, coeffs)
    p_computed = de_casteljau.basic(float(bN), coeffs)
    err_exact = abs((F(p_computed) - p_exact) / p_exact)
    return float(err_exact)
예제 #3
0
def error4(N):
    aN = F(1, 4) + F(6, 16 * N)
    coeffs = (1, -3, 9, -27, 81, -243)
    p_exact = de_casteljau.basic(aN, coeffs)
    p_computed = de_casteljau.basic(float(aN), coeffs)
    err_exact = abs((F(p_computed) - p_exact) / p_exact)
    return float(err_exact)
예제 #4
0
def count_de_casteljau_basic():
    print("de_casteljau.basic() ((3n^2 + 3n + 2) / 2 = 3 T_n + 1):")
    for degree in range(1, 5 + 1):
        parent = operation_count.Computation()
        x = operation_count.Float(0.25, parent)
        coeffs = tuple(
            operation_count.Float((-1.0)**k, parent)
            for k in range(degree + 1))
        p = de_casteljau.basic(x, coeffs)
        assert p.value == 0.5**degree
        assert parent.count == 3 * (degree * (degree + 1) // 2) + 1
        print("  degree {}:     {}".format(degree, parent.display))
예제 #5
0
def _main():
    s_vals = np.linspace(ROOT - DELTA_S, ROOT + DELTA_S, NUM_POINTS)

    horner1 = []
    de_casteljau1 = []

    for s in s_vals:
        horner1.append(horner.basic(s, POLY_COEFFS))
        de_casteljau1.append(de_casteljau.basic(s, BEZIER_COEFFS))

    figure, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
    ax1.plot(s_vals, horner1)
    ax2.plot(s_vals, de_casteljau1)

    # Since ``sharex=True``, ticks only need to be set once.
    ax1.set_xticks([
        ROOT - DELTA_S,
        ROOT - 0.5 * DELTA_S,
        ROOT,
        ROOT + 0.5 * DELTA_S,
        ROOT + DELTA_S,
    ])

    ax1.set_title(r"$\mathtt{Horner}$", fontsize=plot_utils.TEXT_SIZE)
    ax2.set_title(r"$\mathtt{DeCasteljau}$", fontsize=plot_utils.TEXT_SIZE)

    ax1.tick_params(labelsize=plot_utils.TICK_SIZE, which="both")
    ax2.tick_params(labelsize=plot_utils.TICK_SIZE, which="both")

    filename = "horner_inferior.pdf"
    # NOTE: These are (intended to be) the same settings used in
    #       ``compensated_insufficient.py``, so they should probably be
    #       kept in sync.
    figure.set_size_inches(6.0, 2.9)
    figure.subplots_adjust(left=0.07,
                           bottom=0.13,
                           right=0.97,
                           top=0.92,
                           wspace=0.13,
                           hspace=0.20)
    path = plot_utils.get_path("k-compensated", filename)
    figure.savefig(path)
    print("Saved {}".format(filename))
    plt.close(figure)
def main(filename=None):
    s_vals = np.linspace(ROOT - DELTA_S, ROOT + DELTA_S, NUM_POINTS)

    horner1 = []
    de_casteljau1 = []

    for s in s_vals:
        horner1.append(horner.basic(s, POLY_COEFFS))
        de_casteljau1.append(de_casteljau.basic(s, BEZIER_COEFFS))

    figure, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
    ax1.plot(s_vals, horner1)
    ax2.plot(s_vals, de_casteljau1)

    # Since ``sharex=True``, ticks only need to be set once.
    ax1.set_xticks([
        ROOT - DELTA_S,
        ROOT - 0.5 * DELTA_S,
        ROOT,
        ROOT + 0.5 * DELTA_S,
        ROOT + DELTA_S,
    ])

    ax1.set_title(r"$\mathtt{Horner}$")
    ax2.set_title(r"$\mathtt{DeCasteljau}$")

    if filename is None:
        plt.show()
    else:
        figure.set_size_inches(9.87, 4.8)
        figure.subplots_adjust(
            left=0.06,
            bottom=0.12,
            right=0.97,
            top=0.92,
            wspace=0.13,
            hspace=0.20,
        )
        path = plot_utils.get_path(filename)
        figure.savefig(path, bbox_inches="tight")
        print("Saved {}".format(filename))
        plt.close(figure)
예제 #7
0
def standard_residual(s, coeffs1, t, coeffs2):
    x1 = de_casteljau.basic(s, coeffs1[0, :])
    y1 = de_casteljau.basic(s, coeffs1[1, :])
    x2 = de_casteljau.basic(t, coeffs2[0, :])
    y2 = de_casteljau.basic(t, coeffs2[1, :])
    return np.array([[x1 - x2], [y1 - y2]])
예제 #8
0
def do_plot(ax, s_vals, coeffs, title, add_legend=False, add_ylabel=False):
    bounds = []
    rel_errors1 = []
    rel_errors2 = []
    n = len(coeffs) - 1
    gamma3n = utils.gamma(3 * n)
    exact_coeffs = tuple(map(F, coeffs))
    abs_coeffs = tuple(map(abs, exact_coeffs))

    for s in s_vals:
        exact_s = F(s)
        exact_p = de_casteljau.basic(exact_s, exact_coeffs)
        if not isinstance(exact_p, F):
            raise TypeError(exact_p)

        exact_p_tilde = de_casteljau.basic(exact_s, abs_coeffs)
        a_priori_bound = gamma3n * exact_p_tilde / abs(exact_p)
        bounds.append(float(a_priori_bound))

        error1 = F(de_casteljau.basic(s, coeffs)) - exact_p
        rel_errors1.append(float(abs(error1 / exact_p)))

        error2 = F(vs_method.basic(s, coeffs)) - exact_p
        rel_errors2.append(float(abs(error2 / exact_p)))

    size = 5
    ax.semilogy(
        s_vals,
        bounds,
        marker="o",
        markersize=size,
        linestyle=":",
        alpha=0.5,
        color="black",
        label="Bound",
    )
    ax.semilogy(
        s_vals,
        rel_errors1,
        marker="o",
        markersize=size,
        label=r"$\mathtt{DeCasteljau}$",
    )
    ax.semilogy(
        s_vals,
        rel_errors2,
        marker="s",
        markersize=size,
        label=r"$\mathtt{VS}$",
    )
    if add_legend:
        ax.legend(
            loc="lower center",
            framealpha=1.0,
            frameon=True,
            markerscale=1.25,
            fontsize=16,
        )
    # Label the axes.
    ax.set_xlabel("$s$", fontsize=20)
    if add_ylabel:
        ax.set_ylabel("Relative Forward Error", fontsize=20)
    # Set the axis title.
    ax.set_title(title, fontsize=20)