def check_running_and_state(self, t):
    ReportGuard(False)
    x = t()
    x.set_alphabet("abce")
    x.set_identity("e")
    x.add_rule("aa", "e")
    x.add_rule("bc", "e")
    x.add_rule("bbb", "e")
    x.add_rule("ababababababab", "e")
    x.add_rule("abacabacabacabacabacabacabacabac", "e")
    x.run_for(microseconds(1))

    if t != FpSemigroup or compare_version_numbers(libsemigroups_version(),
                                                   "1.1.0"):
        # FpSemigroup doesn't behave correctly wrt run_until or run_for in
        # versions less than 1.1.0
        self.assertTrue(x.stopped())
        self.assertFalse(x.finished())
        self.assertFalse(x.running())
        self.assertTrue(x.started())
        self.assertFalse(x.stopped_by_predicate())
        self.assertTrue(x.timed_out())

    x = t()
    x.set_alphabet("abce")
    x.set_identity("e")
    x.add_rule("aa", "e")
    x.add_rule("bc", "e")
    x.add_rule("bbb", "e")
    x.add_rule("ababababababab", "e")
    x.add_rule("abacabacabacabacabacabacabacabac", "e")

    n = 0

    def func():
        nonlocal n
        n += 1
        return n == 2

    x.run_until(func)

    if t != FpSemigroup or compare_version_numbers(libsemigroups_version(),
                                                   "1.1.0"):
        self.assertTrue(x.stopped())
        self.assertFalse(x.finished())
        self.assertFalse(x.running())
        self.assertTrue(x.started())
        self.assertTrue(x.stopped_by_predicate())
        self.assertFalse(x.timed_out())
 def test_run_until(self):
     if compare_version_numbers(libsemigroups_version(), "1.0.8"):
         ReportGuard(False)
         S = FroidurePin(Transformation([1, 0] + list(range(2, 10))))
         S.add_generator(Transformation(list(range(1, 10)) + [0]))
         S.run_until(lambda: S.current_size() > 10)
         self.assertFalse(S.finished())
         self.assertTrue(S.current_size() > 10)
         S.run_until(lambda: S.current_nr_rules() > 100)
         self.assertFalse(S.finished())
         self.assertTrue(S.current_nr_rules() > 100)
    def test_membership(self):
        S = FroidurePin(Transformation(list(range(1, 5)) + [0]))
        S.add_generator(Transformation([1, 0] + list(range(2, 5))))
        if compare_version_numbers(libsemigroups_version(), "1.0.8"):
            self.assertEqual(
                S.current_position(Transformation([2, 0, 3, 4, 1])),
                18446744073709551615,
            )

        self.assertEqual(S.at(10), Transformation([2, 0, 3, 4, 1]))
        with self.assertRaises(Exception):
            S.at(120)
        with self.assertRaises(Exception):
            S[120]

        self.assertTrue(S.contains(Transformation([2, 0, 3, 4, 1])))
        self.assertEqual(S.position(Transformation([2, 0, 3, 4, 1])), 10)
        self.assertEqual(S.sorted_position(Transformation([2, 0, 3, 4, 1])), 51)
        self.assertEqual(S.sorted_at(51), Transformation([2, 0, 3, 4, 1]))

        if compare_version_numbers(libsemigroups_version(), "1.0.8"):
            self.assertEqual(S.current_position(Transformation([2, 0, 3, 4, 1])), 10)
Esempio n. 4
0
    def test_constructors(self):
        ReportGuard(False)
        S = FroidurePin(Transformation([1, 0, 1]), Transformation([0, 0, 0]))
        T = FpSemigroup(S)
        self.assertFalse(T.has_froidure_pin())
        if compare_version_numbers(libsemigroups_version(), "1.1.0"):
            self.assertEqual(S.nr_rules(), T.nr_rules())
            self.assertEqual(
                S.rules(),
                [[[0, 1], [1]], [[1, 1], [1]], [[0, 0, 0], [0]], [[1, 0, 0], [1]]],
            )
            self.assertEqual(
                T.rules(),
                [
                    ["\x01\x02", "\x02"],
                    ["\x02\x02", "\x02"],
                    ["\x01\x01\x01", "\x01"],
                    ["\x02\x01\x01", "\x02"],
                ],
            )

        self.assertEqual(T.size(), S.size())
def check_attributes(self, t):
    ReportGuard(False)
    x = t()
    x.set_alphabet("abBe")
    x.set_identity("e")
    x.set_inverses("aBbe")

    x.add_rule("bb", "B")
    x.add_rule("BaBa", "abab")
    x.run()

    if compare_version_numbers("1.1.0", libsemigroups_version()):
        self.assertEqual(
            x.rules(),
            [
                ["ae", "a"],
                ["ea", "a"],
                ["be", "b"],
                ["eb", "b"],
                ["Be", "B"],
                ["eB", "B"],
                ["ee", "e"],
                ["aa", "e"],
                ["aa", "e"],
                ["bB", "e"],
                ["Bb", "e"],
                ["Bb", "e"],
                ["bB", "e"],
                ["ee", "e"],
                ["ee", "e"],
                ["bb", "B"],
                ["BaBa", "abab"],
            ],
        )
        self.assertEqual(x.nr_rules(), 17)
    else:
        self.assertEqual(
            x.rules(),
            [
                ["ae", "a"],
                ["ea", "a"],
                ["be", "b"],
                ["eb", "b"],
                ["Be", "B"],
                ["eB", "B"],
                ["ee", "e"],
                ["aa", "e"],
                ["bB", "e"],
                ["Bb", "e"],
                ["bb", "B"],
                ["BaBa", "abab"],
            ],
        )
        self.assertEqual(x.nr_rules(), 12)

    self.assertEqual(x.alphabet(), "abBe")
    self.assertFalse(x.has_froidure_pin())
    self.assertEqual(x.froidure_pin().size(), 24)
    self.assertEqual(x.identity(), "e")
    self.assertEqual(x.inverses(), "aBbe")
    self.assertFalse(x.is_obviously_infinite())
    self.assertTrue(x.is_obviously_finite())
    self.assertEqual(x.size(), 24)
Esempio n. 6
0
from libsemigroups_cppyy import compare_version_numbers, libsemigroups_version

if compare_version_numbers(libsemigroups_version(), "1.3.0"):
    import unittest
    from libsemigroups_cppyy import (
        Konieczny,
        Transformation,
        PartialPerm,
        ReportGuard,
    )
    import cppyy.gbl.std as std

    class TestKonieczny(unittest.TestCase):
        def test_init(self):
            ReportGuard(False)
            S = Konieczny(Transformation([1, 0, 1]), Transformation([0, 0, 0]))
            self.assertEqual(S.size(), 4)
            self.assertEqual(S.number_of_generators(), 2)

            with self.assertRaises(KeyError):
                Konieczny()

            with self.assertRaises(ValueError):
                Konieczny(PartialPerm([1, 2], [0, 1], 3),
                          Transformation([0, 1]))

            with self.assertRaises(TypeError):
                Konieczny({2, 3})
    def test_factorisation_and_relations(self):
        ReportGuard(False)
        S = FroidurePin(Transformation(list(range(1, 10)) + [0]))
        self.assertEqual(S.length_const(0), 1)

        with self.assertRaises(LibsemigroupsException):
            S.length_const(1)

        self.assertEqual(S.current_max_word_length(), 1)
        S.run()
        self.assertEqual(S.current_max_word_length(), 10)

        self.assertTrue(S.equal_to([0, 0], [0, 0]))
        self.assertFalse(S.equal_to([0], [0, 0]))
        self.assertFalse(S.equal_to([0] * 10, [0]))

        with self.assertRaises(LibsemigroupsException):
            self.assertFalse(S.equal_to([0] * 10, [1]))
        with self.assertRaises(LibsemigroupsException):
            self.assertFalse(S.equal_to([1], [0] * 10))

        for i in range(S.size()):
            self.assertEqual(S.length_const(i), i + 1)
            self.assertEqual(S.length_non_const(i), i + 1)

        with self.assertRaises(LibsemigroupsException):
            S.length_non_const(10)

        self.assertEqual(S.letter_to_pos(0), 0)
        with self.assertRaises(LibsemigroupsException):
            S.letter_to_pos(1)

        S.add_generator(Transformation([1, 0] + list(range(2, 10))))
        self.assertEqual(S.letter_to_pos(1), 10)
        S.add_generator(Transformation(list(range(1, 10)) + [0]))
        self.assertEqual(S.letter_to_pos(2), 0)
        self.assertEqual(
            [S.minimal_factorisation(x) for x in range(10)],
            [
                [0],
                [0, 0],
                [0, 0, 0],
                [0, 0, 0, 0],
                [0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [1, 1],
            ],
        )

        with self.assertRaises(LibsemigroupsCppyyException):
            S.next_relation()

        S = FroidurePin(Transformation(list(range(1, 5)) + [0]))
        S.add_generator(Transformation([1, 0] + list(range(2, 5))))
        self.assertEqual(S.current_nr_rules(), 0)
        self.assertEqual(S.word_to_pos([0, 1, 0, 1]), 18446744073709551615)

        self.assertEqual(S.nr_rules(), 25)
        self.assertEqual(
            S.word_to_element([0, 1, 0, 1]), Transformation([0, 3, 4, 1, 2])
        )
        self.assertEqual(S.word_to_pos([0, 1, 0, 1]), 15)

        S = FroidurePin(Transformation([1, 0, 1]), Transformation([0, 0, 0]))
        S.run()
        if compare_version_numbers(libsemigroups_version(), "1.1.0"):
            self.assertEqual(
                S.rules(),
                [[[0, 1], [1]], [[1, 1], [1]], [[0, 0, 0], [0]], [[1, 0, 0], [1]]],
            )