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)
def check_validation(self, t):
    ReportGuard(False)
    x = t()
    x.set_alphabet("ab")

    with self.assertRaises(TypeError):
        x.validate_letter("c")
    try:
        x.validate_letter("a")
    except Exception as e:
        self.fail(
            "unexpected exception raised for FpSemigroupInterface::validate_letter: "
            + e)

    with self.assertRaises(LibsemigroupsException):
        x.validate_letter(3)
    try:
        x.validate_letter(0)
    except Exception as e:
        self.fail(
            "unexpected exception raised for FpSemigroupInterface::validate_letter: "
            + e)
    with self.assertRaises(TypeError):
        x.validate_word("abc")
    try:
        x.validate_word("abab")
    except Exception as e:
        self.fail(
            "unexpected exception raised for FpSemigroupInterface::validate_letter: "
            + e)

    if compare_version_numbers(cppyy_version(), "1.7.1"):
        with self.assertRaises(LibsemigroupsException):
            x.validate_word([0, 1, 2])
    else:
        with self.assertRaises(TypeError):
            x.validate_word([0, 1, 2])
    try:
        x.validate_word([0, 1, 0, 1])
    except Exception as e:
        self.fail(
            "unexpected exception raised for FpSemigroupInterface::validate_letter: "
            + e)
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_operators(self, t):
    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()
    self.assertTrue(x.equal_to("bb", "B"))
    self.assertTrue(x.equal_to([1, 1], [2]))

    if compare_version_numbers(cppyy_version(), "1.7.1"):
        with self.assertRaises(LibsemigroupsException):
            x.equal_to([1, 1], [5])
    else:
        with self.assertRaises(TypeError):
            x.equal_to([1, 1], [5])

    with self.assertRaises(TypeError):
        x.equal_to("aa", "z")

    self.assertEqual(x.normal_form("bb"), "B")
    self.assertEqual(x.normal_form("B"), "B")
    self.assertEqual(x.normal_form([1, 1]), [2])
    self.assertEqual(x.normal_form([0, 0]), [3])

    with self.assertRaises(LibsemigroupsCppyyException):
        x.normal_form([1, 5])
    with self.assertRaises(LibsemigroupsCppyyException):
        x.normal_form("z")

    if hasattr(t, "rewrite"):
        self.assertEqual(x.rewrite("aa"), "")
        self.assertEqual(x.rewrite("bb"), "B")

        with self.assertRaises(TypeError):
            self.assertEqual(x.rewrite("z"))
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. 7
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]]],
            )