def setUp(self):
        gs.random.seed(1234)

        self.n = 3
        self.k = 2
        self.space = Grassmannian(self.n, self.k)
        self.metric = GrassmannianCanonicalMetric(self.n, self.k)
Example #2
0
class TestGrassmannianMethods(geomstats.tests.TestCase):
    def setUp(self):
        gs.random.seed(1234)

        self.n = 3
        self.k = 2
        self.space = Grassmannian(self.n, self.k)
        self.metric = GrassmannianCanonicalMetric(self.n, self.k)

    @geomstats.tests.np_only
    def test_exp_np(self):
        result = self.metric.exp(pi_2 * r_y, gs.array([p_xy, p_yz]))
        expected = gs.array([p_yz, p_xy])
        self.assertAllClose(result, expected)

        result = self.metric.exp(pi_2 * gs.array([r_y, r_z]),
                                 gs.array([p_xy, p_yz]))
        expected = gs.array([p_yz, p_xz])
        self.assertAllClose(result, expected)
Example #3
0
class TestGrassmannianMethods(geomstats.tests.TestCase):
    def setUp(self):
        gs.random.seed(1234)

        self.n = 3
        self.k = 2
        self.space = Grassmannian(self.n, self.k)
        self.metric = GrassmannianCanonicalMetric(self.n, self.k)

    @geomstats.tests.np_only
    def test_exp_np(self):
        result = self.metric.exp(pi_2 * r_y, gs.array([p_xy, p_yz]))
        expected = gs.array([p_yz, p_xy])
        self.assertAllClose(result, expected)

        result = self.metric.exp(pi_2 * gs.array([r_y, r_z]),
                                 gs.array([p_xy, p_yz]))
        expected = gs.array([p_yz, p_xz])
        self.assertAllClose(result, expected)

    @geomstats.tests.np_only
    def test_log(self):
        result = self.metric.log(self.metric.exp(pi_4 * r_y, p_xy), p_xy)
        expected = pi_4 * r_y
        self.assertAllClose(result, expected)

    @geomstats.tests.np_only
    def test_log_vectorized(self):
        tangent_vecs = pi_4 * gs.array([r_y, r_z])
        base_points = gs.array([p_xy, p_xz])
        points = self.metric.exp(tangent_vecs, base_points)
        result = self.metric.log(points, base_points)
        expected = tangent_vecs
        self.assertAllClose(result, expected)
def get_grassmannian_metrics(grassmannian, point_a, point_b):
    assert isinstance(grassmannian, Grassmannian)
    assert grassmannian.belongs(point_a)
    assert grassmannian.belongs(point_b)
    results = {}

    metric = GrassmannianChordalFNormMetric(grassmannian.n, grassmannian.k)
    results["chordal_distance"] = metric.dist(point_a, point_b)

    metric = GrassmannianSubspaceAngleMetric(grassmannian.n, grassmannian.k)
    results["subspace_angle"] = metric.dist(point_a, point_b)

    metric = GrassmannianCanonicalMetric(grassmannian.n, grassmannian.k)
    results["canonical_distance"] = metric.dist(point_a, point_b)

    return results
Example #5
0
class TestGrassmannian(geomstats.tests.TestCase):
    def setUp(self):
        gs.random.seed(1234)

        self.n = 3
        self.k = 2
        self.space = Grassmannian(self.n, self.k)
        self.metric = GrassmannianCanonicalMetric(self.n, self.k)

    def test_exp_np(self):
        vec = Matrices.bracket(pi_2 * r_y, gs.array([p_xy, p_yz]))
        result = self.metric.exp(vec, gs.array([p_xy, p_yz]))
        expected = gs.array([p_yz, p_xy])
        self.assertAllClose(result, expected)

        vec = Matrices.bracket(pi_2 * gs.array([r_y, r_z]),
                               gs.array([p_xy, p_yz]))
        result = self.metric.exp(vec, gs.array([p_xy, p_yz]))
        expected = gs.array([p_yz, p_xz])
        self.assertAllClose(result, expected)

    @geomstats.tests.np_and_tf_only
    def test_log(self):
        expected = Matrices.bracket(pi_4 * r_y, p_xy)
        result = self.metric.log(self.metric.exp(expected, p_xy), p_xy)
        self.assertTrue(self.space.is_tangent(result, p_xy))
        self.assertAllClose(result, expected)

    @geomstats.tests.np_and_tf_only
    def test_log_vectorized(self):
        tangent_vecs = pi_4 * gs.array([r_y, r_z])
        base_points = gs.array([p_xy, p_xz])
        points = self.metric.exp(tangent_vecs, base_points)
        result = self.metric.log(points, base_points)
        expected = tangent_vecs
        self.assertAllClose(result, expected)

    def test_belongs(self):
        point = p_xy
        result = self.space.belongs(point)
        self.assertTrue(result)

        point = gs.array([p_yz, p_xz])
        result = self.space.belongs(point)
        self.assertTrue(gs.all(result))

        not_a_point = gs.random.rand(3, 2)
        self.assertRaises(ValueError, self.space.belongs, not_a_point)

        not_a_point = gs.random.rand(3, 3)
        result = self.space.belongs(not_a_point)
        self.assertTrue(~result)

        point = gs.array([p_xy, not_a_point])
        result = self.space.belongs(point)
        expected = gs.array([True, False])
        self.assertAllClose(result, expected)

    def test_random_and_belongs(self):
        point = self.space.random_uniform()
        result = self.space.belongs(point)
        self.assertTrue(result)

        expected = (self.n, ) * 2
        result_shape = point.shape
        self.assertAllClose(result_shape, expected)

        n_samples = 5
        points = self.space.random_uniform(n_samples)
        result = gs.all(self.space.belongs(points))
        self.assertTrue(result)

        expected = (n_samples, ) + (self.n, ) * 2
        result_shape = points.shape
        self.assertAllClose(result_shape, expected)

    def test_is_to_tangent(self):
        base_point = self.space.random_uniform()
        vector = gs.random.rand(self.n, self.n)
        tangent_vec = self.space.to_tangent(vector, base_point)
        result = self.space.is_tangent(tangent_vec, base_point)
        self.assertTrue(result)

        reprojected = self.space.to_tangent(tangent_vec, base_point)
        self.assertAllClose(tangent_vec, reprojected)