Esempio n. 1
0
def inner_product_embedding(C, ndim):
    n = C.shape[0]
    if ndim > n:
        msg = 'Number of points: %s  Dimensions: %s' % (n, ndim) 
        raise ValueError(msg)

    eigvals = (n - ndim, n - 1)
    print n, eigvals
    S, V = eigh(C, eigvals=eigvals)

    assert S[0] <= S[1]  # eigh returns in ascending order 

    if np.any(S < 0):
        msg = 'The cosine matrix singular values are not all positive: \n'
        msg += formatm('S', S) 
        msg += 'I assume it is rounding error and approximate with:\n'
        S[S < 0] = 0
        msg += formatm('S\'', S)
        logger.warning(msg)

    assert V.shape == (n, ndim)
    assert S.shape == (ndim,)
    #    check_multiple([('K', ndim),
    #                    ('array[NxK]', V),
    #                    ('array[K]', S)])
    coords = V.T
    for i in range(ndim):
        assert S[i] >= 0
        coords[i, :] = coords[i, :] * np.sqrt(S[i])
    return coords
Esempio n. 2
0
def inner_product_embedding(C, ndim):
    n = C.shape[0]
    if ndim > n:
        msg = 'Number of points: %s  Dimensions: %s' % (n, ndim)
        raise ValueError(msg)

    eigvals = (n - ndim, n - 1)
    print n, eigvals
    S, V = eigh(C, eigvals=eigvals)

    assert S[0] <= S[1]  # eigh returns in ascending order

    if np.any(S < 0):
        msg = 'The cosine matrix singular values are not all positive: \n'
        msg += formatm('S', S)
        msg += 'I assume it is rounding error and approximate with:\n'
        S[S < 0] = 0
        msg += formatm('S\'', S)
        logger.warning(msg)

    assert V.shape == (n, ndim)
    assert S.shape == (ndim, )
    #    check_multiple([('K', ndim),
    #                    ('array[NxK]', V),
    #                    ('array[K]', S)])
    coords = V.T
    for i in range(ndim):
        assert S[i] >= 0
        coords[i, :] = coords[i, :] * np.sqrt(S[i])
    return coords
Esempio n. 3
0
def check_logmap3(M, a, b):
    d = M.distance(a, b)
    base, vel = M.logmap(a, b)
    ratios = [0.5, 0.3]
    for ratio in ratios:
        b2 = M.expmap((base, vel * ratio))
        d2 = M.distance(a, b2)
        msg = "Checking that distance is consistent with logmap/expmap"
        msg += formatm('a', a, 'b', b, 'd', d)
        msg += formatm('base', base, 'vel', vel)
        msg += formatm('b2', b2)
        msg += formatm('d2', d2)
        check_allclose(d * ratio, d2, atol=1e-7, err_msg=msg)
Esempio n. 4
0
def check_logmap3(M, a, b):
    d = M.distance(a, b)
    base, vel = M.logmap(a, b)
    ratios = [0.5, 0.3]
    for ratio in ratios:
        b2 = M.expmap((base, vel * ratio))
        d2 = M.distance(a, b2)
        msg = "Checking that distance is consistent with logmap/expmap"
        msg += formatm('a', a, 'b', b, 'd', d)
        msg += formatm('base', base, 'vel', vel)
        msg += formatm('b2', b2)
        msg += formatm('d2', d2)
        check_allclose(d * ratio, d2, atol=1e-7, err_msg=msg)
Esempio n. 5
0
def expect_shape(name, vector, shape):
    if vector.shape != shape:
        msg = ('Expected shape %s for %r but found %s' % 
               (shape, name, vector.shape))
        if vector.size < 100:
            msg += '\n' + formatm(vector) 
        raise ValueError(msg)
Esempio n. 6
0
def check_logmap1(M, a, b):
    ''' This is a test that:
    
            Exp_a( Log_a(b) ) = b
            
    '''
    check_logmap1.description = ('%s: Checking that logmap/expmap work. '
                                 '(a: %s, b: %s)' %
                                 (M, M.friendly(a), M.friendly(b)))

    bv = M.logmap(a, b)
    b2 = M.expmap(bv)

    msg = ""
    msg += formatm('a', a, 'b', b)
    msg += formatm('M.logmap(a,b) base = ', bv[0], 'vel', bv[1])
    msg += formatm('b2', b2)
    check_allclose(M.distance(b, b2), 0, atol=1e-7, err_msg=msg)
Esempio n. 7
0
def check_logmap1(M, a, b):
    ''' This is a test that:
    
            Exp_a( Log_a(b) ) = b
            
    '''
    check_logmap1.description = (
        '%s: Checking that logmap/expmap work. '
        '(a: %s, b: %s)'
        % (M, M.friendly(a), M.friendly(b)))

    bv = M.logmap(a, b)
    b2 = M.expmap(bv)
    
    msg = ""
    msg += formatm('a', a, 'b', b)
    msg += formatm('M.logmap(a,b) base = ', bv[0], 'vel', bv[1])
    msg += formatm('b2', b2)
    check_allclose(M.distance(b, b2), 0, atol=1e-7, err_msg=msg)
Esempio n. 8
0
 def __str__(self):
     try:
         s = ''
         if self.context is not None:
             s += '%s\n' % self.context
         s += ('%s: The point does not belong here:\n%s' %
               (self.M, formatm('p', self.point)))
         s += self.e
         return s
     except Exception as e:
         return "(%s) %s: %s" % (e, self.M, self.point)
Esempio n. 9
0
 def __str__(self):
     try:
         s = ''
         if self.context is not None:
             s += '%s\n' % self.context
         s += ('%s: The point does not belong here:\n%s' %
               (self.M, formatm('p', self.point)))
         s += self.e
         return s
     except Exception as e:
         return "(%s) %s: %s" % (e, self.M, self.point)
Esempio n. 10
0
 def assert_close(self, a, b, atol=1e-8, msg=None):
     ''' 
         Asserts that two points on the manifold are close to the given
         tolerance. 
     '''
     distance = self.distance(a, b)
     if msg is None:
         msg = ""
     if distance > atol:
         msg += "\nThe two points should be the same:\n"
         msg += "- a: %s\n" % self.friendly(a)
         msg += "- b: %s\n" % self.friendly(b)
         msg += formatm('a', a, 'b', b)
         check_allclose(distance, 0, atol=atol, err_msg=msg)
     return distance