예제 #1
0
def test_covariance_deriv():
    # now test full:
    # do covariant derivatives
    # check that it works, preseving everything
    n, d = (5, 3)
    alpha = randint(1, 10, 2) * .1
    beta = randint(1, 10, 2)[0] * .01
    man = ComplexPositiveSemidefinite(n, d, alpha=alpha, beta=beta)
    S = man.rand()

    aa = crandn(n * d, n * d)
    cc = crandn(d * d, d * d)
    icpt = man._rand_ambient()

    def omg_func(S):
        csp = hsym((cc @ S.P.reshape(-1)).reshape(d, d))
        return psd_ambient(
            (aa @ S.Y.reshape(-1) + icpt.tY.reshape(-1)).reshape(n, d),
            csp + icpt.tP)

    xi = man.randvec(S)
    egrad = omg_func(S)
    ecsp = hsym((cc @ xi.tP.reshape(-1)).reshape(d, d))
    ehess = psd_ambient((aa @ xi.tY.reshape(-1)).reshape(n, d), ecsp)

    val1 = man.ehess2rhess(S, egrad, ehess, xi)

    def rgrad_func(W):
        return man.proj_g_inv(W, omg_func(W))

    if False:
        first = ehess
        a = man.J_g_inv(S, egrad)
        rgrad = man.proj_g_inv(S, egrad)
        second = man.D_g(S, xi, man.g_inv(S, egrad)).scalar_mul(-1)
        aout = man.solve_J_g_inv_Jst(S, a)
        third = man.proj(S, man.D_g_inv_Jst(S, xi, aout)).scalar_mul(-1)
        fourth = man.christoffel_form(S, xi, rgrad)
        val1a1 = man.proj_g_inv(S, first + second + fourth) + third
        print(check_zero(man._vec(val1 - val1a1)))
    elif True:
        d_xi_rgrad = num_deriv_amb(man, S, xi, rgrad_func)
        rgrad = man.proj_g_inv(S, egrad)
        fourth = man.christoffel_form(S, xi, rgrad)
        val1a = man.proj(S, d_xi_rgrad) + man.proj_g_inv(S, fourth)
        print(check_zero(man._vec(val1 - val1a)))

    # nabla_v_xi, dxi, cxxi
    val2a, _, _ = calc_covar_numeric(man, S, xi, omg_func)
    val2, _, _ = calc_covar_numeric(man, S, xi, rgrad_func)
    # val2_p = project(prj, val2)
    val2_p = man.proj(S, val2)
    # print(val1)
    # print(val2_p)
    print(check_zero(man._vec(val1) - man._vec(val2_p)))
    if True:
        H = xi
        valrangeA_ = ehess + man.g(S, man.D_proj(
            S, H, man.g_inv(S, egrad))) - man.D_g(
                S, H, man.g_inv(S, egrad)) +\
            man.christoffel_form(S, H, man.proj_g_inv(S, egrad))
        valrangeB = man.proj_g_inv(S, valrangeA_)
    valrange = man.ehess2rhess_alt(S, egrad, ehess, xi)
    print(check_zero(man._vec(valrange) - man._vec(val2_p)))
    print(check_zero(man._vec(valrange) - man._vec(val1)))
    print(check_zero(man._vec(valrange) - man._vec(valrangeB)))
예제 #2
0
def test_all_projections():
    alpha = randint(1, 10, 2) * .1
    beta = randint(1, 10, 1)[0] * .02
    n = 5
    d = 3
    man = ComplexPositiveSemidefinite(n, d, alpha=alpha, beta=beta)
    print(man)
    S = man.rand()

    test_inner(man, S)
    test_J(man, S)

    # now check metric, Jst etc
    # check Jst: vectorize the operator J then compare Jst with jmat.T
    jmat = make_j_mat(man, S)
    test_Jst(man, S, jmat)
    ginv_mat = make_g_inv_mat(man, S)
    ee = man._rand_ambient()
    g1 = man._unvec(ginv_mat @ man._vec(ee))
    print(check_zero(man.g_inv(S, ee).tP - g1.tP))
    print(check_zero(man.g_inv(S, ee).tY - g1.tY))
    # test g_inv_Jst
    for ii in range(10):
        a = man._rand_range_J()
        avec = man._vec_range_J(a)
        jtout = ginv_mat @ jmat.T @ avec
        jtout2 = man._vec(man.g_inv_Jst(S, a))
        diff = check_zero(jtout - jtout2)
        print(diff)
    # test projection
    test_projection(man, S)
    # now diff projection

    for i in range(1):
        e = man._rand_ambient()
        S1 = man.rand()
        xi = man.randvec(S1)
        dlt = 1e-7
        S2 = psd_point(S1.Y + dlt * xi.tY, S1.P + dlt * xi.tP)

        # S = psd_point(S1.Y, S1.P)
        d1P = (man.proj(S2, e).tP - man.proj(S1, e).tP) / dlt
        d1Y = (man.proj(S2, e).tY - man.proj(S1, e).tY) / dlt
        d2 = man.D_proj(S1, xi, e)
        print(check_zero(d1P - d2.tP) + check_zero(d1Y - d2.tY))

    for i in range(10):
        a = man._rand_range_J()
        eta = man._rand_ambient()
        print(man.base_inner_ambient(eta, man.Jst(S, a)))
        print((trace((eta.tP.T.conjugate() - eta.tP) @ a['P'] +
                     (man.alpha[1] * eta.tY.T.conjugate() @ S.Y + man.beta *
                      (S.Pinv @ eta.tP.T.conjugate() -
                       eta.tP.T.conjugate() @ S.Pinv)) @ a['YP'])).real)

        print((trace(2 * eta.tP.T.conjugate() @ a['P']) + trace(
            (man.alpha[1] * eta.tY.T.conjugate() @ S.Y + man.beta *
             (S.Pinv @ eta.tP.T.conjugate() - eta.tP.T.conjugate() @ S.Pinv))
            @ a['YP'])).real)

        print((trace(
            eta.tP.T.conjugate() @ (2 * a['P'] + man.beta *
                                    (a['YP'] @ S.Pinv - S.Pinv @ a['YP']))) +
               trace(eta.tY.T.conjugate() @ (man.alpha[1] * S.Y @ a['YP']))
               ).real)

        print(man.base_inner_E_J(man.J(S, eta), a))
        print((
            trace((eta.tP - eta.tP.T.conjugate()).T.conjugate() @ a['P'] +
                  (man.alpha[1] * S.Y.T.conjugate() @ eta.tY + man.beta *
                   (eta.tP @ S.Pinv - S.Pinv @ eta.tP)).T.conjugate() @ a['YP']
                  )).real)

    for i in range(10):
        a = man._rand_range_J()
        beta = man.beta
        alf = man.alpha
        anew1 = man.J(S, man.g_inv_Jst(S, a))

        anew = {}
        saYP = a['YP'] + a['YP'].T.conjugate()
        anew['P'] = 4 / beta * S.P @ a['P'] @ S.P + S.P @ saYP - saYP @ S.P
        anew['YP'] = alf[1] * a['YP'] + beta * (
            ((2 / man.beta) * S.P @ a['P'] @ S.P + S.P @ a['YP'] -
             a['YP'] @ S.P) @ S.Pinv - S.Pinv @ (
                 (2 / man.beta) * S.P @ a['P'] @ S.P + S.P @ a['YP'] -
                 a['YP'] @ S.P))

        anew['YP'] = alf[1] * a['YP'] + (
            (2 * S.P @ a['P'] + beta * S.P @ a['YP'] @ S.Pinv - beta * a['YP'])
            - (2 * a['P'] @ S.P + beta * a['YP'] -
               beta * S.Pinv @ a['YP'] @ S.P))

        anew['YP'] = (alf[1] - 2 * beta) * a['YP'] + (
            (2 * S.P @ a['P'] + beta * S.P @ a['YP'] @ S.Pinv) -
            (2 * a['P'] @ S.P - beta * S.Pinv @ a['YP'] @ S.P))

        anew['YP'] = (alf[1] - 2 * beta) * a['YP'] + (
            (2 * S.P @ a['P'] - 2 * a['P'] @ S.P +
             beta * S.P @ a['YP'] @ S.Pinv + beta * S.Pinv @ a['YP'] @ S.P))
        print(check_zero(man._vec_range_J(anew1) - man._vec_range_J(anew)))

    for i in range(10):
        a = man._rand_range_J()
        b1 = man.J(S, man.g_inv_Jst(S, a))
        b2 = man.J_g_inv_Jst(S, a)
        print(check_zero(man._vec_range_J(b1) - man._vec_range_J(b2)))
        a1 = man.solve_J_g_inv_Jst(S, b1)
        print(check_zero(man._vec_range_J(a) - man._vec_range_J(a1)))

    for ii in range(10):
        E = man._rand_ambient()
        a2 = man.J_g_inv(S, E)
        a1 = man.J(S, man.g_inv(S, E))
        print(check_zero(man._vec_range_J(a1) - man._vec_range_J(a2)))

    for i in range(20):
        Uran = man._rand_ambient()
        Upr = man.proj(S, man.g_inv(S, Uran))
        Upr2 = man.proj_g_inv(S, Uran)
        print(check_zero(man._vec(Upr) - man._vec(Upr2)))

    for ii in range(10):
        a = man._rand_range_J()
        xi = man.randvec(S)
        jtout2 = man.Jst(S, a)
        dlt = 1e-7
        Snew = psd_point(S.Y + dlt * xi.tY, S.P + dlt * xi.tP)
        jtout2a = man.Jst(Snew, a)
        d1 = (jtout2a - jtout2).scalar_mul(1 / dlt)
        d2 = man.D_Jst(S, xi, a)
        print(check_zero(man._vec(d2) - man._vec(d1)))

    for ii in range(10):
        S1 = man.rand()
        eta = man._rand_ambient()
        xi = man.randvec(S1)
        a1 = man.J(S1, eta)
        dlt = 1e-8
        Snew = psd_point(S1.Y + dlt * xi.tY, S1.P + dlt * xi.tP)
        a2 = man.J(Snew, eta)
        d1 = {
            'P': (a2['P'] - a1['P']) / dlt,
            'YP': (a2['YP'] - a1['YP']) / dlt
        }
        d2 = man.D_J(S1, xi, eta)
        print(check_zero(man._vec_range_J(d2) - man._vec_range_J(d1)))

    # derives metrics
    for ii in range(10):
        S1 = man.rand()
        xi = man.randvec(S1)
        omg1 = man._rand_ambient()
        omg2 = man._rand_ambient()
        dlt = 1e-7
        S2 = psd_point(S1.Y + dlt * xi.tY, S1.P + dlt * xi.tP)
        p1 = man.inner(S1, omg1, omg2)
        p2 = man.inner(S2, omg1, omg2)
        der1 = (p2 - p1) / dlt
        der2 = man.base_inner_ambient(man.D_g(S1, xi, omg2), omg1)
        print(der1 - der2)
        if np.abs(der1 - der2) > 1e-4:
            print(der1, der2)
            break

    # cross term for christofel
    for i in range(10):
        S1 = man.rand()
        xi = man.randvec(S1)
        eta1 = man.randvec(S1)
        eta2 = man.randvec(S1)
        dr1 = man.D_g(S1, xi, eta1)
        x12 = man.contract_D_g(S1, eta1, eta2)

        p1 = man.base_inner_ambient(dr1, eta2)
        p2 = man.base_inner_ambient(x12, xi)
        print(p1, p2, p1 - p2)

    # now test christofel:
    # two things: symmetric on vector fields
    # and christofel relation
    # in the case metric
    for i in range(10):
        S1 = man.rand()
        xi = man.randvec(S1)
        eta1 = man.randvec(S1)
        eta2 = man.randvec(S1)
        p1 = man.proj_g_inv(S1, man.christoffel_form(S1, xi, eta1))
        p2 = man.proj_g_inv(S1, man.christoffel_form(S1, eta1, xi))
        print(check_zero(man._vec(p1) - man._vec(p2)))
        v1 = man.base_inner_ambient(man.christoffel_form(S1, eta1, eta2), xi)
        v2 = man.base_inner_ambient(man.D_g(S1, eta1, eta2), xi)
        v3 = man.base_inner_ambient(man.D_g(S1, eta2, eta1), xi)
        v4 = man.base_inner_ambient(man.D_g(S1, xi, eta1), eta2)
        print(v1, 0.5 * (v2 + v3 - v4), v1 - 0.5 * (v2 + v3 - v4))