I am trying to implement binary relations (sets of 2-tuples) in Sage.

So far I've written:

class FiniteBinaryRelation(Set_object_enumerated):
    def __init__(self, x_range, y_range, points=[]):
        self._x_range = Set_object_enumerated(x_range)
        self._y_range = Set_object_enumerated(y_range)
        if not self._x_range.is_finite() or not
self._y_range.is_finite():
            raise ValueError("FiniteBinaryRelation ranges must be
finite.")
        points = [(p[0],p[1]) for p in points]
        super(Set_object_enumerated, self).__init__(points)
        if not self.is_finite():
            raise ValueError("FiniteBinaryRelation must be finite.")

    def contains(self, point_x, point_y):
        r"""
        Test if the relation contains the given point.
        """
        return (point_x,point_y) in self

    # ...

The x/y ranges were used by me to implement composition operation:

    def compose(self, rel2):
        if self.x_range != rel2.y_range:
            raise ValueError("y_range of composed binary relation is
not equal to x_range of self.")
        result = set()
        for x in rel2.x_range:
            for y in rel2.y_range:
                for z in self.y_range:
                    if (x,y) in rel2 and (y,z) in self:
                        result.add((x,z))
        return FiniteBinaryRelation(rel2.x_range, self.y_range, result)

But later I realized that I can implement composition efficiently
enough without specifying explicit ranges (but with extracting ranges
from 2-tuples when necessary).

This simplifies the stuff considerably, but some issues remain:

- .compose() is a method of FiniteBinaryRelation. If I do r1.union(r2)
for two binary relations r1 and r2 the resulting object is just
Set_object_enumerated not a FiniteBinaryRelation. So stuff like this
won't work: (r1.union(r2)).compose(r3).

What is the easiest way to make all set operations (such as .union())
to work property on the class of binary relations?

- I considered also to make binary_relation_composition(r1,r2)
functions (not .compose() method) instead. This would solve the above
problem as: binary_relation_composition(r1.union(r2)),compose(r3)) but:

-- This is against OOP as I understand it, because it uses plain
function rather than a method. I somehow dislike it.

-- binary_relation_composition(r1,r2) should check that all elements of
r1 and r2 are 2-tuples every time it is invoked. It is both inefficient
to do the checks and somehow silly, as it makes us unable to encode
their types in r1 and r2 objects.

So what is the recommended solution for binary relations in Sage?
Please advise me, after hearing your advice I am going to implement it
and submit a patch.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to