Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
added FiniteBinaryRelation.enumerate_all()
Browse files Browse the repository at this point in the history
  • Loading branch information
vporton committed May 23, 2018
1 parent 5fc5429 commit aa1564c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/sage/relations/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ def BinaryRelation(points=[]):
{0, 1}
sage: FiniteBinaryRelation.square(xrange(2)).reverse_image({})
{}
Enumerating relations:
sage: len(list(FiniteBinaryRelation.enumerate_all({0, 1}, {0, 1})))
16
"""
return FiniteBinaryRelation(points)

Expand Down Expand Up @@ -318,4 +324,39 @@ def reverse_image(self, Y):
"""
return Set([p[0] for p in self if p[1] in Y])

@staticmethod
def enumerate_all(X, Y):
r"""
OUTPUT: Enumerator of all binary relations between sets `X` and `Y`.
"""
return AllBinaryRelationsIterator(X, Y)

# TODO: subsets() method?

class AllBinaryRelationsIterator:
r"""
TODO: Should it instead be implemented as a generator function?
"""
def __init__(self, X, Y):
self.X = X
self.Y = Y
self.i = 0
self.max = 1 << (len(X) * len(Y))

def __iter__(self):
return self

def __next__(self):
if self.i >= self.max:
raise StopIteration
s = set()
j = 0
for x in self.X:
for y in self.Y:
if self.i & (1 << j):
s.add((x, y))
j += 1
self.i += 1
return Set(s)

next = __next__ # Python 2

0 comments on commit aa1564c

Please sign in to comment.