Ejemplo n.º 1
0
    def test_integrated_parallel_transport(self, group, n, n_samples):
        metric = InvariantMetric(group=group)
        point = group.identity
        tan_b = Matrices(n + 1, n + 1).random_point(n_samples)
        tan_b = group.to_tangent(tan_b)

        # use a vector orthonormal to tan_b
        tan_a = Matrices(n + 1, n + 1).random_point(n_samples)
        tan_a = group.to_tangent(tan_a)
        coef = metric.inner_product(tan_a, tan_b) / metric.squared_norm(tan_b)
        tan_a -= gs.einsum("...,...ij->...ij", coef, tan_b)
        tan_b = gs.einsum("...ij,...->...ij", tan_b,
                          1.0 / metric.norm(tan_b, base_point=point))
        tan_a = gs.einsum("...ij,...->...ij", tan_a,
                          1.0 / metric.norm(tan_a, base_point=point))

        expected = group.left_canonical_metric.parallel_transport(
            tan_a, point, tan_b)
        result, end_point_result = metric.parallel_transport(
            tan_a, point, tan_b, n_steps=20, step="rk4", return_endpoint=True)
        expected_end_point = metric.exp(tan_b, point, n_steps=20)

        self.assertAllClose(end_point_result,
                            expected_end_point,
                            atol=gs.atol * 1000)
        self.assertAllClose(expected, result, atol=gs.atol * 1000)
Ejemplo n.º 2
0
 def flatten_reshape_test_data(self):
     random_data = [
         dict(m=1, n=1, mat=Matrices(1, 1).random_point(10000)),
         dict(m=2, n=2, mat=Matrices(2, 2).random_point(1000)),
         dict(m=2, n=10, mat=Matrices(2, 10).random_point(100)),
         dict(m=20, n=10, mat=Matrices(20, 10).random_point(100)),
     ]
     return self.generate_tests([], random_data)
Ejemplo n.º 3
0
    def setUp(self):
        gs.random.seed(1234)

        self.m = 2
        self.n = 3
        self.space = Matrices(m=self.n, n=self.n)
        self.space_nonsquare = Matrices(m=self.m, n=self.n)
        self.metric = self.space.metric
        self.n_samples = 2
Ejemplo n.º 4
0
 def __init__(self, n, positive_det=False, **kwargs):
     if "dim" not in kwargs.keys():
         kwargs["dim"] = n**2
     super(GeneralLinear, self).__init__(ambient_space=Matrices(n, n),
                                         n=n,
                                         **kwargs)
     self.positive_det = positive_det
Ejemplo n.º 5
0
 def __init__(self, m, n, **kwargs):
     if "dim" not in kwargs.keys():
         kwargs["dim"] = m * n
     super(FullRankMatrices, self).__init__(ambient_space=Matrices(m, n),
                                            metric=MatricesMetric(m, n),
                                            **kwargs)
     self.rank = min(m, n)
Ejemplo n.º 6
0
 def setUp(self):
     self.dimension = 4
     self.dt = 0.1
     self.euclidean = Euclidean(self.dimension)
     self.matrices = Matrices(self.dimension, self.dimension)
     self.intercept = self.euclidean.random_uniform(1)
     self.slope = Matrices.make_symmetric(self.matrices.random_uniform(1))
Ejemplo n.º 7
0
    def to_networkx_test_data(self):
        adj = Matrices(3, 3).random_point()
        point = self._Point(adj)

        smoke_data = [dict(point=point)]

        return self.generate_tests(smoke_data)
Ejemplo n.º 8
0
 def setUp(self):
     self.dimension = 4
     self.dt = 0.1
     self.euclidean = Euclidean(self.dimension)
     self.matrices = Matrices(self.dimension, self.dimension)
     self.intercept = self.euclidean.random_point()
     self.slope = Matrices.to_symmetric(self.matrices.random_point())
Ejemplo n.º 9
0
    def setUp(self):
        self.n_samples = 10
        self.SO3_GROUP = SpecialOrthogonal(n=3, point_type='vector')
        self.SE3_GROUP = SpecialEuclidean(n=3, point_type='vector')
        self.S1 = Hypersphere(dim=1)
        self.S2 = Hypersphere(dim=2)
        self.H2 = Hyperbolic(dim=2)
        self.H2_half_plane = PoincareHalfSpace(dim=2)
        self.M32 = Matrices(m=3, n=2)
        self.S32 = PreShapeSpace(k_landmarks=3, m_ambient=2)
        self.KS = visualization.KendallSphere()
        self.M33 = Matrices(m=3, n=3)
        self.S33 = PreShapeSpace(k_landmarks=3, m_ambient=3)
        self.KD = visualization.KendallDisk()

        plt.figure()
Ejemplo n.º 10
0
    def is_tangent(self, vector, base_point):
        r"""Check if the vector belongs to the tangent space.

        Parameters
        ----------
        vector : array-like, shape=[..., n, n]
            Matrix to check if it belongs to the tangent space.
        base_point : array-like, shape=[..., n, n]
            Base point of the tangent space.
            Optional, default: None.

        Returns
        -------
        belongs : array-like, shape=[...,]
            Boolean denoting if vector belongs to tangent space
            at base_point.
        """
        vector_sym = Matrices(self.n, self.n).to_symmetric(vector)

        _, r = gs.linalg.eigh(base_point)
        r_ort = r[..., :, self.n - self.rank:self.n]
        r_ort_t = Matrices.transpose(r_ort)
        rr = gs.matmul(r_ort, r_ort_t)
        candidates = Matrices.mul(rr, vector_sym, rr)
        result = gs.all(gs.isclose(candidates, 0., gs.atol), axis=(-2, -1))
        return result
Ejemplo n.º 11
0
 def __init__(self, n, k, **kwargs):
     kwargs.setdefault("dim", n * k)
     kwargs.setdefault("metric", MatricesMetric(n, k))
     super(FullRankMatrices, self).__init__(ambient_space=Matrices(n, k),
                                            **kwargs)
     self.rank = min(n, k)
     self.n = n
     self.k = k
Ejemplo n.º 12
0
 def test_dist_pairwise_parallel(self):
     n_samples = 15
     points = self.space.random_uniform(n_samples)
     result = self.metric.dist_pairwise(points, n_jobs=2, prefer="threads")
     is_sym = Matrices.is_symmetric(result)
     belongs = Matrices(n_samples, n_samples).belongs(result)
     self.assertTrue(is_sym)
     self.assertTrue(belongs)
Ejemplo n.º 13
0
    def set_to_networkx_test_data(self):
        smoke_data = [
            dict(
                space=self._PointSet(2),
                points=Matrices(2, 2).random_point(),
            ),
        ]

        return self.generate_tests(smoke_data)
Ejemplo n.º 14
0
    def pad_with_zeros_test_data(self):

        space = self._PointSet(3)

        adj_2 = Matrices(2, 2).random_point(3)
        adj_3 = Matrices(3, 3).random_point(2)
        points = [self._Point(adj_2[0]), self._Point(adj_3[0])]

        smoke_data = [
            dict(space=space, points=adj_2),
            dict(space=space, points=adj_2[0]),
            dict(space=space, points=adj_3),
            dict(space=space, points=adj_3[0]),
            dict(space=space, points=points),
            dict(space=space, points=points[0]),
        ]

        return self.generate_tests(smoke_data)
Ejemplo n.º 15
0
 def __init__(self, k_landmarks, m_ambient):
     super(PreShapeSpace,
           self).__init__(dim=m_ambient * (k_landmarks - 1) - 1,
                          embedding_manifold=Matrices(
                              k_landmarks, m_ambient),
                          default_point_type='matrix')
     self.embedding_metric = self.embedding_manifold.metric
     self.k_landmarks = k_landmarks
     self.m_ambient = m_ambient
     self.metric = ProcrustesMetric(k_landmarks, m_ambient)
Ejemplo n.º 16
0
    def __init__(self, n, k):
        assert isinstance(n, int) and isinstance(k, int)
        assert k <= n

        self.n = n
        self.k = k

        dimension = int(k * (n - k))
        super(Grassmannian, self).__init__(dimension=dimension,
                                           embedding_manifold=Matrices(n, n))
Ejemplo n.º 17
0
    def __init__(self, n, positive_det=False, **kwargs):
        ambient_space = Matrices(n, n)
        kwargs.setdefault("dim", n**2)
        kwargs.setdefault("metric", ambient_space.metric)

        super(GeneralLinear, self).__init__(
            ambient_space=ambient_space, n=n, lie_algebra=SquareMatrices(n), **kwargs
        )

        self.positive_det = positive_det
Ejemplo n.º 18
0
 def __init__(self, n_disks, coords_type='extrinsic'):
     self.n_disks = n_disks
     self.coords_type = coords_type
     self.point_type = PoincarePolydisk.default_point_type
     disk = Hyperboloid(2, coords_type=coords_type)
     list_disks = [disk, ] * n_disks
     super(PoincarePolydisk, self).__init__(
         manifolds=list_disks, default_point_type='matrix',
         ambient_space=Matrices(n_disks, 2))
     self.metric = PoincarePolydiskMetric(
         n_disks=n_disks, coords_type=coords_type)
Ejemplo n.º 19
0
 def test_dist_pairwise_parallel(self):
     gs.random.seed(0)
     n_samples = 2
     group = self.matrix_so3
     metric = InvariantMetric(group=group)
     points = group.random_uniform(n_samples)
     result = metric.dist_pairwise(points, n_jobs=2)
     is_sym = Matrices.is_symmetric(result)
     belongs = Matrices(n_samples, n_samples).belongs(result)
     self.assertTrue(is_sym)
     self.assertTrue(belongs)
Ejemplo n.º 20
0
    def _get_data(self, fnc):
        adj = Matrices(3, 3).random_point(2)
        points = [self._Point(adj_) for adj_ in adj]

        smoke_data = [
            dict(fnc=fnc, points=points[0]),
            dict(fnc=fnc, points=points),
            dict(fnc=fnc, points=adj[0]),
            dict(fnc=fnc, points=adj),
        ]

        return self.generate_tests(smoke_data)
Ejemplo n.º 21
0
    def __init__(self, n, p):
        assert isinstance(n, int) and isinstance(p, int)
        assert p <= n

        self.n = n
        self.p = p

        dimension = int(p * n - (p * (p + 1) / 2))
        super(Stiefel, self).__init__(dimension=dimension,
                                      embedding_manifold=Matrices(n, p))

        self.canonical_metric = StiefelCanonicalMetric(n, p)
Ejemplo n.º 22
0
 def __init__(self, k_landmarks, m_ambient):
     embedding_manifold = Matrices(k_landmarks, m_ambient)
     super(PreShapeSpace,
           self).__init__(dim=m_ambient * (k_landmarks - 1) - 1,
                          embedding_manifold=embedding_manifold,
                          default_point_type='matrix',
                          total_space=embedding_manifold,
                          ambient_metric=PreShapeMetric(
                              k_landmarks, m_ambient))
     self.embedding_metric = self.embedding_manifold.metric
     self.k_landmarks = k_landmarks
     self.m_ambient = m_ambient
     self.ambient_metric = PreShapeMetric(k_landmarks, m_ambient)
Ejemplo n.º 23
0
    def __init__(self, n, p):
        geomstats.error.check_integer(n, 'n')
        geomstats.error.check_integer(p, 'p')
        if p > n:
            raise ValueError('p needs to be smaller than n.')

        dim = int(p * n - (p * (p + 1) / 2))
        super(Stiefel, self).__init__(dim=dim,
                                      embedding_manifold=Matrices(n, p))

        self.n = n
        self.p = p
        self.canonical_metric = StiefelCanonicalMetric(n, p)
Ejemplo n.º 24
0
 def __init__(self, k_landmarks, m_ambient):
     embedding_manifold = Matrices(k_landmarks, m_ambient)
     embedding_metric = embedding_manifold.metric
     super(PreShapeSpace, self).__init__(
         dim=m_ambient * (k_landmarks - 1) - 1,
         embedding_space=embedding_manifold,
         submersion=embedding_metric.squared_norm,
         value=1.0,
         tangent_submersion=embedding_metric.inner_product,
         ambient_metric=PreShapeMetric(k_landmarks, m_ambient),
     )
     self.k_landmarks = k_landmarks
     self.m_ambient = m_ambient
     self.ambient_metric = PreShapeMetric(k_landmarks, m_ambient)
Ejemplo n.º 25
0
    def __init__(self, n, k):
        geomstats.errors.check_integer(k, 'k')
        geomstats.errors.check_integer(n, 'n')
        if k > n:
            raise ValueError(
                'k <= n is required: k-dimensional subspaces in n dimensions.')

        self.n = n
        self.k = k
        self.metric = GrassmannianCanonicalMetric(n, k)

        dim = int(k * (n - k))
        super(Grassmannian, self).__init__(dim=dim,
                                           embedding_manifold=Matrices(n, n),
                                           default_point_type='matrix')
Ejemplo n.º 26
0
    def belongs(self, mat, atol=TOLERANCE):
        """Evaluate if mat is a skew-symmetric matrix.

        Parameters
        ----------
        mat : array-like, shape=[..., n, n]
            The square matrix to check.
        atol : float
            Tolerance for the equality evaluation.

        Returns
        -------
        belongs : bool
        """
        return Matrices(self.n, self.n).is_skew_symmetric(mat=mat, atol=atol)
Ejemplo n.º 27
0
    def belongs(self, mat, atol=TOLERANCE):
        """Evaluate if mat is a skew-symmetric matrix.

        Parameters
        ----------
        mat : array-like, shape=[..., n, n]
            Square matrix to check.
        atol : float
            Tolerance for the equality evaluation.
            Optional, default: TOLERANCE.

        Returns
        -------
        belongs : array-like, shape=[...,]
            Boolean evaluating if matrix is skew symmetric.
        """
        return Matrices(self.n, self.n).is_skew_symmetric(mat=mat, atol=atol)
Ejemplo n.º 28
0
    def __init__(self, n, p):
        geomstats.errors.check_integer(n, 'n')
        geomstats.errors.check_integer(p, 'p')
        if p > n:
            raise ValueError('p needs to be smaller than n.')

        dim = int(p * n - (p * (p + 1) / 2))
        matrices = Matrices(n, p)
        super(Stiefel, self).__init__(
            dim=dim,
            embedding_space=matrices,
            submersion=lambda x: matrices.mul(matrices.transpose(x), x),
            value=gs.eye(p),
            tangent_submersion=lambda v, x: 2 * matrices.to_symmetric(
                matrices.mul(matrices.transpose(x), v)),
            metric=StiefelCanonicalMetric(n, p))

        self.n = n
        self.p = p
        self.canonical_metric = self.metric
Ejemplo n.º 29
0
    def __init__(self, n, p, **kwargs):
        geomstats.errors.check_integer(n, "n")
        geomstats.errors.check_integer(p, "p")
        if p > n:
            raise ValueError("p needs to be smaller than n.")

        dim = int(p * n - (p * (p + 1) / 2))
        matrices = Matrices(n, p)
        canonical_metric = StiefelCanonicalMetric(n, p)
        kwargs.setdefault("metric", canonical_metric)
        super(Stiefel, self).__init__(
            dim=dim,
            embedding_space=matrices,
            submersion=lambda x: matrices.mul(matrices.transpose(x), x),
            value=gs.eye(p),
            tangent_submersion=lambda v, x: 2 * matrices.to_symmetric(
                matrices.mul(matrices.transpose(x), v)),
            **kwargs)
        self.canonical_metric = canonical_metric
        self.n = n
        self.p = p
Ejemplo n.º 30
0
    def __init__(self, n):
        dim = int(n * (n - 1) / 2)
        super(SkewSymmetricMatrices, self).__init__(dim, n)
        self.ambient_space = Matrices(n, n)

        if n == 2:
            self.basis = gs.array([[[0.0, -1.0], [1.0, 0.0]]])
        elif n == 3:
            self.basis = gs.array([
                [[0.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]],
                [[0.0, 0.0, 1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, 0.0]],
                [[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
            ])
        else:
            self.basis = gs.zeros((dim, n, n))
            basis = []
            for row in gs.arange(n - 1):
                for col in gs.arange(row + 1, n):
                    basis.append(
                        gs.array_from_sparse([(row, col), (col, row)],
                                             [1.0, -1.0], (n, n)))
            self.basis = gs.stack(basis)