def __init__(self, n, point_type=None, epsilon=0.): """Initiate an object of class SpecialEuclidean. Parameter --------- n : int the dimension of the euclidean space that SE(n) acts upon point_type : str, {'vector', 'matrix'}, optional whether to represent elmenents of SE(n) by vectors or matrices if None is given, point_type is set to 'vector' for dimension 3 and 'matrix' otherwise epsilon : float, optional precision to use for calculations involving potential division by rotations default: 0 """ assert isinstance(n, int) and n > 1 self.n = n self.dimension = int((n * (n - 1)) / 2 + n) self.epsilon = epsilon self.default_point_type = point_type if point_type is None: self.default_point_type = 'vector' if n == 3 else 'matrix' super(SpecialEuclidean, self).__init__(dimension=self.dimension) self.rotations = SpecialOrthogonal(n=n, epsilon=epsilon) self.translations = Euclidean(dimension=n)
def __init__(self, n): super(_SpecialEuclideanMatrices, self).__init__(default_point_type='matrix', n=n + 1) self.rotations = SpecialOrthogonal(n=n) self.translations = Euclidean(dim=n) self.n = n self.dim = int((n * (n + 1)) / 2)
def setUp(self): """Set up the test""" self.n = 3 self.spd_cov_n = (self.n * (self.n + 1)) // 2 self.samples = 5 self.SPDManifold = SPDMatrices(self.n) self.Euclidean = Euclidean(self.n)
def setUp(self): warnings.simplefilter("ignore", category=UserWarning) gs.random.seed(0) self.dim = 2 self.euc = Euclidean(dim=self.dim) self.sphere = Hypersphere(dim=self.dim) self.euc_metric = EuclideanMetric(dim=self.dim) self.sphere_metric = HypersphereMetric(dim=self.dim) def _euc_metric_matrix(base_point): """Return matrix of Euclidean inner-product.""" dim = base_point.shape[-1] return gs.eye(dim) def _sphere_metric_matrix(base_point): """Return sphere's metric in spherical coordinates.""" theta = base_point[..., 0] mat = gs.array([[1.0, 0.0], [0.0, gs.sin(theta) ** 2]]) return mat new_euc_metric = RiemannianMetric(dim=self.dim) new_euc_metric.metric_matrix = _euc_metric_matrix new_sphere_metric = RiemannianMetric(dim=self.dim) new_sphere_metric.metric_matrix = _sphere_metric_matrix self.new_euc_metric = new_euc_metric self.new_sphere_metric = new_sphere_metric
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))
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())
def __new__(cls, dim, **kwargs): """Instantiate a Minkowski space. This is an instance of the `Euclidean` class endowed with the `MinkowskiMetric`. """ space = Euclidean(dim) space._metric = MinkowskiMetric(dim) return space
def setup_method(self): """Set up the test""" self.n = 3 self.spd_cov_n = (self.n * (self.n + 1)) // 2 self.samples = 5 self.spd = SPDMatrices(self.n) self.log_euclidean = SPDMetricLogEuclidean(self.n) self.affine_invariant = SPDMetricAffine(self.n) self.euclidean = Euclidean(self.n)
def __init__(self, epsilon=0.): super(_SpecialEuclidean3Vectors, self).__init__(dim=6, default_point_type='vector') self.n = 3 self.epsilon = epsilon self.rotations = SpecialOrthogonal(n=3, point_type='vector', epsilon=epsilon) self.translations = Euclidean(dim=3)
def __init__(self, n, epsilon=0.0): dim = n * (n + 1) // 2 LieGroup.__init__(self, dim=dim, default_point_type="vector") self.n = n self.epsilon = epsilon self.rotations = SpecialOrthogonal(n=n, point_type="vector", epsilon=epsilon) self.translations = Euclidean(dim=n)
def __init__(self, n): super().__init__( n=n + 1, dim=int((n * (n + 1)) / 2), default_point_type='matrix', lie_algebra=SpecialEuclideanMatrixLieAlgebra(n=n)) self.rotations = SpecialOrthogonal(n=n) self.translations = Euclidean(dim=n) self.n = n self.left_canonical_metric = \ SpecialEuclideanMatrixCannonicalLeftMetric(group=self)
def test_linear_mean(self): euclidean = Euclidean(3) point = euclidean.random_point(self.n_samples) estimator = ExponentialBarycenter(euclidean) estimator.fit(point) result = estimator.estimate_ expected = gs.mean(point, axis=0) self.assertAllClose(result, expected)
def __init__(self, n): super(_SpecialEuclideanMatrices, self).__init__(default_point_type='matrix', n=n + 1) self.rotations = SpecialOrthogonal(n=n) self.translations = Euclidean(dim=n) self.n = n self.dim = int((n * (n + 1)) / 2) translation_mask = gs.hstack( [gs.ones((self.n, ) * 2), 2 * gs.ones((self.n, 1))]) translation_mask = gs.concatenate( [translation_mask, gs.zeros((1, self.n + 1))], axis=0) self.translation_mask = translation_mask
def setup_method(self): gs.random.seed(1234) self.dimension = 2 self.space = Euclidean(self.dimension) self.metric = self.space.metric self.n_samples = 3 self.one_point_a = gs.array([0.0, 1.0]) self.one_point_b = gs.array([2.0, 10.0]) self.n_points_a = gs.array([[2.0, 1.0], [-2.0, -4.0], [-5.0, 1.0]]) self.n_points_b = gs.array([[2.0, 10.0], [8.0, -1.0], [-3.0, 6.0]])
def setUp(self): gs.random.seed(1234) self.dimension = 2 self.space = Euclidean(self.dimension) self.metric = self.space.metric self.n_samples = 3 self.one_point_a = gs.array([0., 1.]) self.one_point_b = gs.array([2., 10.]) self.n_points_a = gs.array([[2., 1.], [-2., -4.], [-5., 1.]]) self.n_points_b = gs.array([[2., 10.], [8., -1.], [-3., 6.]])
class ClosedDiscreteCurvesTestData(_ManifoldTestData): s2 = Hypersphere(dim=2) r2 = Euclidean(dim=2) r3 = Euclidean(dim=3) space_args_list = [(r2, ), (r3, )] shape_list = [(10, 2), (10, 3)] n_samples_list = random.sample(range(2, 5), 2) n_points_list = random.sample(range(2, 5), 2) n_vecs_list = random.sample(range(2, 5), 2) def random_point_belongs_test_data(self): smoke_space_args_list = [(self.s2, ), (self.r2, )] smoke_n_points_list = [1, 2] return self._random_point_belongs_test_data( smoke_space_args_list, smoke_n_points_list, self.space_args_list, self.n_points_list, ) def projection_belongs_test_data(self): smoke_space_args_list = [(self.r2, )] smoke_n_points_list = [1, 2] return self._projection_belongs_test_data(smoke_space_args_list, self.shape_list, smoke_n_points_list) def to_tangent_is_tangent_test_data(self): return self._to_tangent_is_tangent_test_data( ClosedDiscreteCurves, self.space_args_list, self.shape_list, self.n_vecs_list, ) def random_tangent_vec_is_tangent_test_data(self): return self._random_tangent_vec_is_tangent_test_data( ClosedDiscreteCurves, self.space_args_list, self.n_vecs_list) def projection_closed_curves_test_data(self): cells, _, _ = data_utils.load_cells() curves = [cell[:-10] for cell in cells[:5]] ambient_manifold = Euclidean(dim=2) smoke_data = [] for curve in curves: smoke_data += [ dict(ambient_manifold=ambient_manifold, curve=curve) ] return self.generate_tests(smoke_data)
class TestIntegrator(geomstats.tests.TestCase): def setup_method(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()) @staticmethod def function_linear(_state, _time): return 2.0 def _test_step(self, step): state = self.intercept result = step(self.function_linear, state, 0.0, self.dt) expected = state + 2 * self.dt self.assertAllClose(result, expected) def test_symplectic_euler_step(self): with pytest.raises(NotImplementedError): self._test_step(integrator.symplectic_euler_step) def test_leapfrog_step(self): with pytest.raises(NotImplementedError): self._test_step(integrator.leapfrog_step) def test_euler_step(self): self._test_step(integrator.euler_step) def test_rk2_step(self): self._test_step(integrator.rk2_step) def test_rk4_step(self): self._test_step(integrator.rk4_step) def test_integrator(self): initial_state = self.euclidean.random_point(2) def function(state, _time): _, velocity = state return gs.stack([velocity, gs.zeros_like(velocity)]) for step in ["euler", "rk2", "rk4"]: flow = integrator.integrate(function, initial_state, step=step) result = flow[-1][0] expected = initial_state[0] + initial_state[1] self.assertAllClose(result, expected)
def __init__(self, n): super().__init__( n=n + 1, dim=int((n * (n + 1)) / 2), embedding_space=GeneralLinear(n + 1, positive_det=True), submersion=submersion, value=gs.eye(n + 1), tangent_submersion=tangent_submersion, lie_algebra=SpecialEuclideanMatrixLieAlgebra(n=n)) self.rotations = SpecialOrthogonal(n=n) self.translations = Euclidean(dim=n) self.n = n self.left_canonical_metric = \ SpecialEuclideanMatrixCannonicalLeftMetric(group=self) self.metric = self.left_canonical_metric
def __init__(self, n, point_type=None, epsilon=0.): assert isinstance(n, int) and n > 1 self.n = n self.dimension = int((n * (n - 1)) / 2 + n) self.epsilon = epsilon self.default_point_type = point_type if point_type is None: self.default_point_type = 'vector' if n == 3 else 'matrix' super(SpecialEuclidean, self).__init__(dimension=self.dimension) self.rotations = SpecialOrthogonal(n=n, epsilon=epsilon) self.translations = Euclidean(dimension=n)
def test_srv_inner_product_elastic(self): """Test inner product of SRVMetric. Check that the pullback metric gives an elastic metric with parameters a=1, b=1/2. """ tangent_vec_a = gs.random.rand(self.n_sampling_points, 3) tangent_vec_b = gs.random.rand(self.n_sampling_points, 3) result = self.srv_metric_r3.inner_product( tangent_vec_a, tangent_vec_b, self.curve_a ) r3 = Euclidean(3) d_vec_a = (self.n_sampling_points - 1) * ( tangent_vec_a[1:, :] - tangent_vec_a[:-1, :] ) d_vec_b = (self.n_sampling_points - 1) * ( tangent_vec_b[1:, :] - tangent_vec_b[:-1, :] ) velocity_vec = (self.n_sampling_points - 1) * ( self.curve_a[1:, :] - self.curve_a[:-1, :] ) velocity_norm = r3.metric.norm(velocity_vec) unit_velocity_vec = gs.einsum("ij,i->ij", velocity_vec, 1 / velocity_norm) a_param = 1 b_param = 1 / 2 integrand = ( a_param**2 * gs.sum(d_vec_a * d_vec_b, axis=1) - (a_param**2 - b_param**2) * gs.sum(d_vec_a * unit_velocity_vec, axis=1) * gs.sum(d_vec_b * unit_velocity_vec, axis=1) ) / velocity_norm expected = gs.sum(integrand) / self.n_sampling_points self.assertAllClose(result, expected)
def setup_method(self): """Define the parameters to test.""" gs.random.seed(1234) self.n_neighbors = 3 self.dimension = 2 self.space = Euclidean(dim=self.dimension) self.distance = self.space.metric.dist
def __init__(self, n_draws): super(BinomialDistributions, self).__init__( dim=1, ambient_space=Euclidean(dim=1), metric=BinomialFisherRaoMetric(n_draws), ) self.n_draws = n_draws
def __init__(self, dim): super(_Hypersphere, self).__init__( dim=dim, embedding_space=Euclidean(dim + 1), submersion=lambda x: gs.sum(x**2, axis=-1), value=1., tangent_submersion=lambda v, x: 2 * gs.sum(x * v, axis=-1))
def test_space_derivative( self, dim, n_points, n_discretized_curves, n_sampling_points ): """Test space derivative. Check result on an example and vectorization. """ n_points = 3 dim = 3 srv_metric_r3 = SRVMetric(Euclidean(dim)) curve = gs.random.rand(n_points, dim) result = srv_metric_r3.space_derivative(curve) delta = 1 / n_points d_curve_1 = (curve[1] - curve[0]) / delta d_curve_2 = (curve[2] - curve[0]) / (2 * delta) d_curve_3 = (curve[2] - curve[1]) / delta expected = gs.squeeze( gs.vstack( ( gs.to_ndarray(d_curve_1, 2), gs.to_ndarray(d_curve_2, 2), gs.to_ndarray(d_curve_3, 2), ) ) ) self.assertAllClose(result, expected) path_of_curves = gs.random.rand(n_discretized_curves, n_sampling_points, dim) result = srv_metric_r3.space_derivative(path_of_curves) expected = [] for i in range(n_discretized_curves): expected.append(srv_metric_r3.space_derivative(path_of_curves[i])) expected = gs.stack(expected) self.assertAllClose(result, expected)
def setUp(self): """Define the parameters to test.""" gs.random.seed(1234) self.bandwidth = 1 self.dim = 2 self.space = Euclidean(dim=self.dim) self.distance = self.space.metric.dist
def __init__(self, dimension): assert isinstance(dimension, int) and dimension > 0 super(Hypersphere, self).__init__(dimension=dimension, embedding_manifold=Euclidean(dimension + 1)) self.embedding_metric = self.embedding_manifold.metric self.metric = HypersphereMetric(dimension)
def test_check_belongs(self): euclidean = Euclidean(5) point = gs.array([1, 2]) self.assertRaises( RuntimeError, lambda: geomstats.errors.check_belongs(point, euclidean))
def setUp(self): gs.random.seed(123) self.sphere = Hypersphere(dim=4) self.hyperbolic = Hyperboloid(dim=3) self.euclidean = Euclidean(dim=2) self.minkowski = Minkowski(dim=2) self.so3 = SpecialOrthogonal(n=3, point_type='vector') self.so_matrix = SpecialOrthogonal(n=3, point_type='matrix')
def __init__(self, dim, scale=1): super(PoincareBall, self).__init__(dim=dim, ambient_space=Euclidean(dim), scale=scale, metric=PoincareBallMetric(dim, scale)) self.coords_type = PoincareBall.default_coords_type self.point_type = PoincareBall.default_point_type
def __init__(self, dim, default_coords_type="extrinsic"): super(_Hypersphere, self).__init__( dim=dim, embedding_space=Euclidean(dim + 1), submersion=lambda x: gs.sum(x**2, axis=-1), value=1.0, tangent_submersion=lambda v, x: 2 * gs.sum(x * v, axis=-1), default_coords_type=default_coords_type, )