Пример #1
0
    def test_018(self):
        ReportGuard(False)
        twosided = congruence_kind.twosided
        S = FpSemigroup()
        S.set_alphabet(3)
        S.add_rule([0, 0], [0, 0])
        S.add_rule([0, 1], [1, 0])
        S.add_rule([0, 2], [2, 0])
        S.add_rule([0, 0], [0])
        S.add_rule([0, 2], [0])
        S.add_rule([2, 0], [0])
        S.add_rule([1, 0], [0, 1])
        S.add_rule([1, 1], [1, 1])
        S.add_rule([1, 2], [2, 1])
        S.add_rule([1, 1, 1], [1])
        S.add_rule([1, 2], [1])
        S.add_rule([2, 1], [1])

        self.assertTrue(S.is_obviously_infinite())

        cong = Congruence(twosided, S)
        cong.add_pair([0], [1])
        self.assertFalse(cong.finished())
        self.assertEqual(cong.number_of_non_trivial_classes(), 1)
        self.assertTrue(cong.finished())
        self.assertEqual(cong.non_trivial_classes(0),
                         [[0], [1], [0, 1], [1, 1], [0, 1, 1]])
 def test_020(self):
     ReportGuard(False)
     tc = ToddCoxeter(congruence_type.twosided)
     tc.set_nr_generators(1)
     tc.strategy(strategy.hlt)
     self.assertTrue(tc.contains([0, 0], [0, 0]))
     self.assertFalse(tc.contains([0, 0], [0]))
    def test_all(self):
        ReportGuard(False)
        S = FpSemigroup()
        S.set_alphabet("abcde")
        S.set_identity("e")
        S.add_rule("cacac", "aacaa")
        S.add_rule("acaca", "ccacc")
        S.add_rule("ada", "bbcbb")
        S.add_rule("bcb", "aadaa")
        S.add_rule("aaaa", "e")
        S.add_rule("ab", "e")
        S.add_rule("ba", "e")
        S.add_rule("cd", "e")
        S.add_rule("dc", "e")
        S.run()

        self.assertEqual(S.nr_rules(), 18)
        # self.assertEqual()
        self.assertTrue(
            S.equal_to(
                "abbbbbbbbbbbadddddddddddddddacccccccccccc",
                "aaccccccccccccccccccccccccccc",
            ))

        self.assertFalse(
            S.equal_to("abbbbbbbbbbbadddddddddddddddacccccccccccc", "a"))
    def test_operators(self):
        ReportGuard(False)
        tc = ToddCoxeter(congruence_kind.left)
        tc.set_number_of_generators(2)
        tc.add_pair([0, 0, 0, 0], [1])
        tc.add_pair([1, 1, 1, 1], [1])
        tc.run()
        tc = ToddCoxeter(congruence_kind.left)
        tc.set_number_of_generators(1)
        tc.add_pair([0, 0, 0], [0])
        tc.run_for(timedelta(microseconds=2))
        tc = ToddCoxeter(congruence_kind.right)
        tc.set_number_of_generators(1)
        tc.add_pair([0, 0, 0, 0, 0, 0, 0, 0], [0])
        tc.run_until(lambda: tc.const_contains([0, 0, 0, 0, 0, 0, 0, 0], [0])
                     == tril.true)
        self.assertTrue(tc.stopped_by_predicate())
        self.assertFalse(tc.finished())

        tc = ToddCoxeter(congruence_kind.left)
        tc.set_number_of_generators(2)
        tc.add_pair([0, 0, 0, 0], [1])
        tc.add_pair([1, 0], [0, 1])
        tc.add_pair([1, 1, 1, 1], [0])
        tc.run_for(timedelta(microseconds=1))
        # If this is run on an incredibly fast computer, this
        # test of timed_out() may not work.
        self.assertTrue(tc.timed_out())
        tc.run()
        self.assertFalse(tc.timed_out())
def check_initialisation(self, t):
    ReportGuard(False)
    x = t()
    x.set_alphabet("ba")
    x.add_rule([0, 1], [1, 0])

    with self.assertRaises(RuntimeError):
        x.add_rule([0, 1], [2])

    # S = FroidurePin(Transformation([1, 2, 0]), Transformation([1, 0, 2]))
    # x.add_rules(S)
    # self.assertEqual(x.size(), 2)

    x = t()
    x.set_alphabet("abBe")
    x.set_identity("e")

    x.set_inverses("aBbe")

    x.add_rule("bb", "B")
    x.add_rule("BaBa", "abab")
    self.assertEqual(x.size(), 24)

    x = t()
    x.set_alphabet(1)
    x.set_identity(0)
    self.assertEqual(x.size(), 1)
 def test_036(self):
     ReportGuard(False)
     S = FroidurePin(Transf([1, 3, 4, 2, 3]), Transf([3, 2, 1, 3, 3]))
     tc = ToddCoxeter(congruence_kind.twosided, S)
     tc.set_number_of_generators(2)
     tc.add_pair([0], [1])
     tc.add_pair([0, 0], [0])
     self.assertEqual(tc.number_of_classes(), 1)
Пример #7
0
def check_cayley_graphs(S):
    ReportGuard(False)

    g = S.right_cayley_graph()
    assert len(g) == S.size()
    assert len(g[0]) == S.number_of_generators()
    g = S.left_cayley_graph()
    assert len(g) == S.size()
    assert len(g[0]) == S.number_of_generators()
Пример #8
0
def test_froidure_pin_int_mat(checks_for_froidure_pin, checks_for_generators):
    ReportGuard(False)
    gens = [IntMat([[0, -3], [-2, -10]])]
    assert FroidurePin(gens).size() == 64

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
Пример #9
0
def test_froidure_pin_max_plus(checks_for_froidure_pin, checks_for_generators):
    ReportGuard(False)
    gens = [MaxPlusMat([[0, -3], [-2, -10]])]
    assert FroidurePin(gens).size() == 2

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
Пример #10
0
def test_froidure_pin_ntp(checks_for_froidure_pin, checks_for_generators):
    ReportGuard(False)
    gens = [Matrix(MatrixKind.NTP, 5, 7, [[1, 1], [1, 1]])]
    assert FroidurePin(gens).size() == 6

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
Пример #11
0
def check_settings(S):
    ReportGuard(False)
    assert not S.immutable()
    S.immutable(False)
    assert S.batch_size() == 8192
    S.batch_size(S.batch_size())
    assert S.concurrency_threshold() == 823543
    S.concurrency_threshold(S.concurrency_threshold())
    S.max_threads(2)
    assert S.max_threads() == 2
    S.reserve(100)
def check_running_and_state(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")
    check_runner(x, timedelta(microseconds=1000))
Пример #13
0
def test_froidure_pin_min_plus(checks_for_froidure_pin, checks_for_generators):
    ReportGuard(False)
    x = MinPlusMat(2, 2)
    gens = [MinPlusMat([[1, 0], [0, x.zero()]])]
    assert FroidurePin(gens).size() == 3

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
Пример #14
0
def check_constructors(coll):
    ReportGuard(False)
    # default constructor
    S = FroidurePin(coll[0])
    S.add_generators(coll[1:])

    # copy constructor
    U = FroidurePin(S)
    assert S is not U
    assert S.number_of_generators() == U.number_of_generators()
    assert S.current_size() == U.current_size()
Пример #15
0
def test_froidure_pin_kbe(checks_for_froidure_pin):
    ReportGuard(False)
    kb = KnuthBendix()
    kb.set_alphabet(2)
    kb.add_rule([0, 0, 0, 0], [0])
    kb.add_rule([1, 1, 1, 1], [1])
    kb.add_rule([0, 1], [1, 0])

    assert kb.size() == 15

    for check in checks_for_froidure_pin:
        check(FroidurePin(kb.froidure_pin()))
Пример #16
0
def test_froidure_pin_tce(checks_for_froidure_pin):
    ReportGuard(False)
    tc = ToddCoxeter(congruence_kind.twosided)
    tc.set_number_of_generators(2)
    tc.add_pair([0, 0, 0, 0], [0])
    tc.add_pair([1, 1, 1, 1], [1])
    tc.add_pair([0, 1], [1, 0])

    assert tc.number_of_classes() == 15

    for check in checks_for_froidure_pin:
        check(FroidurePin(tc.quotient_froidure_pin()))
Пример #17
0
def test_froidure_pin_min_plus_trunc(
    checks_for_froidure_pin, checks_for_generators
):
    ReportGuard(False)
    gens = [Matrix(MatrixKind.MinPlusTrunc, 11, [[1, 0], [0, 1]])]
    assert FroidurePin(gens).size() == 2

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
Пример #18
0
def test_froidure_pin_proj_max_plus(
    checks_for_froidure_pin, checks_for_generators
):
    ReportGuard(False)
    x = Matrix(MatrixKind.ProjMaxPlus, 2, 2)
    gens = [Matrix(MatrixKind.ProjMaxPlus, [[1, 0], [0, x.zero()]])]
    assert FroidurePin(gens).size() == 2

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
 def test_033(self):
     ReportGuard(False)
     tc1 = ToddCoxeter(congruence_type.twosided)
     tc1.set_nr_generators(2)
     tc1.add_pair([0, 0, 0], [0])
     tc1.add_pair([0], [1, 1])
     self.assertEqual(tc1.nr_classes(), 5)
     tc2 = ToddCoxeter(congruence_type.left, tc1)
     tc2.next_lookahead(1)
     tc2.report_every(timedelta(microseconds=1))
     self.assertFalse(tc2.empty())
     tc2.add_pair([0], [0, 0])
     self.assertEqual(tc2.nr_classes(), 3)
Пример #20
0
def test_froidure_pin_bmat(checks_for_froidure_pin, checks_for_generators):
    ReportGuard(False)
    gens = [
        Matrix(MatrixKind.Boolean, [[0, 1], [1, 0]]),
        Matrix(MatrixKind.Boolean, [[1, 0], [1, 1]]),
        Matrix(MatrixKind.Boolean, [[1, 0], [0, 0]]),
    ]
    assert FroidurePin(gens).size() == 16

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
Пример #21
0
def test_froidure_pin_bmat8(checks_for_froidure_pin, checks_for_generators):
    ReportGuard(False)
    gens = [
        BMat8([[0, 1], [1, 0]]),
        BMat8([[1, 0], [1, 1]]),
        BMat8([[1, 0], [0, 0]]),
    ]
    assert FroidurePin(gens).size() == 16

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
Пример #22
0
def check_accessors(S):
    ReportGuard(False)
    # current_size
    assert S.current_size() == S.number_of_generators()
    S.run()
    # size
    assert S.current_size() == S.size()

    # at, sorted_at
    for i in range(S.size()):
        assert S.at(i) == S.sorted_at(S.position_to_sorted_position(i))

    # __iter__, sorted
    assert sorted(list(S)) == list(S.sorted())
Пример #23
0
def test_froidure_pin_perm(checks_for_froidure_pin, checks_for_generators):
    ReportGuard(False)
    for T in (Perm16, Perm1, Perm2, Perm4):
        add = list(range(4, 16)) if T is Perm16 else []
        gens = [
            T.make([1, 0] + list(range(2, 4)) + add),
            T.make(list(range(1, 4)) + [0] + add),
        ]
        assert FroidurePin(gens).size() == 24

        for check in checks_for_generators:
            check(gens)

        for check in checks_for_froidure_pin:
            check(FroidurePin(gens))
Пример #24
0
def test_froidure_pin_bipart(checks_for_froidure_pin, checks_for_generators):
    ReportGuard(False)
    T = Bipartition
    gens = [
        T.make([0, 1, 1, 0]),
        T.make([0, 1, 2, 1]),
        T.make([0, 0, 0, 0]),
    ]
    assert FroidurePin(gens).size() == 15

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
Пример #25
0
def test_froidure_pin_pbr(checks_for_froidure_pin, checks_for_generators):
    ReportGuard(False)
    T = PBR
    gens = [
        T.make([[], [0]]),
        T.make([[0, 1], [0]]),
        T.make([[1], []]),
        T.make([[1], [0, 1]]),
    ]
    assert FroidurePin(gens).size() == 15

    for check in checks_for_generators:
        check(gens)

    for check in checks_for_froidure_pin:
        check(FroidurePin(gens))
    def test_settings(self):
        ReportGuard(False)
        tc = ToddCoxeter(congruence_kind.left)
        tc.set_number_of_generators(1)
        tc.add_pair([0, 0, 0, 0], [0, 0])
        tc.reserve(10)
        tc.run()

        self.assertFalse(tc.is_standardized())
        tc.standardize(order.lex)
        self.assertTrue(tc.is_standardized())
        tc.standardize(order.shortlex)
        tc.standardize(order.recursive)
        tc.standardize(True)
        tc.standardize(False)
        tc.standardize(1)
        with self.assertRaises(TypeError):
            tc.standardize("shooortlex")

        S = FroidurePin(Transf([3, 1, 2, 1, 2]), Transf([1, 1, 1, 2, 2]))
        tc = ToddCoxeter(congruence_kind.twosided, S)
        tc.froidure_pin_policy()
        tc.froidure_pin_policy(fpp.none)
        self.assertEqual(tc.froidure_pin_policy(), fpp.none)
        tc.froidure_pin_policy(fpp.use_relations)
        self.assertEqual(tc.froidure_pin_policy(), fpp.use_relations)
        tc.froidure_pin_policy(fpp.use_cayley_graph)
        self.assertEqual(tc.froidure_pin_policy(), fpp.use_cayley_graph)
        with self.assertRaises(TypeError):
            tc.froidure_pin_policy(1)
        with self.assertRaises(TypeError):
            tc.froidure_pin_policy("userelations")

        tc = ToddCoxeter(congruence_kind.left)
        tc.set_number_of_generators(2)
        tc.add_pair([0, 0, 0, 0], [0])
        tc.add_pair([1, 1, 1, 1], [0])
        with self.assertRaises(TypeError):
            tc.strategy("feelsch")
        with self.assertRaises(TypeError):
            tc.strategy(1)
        tc.strategy(strategy.felsch)
        self.assertEqual(tc.strategy(), strategy.felsch)
        tc.strategy(strategy.hlt)
        self.assertEqual(tc.strategy(), strategy.hlt)
        tc.strategy(strategy.random)
        self.assertEqual(tc.strategy(), strategy.random)
Пример #27
0
def check_idempotents(S):
    ReportGuard(False)

    try:
        assert all((x * x == x for x in S.idempotents()))
    except TypeError:  # no multiplication provides
        pass

    assert all(
        S.fast_product(S.position(x), S.position(x)) == S.position(x)
        for x in S.idempotents()
    )

    assert (
        sum(1 for x in range(S.size()) if S.is_idempotent(x))
        == S.number_of_idempotents()
    )
    def test_attributes(self):
        ReportGuard(False)
        tc = ToddCoxeter(congruence_type.left)
        tc.set_nr_generators(1)
        tc.add_pair([0, 0, 0, 0, 0, 0], [0, 0, 0])
        self.assertEqual(tc.nr_classes(), 5)
        self.assertTrue(tc.contains([0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0]))
        self.assertFalse(tc.contains([0, 0, 0], [0, 0]))
        self.assertEqual(tc.const_contains([0, 0, 0], [0, 0]), tril.false)
        self.assertEqual(tc.kind(), congruence_type.left)
        self.assertFalse(tc.empty())
        self.assertEqual(tc.class_index_to_word(1), [0, 0])
        self.assertEqual(tc.word_to_class_index([0, 0]), 1)
        self.assertFalse(tc.has_parent_froidure_pin())
        self.assertEqual(tc.nr_generators(), 1)
        self.assertEqual(tc.nr_generating_pairs(), 1)
        self.assertTrue(tc.less([0], [0, 0]))

        # TODO(now): uncomment
        # S = FroidurePin(Transformation([1, 2, 2]), Transformation([2, 0, 1]))
        # tc = ToddCoxeter(congruence_type.twosided, S)
        # self.assertEqual(tc.nr_classes(), 24)
        # self.assertTrue(tc.has_parent_froidure_pin())
        # try:
        #     tc.parent_froidure_pin()
        # except:
        #     self.fail("unexpected exception thrown")
        # try:
        #     tc.quotient_froidure_pin()
        # except:
        #     self.fail("unexpected exception thrown")

        # K = KnuthBendix()
        # K.set_alphabet("a")
        # K.add_rule("aaaa", "aa")
        # tc = ToddCoxeter(congruence_type.left, K)
        # self.assertEqual(tc.nr_classes(), 3)

        tc = ToddCoxeter(congruence_type.twosided)
        tc.set_nr_generators(1)
        self.assertTrue(tc.is_quotient_obviously_infinite())
        self.assertFalse(tc.is_quotient_obviously_finite())
        tc.add_pair([0, 0, 0], [0])
        self.assertFalse(tc.is_quotient_obviously_infinite())
        tc.run()
        self.assertTrue(tc.is_quotient_obviously_finite())
def check_validation(self, t):
    ReportGuard(False)
    x = t()
    x.set_alphabet("ab")

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

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

    with self.assertRaises(RuntimeError):
        x.validate_word([0, 1, 2])

    try:
        x.validate_word([0, 1, 0, 1])
    except RuntimeError as e:
        self.fail(
            "unexpected exception raised for FpSemigroupInterface::validate_letter: "
            + e
        )
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(timedelta(microseconds=1))

    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)

    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())