def test_forward(self): precise_guess_loss = PreciseGuessLoss() predicted_cubes = (torch.stack([ Magma(FOUR_GROUP).probabilistic_cube, CyclicGroup(4).probabilistic_cube, ]).to(CURRENT_DEVICE).view(-1, 4, 4, 4)) target_cubes = (torch.stack([ Magma(FOUR_GROUP).probabilistic_cube, Magma(FOUR_GROUP).probabilistic_cube, ]).to(CURRENT_DEVICE).view(-1, 4, 4, 4)) self.assertEqual(precise_guess_loss(predicted_cubes, target_cubes), 0.5) self.assertEqual(precise_guess_loss(target_cubes, target_cubes), 1.0)
def test_forward(self): cayley_cube = (torch.stack([ Magma(FOUR_GROUP).probabilistic_cube, Magma(FOUR_GROUP).probabilistic_cube, ]).view(-1, 4, 4, 4).to(CURRENT_DEVICE)) self.assertEqual( self.magma_dae(cayley_cube).detach().sum().item(), 32.0, ) self.assertEqual( self.magma_vae(cayley_cube).detach().sum().item(), 32.0, )
def random_semigroup(dim: int, maximal_tries: int) -> Tuple[bool, torch.Tensor]: """ randomly search for a semigroup Cayley table. Not recommended to use with dim > 4 :param dim: number of elements in a semigroup :param maximal_tries: how many times to try at most :returns: a pair (whether the Cayley table is associative, a Cayley table of a magma) """ associative = False try_count = 0 magma = Magma(cardinality=dim) while not associative and try_count <= maximal_tries: magma = Magma(cardinality=dim) associative = magma.is_associative try_count += 1 return associative, magma.cayley_table
def get_magma_by_index(cardinality: int, index: int) -> Magma: """ find a magma from a lexicographical order by its index :param cardinality: the number of elements in a magma :param index: an index of magma in a lexicographical order :returns: a magma with a given index """ square = cardinality**2 if index < 0 or index >= cardinality**square: raise ValueError(""" An index must be non negative and less than $n^(n^2)$""") cayley_table = list() residual = index for _ in range(square): cayley_table.append(residual % cardinality) residual = residual // cardinality return Magma( torch.tensor(list(reversed(cayley_table))).reshape( cardinality, cardinality))
def test_guessed_ratio(self): ratio = guessed_ratio( Magma(FOUR_GROUP).probabilistic_cube.view(-1, 4, 4, 4), Magma(FOUR_GROUP).probabilistic_cube.view(-1, 4, 4, 4), ) self.assertTrue(torch.tensor(1.0).allclose(ratio))
def test_associate_ratio(self): ratio = associative_ratio( Magma(FOUR_GROUP).probabilistic_cube.view(-1, 4, 4, 4), torch.tensor(0.0), ) self.assertTrue(torch.tensor(1.0).allclose(ratio))
def test_is_associative(self): self.assertFalse(Magma(torch.tensor([[0, 0], [1, 0]])).is_associative) self.assertTrue(self.klein_group.is_associative) self.assertFalse(self.some_magma.is_associative)
def setUp(self): torch.manual_seed(777) self.some_magma = Magma(NON_ASSOCIATIVE_MAGMA) self.klein_group = Magma(FOUR_GROUP)