1515# (at your option) any later version.
1616# https://www.gnu.org/licenses/
1717# ****************************************************************************
18- from __future__ import print_function
19-
2018from sage .graphs .digraph import DiGraph
2119from sage .matrix .constructor import matrix
2220from 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
0 commit comments