コード例 #1
0
def test1():
    dim = 3
    alpha = 1 / 2
    beta = -1.
    scale = 100.

    A = make_positive_definite_matrix(scale=scale, dim=dim)
    evals, U = get_eigens_for_positive_definite_matrix(A)
    L = np.diag(evals)
    assert np.allclose(A, U.dot(L).dot(U.T)), "Eigendecomposition is wrong"

    b = scale * (np.random.rand(dim) * 2 - 1)
    c = np.random.uniform(low=-scale, high=scale)

    x0 = scale * (np.random.rand(dim) * 2 - 1)
    SD_steps = steepest_descent(A, b, x0)
    print("test1: number of steepest descent steps = {}".format(
        SD_steps.shape[1]))

    x0c = SD_steps[:, 0]
    assert np.allclose(x0, x0c), "x0={} not equal to x0c={}".format(x0, x0c)

    xs = SD_steps[:, -1]
    _, xsc = get_scipy_mins_for_quadratic_form(x0, A, b, c, alpha, beta)
    assert np.allclose(xs, xsc), "xs={} not equal to xsc={}".format(xs, xsc)

    x1 = SD_steps[:, 1]
    x2 = SD_steps[:, 2]

    u = x1 - x0
    v = xs - x1
    w = x2 - x1
    x = xs - x2
    assert_A_orthogonal(A, u, v, atol=1e-5)
    assert_A_orthogonal(A, w, x, atol=1e-5)

    D_k, phi_k = compute_Dk_for_x(u, A, b, c, alpha, beta, evals, U)
    D_q, phi_q = compute_Dk_for_x(v, A, b, c, alpha, beta, evals, U)

    D_q_inv = np.linalg.inv(D_q)
    W = U.dot(D_q_inv).dot(U.T)
    print("D_q_inv={}".format(D_q_inv))
    print("W={}".format(W))
    a = W.dot(u)
    b = W.dot(v)
    assert_orthogonal(a, b)
    print("a={} and b={} are orthogonal".format(a, b))

    SD_steps_2 = steepest_descent(np.eye(dim), b, x)
    print("test1: number of steepest descent steps = {}".format(
        SD_steps_2.shape[1]))
    ax0 = SD_steps_2[:, 0]
    ax1 = SD_steps_2[:, -1]
    print("x2={} xs={} ax0={} ax1={}".format(x2, xs, ax0, ax1))
    w0 = A.dot(ax0)
    w1 = A.dot(ax1)
    w1 = A.dot(ax1 + b)
    print("w0={} w1={}".format(w0, w1))
    return
コード例 #2
0
def figure_GSC_1(debug_print=False):
    A = np_array([4., 1, 1, 15]).reshape(2, 2)
    data1, data2, data3 = [], [], []
    u = np_array([1., 1])
    v = np_array([-0.5, 2])

    w0, w1 = gram_schmidt_conjugation(A, [u, v])
    assert_A_orthogonal(A, w0, w1)

    c = 2.3
    u0 = w0
    u1 = w1 + c * w0

    du0, m1 = draw_vector_R2(u0, color='blue', name=py_text_sub('u', 0))
    du1, m2 = draw_vector_R2(u1, color='blue', name=py_text_sub('u', 1))

    data1.extend(du0)
    data1.extend(du1)

    dw0, m1 = draw_vector_R2(u1, t=w1, color='red')
    dw1, m2 = draw_vector_R2(w1, color='green')
    Au0 = A.dot(u0)
    Au0n = Au0 / norm(Au0)
    dAu0, m1 = draw_vector_R2(3.5 * Au0n,
                              color='grey',
                              name=py_text_sub('Au', 0))

    data2.extend(dw0)
    data2.extend(dw1)
    data2.extend(du0)
    data2.extend(du1)
    data2.extend(dAu0)

    d0, d1 = gram_schmidt_conjugation(A, [u0, u1])
    assert_A_orthogonal(A, d0, d1)

    dd0, m1 = draw_vector_R2(d0, color='blue', name=py_text_sub('d', '(0)'))
    dd1, m2 = draw_vector_R2(d1, color='blue', name=py_text_sub('d', '(1)'))

    data3.extend(dd0)
    data3.extend(dd1)

    m = np_max([m1, m2])
    plot_it_R2_short_3in1(data1,
                          data2,
                          data3,
                          axes_max1=m,
                          axes_max2=m,
                          axes_max3=m,
                          title='GSC.Fig.1',
                          filename='Fig_GSC_1.html')
    return
コード例 #3
0
def figure_ISD_1(debug_print=False):
    steps = steepest_descent(A, b, x0)
    assert np_allclose(steps[:, -1], center.flatten()
                       ), "steepest_descent didn't find the minimum point of f"
    for i in range(steps.shape[1] - 1):
        x_i = steps[:, i]
        r_i = b.flatten() - A.dot(x_i)
        x_ip1 = steps[:, i + 1]
        e_ip1 = x_ip1 - center.flatten()
        assert_A_orthogonal(A, r_i, e_ip1)

    text = [py_text_sub('x', '(0)'), py_text_sub('x', '(1)')]
    t2 = [''] * (steps.shape[1] - 3)
    text.extend(t2)
    text.append(py_text_sub('x', '(*)'))
    textposition = ['middle left', 'top right']
    tp2 = ['top center'] * len(t2)
    textposition.extend(tp2)
    textposition.append('bottom center')

    plotly_data = []

    r0 = b - A.dot(x0)
    alpha0 = innprd(r0, r0) / innprd(r0, A.dot(r0))
    x1 = x0 + alpha0 * r0

    r0_normalized = r0.flatten() / norm(r0)
    d, _ = qfc.prep_vector_data(r0_normalized,
                                center=x0,
                                M=None,
                                color='rgb(0,140,0)',
                                name=py_text_sub('r', '(0)'))
    plotly_data.extend(d)

    e1 = x1 - center
    d, _ = qfc.prep_vector_data(e1,
                                center=center,
                                M=None,
                                color='rgb(0,140,0)',
                                name=py_text_sub('e', '(1)'),
                                textposition='bottom left')
    plotly_data.extend(d)

    draw_concentric_contours(steps,
                             text,
                             textposition,
                             plotly_data=plotly_data,
                             title='ISD.Fig.1',
                             filename='Fig_ISD_1.html',
                             debug_print=debug_print)
    return
コード例 #4
0
def figure_CJDR_5(debug_print=False):
    steps = compute_steps_in_ball_space()
    for i in [0, 1]:
        steps[:, i] = (M.dot(steps[:, i].reshape(2, 1) - center) +
                       center).reshape(2, )

    u = steps[:, 1] - steps[:, 0]
    v = steps[:, 2] - steps[:, 1]
    assert_A_orthogonal(A, u, v)

    text = [
        py_text_sub('x', '(0)'),
        py_text_sub('x', '(1)'),
        py_text_sub('x', '(*)')
    ]
    textposition = ['middle left', 'top center', 'bottom center']

    draw_concentric_contours(steps,
                             text,
                             textposition,
                             title='CJDR.Fig.5',
                             filename='Fig_CJDR_5.html',
                             debug_print=debug_print)
    return
コード例 #5
0
def figure_CJDR_1(debug_print=False):
    r0 = b - A.dot(x0)
    alpha0 = innprd(r0, r0) / innprd(r0, A.dot(r0))
    x1 = x0 + alpha0 * r0
    steps = np_hstack((x0, x1, center))

    text = [
        py_text_sub('x', '(0)'),
        py_text_sub('x', '(1)'),
        py_text_sub('x', '(*)')
    ]
    textposition = ['middle left', 'middle left', 'bottom center']

    plotly_data = []

    r0_normalized = r0.flatten() / norm(r0)
    d, _ = qfc.prep_vector_data(r0_normalized,
                                center=x0,
                                M=None,
                                color='rgb(0,140,0)',
                                name=py_text_sub('r', '(0)'))
    plotly_data.extend(d)

    Ar0 = A.dot(r0_normalized)
    d, _ = qfc.prep_vector_data(Ar0,
                                center=x0,
                                M=None,
                                color='rgb(170,0,0)',
                                name=py_text_sub('Ar', '(0)'))
    plotly_data.extend(d)

    e1 = x1 - center
    d, _ = qfc.prep_vector_data(e1,
                                center=center,
                                M=None,
                                color='rgb(0,140,0)',
                                name=py_text_sub('e', '(1)'),
                                textposition='top right')
    plotly_data.extend(d)

    e1_normalized = e1.flatten() / norm(e1)
    d, _ = qfc.prep_vector_data(e1_normalized,
                                center=x0,
                                M=None,
                                color='rgb(170,0,0)',
                                name=py_text_sub('e', '(1)'))
    plotly_data.extend(d)

    assert_A_orthogonal(A, r0, -e1)
    d, _ = qfc.prep_vector_data(-e1_normalized,
                                center=x0,
                                M=None,
                                color='rgb(170,0,0)',
                                name=py_text_sub('-e', '(1)'))
    plotly_data.extend(d)

    draw_concentric_contours(steps,
                             text,
                             textposition,
                             plotly_data=plotly_data,
                             title='CJDR.Fig.1',
                             filename='Fig_CJDR_1.html',
                             steps_mode='text, markers',
                             debug_print=debug_print)
    return