On Sunday, January 14, 2018 at 2:23:36 AM UTC+1, Victor Porton wrote:
>
> 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 
>
>
I think you do not want to inherit from Set_object_enumerated. I know your 
object is a Set, but to me this is not a sufficient reason for deciding to 
inherit from it. Sometimes, just having attributes (2 in your case) of the 
good type (sets) is enough and exactly what you need. Inheriting from 
set_object_enumerated will allow plenty of thing that you do not really 
want for example:

sage: B = FiniteBinaryRelation(range(10, range(10), points)
sage: S = Set_object_enumerated('abcdef')
sage: B.union(S)

+ others

You will lose more time (and lines of code) disallowing this kind of thing 
rather than allowing what you  want.

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

Maybe you should first compute the intersection of the left part of self 
with the right part of rel2. Then do the double for loop with x and y...


> 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? 
>

The easiest way would be to reimplement the method union (and others) in 
your class so that they return what you want. But as I wrote above. I think 
this is not what you want do do.
 

>
> - 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: 
>
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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