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
def belongs(self, mat, atol=gs.atol): """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: backend atol. Returns ------- belongs : array-like, shape=[...,] Boolean evaluating if matrix is skew symmetric. """ has_right_shape = self.ambient_space.belongs(mat) if gs.all(has_right_shape): return Matrices.is_skew_symmetric(mat=mat, atol=atol) return has_right_shape
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=[n_samples, 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=[n_samples, {dim, [n, n]} Data lying on the manifold. """ if base_point is None: base_point = self.estimate_ if self.estimate_ is None: raise RuntimeError('fit needs to be called first or a ' 'base_point passed.') if self.point_type == 'matrix': if gs.all(Matrices.is_symmetric(base_point)): tangent_vecs = SymmetricMatrices( base_point.shape[-1]).symmetric_matrix_from_vector(X) elif gs.all(Matrices.is_skew_symmetric(base_point)): tangent_vecs = SkewSymmetricMatrices( base_point.shape[-1]).matrix_representation(X) else: dim = base_point.shape[-1] tangent_vecs = gs.reshape(X, (len(X), dim, dim)) else: tangent_vecs = X return self.metric.exp(tangent_vecs, base_point)
def is_tangent(self, vector, base_point, atol=EPSILON): """Check whether the vector is tangent at base_point. A matrix :math: `X` is tangent to the Stiefel manifold at a point :math: `U` if :math: `U^TX` is skew-symmetric. Parameters ---------- vector : array-like, shape=[..., n, p] Vector. base_point : array-like, shape=[..., n, p] Point on the manifold. atol : float Absolute tolerance. Optional, default: 1e-6. Returns ------- is_tangent : bool Boolean denoting if vector is a tangent vector at the base point. """ aux = Matrices.mul(Matrices.transpose(base_point), vector) return Matrices.is_skew_symmetric(aux, atol=1e-5)
class TestMatrices(geomstats.tests.TestCase): 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 @geomstats.tests.np_only def test_mul(self): a = gs.eye(3, 3, 1) b = gs.eye(3, 3, -1) c = gs.array([ [1., 0., 0.], [0., 1., 0.], [0., 0., 0.]]) d = gs.array([ [0., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) result = self.space.mul([a, b], [b, a]) expected = gs.array([c, d]) self.assertAllClose(result, expected) result = self.space.mul(a, [a, b]) expected = gs.array([gs.eye(3, 3, 2), c]) self.assertAllClose(result, expected) @geomstats.tests.np_only def test_bracket(self): x = gs.array([ [0., 0., 0.], [0., 0., -1.], [0., 1., 0.]]) y = gs.array([ [0., 0., 1.], [0., 0., 0.], [-1., 0., 0.]]) z = gs.array([ [0., -1., 0.], [1., 0., 0.], [0., 0., 0.]]) result = self.space.bracket([x, y], [y, z]) expected = gs.array([z, x]) self.assertAllClose(result, expected) result = self.space.bracket(x, [x, y, z]) expected = gs.array([gs.zeros((3, 3)), z, -y]) self.assertAllClose(result, expected) @geomstats.tests.np_only def test_transpose(self): tr = self.space.transpose ar = gs.array a = gs.eye(3, 3, 1) b = gs.eye(3, 3, -1) self.assertAllClose(tr(a), b) self.assertAllClose(tr(ar([a, b])), ar([b, a])) def test_is_symmetric(self): not_squared = gs.array([[1., 2.], [2., 1.], [3., 1.]]) result = self.space.is_symmetric(not_squared) expected = False self.assertAllClose(result, expected) sym_mat = gs.array([[1., 2.], [2., 1.]]) result = self.space.is_symmetric(sym_mat) expected = gs.array(True) self.assertAllClose(result, expected) not_a_sym_mat = gs.array([[1., 0.6, -3.], [6., -7., 0.], [0., 7., 8.]]) result = self.space.is_symmetric(not_a_sym_mat) expected = gs.array(False) self.assertAllClose(result, expected) @geomstats.tests.np_only def test_is_skew_symmetric(self): skew_mat = gs.array([[0, - 2.], [2., 0]]) result = self.space.is_skew_symmetric(skew_mat) expected = gs.array(True) self.assertAllClose(result, expected) not_a_sym_mat = gs.array([[1., 0.6, -3.], [6., -7., 0.], [0., 7., 8.]]) result = self.space.is_skew_symmetric(not_a_sym_mat) expected = gs.array(False) self.assertAllClose(result, expected) @geomstats.tests.np_and_tf_only def test_is_symmetric_vectorization(self): points = gs.array([ [[1., 2.], [2., 1.]], [[3., 4.], [4., 5.]], [[1., 2.], [3., 4.]]]) result = self.space.is_symmetric(points) expected = [True, True, False] self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_make_symmetric(self): sym_mat = gs.array([[1., 2.], [2., 1.]]) result = self.space.to_symmetric(sym_mat) expected = sym_mat self.assertAllClose(result, expected) mat = gs.array([[1., 2., 3.], [0., 0., 0.], [3., 1., 1.]]) result = self.space.to_symmetric(mat) expected = gs.array([[1., 1., 3.], [1., 0., 0.5], [3., 0.5, 1.]]) self.assertAllClose(result, expected) mat = gs.array([[1e100, 1e-100, 1e100], [1e100, 1e-100, 1e100], [1e-100, 1e-100, 1e100]]) result = self.space.to_symmetric(mat) res = 0.5 * (1e100 + 1e-100) expected = gs.array([[1e100, res, res], [res, 1e-100, res], [res, res, 1e100]]) self.assertAllClose(result, expected) @geomstats.tests.np_and_tf_only def test_make_symmetric_and_is_symmetric_vectorization(self): points = gs.array([ [[1., 2.], [3., 4.]], [[5., 6.], [4., 9.]]]) sym_points = self.space.to_symmetric(points) result = gs.all(self.space.is_symmetric(sym_points)) expected = True self.assertAllClose(result, expected) def test_inner_product(self): base_point = gs.array([ [1., 2., 3.], [0., 0., 0.], [3., 1., 1.]]) tangent_vector_1 = gs.array([ [1., 2., 3.], [0., -10., 0.], [30., 1., 1.]]) tangent_vector_2 = gs.array([ [1., 4., 3.], [5., 0., 0.], [3., 1., 1.]]) result = self.metric.inner_product( tangent_vector_1, tangent_vector_2, base_point=base_point) expected = gs.trace( gs.matmul( gs.transpose(tangent_vector_1), tangent_vector_2)) self.assertAllClose(result, expected) def test_cong(self): base_point = gs.array([ [1., 2., 3.], [0., 0., 0.], [3., 1., 1.]]) tangent_vector = gs.array([ [1., 2., 3.], [0., -10., 0.], [30., 1., 1.]]) result = self.space.congruent(tangent_vector, base_point) expected = gs.matmul( tangent_vector, gs.transpose(base_point)) expected = gs.matmul(base_point, expected) self.assertAllClose(result, expected) def test_belongs(self): base_point_square = gs.zeros((self.n, self.n)) base_point_nonsquare = gs.zeros((self.m, self.n)) result = self.space.belongs(base_point_square) expected = True self.assertAllClose(result, expected) result = self.space_nonsquare.belongs(base_point_square) expected = False self.assertAllClose(result, expected) result = self.space.belongs(base_point_nonsquare) expected = False self.assertAllClose(result, expected) result = self.space_nonsquare.belongs(base_point_nonsquare) expected = True self.assertAllClose(result, expected) result = self.space.belongs(gs.zeros((2, 2, 3))) self.assertFalse(gs.all(result)) result = self.space.belongs(gs.zeros((2, 3, 3))) self.assertTrue(gs.all(result)) def test_is_diagonal(self): base_point = gs.array([ [1., 2., 3.], [0., 0., 0.], [3., 1., 1.]]) result = self.space.is_diagonal(base_point) expected = False self.assertAllClose(result, expected) diagonal = gs.eye(3) result = self.space.is_diagonal(diagonal) self.assertTrue(result) base_point = gs.stack([base_point, diagonal]) result = self.space.is_diagonal(base_point) expected = gs.array([False, True]) self.assertAllClose(result, expected) base_point = gs.stack([diagonal] * 2) result = self.space.is_diagonal(base_point) self.assertTrue(gs.all(result)) base_point = gs.reshape(gs.arange(6), (2, 3)) result = self.space.is_diagonal(base_point) self.assertTrue(~result) def test_norm(self): for n_samples in [1, 2]: mat = self.space.random_point(n_samples) result = self.metric.norm(mat) expected = self.space.frobenius_product(mat, mat) ** .5 self.assertAllClose(result, expected)