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

Commit cd063f3

Browse files
committed
Merge branch 'u/chapoton/29857' in 9.2.b2
2 parents 69d2b2d + 50272c5 commit cd063f3

File tree

2 files changed

+43
-63
lines changed

2 files changed

+43
-63
lines changed

src/sage/combinat/posets/hasse_diagram.py

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
# (at your option) any later version.
1616
# https://www.gnu.org/licenses/
1717
# ****************************************************************************
18-
from __future__ import print_function
19-
2018
from sage.graphs.digraph import DiGraph
2119
from sage.matrix.constructor import matrix
2220
from sage.rings.integer_ring import ZZ
@@ -162,17 +160,16 @@ def greedy_rec(H, linext):
162160

163161
S = []
164162
if linext:
165-
S = [x for x in H.neighbors_out(linext[-1])
166-
if all(low in linext for low in H.neighbors_in(x))]
163+
S = [x for x in H.neighbor_out_iterator(linext[-1])
164+
if all(low in linext for low in H.neighbor_in_iterator(x))]
167165
if not S:
168166
S_ = set(self).difference(set(linext))
169167
S = [x for x in S_
170-
if not any(low in S_ for low in self.neighbors_in(x))]
168+
if not any(low in S_
169+
for low in self.neighbor_in_iterator(x))]
171170

172171
for e in S:
173-
# Python3-todo: use yield from
174-
for tmp in greedy_rec(H, linext + [e]):
175-
yield tmp
172+
yield from greedy_rec(H, linext + [e])
176173

177174
return greedy_rec(self, [])
178175

@@ -228,16 +225,14 @@ def supergreedy_rec(H, linext):
228225
if not k: # Start from new minimal element
229226
S = [x for x in self.sources() if x not in linext]
230227
else:
231-
S = [x for x in self.neighbors_out(linext[k - 1])
228+
S = [x for x in self.neighbor_out_iterator(linext[k - 1])
232229
if x not in linext and
233230
all(low in linext
234-
for low in self.neighbors_in(x))]
231+
for low in self.neighbor_in_iterator(x))]
235232
k -= 1
236233

237234
for e in S:
238-
# Python3-todo: use yield from
239-
for tmp in supergreedy_rec(H, linext + [e]):
240-
yield tmp
235+
yield from supergreedy_rec(H, linext + [e])
241236

242237
return supergreedy_rec(self, [])
243238

@@ -255,15 +250,10 @@ def is_linear_extension(self, lin_ext=None):
255250
False
256251
"""
257252
if lin_ext is None or lin_ext == list(range(len(self))):
258-
for x, y in self.cover_relations_iterator():
259-
if not x < y:
260-
return False
261-
return True
253+
return all(x < y for x, y in self.cover_relations_iterator())
262254
else:
263-
for x, y in self.cover_relations_iterator():
264-
if not lin_ext.index(x) < lin_ext.index(y):
265-
return False
266-
return True
255+
return all(lin_ext.index(x) < lin_ext.index(y)
256+
for x, y in self.cover_relations_iterator())
267257

268258
def cover_relations_iterator(self):
269259
r"""
@@ -276,8 +266,7 @@ def cover_relations_iterator(self):
276266
sage: list(H.cover_relations_iterator())
277267
[(0, 2), (0, 3), (1, 3), (1, 4), (2, 5), (3, 5), (4, 5)]
278268
"""
279-
for u, v, l in self.edge_iterator():
280-
yield (u, v)
269+
yield from self.edge_iterator(labels=False)
281270

282271
def cover_relations(self):
283272
r"""
@@ -322,7 +311,7 @@ def is_lequal(self, i, j):
322311

323312
def is_less_than(self, x, y):
324313
r"""
325-
Return ``True`` if ``x`` is less than or equal to ``y`` in the
314+
Return ``True`` if ``x`` is less than but not equal to ``y`` in the
326315
poset, and ``False`` otherwise.
327316
328317
EXAMPLES::
@@ -599,13 +588,11 @@ def _precompute_intervals(self):
599588
True
600589
"""
601590
n = self.order()
602-
603-
v_up = [frozenset(self.depth_first_search(v)) for v in range(n)]
591+
v_up = (frozenset(self.depth_first_search(v)) for v in range(n))
604592
v_down = [frozenset(self.depth_first_search(v, neighbors=self.neighbors_in))
605593
for v in range(n)]
606594
self._intervals = [[sorted(up.intersection(down)) for down in v_down]
607595
for up in v_up]
608-
609596
self.interval = self._alternate_interval
610597

611598
def _alternate_interval(self, x, y):
@@ -628,7 +615,6 @@ def _alternate_interval(self, x, y):
628615
sage: P._hasse_diagram._precompute_intervals()
629616
sage: P.interval(1, 7) # Uses this function
630617
[1, 3, 5, 7]
631-
632618
"""
633619
return self._intervals[x][y]
634620

@@ -678,7 +664,7 @@ def open_interval(self, x, y):
678664
[]
679665
"""
680666
ci = self.interval(x, y)
681-
if len(ci) == 0:
667+
if not ci:
682668
return []
683669
else:
684670
return ci[1:-1]
@@ -785,12 +771,12 @@ def _rank(self):
785771
# look at the neighbors of y and set the ranks;
786772
# then look at the neighbors of the neighbors ...
787773
y = queue.pop()
788-
for x in self.neighbors_out(y):
774+
for x in self.neighbor_out_iterator(y):
789775
if rank[x] is None:
790776
rank[x] = rank[y] + 1
791777
queue.add(x)
792778
component.add(x)
793-
for x in self.neighbors_in(y):
779+
for x in self.neighbor_in_iterator(y):
794780
if rank[x] is None:
795781
rank[x] = rank[y] - 1
796782
queue.add(x)
@@ -1100,7 +1086,7 @@ def order_filter(self, elements):
11001086
sage: H.order_filter([3,8])
11011087
[3, 7, 8, 9, 10, 11, 12, 13, 14, 15]
11021088
"""
1103-
return sorted(list(self.depth_first_search(elements)))
1089+
return sorted(self.depth_first_search(elements))
11041090

11051091
def principal_order_filter(self, i):
11061092
"""
@@ -1127,8 +1113,8 @@ def order_ideal(self, elements):
11271113
sage: H.order_ideal([7,10])
11281114
[0, 1, 2, 3, 4, 5, 6, 7, 8, 10]
11291115
"""
1130-
return sorted(list(
1131-
self.depth_first_search(elements, neighbors=self.neighbors_in)))
1116+
return sorted(self.depth_first_search(elements,
1117+
neighbors=self.neighbors_in))
11321118

11331119
def principal_order_ideal(self, i):
11341120
"""
@@ -1159,7 +1145,7 @@ def _leq_storage(self):
11591145
greater_than = [set([i]) for i in range(n)]
11601146
for i in range(n - 1, -1, -1):
11611147
gt = greater_than[i]
1162-
for j in self.neighbors_out(i):
1148+
for j in self.neighbor_out_iterator(i):
11631149
gt = gt.union(greater_than[j])
11641150
greater_than[i] = gt
11651151

@@ -1968,13 +1954,12 @@ def recursive_fit(orthocomplements, unbinded):
19681954
new_unbinded = unbinded[1:] # Remove next_to_fit
19691955
new_unbinded.remove(e)
19701956

1971-
for i_want_python3_yield_from in recursive_fit(new_binded, new_unbinded):
1972-
yield i_want_python3_yield_from
1957+
yield from recursive_fit(new_binded, new_unbinded)
19731958

19741959
start = [None] * n
19751960
# A little optimization
19761961
for e in range(n):
1977-
if len(comps[e]) == 0: # Not any possible orthocomplement
1962+
if not comps[e]: # Not any possible orthocomplement
19781963
return
19791964
if len(comps[e]) == 1: # Do not re-fit this every time
19801965
e_ = comps[e][0]
@@ -1989,8 +1974,7 @@ def recursive_fit(orthocomplements, unbinded):
19891974
start[e_] = e
19901975
start_unbinded = [e for e in range(n) if start[e] is None]
19911976

1992-
for i_want_python3_yield_from in recursive_fit(start, start_unbinded):
1993-
yield i_want_python3_yield_from
1977+
yield from recursive_fit(start, start_unbinded)
19941978

19951979
def find_nonsemimodular_pair(self, upper):
19961980
"""
@@ -2311,7 +2295,6 @@ def sublattices_iterator(self, elms, min_e):
23112295
sage: next(it)
23122296
{0}
23132297
"""
2314-
# Python3-note: "yield from" would be simpler.
23152298
yield elms
23162299
for e in range(min_e, self.cardinality()):
23172300
if e in elms:
@@ -2329,8 +2312,7 @@ def sublattices_iterator(self, elms, min_e):
23292312
gens.add(self._join[x, g])
23302313
current_set.add(g)
23312314
else:
2332-
for x in self.sublattices_iterator(current_set, e + 1):
2333-
yield x
2315+
yield from self.sublattices_iterator(current_set, e + 1)
23342316

23352317
def maximal_sublattices(self):
23362318
"""
@@ -2498,10 +2480,10 @@ def kappa_dual(self, a):
24982480
if self.in_degree(uc) == 1:
24992481
return uc
25002482
lt_a = set(self.depth_first_search(a, neighbors=self.neighbors_in))
2501-
tmp = list(self.depth_first_search(uc, neighbors=lambda v: [v_ for v_ in self.neighbors_in(v) if v_ not in lt_a]))
2483+
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]))
25022484
result = None
25032485
for e in tmp:
2504-
if all(x not in tmp for x in self.neighbors_in(e)):
2486+
if all(x not in tmp for x in self.neighbor_in_iterator(e)):
25052487
if result:
25062488
return None
25072489
result = e
@@ -2738,10 +2720,10 @@ def kappa(self, a):
27382720
if self.out_degree(lc) == 1:
27392721
return lc
27402722
gt_a = set(self.depth_first_search(a))
2741-
tmp = list(self.depth_first_search(lc, neighbors=lambda v: [v_ for v_ in self.neighbors_out(v) if v_ not in gt_a]))
2723+
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]))
27422724
result = None
27432725
for e in tmp:
2744-
if all(x not in tmp for x in self.neighbors_out(e)):
2726+
if all(x not in tmp for x in self.neighbor_out_iterator(e)):
27452727
if result:
27462728
return None
27472729
result = e

src/sage/combinat/posets/posets.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ def Poset(data=None, element_labels=None, cover_relations=False, linear_extensio
719719
vertices = sorted(set(x for item in data for x in item))
720720
if len(vertices) != len(data):
721721
# by default, assuming vertices are the range 0..n
722-
vertices = list(range(len(data)))
722+
vertices = range(len(data))
723723
D = DiGraph({v: [u for u in cov if u != v]
724724
for v, cov in zip(vertices, data)},
725725
format="dict_of_lists")
@@ -1556,7 +1556,7 @@ def linear_extension(self, linear_extension=None, check=True):
15561556
@cached_method
15571557
def linear_extensions(self, facade=False):
15581558
"""
1559-
Return the enumerated set of all the linear extensions of this poset
1559+
Return the enumerated set of all the linear extensions of this poset.
15601560
15611561
INPUT:
15621562
@@ -1776,7 +1776,7 @@ def atkinson(self, a):
17761776

17771777
def is_linear_extension(self, l):
17781778
"""
1779-
Return whether ``l`` is a linear extension of ``self``
1779+
Return whether ``l`` is a linear extension of ``self``.
17801780
17811781
INPUT:
17821782
@@ -3126,7 +3126,7 @@ def is_EL_labelling(self, f, return_raising_chains=False):
31263126
if max_chains[0] != sorted(max_chains[0]) or any( max_chains[i] == sorted(max_chains[i]) for i in range(1,len(max_chains)) ):
31273127
return False
31283128
elif return_raising_chains:
3129-
raising_chains[(a,b)] = max_chains[0]
3129+
raising_chains[(a, b)] = max_chains[0]
31303130
if return_raising_chains:
31313131
return raising_chains
31323132
else:
@@ -5889,9 +5889,9 @@ def with_linear_extension(self, linear_extension):
58895889
category=self.category(),
58905890
facade=self._is_facade)
58915891

5892-
def graphviz_string(self,graph_string="graph",edge_string="--"):
5892+
def graphviz_string(self, graph_string="graph", edge_string="--"):
58935893
r"""
5894-
Returns a representation in the DOT language, ready to render in
5894+
Return a representation in the DOT language, ready to render in
58955895
graphviz.
58965896
58975897
See http://www.graphviz.org/doc/info/lang.html for more information
@@ -6188,7 +6188,7 @@ def random_linear_extension(self):
61886188
new = mins[new_index]
61896189
result.append(new)
61906190
mins = mins[:new_index]+mins[new_index+1:]
6191-
for u in H.neighbors_out(new):
6191+
for u in H.neighbor_out_iterator(new):
61926192
indegs[u] -= 1
61936193
if indegs[u] == 0:
61946194
mins.append(u)
@@ -6223,7 +6223,7 @@ def order_filter(self, elements):
62236223
sage: C.order_filter([])
62246224
[]
62256225
"""
6226-
vertices = sorted(map(self._element_to_vertex,elements))
6226+
vertices = sorted(map(self._element_to_vertex, elements))
62276227
of = self._hasse_diagram.order_filter(vertices)
62286228
return [self._vertex_to_element(_) for _ in of]
62296229

@@ -6531,8 +6531,6 @@ def maximal_chains(self, partial=None):
65316531
- ``partial`` -- list (optional); if present, find all maximal
65326532
chains starting with the elements in partial
65336533
6534-
Returns list of the maximal chains of this poset.
6535-
65366534
This is used in constructing the order complex for the poset.
65376535
65386536
EXAMPLES::
@@ -8338,13 +8336,13 @@ def __contains__(self, P):
83388336

83398337
def __iter__(self):
83408338
"""
8341-
Returns an iterator of representatives of the isomorphism classes
8339+
Return an iterator of representatives of the isomorphism classes
83428340
of finite posets of a given size.
83438341
8344-
.. note::
8342+
.. NOTE::
83458343
8346-
This uses the DiGraph iterator as a backend to construct
8347-
transitively-reduced, acyclic digraphs.
8344+
This uses the DiGraph iterator as a backend to construct
8345+
transitively-reduced, acyclic digraphs.
83488346
83498347
EXAMPLES::
83508348
@@ -8356,8 +8354,8 @@ def __iter__(self):
83568354
for dig in DiGraphGenerators()(self._n, is_poset):
83578355
# We need to relabel the digraph since range(self._n) must be a linear
83588356
# extension. Too bad we need to compute this again. TODO: Fix this.
8359-
label_dict = dict(zip(dig.topological_sort(),range(dig.order())))
8360-
yield FinitePoset(dig.relabel(label_dict,inplace=False))
8357+
label_dict = dict(zip(dig.topological_sort(), range(dig.order())))
8358+
yield FinitePoset(dig.relabel(label_dict, inplace=False))
83618359

83628360
def cardinality(self, from_iterator=False):
83638361
r"""

0 commit comments

Comments
 (0)