Пример #1
0
 def __init__(self, n):
     super(_SpecialOrthogonalMatrices,
           self).__init__(dim=int((n * (n - 1)) / 2),
                          default_point_type='matrix',
                          n=n)
     self.lie_algebra = SkewSymmetricMatrices(n=n)
     self.bi_invariant_metric = BiInvariantMetric(group=self)
Пример #2
0
    def test_structure_constant(self):
        group = self.matrix_so3
        lie_algebra = SkewSymmetricMatrices(3)
        metric = InvariantMetric(group=group, algebra=lie_algebra)
        basis = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)
        x, y, z = basis
        result = metric.structure_constant(-z, y, -x)
        expected = 2.**.5 / 2.
        self.assertAllClose(result, expected)

        result = -metric.structure_constant(y, -z, -x)
        self.assertAllClose(result, expected)

        result = metric.structure_constant(y, -x, -z)
        self.assertAllClose(result, expected)

        result = -metric.structure_constant(-x, y, -z)
        self.assertAllClose(result, expected)

        result = metric.structure_constant(-x, -z, y)
        self.assertAllClose(result, expected)

        result = -metric.structure_constant(-z, -x, y)
        self.assertAllClose(result, expected)

        result = metric.structure_constant(x, x, z)
        expected = 0.
        self.assertAllClose(result, expected)
Пример #3
0
    def test_inner_product_left(self):
        lie_algebra = SkewSymmetricMatrices(3)
        tangent_vec_a = lie_algebra.matrix_representation(gs.array([1., 0,
                                                                    2.]))
        tangent_vec_a = self.matrix_so3.compose(self.point_1_matrix,
                                                tangent_vec_a)
        tangent_vec_b = lie_algebra.matrix_representation(
            gs.array([1., 0, 0.5]))
        tangent_vec_b = self.matrix_so3.compose(self.point_1_matrix,
                                                tangent_vec_b)
        result = self.matrix_left_metric.inner_product(tangent_vec_a,
                                                       tangent_vec_b,
                                                       self.point_1_matrix)
        expected = 4.
        self.assertAllClose(result, expected)

        tangent_vec_a = lie_algebra.matrix_representation(
            gs.array([[1., 0, 2.], [0, 3., 5.]]))
        tangent_vec_a = self.matrix_so3.compose(self.point_1_matrix,
                                                tangent_vec_a)
        result = self.matrix_left_metric.inner_product(tangent_vec_a,
                                                       tangent_vec_b,
                                                       self.point_1_matrix)
        expected = gs.array([4., 5.])
        self.assertAllClose(result, expected)
Пример #4
0
    def test_sectional_curvature(self):
        group = self.matrix_so3
        lie_algebra = SkewSymmetricMatrices(3)
        metric = InvariantMetric(group=group, algebra=lie_algebra)
        x, y, z = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)

        result = metric.sectional_curvature(x, y)
        expected = 1. / 8
        self.assertAllClose(result, expected)

        point = group.random_uniform()
        translation_map = group.tangent_translation_map(point)
        tan_a = translation_map(-z)
        tan_b = translation_map(y)
        result = metric.sectional_curvature(tan_a, tan_b, point)
        self.assertAllClose(result, expected)

        tan_a = gs.stack([x, y])
        tan_b = gs.stack([z] * 2)
        result = metric.sectional_curvature(tan_a, tan_b)
        self.assertAllClose(result, gs.array([expected] * 2))

        result = metric.sectional_curvature(y, y)
        expected = 0.
        self.assertAllClose(result, expected)
 def test_baker_campbell_hausdorff(self, n, matrix_a, matrix_b, order,
                                   expected):
     skew = SkewSymmetricMatrices(n)
     result = skew.baker_campbell_hausdorff(gs.array(matrix_a),
                                            gs.array(matrix_b),
                                            order=order)
     self.assertAllClose(result, gs.array(expected))
Пример #6
0
class TestLieAlgebraMethods(geomstats.tests.TestCase):
    def setUp(self):
        self.n = 4
        self.dim = int(self.n * (self.n - 1) / 2)
        self.algebra = SkewSymmetricMatrices(n=self.n)

    def test_dimension(self):
        result = self.algebra.dim
        expected = self.dim
        self.assertAllClose(result, expected)

    @geomstats.tests.np_only
    def test_matrix_representation_and_belongs(self):
        n_samples = 2
        point = gs.random.rand(n_samples * self.dim)
        point = gs.reshape(point, (n_samples, self.dim))
        mat = self.algebra.matrix_representation(point)
        result = self.algebra.belongs(mat).all()
        expected = True
        self.assertAllClose(result, expected)

    @geomstats.tests.np_only
    def test_basis_and_matrix_representation(self):
        n_samples = 2
        expected = gs.random.rand(n_samples * self.dim)
        expected = gs.reshape(expected, (n_samples, self.dim))
        mat = self.algebra.matrix_representation(expected)
        result = self.algebra.basis_representation(mat)
        self.assertAllClose(result, expected)
Пример #7
0
    def test_curvature(self):
        group = self.matrix_so3
        lie_algebra = SkewSymmetricMatrices(3)
        metric = InvariantMetric(group=group, algebra=lie_algebra)
        x, y, z = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)

        result = metric.curvature_at_identity(x, y, x)
        expected = 1. / 8 * y
        self.assertAllClose(result, expected)

        tan_a = gs.stack([x, x])
        tan_b = gs.stack([y] * 2)
        result = metric.curvature(tan_a, tan_b, tan_a)
        self.assertAllClose(result, gs.array([expected] * 2))

        point = group.random_uniform()
        translation_map = group.tangent_translation_map(point)
        tan_a = translation_map(x)
        tan_b = translation_map(y)
        result = metric.curvature(tan_a, tan_b, tan_a, point)
        expected = translation_map(expected)
        self.assertAllClose(result, expected)

        result = metric.curvature(y, y, z)
        expected = gs.zeros_like(z)
        self.assertAllClose(result, expected)
Пример #8
0
 def test_dual_adjoint(self):
     group = self.matrix_so3
     lie_algebra = SkewSymmetricMatrices(3)
     metric = InvariantMetric(group=group, algebra=lie_algebra)
     basis = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)
     for x in basis:
         for y in basis:
             for z in basis:
                 result = metric.inner_product_at_identity(
                     metric.dual_adjoint(x, y), z)
                 expected = metric.structure_constant(x, z, y)
                 self.assertAllClose(result, expected)
Пример #9
0
    def __init__(self, n):
        dim = int(n * (n + 1) / 2)
        super(SpecialEuclideanMatrixLieAlgebra, self).__init__(dim, n)

        self.skew = SkewSymmetricMatrices(n)
        basis = homogeneous_representation(
            self.skew.basis,
            gs.zeros((self.skew.dim, n)), (self.skew.dim, n + 1, n + 1), 0.)
        basis = list(basis)

        for row in gs.arange(n):
            basis.append(gs.array_from_sparse(
                [(row, n)], [1.], (n + 1, n + 1)))
        self.basis = gs.stack(basis)
Пример #10
0
class TestLieAlgebra(geomstats.tests.TestCase):
    def setUp(self):
        self.n = 4
        self.dim = int(self.n * (self.n - 1) / 2)
        self.algebra = SkewSymmetricMatrices(n=self.n)

    def test_dimension(self):
        result = self.algebra.dim
        expected = self.dim
        self.assertAllClose(result, expected)

    def test_matrix_representation_and_belongs(self):
        n_samples = 2
        point = gs.random.rand(n_samples * self.dim)
        point = gs.reshape(point, (n_samples, self.dim))
        mat = self.algebra.matrix_representation(point)
        result = gs.all(self.algebra.belongs(mat))
        expected = True
        self.assertAllClose(result, expected)

    @geomstats.tests.np_and_pytorch_only
    def test_basis_and_matrix_representation(self):
        n_samples = 2
        expected = gs.random.rand(n_samples * self.dim)
        expected = gs.reshape(expected, (n_samples, self.dim))
        mat = self.algebra.matrix_representation(expected)
        result = self.algebra.basis_representation(mat)
        self.assertAllClose(result, expected)

    def test_orthonormal_basis(self):
        group = SpecialOrthogonal(3)
        lie_algebra = SkewSymmetricMatrices(3)
        metric = InvariantMetric(group=group, algebra=lie_algebra)
        basis = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)
        result = metric.inner_product_at_identity(basis[0], basis[1])
        self.assertAllClose(result, 0.)

        result = metric.inner_product_at_identity(basis[1], basis[1])
        self.assertAllClose(result, 1.)

        metric_mat = from_vector_to_diagonal_matrix(gs.array([1., 2., 3.]))
        metric = InvariantMetric(group=group,
                                 algebra=lie_algebra,
                                 metric_mat_at_identity=metric_mat)
        basis = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)
        result = metric.inner_product_at_identity(basis[0], basis[1])
        self.assertAllClose(result, 0.)

        result = metric.inner_product_at_identity(basis[1], basis[1])
        self.assertAllClose(result, 1.)
Пример #11
0
    def test_inner_product_at_identity(self):
        lie_algebra = SkewSymmetricMatrices(3)
        tangent_vec_a = lie_algebra.matrix_representation([1., 0, 2.])[0]
        tangent_vec_b = lie_algebra.matrix_representation([1., 0, 0.5])[0]
        result = self.matrix_left_metric.inner_product_at_identity(
            tangent_vec_a, tangent_vec_b)
        expected = 4.
        self.assertAllClose(result, expected)

        tangent_vec_a = lie_algebra.matrix_representation(
            [[1., 0, 2.], [0, 3., 5.]])
        result = self.matrix_left_metric.inner_product_at_identity(
            tangent_vec_a, tangent_vec_b)
        expected = gs.array([4., 5.])
        self.assertAllClose(result, expected)
Пример #12
0
    def test_connection(self):
        group = self.matrix_so3
        lie_algebra = SkewSymmetricMatrices(3)
        metric = InvariantMetric(group=group, algebra=lie_algebra)
        x, y, z = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)
        result = metric.connection(-z, y)
        expected = -1. / 2**.5 / 2. * x
        self.assertAllClose(result, expected)

        point = group.random_uniform()
        translation_map = group.tangent_translation_map(point)
        tan_a = translation_map(-z)
        tan_b = translation_map(y)
        result = metric.connection(tan_a, tan_b, point)
        expected = translation_map(expected)
        self.assertAllClose(result, expected)
Пример #13
0
    def test_curvature_derivative_at_identity(self):
        group = self.matrix_so3
        lie_algebra = SkewSymmetricMatrices(3)
        metric = InvariantMetric(group=group, algebra=lie_algebra)
        basis = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)

        result = True
        for x in basis:
            for i, y in enumerate(basis):
                for z in basis[i:]:
                    for t in basis:
                        nabla_r = metric.curvature_derivative_at_identity(
                            x, y, z, t)
                        if not gs.all(gs.isclose(nabla_r, 0., atol=1e-5)):
                            print(nabla_r)
                            result = False
        self.assertTrue(result)
Пример #14
0
    def test_curvature_derivative(self):
        group = self.matrix_so3
        lie_algebra = SkewSymmetricMatrices(3)
        metric = InvariantMetric(group=group, algebra=lie_algebra)
        x, y, z = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)
        result = metric.curvature_derivative(x, y, z, x)
        expected = gs.zeros_like(x)
        self.assertAllClose(result, expected)

        point = group.random_uniform()
        translation_map = group.tangent_translation_map(point)
        tan_a = translation_map(x)
        tan_b = translation_map(y)
        tan_c = translation_map(z)
        result = metric.curvature_derivative(tan_a, tan_b, tan_c, tan_a, point)
        expected = gs.zeros_like(x)
        self.assertAllClose(result, expected)
Пример #15
0
class _SpecialOrthogonalMatrices(GeneralLinear, LieGroup):
    """Class for special orthogonal groups in matrix representation.

    Parameters
    ----------
    n : int
        Integer representing the shape of the matrices: n x n.
    """
    def __init__(self, n):
        super(_SpecialOrthogonalMatrices,
              self).__init__(dim=int((n * (n - 1)) / 2),
                             default_point_type='matrix',
                             n=n)
        self.lie_algebra = SkewSymmetricMatrices(n=n)
        self.bi_invariant_metric = BiInvariantMetric(group=self)

    def belongs(self, point):
        """Check whether point is an orthogonal matrix."""
        return self.equal(self.mul(point, self.transpose(point)),
                          self.identity)

    @classmethod
    def inverse(cls, point):
        """Return the transpose matrix of point."""
        return cls.transpose(point)

    def _is_in_lie_algebra(self, tangent_vec, atol=ATOL):
        return self.lie_algebra.belongs(tangent_vec, atol=atol)

    @classmethod
    def _to_lie_algebra(cls, tangent_vec):
        """Project vector onto skew-symmetric matrices."""
        return cls.to_skew_symmetric(tangent_vec)

    def random_uniform(self, n_samples=1, tol=1e-6):
        """Sample in SO(n) from the uniform distribution.

        Parameters
        ----------
        n_samples : int, optional (1)
            Number of samples.
        tol : unused

        Returns
        -------
        samples : array-like, shape=[..., n, n]
            Points sampled on the SO(n).
        """
        if n_samples == 1:
            random_mat = gs.random.rand(self.n, self.n)
        else:
            random_mat = gs.random.rand(n_samples, self.n, self.n)
        skew = self.to_tangent(random_mat)
        return self.exp(skew)
Пример #16
0
    def test_orthonormal_basis(self):
        group = SpecialOrthogonal(3)
        lie_algebra = SkewSymmetricMatrices(3)
        metric = InvariantMetric(group=group, algebra=lie_algebra)
        basis = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)
        result = metric.inner_product_at_identity(basis[0], basis[1])
        self.assertAllClose(result, 0.)

        result = metric.inner_product_at_identity(basis[1], basis[1])
        self.assertAllClose(result, 1.)

        metric_mat = from_vector_to_diagonal_matrix(gs.array([1., 2., 3.]))
        metric = InvariantMetric(group=group,
                                 algebra=lie_algebra,
                                 metric_mat_at_identity=metric_mat)
        basis = lie_algebra.orthonormal_basis(metric.metric_mat_at_identity)
        result = metric.inner_product_at_identity(basis[0], basis[1])
        self.assertAllClose(result, 0.)

        result = metric.inner_product_at_identity(basis[1], basis[1])
        self.assertAllClose(result, 1.)
        def baker_campbell_hausdorff_data(self):
            n_list = range(3, 10)
            smoke_data = []
            for n in n_list:
                space = SkewSymmetricMatrices(n)
                fb = space.basis[0]
                sb = space.basis[1]
                fb_sb_bracket = space.bracket(fb, sb)
                expected1 = fb + sb
                expected2 = expected1 + 0.5 * fb_sb_bracket
                expected3 = (expected2 +
                             1.0 / 12.0 * space.bracket(fb, fb_sb_bracket) -
                             1.0 / 12.0 * space.bracket(sb, fb_sb_bracket))
                expected4 = expected3 - 1.0 / 24.0 * space.bracket(
                    sb, space.bracket(fb, fb_sb_bracket))
                expected = [expected1, expected2, expected3, expected4]
                for order in range(1, 5):
                    smoke_data.append(
                        dict(
                            n=n,
                            matrix_a=fb,
                            matrix_b=sb,
                            order=order,
                            expected=expected[order - 1],
                        ))

            return self.generate_tests(smoke_data)
Пример #18
0
    def transform(self, X, base_point=None):
        """Lift data to a tangent space.

        Compute the logs of all data point and reshapes them to
        1d vectors if necessary. By default the logs are taken at the mean
        but any other base point can be passed. Any machine learning
        algorithm can then be used with the output array.

        Parameters
        ----------
        X : array-like, shape=[..., {dim, [n, n]}]
            Data to transform.
        y : Ignored (Compliance with scikit-learn interface)
        base_point : array-like, shape={dim, [n,n]}, optional (mean)
            Point on the manifold, the returned samples will be tangent
            vectors at the base point.

        Returns
        -------
        X_new : array-like, shape=[..., dim]
            Lifted data.
        """
        if base_point is None:
            base_point = self.estimator.estimate_

            if self.estimator.estimate_ is None:
                raise RuntimeError(
                    "fit needs to be called first or a " "base_point passed."
                )

        tangent_vecs = self._used_geometry.log(X, base_point=base_point)

        if self.point_type == "vector":
            return tangent_vecs

        if gs.all(Matrices.is_symmetric(tangent_vecs)):
            X = SymmetricMatrices.to_vector(tangent_vecs)
        elif gs.all(Matrices.is_skew_symmetric(tangent_vecs)):
            X = SkewSymmetricMatrices(tangent_vecs.shape[-1]).basis_representation(
                tangent_vecs
            )
        else:
            X = gs.reshape(tangent_vecs, (len(X), -1))

        return X
Пример #19
0
    def inverse_transform(self, X, base_point=None):
        """Reconstruction of X.

        The reconstruction will match X_original whose transform would be X.

        Parameters
        ----------
        X : array-like, shape=[..., dim]
            New data, where dim is the dimension of the manifold data belong
            to.
        base_point : array-like, shape={dim, [n,n]}, optional (mean)
            Point on the manifold, where the input samples are tangent
            vectors.

        Returns
        -------
        X_original : array-like, shape=[..., {dim, [n, n]}
            Data lying on the manifold.
        """
        if base_point is None:
            base_point = self.estimator.estimate_

            if self.estimator.estimate_ is None:
                raise RuntimeError('fit needs to be called first or a '
                                   'base_point passed.')

        if self.point_type == 'matrix':
            n_base_point = base_point.shape[-1]
            n_vecs = X.shape[-1]
            dim_sym = int(n_base_point * (n_base_point + 1) / 2)
            dim_skew = int(n_base_point * (n_base_point - 1) / 2)

            if gs.all(Matrices.is_symmetric(base_point)) and dim_sym == n_vecs:
                tangent_vecs = SymmetricMatrices(
                    base_point.shape[-1]).from_vector(X)
            elif dim_skew == n_vecs:
                tangent_vecs = SkewSymmetricMatrices(
                    dim_skew).matrix_representation(X)
            else:
                dim = base_point.shape[-1]
                tangent_vecs = gs.reshape(X, (len(X), dim, dim))
        else:
            tangent_vecs = X
        return self._used_geometry.exp(tangent_vecs, base_point)
Пример #20
0
    def projection(self, mat):
        """Project a matrix to the Lie Algebra.

        Compute the skew-symmetric projection of the rotation part of matrix.

        Parameters
        ----------
        mat : array-like, shape=[..., n + 1, n + 1]
            Matrix.

        Returns
        -------
        projected : array-like, shape=[..., n + 1, n + 1]
            Matrix belonging to Lie Algebra.
        """
        rotation = mat[..., :self.n, :self.n]
        skew = SkewSymmetricMatrices.projection(rotation)
        return homogeneous_representation(skew, mat[..., :self.n, self.n],
                                          mat.shape, 0.0)
Пример #21
0
def load_connectomes(as_vectors=False):
    """Load data from brain connectomes.

    Load the correlation data from the kaggle MSLP 2014 Schizophrenia
    Challenge. The original data came as flattened vectors, but if `raw=True`
    is passed, the correlation values are reshaped as symmetric matrices with
    ones on the diagonal.

    Parameters
    ----------
    as_vectors : bool
        Whether to return raw data as vectors or as symmetric matrices.
        Optional, default: False

    Returns
    -------
    mat : array-like, shape=[86, {[28, 28], 378}
        Connectomes.
    patient_id : array-like, shape=[86,]
        Patient unique identifiers
    target : array-like, shape=[86,]
        Labels, whether patients belong to the diseased class (1) or control
        (0).
    """
    with open(CONNECTOMES_PATH) as csvfile:
        data_list = list(csv.reader(csvfile))
    patient_id = gs.array([int(row[0]) for row in data_list[1:]])
    data = gs.array([[float(value) for value in row[1:]]
                     for row in data_list[1:]])

    with open(CONNECTOMES_LABELS_PATH) as csvfile:
        labels = list(csv.reader(csvfile))
    target = gs.array([int(row[1]) for row in labels[1:]])
    if as_vectors:
        return data, patient_id, target
    mat = SkewSymmetricMatrices(28).matrix_representation(data)
    mat = gs.eye(28) - gs.transpose(gs.tril(mat), (0, 2, 1))
    mat = 1.0 / 2.0 * (mat + gs.transpose(mat, (0, 2, 1)))

    return mat, patient_id, target
Пример #22
0
 def setUp(self):
     self.n = 4
     self.dim = int(self.n * (self.n - 1) / 2)
     self.algebra = SkewSymmetricMatrices(n=self.n)
Пример #23
0
class SpecialEuclideanMatrixLieAlgebra(MatrixLieAlgebra):
    r"""Lie Algebra of the special Euclidean group.

    This is the tangent space at the identity. It is identified with the
    :math:`n + 1 \times n + 1` block matrices of the form:
    .. math:
                ((A, t), (0, 0))

    where A is an :math:`n \times n` skew-symmetric matrix, :math: `t` is an
    n-dimensional vector.

    Parameters
    ----------
    n : int
        Integer dimension of the underlying Euclidean space. Matrices will
        be of size: (n+1) x (n+1).
    """
    def __init__(self, n):
        dim = int(n * (n + 1) / 2)
        super(SpecialEuclideanMatrixLieAlgebra, self).__init__(dim, n + 1)

        self.skew = SkewSymmetricMatrices(n)
        self.n = n

    def _create_basis(self):
        """Create the canonical basis."""
        n = self.n
        basis = homogeneous_representation(
            self.skew.basis,
            gs.zeros((self.skew.dim, n)),
            (self.skew.dim, n + 1, n + 1),
            0.0,
        )
        basis = list(basis)

        for row in gs.arange(n):
            basis.append(
                gs.array_from_sparse([(row, n)], [1.0], (n + 1, n + 1)))
        return gs.stack(basis)

    def belongs(self, mat, atol=ATOL):
        """Evaluate if the rotation part of mat is a skew-symmetric matrix.

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

        Returns
        -------
        belongs : array-like, shape=[...,]
            Boolean evaluating if rotation part of matrix is skew symmetric.
        """
        point_dim1, point_dim2 = mat.shape[-2:]
        belongs = point_dim1 == point_dim2 == self.n + 1

        rotation = mat[..., :self.n, :self.n]
        rot_belongs = self.skew.belongs(rotation, atol=atol)

        belongs = gs.logical_and(belongs, rot_belongs)

        last_line = mat[..., -1, :]
        all_zeros = ~gs.any(last_line, axis=-1)

        belongs = gs.logical_and(belongs, all_zeros)
        return belongs

    def random_point(self, n_samples=1, bound=1.0):
        """Sample in the lie algebra with a uniform distribution in a box.

        Parameters
        ----------
        n_samples : int
            Number of samples.
            Optional, default: 1.
        bound : float
            Side of hypercube support of the uniform distribution.
            Optional, default: 1.0

        Returns
        -------
        point : array-like, shape=[..., n + 1, n + 1]
           Sample.
        """
        point = super(SpecialEuclideanMatrixLieAlgebra,
                      self).random_point(n_samples, bound)
        return self.projection(point)

    def projection(self, mat):
        """Project a matrix to the Lie Algebra.

        Compute the skew-symmetric projection of the rotation part of matrix.

        Parameters
        ----------
        mat : array-like, shape=[..., n + 1, n + 1]
            Matrix.

        Returns
        -------
        projected : array-like, shape=[..., n + 1, n + 1]
            Matrix belonging to Lie Algebra.
        """
        rotation = mat[..., :self.n, :self.n]
        skew = SkewSymmetricMatrices.projection(rotation)
        return homogeneous_representation(skew, mat[..., :self.n, self.n],
                                          mat.shape, 0.0)

    def basis_representation(self, matrix_representation):
        """Calculate the coefficients of given matrix in the basis.

        Compute a 1d-array that corresponds to the input matrix in the basis
        representation.

        Parameters
        ----------
        matrix_representation : array-like, shape=[..., n + 1, n + 1]
            Matrix.

        Returns
        -------
        basis_representation : array-like, shape=[..., dim]
            Representation in the basis.
        """
        skew_part = self.skew.basis_representation(
            matrix_representation[..., :self.n, :self.n])
        translation_part = matrix_representation[..., :-1, self.n]
        return gs.concatenate([skew_part, translation_part[..., :]], axis=-1)
Пример #24
0
    def __init__(self, n):
        dim = int(n * (n + 1) / 2)
        super(SpecialEuclideanMatrixLieAlgebra, self).__init__(dim, n + 1)

        self.skew = SkewSymmetricMatrices(n)
        self.n = n
Пример #25
0
class TestLieAlgebra(geomstats.tests.TestCase):
    def setup_method(self):
        self.n = 4
        self.dim = int(self.n * (self.n - 1) / 2)
        self.algebra = SkewSymmetricMatrices(n=self.n)

    def test_dimension(self):
        result = self.algebra.dim
        expected = self.dim
        self.assertAllClose(result, expected)

    def test_matrix_representation_and_belongs(self):
        n_samples = 2
        point = gs.random.rand(n_samples * self.dim)
        point = gs.reshape(point, (n_samples, self.dim))
        mat = self.algebra.matrix_representation(point)
        result = gs.all(self.algebra.belongs(mat))
        self.assertTrue(result)

    def test_basis_and_matrix_representation(self):
        n_samples = 2
        expected = gs.random.rand(n_samples * self.dim)
        expected = gs.reshape(expected, (n_samples, self.dim))
        mat = self.algebra.matrix_representation(expected)
        result = self.algebra.basis_representation(mat)
        self.assertAllClose(result, expected)

    def test_orthonormal_basis(self):
        group = SpecialOrthogonal(3)
        lie_algebra = SkewSymmetricMatrices(3)
        metric = InvariantMetric(group=group)
        basis = metric.normal_basis(lie_algebra.basis)
        result = metric.inner_product_at_identity(basis[0], basis[1])
        self.assertAllClose(result, 0.0)

        result = metric.inner_product_at_identity(basis[1], basis[1])
        self.assertAllClose(result, 1.0)

        metric_mat = from_vector_to_diagonal_matrix(gs.array([1.0, 2.0, 3.0]))
        metric = InvariantMetric(group=group, metric_mat_at_identity=metric_mat)
        basis = metric.normal_basis(lie_algebra.basis)
        result = metric.inner_product_at_identity(basis[0], basis[1])
        self.assertAllClose(result, 0.0)

        result = metric.inner_product_at_identity(basis[1], basis[1])
        self.assertAllClose(result, 1.0)

    def test_orthonormal_basis_se3(self):
        group = SpecialEuclidean(3)
        lie_algebra = group.lie_algebra
        metric = InvariantMetric(group=group)
        basis = metric.normal_basis(lie_algebra.basis)
        for i, x in enumerate(basis):
            for y in basis[i:]:
                result = metric.inner_product_at_identity(x, y)
                expected = 0.0 if gs.any(x != y) else 1.0
                self.assertAllClose(result, expected)

        metric_mat = from_vector_to_diagonal_matrix(
            gs.cast(gs.arange(1, group.dim + 1), gs.float32)
        )
        metric = InvariantMetric(group=group, metric_mat_at_identity=metric_mat)
        basis = metric.normal_basis(lie_algebra.basis)
        for i, x in enumerate(basis):
            for y in basis[i:]:
                result = metric.inner_product_at_identity(x, y)
                expected = 0.0 if gs.any(x != y) else 1.0
                self.assertAllClose(result, expected)
Пример #26
0
 def setUp(self):
     self.n_seq = [3, 4, 5, 6, 7, 8, 9, 10]
     self.skew = {n: SkewSymmetricMatrices(n=n) for n in self.n_seq}
Пример #27
0
"""
import timeit

import matplotlib.pyplot as plt

import geomstats.backend as gs
from geomstats.geometry.skew_symmetric_matrices import SkewSymmetricMatrices
from geomstats.geometry.special_orthogonal import SpecialOrthogonal

N = 3
MAX_ORDER = 10

GROUP = SpecialOrthogonal(n=N)

DIM = int(N * (N - 1) / 2)
ALGEBRA = SkewSymmetricMatrices(n=N)


def main():
    """Visualize convergence of the BCH formula approximation on so(n)."""
    norm_rv_1 = gs.random.normal(size=DIM)
    tan_rv_1 = ALGEBRA.matrix_representation(
        norm_rv_1 / gs.linalg.norm(norm_rv_1, axis=0) / 2)
    exp_1 = gs.linalg.expm(tan_rv_1)

    norm_rv_2 = gs.random.normal(size=DIM)
    tan_rv_2 = ALGEBRA.matrix_representation(
        norm_rv_2 / gs.linalg.norm(norm_rv_2, axis=0) / 2)
    exp_2 = gs.linalg.expm(tan_rv_2)

    composition = GROUP.compose(exp_1, exp_2)
Пример #28
0
 def matrix_representation_and_belongs_test_data(self):
     smoke_data = [
         dict(algebra=SkewSymmetricMatrices(4),
              point=gs.random.rand(2, 6))
     ]
     return self.generate_tests(smoke_data)
Пример #29
0
 def dimension_test_data(self):
     smoke_data = [dict(algebra=SkewSymmetricMatrices(4), expected=6)]
     return self.generate_tests(smoke_data)