Esempio n. 1
0
    def test_diagonal(self):
        base_set = Set(Atom(1))
        diag_rels = [relations.diag(*base_set), relations.diag(1)]
        for diag_rel in diag_rels:
            self.assertEqual(diag_rel.cardinality, 1)
            self.assertTrue(relations.is_member(diag_rel))
            self.assertTrue(diag_rel.has_element(Couplet(1, 1)))

        base_set = Set(Atom(1), Atom('a'))
        diag_clan = clans.diag(1, 'a')
        self.assertEqual(diag_clan.cardinality, 1)
        diag_rels = [
            relations.diag(*base_set),
            relations.diag(1, 'a'),
            sets.single(diag_clan)
        ]
        for diag_rel in diag_rels:
            self.assertEqual(diag_rel.cardinality, 2)
            self.assertTrue(relations.is_member(diag_rel))
            self.assertTrue(diag_rel.has_element(Couplet(1, 1)))
            self.assertTrue(diag_rel.has_element(Couplet('a', 'a')))

        arg1 = Set(1, 2, 3)
        arg2 = [v for v in Set(1, 2, 3)]
        result_diag = Set(Couplet(1, 1), Couplet(2, 2), Couplet(3, 3))
        self.assertEqual(relations.diag(*arg1), result_diag)
        self.assertEqual(relations.diag(*arg2), result_diag)
Esempio n. 2
0
    def test_diagonal(self):
        base_set = Set(Atom(1))
        diag_rels = [
            relations.diag(*base_set), relations.diag(1)]
        for diag_rel in diag_rels:
            self.assertEqual(diag_rel.cardinality, 1)
            self.assertTrue(relations.is_member(diag_rel))
            self.assertTrue(diag_rel.has_element(Couplet(1, 1)))

        base_set = Set(Atom(1), Atom('a'))
        diag_clan = clans.diag(1, 'a')
        self.assertEqual(diag_clan.cardinality, 1)
        diag_rels = [
            relations.diag(*base_set), relations.diag(1, 'a'), sets.single(diag_clan)]
        for diag_rel in diag_rels:
            self.assertEqual(diag_rel.cardinality, 2)
            self.assertTrue(relations.is_member(diag_rel))
            self.assertTrue(diag_rel.has_element(Couplet(1, 1)))
            self.assertTrue(diag_rel.has_element(Couplet('a', 'a')))

        arg1 = Set(1, 2, 3)
        arg2 = [v for v in Set(1, 2, 3)]
        result_diag = Set(Couplet(1, 1), Couplet(2, 2), Couplet(3, 3))
        self.assertEqual(relations.diag(*arg1), result_diag)
        self.assertEqual(relations.diag(*arg2), result_diag)
Esempio n. 3
0
# By extending superstriction to clans, which are sets of sets (of Couplets), we can define a helpful
# mechanism to restrict vocab_clan to only those relations that contain particular values.
import algebraixlib.algebras.clans as clans
salutation_records_clan = clans.superstrict(vocab_clan, Set(Set(Couplet('meaning', 'salutation'))))
earth_records_clan = clans.superstrict(vocab_clan, Set(Set(Couplet('meaning', 'earth'))))
print("salutation_records_clan:", salutation_records_clan)
print("earth_records_clan:", earth_records_clan)

# By choosing an appropriate right-hand argument, our extended composition operation from earlier can
# model projection.
words_langs_clan = Set(Set(Couplet('word', 'word'), Couplet('language', 'language')))
print("words_langs_clan:", words_langs_clan)

# The relations.diag and clans.diag utility functions create a "diagonal" relation or clan,
# respectively, with simpler syntax.
assert words_langs_clan == clans.diag('word', 'language')

# Since the meaning of each set of records ('salutation') is invariant among the relations in
# salutation_records_clan, we can drop those Couplets. Note that the cardinality of the resulting
# clan is the same, but each relation now contains only two Couplets.
salutation_words_n_langs_clan = clans.compose(salutation_records_clan, words_langs_clan)
print("salutation_words_n_langs_clan:", salutation_words_n_langs_clan)

# However, we can take this one step further and "rename" the 'word' attribute to something more
# specific by replacing the value 'word' with 'salutation' everywhere we find it as the left of a
# Couplet. By doing this, we both compress the information in each relation and also set our data up
# for later processing.
salutations_n_langs_clan = clans.compose(salutation_words_n_langs_clan,
                                         Set(Set(Couplet("salutation", "word"),
                                                 Couplet("language", "language"))))
print("salutations_n_langs_clan:", salutations_n_langs_clan)
Esempio n. 4
0
salutation_records_clan = clans.superstrict(
    vocab_clan, Set(Set(Couplet('meaning', 'salutation'))))
earth_records_clan = clans.superstrict(vocab_clan,
                                       Set(Set(Couplet('meaning', 'earth'))))
print("salutation_records_clan:", salutation_records_clan)
print("earth_records_clan:", earth_records_clan)

# By choosing an appropriate right-hand argument, our extended composition operation from earlier
# can model projection.
words_langs_clan = Set(
    Set(Couplet('word', 'word'), Couplet('language', 'language')))
print("words_langs_clan:", words_langs_clan)

# The relations.diag and clans.diag utility functions create a "diagonal" relation or clan,
# respectively, with simpler syntax.
assert words_langs_clan == clans.diag('word', 'language')

# Since the meaning of each set of records ('salutation') is invariant among the relations in
# salutation_records_clan, we can drop those Couplets. Note that the cardinality of the resulting
# clan is the same, but each relation now contains only two Couplets.
salutation_words_n_langs_clan = clans.compose(salutation_records_clan,
                                              words_langs_clan)
print("salutation_words_n_langs_clan:", salutation_words_n_langs_clan)

# However, we can take this one step further and "rename" the 'word' attribute to something more
# specific by replacing the value 'word' with 'salutation' everywhere we find it as the left of a
# Couplet. By doing this, we both compress the information in each relation and also set our data up
# for later processing.
salutations_n_langs_clan = clans.compose(
    salutation_words_n_langs_clan,
    Set(Set(Couplet("salutation", "word"), Couplet("language", "language"))))