示例#1
0
    def __test(d, m):
        X = numpy.random.randn(2 * d, m)
        V = _cdl.columns_to_vector(X)
        X_back = _cdl.vector_to_columns(V, m)

        assert EQ(X, X_back)
        pass
示例#2
0
    def __test(d, m):
        # Build a random dictionary in diagonal form
        X       = numpy.random.randn(2 * d, m)

        # Normalize the dictionary and convert back to columns
        X_norm  = _cdl.diags_to_columns(_cdl.normalize_dictionary(_cdl.columns_to_diags(X)))

        
        # Rescale the normalized dictionary to have some inside, some outside
        R       = 2.0**numpy.linspace(-4, 4, m)
        X_scale = X_norm * R

        # Vectorize
        V_scale = _cdl.columns_to_vector(X_scale)

        # Project
        V_proj  = _cdl.proj_l2_ball(V_scale, m)

        # Rearrange the projected matrix into columns
        X_proj  = _cdl.vector_to_columns(V_proj, m)


        # Compute norms
        X_scale_norms   = numpy.sum(X_scale**2, axis=0)**0.5
        X_proj_norms    = numpy.sum(X_proj**2, axis=0)**0.5

        Xdot            = numpy.sum(X_scale * X_proj, axis=0)

        for k in range(m):
            # 1. verify norm is at most 1
            #       allow some fudge factor here for numerical instability
            assert X_proj_norms[k] <= 1.0 + 1e-10

            # 2. verify that points with R < 1 were untouched
            assert R[k] > 1.0 or EQ(X_proj[:, k], X_scale[:, k])

            # 3. verify that points with R >= 1.0 preserve direction
            assert R[k] < 1.0 or EQ(Xdot[k], X_scale_norms[k])

        pass