Skip to content

relation_list.py

from pymwp import RelationList

RelationList

Relation list holds a list of Relations.

It provides methods for performing operations collectively on all relations in the list.

Source code in pymwp/relation_list.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
class RelationList:
    """
    Relation list holds a list of [`Relations`](relation.md).

    It provides methods for performing operations collectively on all
    relations in the list.
    """

    def __init__(self, variables: Optional[List[str]] = None,
                 relation_list: Optional[List[Relation]] = None):
        """Create relation list.

        When creating a relations list, specify either `variables` or
        `relation_list`.

        Specifying a `relation_list` argument, whose value is a list of
        `Relations`, will initialize a `RelationList` containing the provided
        relations.

        If no `relation_list` argument is provided, the constructor will
        create a relation list with 1 relation, using the provided `variables`
        to initialize that relation.

        See [`RelationList.identity()`](relation_list.md#pymwp.relation_list
        .RelationList.identity) for creating a relation list containing an
        identity relation.

        Example:

        Create relation list using specific variables

        ```python
        rel_list = RelationList(['X0', 'X1', 'X2'])

        # Generates a list with 1 relation:
        #
        # 1:      X0 |  +o  +o  +o
        #         X1 |  +o  +o  +o
        #         X2 |  +o  +o  +o
        ```

        Create relation list by providing relations

        ```python
        rel_list = RelationList(relation_list = [Relation(['X0', 'X1']),
        Relation(['X0'])])

        # Generates a list with 2 relations:
        #
        # 1:      X0  |  +o  +o
        #         X1  |  +o  +o
        #
        # 2:      X0  |  +o
        ```

        If no arguments are provided, the result is a relation list with an
        empty relation.

        ```python
        rel_list = RelationList()

        # Generates a list with 1 empty relation:
        #
        # 1:       ε
        ```


        Arguments:
            variables: list of variables used to initialize a relation on
                relation list.
            relation_list: list of relations for initializing relation list
        """
        self.relations = relation_list or [Relation(variables)]

    @staticmethod
    def identity(variables: List[str]) -> RelationList:
        """Create relation list that contains 1
        [identity relation](relation.md#pymwp.relation.Relation.identity).

        This is an alternative way to construct a relation list.

        Example:

        Create relation list containing an identity relation

        ```python
        rel_list = RelationList.identity(['X0', 'X1', 'X2'])

        # Generates a list with 1 identity relation:
        #
        # 1:      X0 |  +m  +o  +o
        #         X1 |  +o  +m  +o
        #         X2 |  +o  +o  +m
        ```

        Arguments:
            variables: list of variables

        Returns:
            RelationList that contains identity relation generated using the
                provided variables.
        """
        return RelationList(relation_list=[Relation.identity(variables)])

    def __str__(self) -> str:
        relations = '\n\n'.join([f'{r}' for r in self.relations])
        divider = '\n' + ('-' * 72)
        return divider + '\n' + relations + divider

    def __add__(self, other):
        return RelationList(relation_list=[
            r1 + r2 for r1 in self.relations
            for r2 in other.relations])

    @property
    def first(self):
        """Gets the first relation in relation list."""
        return self.relations[0]

    def replace_column(self, vector: list, variable: str) -> None:
        """For each relation in a relation list, replace column with a provided
         vector, in place.

        This method takes as input a variable, then finds the index of that
        variable based on its name, then applies the column replacement at the
        discovered index.

        Arguments:
            vector: vector that will replace a column
            variable: variable value; replace will occur at the index of this
                variable.

        Raises:
            ValueError: if variable does not exists some relation belonging
                to this relation list.
        """

        self.relations = [rel.replace_column(vector, variable)
                          for rel in self.relations]

    def composition(self, other: RelationList) -> None:
        """Apply composition to all relations in two relation lists.

        This method takes as argument `other` relation list, then composes the
        product of `self` and `other` by computing the product of each
        relation, for all combinations.

        Composition occurs in place. After composition `self` will contain all
        unique relations obtained during composition.

        To compose `RelationList` and a single `Relation`, see
        [`one_composition()`](relation_list.md#pymwp.relation_list.
        RelationList.one_composition).

        Arguments:
            other: RelationList to compose with `self`
        """
        new_list = []
        for r1 in self.relations:
            for r2 in other.relations:
                output = r1 * r2
                if not RelationList.contains_matrix(new_list, output.matrix):
                    new_list.append(output)

        self.relations = new_list

    @staticmethod
    def contains_matrix(search_in: List[Relation], matrix: List[List]) -> bool:
        """Check if a list of relations contains the provided matrix.

        Arguments:
            search_in: list of relations to search
            matrix: search value to look for

        Returns:
            `True` if matrix is found somewhere in the list of relations and
                `False` otherwise.
        """
        for relation in search_in:
            if relation.matrix == matrix:
                return True
        return False

    def one_composition(self, relation: Relation) -> None:
        """Compose each relation in a relation list × relation.

        This method iterates current relation list and applies
        [`composition()`](relation.md#pymwp.relation.Relation.composition) to
        each of its relations, using argument `relation` as the other operand.

        Arguments:
            relation: relation to compose with relations in current list.
        """
        self.relations = [rel * relation for rel in self.relations]

    def fixpoint(self) -> None:
        """Apply [fixpoint](relation.md#pymwp.relation.Relation.fixpoint)
         to all relations in relation list."""
        self.relations = [rel.fixpoint() for rel in self.relations]

    def show(self) -> None:
        """Display relation list."""
        print(str(self))

    def while_correction(self, dg: DeltaGraph) -> None:
        """Apply [`while_correction()`](relation.md#pymwp.relation.Relation
        .while_correction) to all relations in a relation list."""
        for rel in self.relations:
            rel.while_correction(dg)

first property

Gets the first relation in relation list.

__init__(variables=None, relation_list=None)

Create relation list.

When creating a relations list, specify either variables or relation_list.

Specifying a relation_list argument, whose value is a list of Relations, will initialize a RelationList containing the provided relations.

If no relation_list argument is provided, the constructor will create a relation list with 1 relation, using the provided variables to initialize that relation.

See RelationList.identity() for creating a relation list containing an identity relation.

Example:

Create relation list using specific variables

rel_list = RelationList(['X0', 'X1', 'X2'])

# Generates a list with 1 relation:
#
# 1:      X0 |  +o  +o  +o
#         X1 |  +o  +o  +o
#         X2 |  +o  +o  +o

Create relation list by providing relations

rel_list = RelationList(relation_list = [Relation(['X0', 'X1']),
Relation(['X0'])])

# Generates a list with 2 relations:
#
# 1:      X0  |  +o  +o
#         X1  |  +o  +o
#
# 2:      X0  |  +o

If no arguments are provided, the result is a relation list with an empty relation.

rel_list = RelationList()

# Generates a list with 1 empty relation:
#
# 1:       ε

Parameters:

Name Type Description Default
variables Optional[List[str]]

list of variables used to initialize a relation on relation list.

None
relation_list Optional[List[Relation]]

list of relations for initializing relation list

None
Source code in pymwp/relation_list.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def __init__(self, variables: Optional[List[str]] = None,
             relation_list: Optional[List[Relation]] = None):
    """Create relation list.

    When creating a relations list, specify either `variables` or
    `relation_list`.

    Specifying a `relation_list` argument, whose value is a list of
    `Relations`, will initialize a `RelationList` containing the provided
    relations.

    If no `relation_list` argument is provided, the constructor will
    create a relation list with 1 relation, using the provided `variables`
    to initialize that relation.

    See [`RelationList.identity()`](relation_list.md#pymwp.relation_list
    .RelationList.identity) for creating a relation list containing an
    identity relation.

    Example:

    Create relation list using specific variables

    ```python
    rel_list = RelationList(['X0', 'X1', 'X2'])

    # Generates a list with 1 relation:
    #
    # 1:      X0 |  +o  +o  +o
    #         X1 |  +o  +o  +o
    #         X2 |  +o  +o  +o
    ```

    Create relation list by providing relations

    ```python
    rel_list = RelationList(relation_list = [Relation(['X0', 'X1']),
    Relation(['X0'])])

    # Generates a list with 2 relations:
    #
    # 1:      X0  |  +o  +o
    #         X1  |  +o  +o
    #
    # 2:      X0  |  +o
    ```

    If no arguments are provided, the result is a relation list with an
    empty relation.

    ```python
    rel_list = RelationList()

    # Generates a list with 1 empty relation:
    #
    # 1:       ε
    ```


    Arguments:
        variables: list of variables used to initialize a relation on
            relation list.
        relation_list: list of relations for initializing relation list
    """
    self.relations = relation_list or [Relation(variables)]

composition(other)

Apply composition to all relations in two relation lists.

This method takes as argument other relation list, then composes the product of self and other by computing the product of each relation, for all combinations.

Composition occurs in place. After composition self will contain all unique relations obtained during composition.

To compose RelationList and a single Relation, see one_composition().

Parameters:

Name Type Description Default
other RelationList

RelationList to compose with self

required
Source code in pymwp/relation_list.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def composition(self, other: RelationList) -> None:
    """Apply composition to all relations in two relation lists.

    This method takes as argument `other` relation list, then composes the
    product of `self` and `other` by computing the product of each
    relation, for all combinations.

    Composition occurs in place. After composition `self` will contain all
    unique relations obtained during composition.

    To compose `RelationList` and a single `Relation`, see
    [`one_composition()`](relation_list.md#pymwp.relation_list.
    RelationList.one_composition).

    Arguments:
        other: RelationList to compose with `self`
    """
    new_list = []
    for r1 in self.relations:
        for r2 in other.relations:
            output = r1 * r2
            if not RelationList.contains_matrix(new_list, output.matrix):
                new_list.append(output)

    self.relations = new_list

contains_matrix(search_in, matrix) staticmethod

Check if a list of relations contains the provided matrix.

Parameters:

Name Type Description Default
search_in List[Relation]

list of relations to search

required
matrix List[List]

search value to look for

required

Returns:

Type Description
bool

True if matrix is found somewhere in the list of relations and False otherwise.

Source code in pymwp/relation_list.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
@staticmethod
def contains_matrix(search_in: List[Relation], matrix: List[List]) -> bool:
    """Check if a list of relations contains the provided matrix.

    Arguments:
        search_in: list of relations to search
        matrix: search value to look for

    Returns:
        `True` if matrix is found somewhere in the list of relations and
            `False` otherwise.
    """
    for relation in search_in:
        if relation.matrix == matrix:
            return True
    return False

fixpoint()

Apply fixpoint to all relations in relation list.

Source code in pymwp/relation_list.py
205
206
207
208
def fixpoint(self) -> None:
    """Apply [fixpoint](relation.md#pymwp.relation.Relation.fixpoint)
     to all relations in relation list."""
    self.relations = [rel.fixpoint() for rel in self.relations]

identity(variables) staticmethod

Create relation list that contains 1 identity relation.

This is an alternative way to construct a relation list.

Example:

Create relation list containing an identity relation

rel_list = RelationList.identity(['X0', 'X1', 'X2'])

# Generates a list with 1 identity relation:
#
# 1:      X0 |  +m  +o  +o
#         X1 |  +o  +m  +o
#         X2 |  +o  +o  +m

Parameters:

Name Type Description Default
variables List[str]

list of variables

required

Returns:

Type Description
RelationList

RelationList that contains identity relation generated using the provided variables.

Source code in pymwp/relation_list.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
@staticmethod
def identity(variables: List[str]) -> RelationList:
    """Create relation list that contains 1
    [identity relation](relation.md#pymwp.relation.Relation.identity).

    This is an alternative way to construct a relation list.

    Example:

    Create relation list containing an identity relation

    ```python
    rel_list = RelationList.identity(['X0', 'X1', 'X2'])

    # Generates a list with 1 identity relation:
    #
    # 1:      X0 |  +m  +o  +o
    #         X1 |  +o  +m  +o
    #         X2 |  +o  +o  +m
    ```

    Arguments:
        variables: list of variables

    Returns:
        RelationList that contains identity relation generated using the
            provided variables.
    """
    return RelationList(relation_list=[Relation.identity(variables)])

one_composition(relation)

Compose each relation in a relation list × relation.

This method iterates current relation list and applies composition() to each of its relations, using argument relation as the other operand.

Parameters:

Name Type Description Default
relation Relation

relation to compose with relations in current list.

required
Source code in pymwp/relation_list.py
193
194
195
196
197
198
199
200
201
202
203
def one_composition(self, relation: Relation) -> None:
    """Compose each relation in a relation list × relation.

    This method iterates current relation list and applies
    [`composition()`](relation.md#pymwp.relation.Relation.composition) to
    each of its relations, using argument `relation` as the other operand.

    Arguments:
        relation: relation to compose with relations in current list.
    """
    self.relations = [rel * relation for rel in self.relations]

replace_column(vector, variable)

For each relation in a relation list, replace column with a provided vector, in place.

This method takes as input a variable, then finds the index of that variable based on its name, then applies the column replacement at the discovered index.

Parameters:

Name Type Description Default
vector list

vector that will replace a column

required
variable str

variable value; replace will occur at the index of this variable.

required

Raises:

Type Description
ValueError

if variable does not exists some relation belonging to this relation list.

Source code in pymwp/relation_list.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def replace_column(self, vector: list, variable: str) -> None:
    """For each relation in a relation list, replace column with a provided
     vector, in place.

    This method takes as input a variable, then finds the index of that
    variable based on its name, then applies the column replacement at the
    discovered index.

    Arguments:
        vector: vector that will replace a column
        variable: variable value; replace will occur at the index of this
            variable.

    Raises:
        ValueError: if variable does not exists some relation belonging
            to this relation list.
    """

    self.relations = [rel.replace_column(vector, variable)
                      for rel in self.relations]

show()

Display relation list.

Source code in pymwp/relation_list.py
210
211
212
def show(self) -> None:
    """Display relation list."""
    print(str(self))

while_correction(dg)

Apply while_correction() to all relations in a relation list.

Source code in pymwp/relation_list.py
214
215
216
217
218
def while_correction(self, dg: DeltaGraph) -> None:
    """Apply [`while_correction()`](relation.md#pymwp.relation.Relation
    .while_correction) to all relations in a relation list."""
    for rel in self.relations:
        rel.while_correction(dg)