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

Commit

Permalink
some care for Hasse diagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Jun 14, 2020
1 parent e2dcdee commit c4bf7a3
Showing 1 changed file with 29 additions and 47 deletions.
76 changes: 29 additions & 47 deletions src/sage/combinat/posets/hasse_diagram.py
Expand Up @@ -15,8 +15,6 @@
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from __future__ import print_function

from sage.graphs.digraph import DiGraph
from sage.matrix.constructor import matrix
from sage.rings.integer_ring import ZZ
Expand Down Expand Up @@ -161,17 +159,16 @@ def greedy_rec(H, linext):

S = []
if linext:
S = [x for x in H.neighbors_out(linext[-1])
if all(low in linext for low in H.neighbors_in(x))]
S = [x for x in H.neighbor_out_iterator(linext[-1])
if all(low in linext for low in H.neighbor_in_iterator(x))]
if not S:
S_ = set(self).difference(set(linext))
S = [x for x in S_
if not any(low in S_ for low in self.neighbors_in(x))]
if not any(low in S_
for low in self.neighbor_in_iterator(x))]

for e in S:
# Python3-todo: use yield from
for tmp in greedy_rec(H, linext + [e]):
yield tmp
yield from greedy_rec(H, linext + [e])

return greedy_rec(self, [])

Expand Down Expand Up @@ -227,16 +224,14 @@ def supergreedy_rec(H, linext):
if not k: # Start from new minimal element
S = [x for x in self.sources() if x not in linext]
else:
S = [x for x in self.neighbors_out(linext[k - 1])
S = [x for x in self.neighbor_out_iterator(linext[k - 1])
if x not in linext and
all(low in linext
for low in self.neighbors_in(x))]
for low in self.neighbor_in_iterator(x))]
k -= 1

for e in S:
# Python3-todo: use yield from
for tmp in supergreedy_rec(H, linext + [e]):
yield tmp
yield from supergreedy_rec(H, linext + [e])

return supergreedy_rec(self, [])

Expand All @@ -254,15 +249,10 @@ def is_linear_extension(self, lin_ext=None):
False
"""
if lin_ext is None or lin_ext == list(range(len(self))):
for x, y in self.cover_relations_iterator():
if not x < y:
return False
return True
return all(x < y for x, y in self.cover_relations_iterator())
else:
for x, y in self.cover_relations_iterator():
if not lin_ext.index(x) < lin_ext.index(y):
return False
return True
return all(lin_ext.index(x) < lin_ext.index(y)
for x, y in self.cover_relations_iterator())

def cover_relations_iterator(self):
r"""
Expand All @@ -275,8 +265,7 @@ def cover_relations_iterator(self):
sage: list(H.cover_relations_iterator())
[(0, 2), (0, 3), (1, 3), (1, 4), (2, 5), (3, 5), (4, 5)]
"""
for u, v, l in self.edge_iterator():
yield (u, v)
yield from self.edge_iterator(labels=False)

def cover_relations(self):
r"""
Expand Down Expand Up @@ -321,7 +310,7 @@ def is_lequal(self, i, j):

def is_less_than(self, x, y):
r"""
Return ``True`` if ``x`` is less than or equal to ``y`` in the
Return ``True`` if ``x`` is less than but not equal to ``y`` in the
poset, and ``False`` otherwise.
EXAMPLES::
Expand Down Expand Up @@ -598,13 +587,11 @@ def _precompute_intervals(self):
True
"""
n = self.order()

v_up = [frozenset(self.depth_first_search(v)) for v in range(n)]
v_up = (frozenset(self.depth_first_search(v)) for v in range(n))
v_down = [frozenset(self.depth_first_search(v, neighbors=self.neighbors_in))
for v in range(n)]
self._intervals = [[sorted(up.intersection(down)) for down in v_down]
for up in v_up]

self.interval = self._alternate_interval

def _alternate_interval(self, x, y):
Expand All @@ -627,7 +614,6 @@ def _alternate_interval(self, x, y):
sage: P._hasse_diagram._precompute_intervals()
sage: P.interval(1, 7) # Uses this function
[1, 3, 5, 7]
"""
return self._intervals[x][y]

Expand Down Expand Up @@ -677,7 +663,7 @@ def open_interval(self, x, y):
[]
"""
ci = self.interval(x, y)
if len(ci) == 0:
if not ci:
return []
else:
return ci[1:-1]
Expand Down Expand Up @@ -784,12 +770,12 @@ def _rank(self):
# look at the neighbors of y and set the ranks;
# then look at the neighbors of the neighbors ...
y = queue.pop()
for x in self.neighbors_out(y):
for x in self.neighbor_out_iterator(y):
if rank[x] is None:
rank[x] = rank[y] + 1
queue.add(x)
component.add(x)
for x in self.neighbors_in(y):
for x in self.neighbor_in_iterator(y):
if rank[x] is None:
rank[x] = rank[y] - 1
queue.add(x)
Expand Down Expand Up @@ -1099,7 +1085,7 @@ def order_filter(self, elements):
sage: H.order_filter([3,8])
[3, 7, 8, 9, 10, 11, 12, 13, 14, 15]
"""
return sorted(list(self.depth_first_search(elements)))
return sorted(self.depth_first_search(elements))

def principal_order_filter(self, i):
"""
Expand All @@ -1126,8 +1112,8 @@ def order_ideal(self, elements):
sage: H.order_ideal([7,10])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 10]
"""
return sorted(list(
self.depth_first_search(elements, neighbors=self.neighbors_in)))
return sorted(self.depth_first_search(elements,
neighbors=self.neighbors_in))

def principal_order_ideal(self, i):
"""
Expand Down Expand Up @@ -1158,7 +1144,7 @@ def _leq_storage(self):
greater_than = [set([i]) for i in range(n)]
for i in range(n - 1, -1, -1):
gt = greater_than[i]
for j in self.neighbors_out(i):
for j in self.neighbor_out_iterator(i):
gt = gt.union(greater_than[j])
greater_than[i] = gt

Expand Down Expand Up @@ -1967,13 +1953,12 @@ def recursive_fit(orthocomplements, unbinded):
new_unbinded = unbinded[1:] # Remove next_to_fit
new_unbinded.remove(e)

for i_want_python3_yield_from in recursive_fit(new_binded, new_unbinded):
yield i_want_python3_yield_from
yield from recursive_fit(new_binded, new_unbinded)

start = [None] * n
# A little optimization
for e in range(n):
if len(comps[e]) == 0: # Not any possible orthocomplement
if not comps[e]: # Not any possible orthocomplement
return
if len(comps[e]) == 1: # Do not re-fit this every time
e_ = comps[e][0]
Expand All @@ -1988,8 +1973,7 @@ def recursive_fit(orthocomplements, unbinded):
start[e_] = e
start_unbinded = [e for e in range(n) if start[e] is None]

for i_want_python3_yield_from in recursive_fit(start, start_unbinded):
yield i_want_python3_yield_from
yield from recursive_fit(start, start_unbinded)

def find_nonsemimodular_pair(self, upper):
"""
Expand Down Expand Up @@ -2310,7 +2294,6 @@ def sublattices_iterator(self, elms, min_e):
sage: next(it)
{0}
"""
# Python3-note: "yield from" would be simpler.
yield elms
for e in range(min_e, self.cardinality()):
if e in elms:
Expand All @@ -2328,8 +2311,7 @@ def sublattices_iterator(self, elms, min_e):
gens.add(self._join[x, g])
current_set.add(g)
else:
for x in self.sublattices_iterator(current_set, e + 1):
yield x
yield from self.sublattices_iterator(current_set, e + 1)

def maximal_sublattices(self):
"""
Expand Down Expand Up @@ -2497,10 +2479,10 @@ def kappa_dual(self, a):
if self.in_degree(uc) == 1:
return uc
lt_a = set(self.depth_first_search(a, neighbors=self.neighbors_in))
tmp = list(self.depth_first_search(uc, neighbors=lambda v: [v_ for v_ in self.neighbors_in(v) if v_ not in lt_a]))
tmp = list(self.depth_first_search(uc, neighbors=lambda v: [v_ for v_ in self.neighbor_in_iterator(v) if v_ not in lt_a]))
result = None
for e in tmp:
if all(x not in tmp for x in self.neighbors_in(e)):
if all(x not in tmp for x in self.neighbor_in_iterator(e)):
if result:
return None
result = e
Expand Down Expand Up @@ -2737,10 +2719,10 @@ def kappa(self, a):
if self.out_degree(lc) == 1:
return lc
gt_a = set(self.depth_first_search(a))
tmp = list(self.depth_first_search(lc, neighbors=lambda v: [v_ for v_ in self.neighbors_out(v) if v_ not in gt_a]))
tmp = list(self.depth_first_search(lc, neighbors=lambda v: [v_ for v_ in self.neighbor_out_iterator(v) if v_ not in gt_a]))
result = None
for e in tmp:
if all(x not in tmp for x in self.neighbors_out(e)):
if all(x not in tmp for x in self.neighbor_out_iterator(e)):
if result:
return None
result = e
Expand Down

0 comments on commit c4bf7a3

Please sign in to comment.