Skip to content

Commit 224b915

Browse files
committed
moving linear_intervals_count to hasse as iterator
1 parent 9352a32 commit 224b915

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

src/sage/combinat/posets/hasse_diagram.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,13 +2433,56 @@ def chain_polynomial(self):
24332433
"""
24342434
return chain_poly(self._leq_storage)._sage_('q') # noqa: F821
24352435

2436+
def linear_intervals_count(self):
2437+
"""
2438+
Return the enumeration of linear intervals w.r.t. their cardinality.
2439+
2440+
An interval is linear if it is a total order.
2441+
2442+
OUTPUT: an iterator of integers
2443+
2444+
.. SEEALSO:: :meth:`is_linear_interval`
2445+
2446+
EXAMPLES::
2447+
2448+
sage: P = posets.BubblePoset(3,3)
2449+
sage: H = P._hasse_diagram
2450+
sage: list(H.linear_intervals_count())
2451+
[245, 735, 438, 144, 24]
2452+
"""
2453+
if not self:
2454+
return
2455+
# precomputation helps for speed:
2456+
_ = self._leq_storage
2457+
2458+
stock = [(x, x, x) for x in self]
2459+
yield len(stock)
2460+
exposant = 0
2461+
while True:
2462+
exposant += 1
2463+
next_stock = []
2464+
short_stock = [(ch[0], ch[2]) for ch in stock]
2465+
for xmin, cov_xmin, xmax in stock:
2466+
for y in self.neighbor_out_iterator(xmax):
2467+
if exposant == 1:
2468+
next_stock.append((xmin, y, y))
2469+
elif (cov_xmin, y) in short_stock:
2470+
if self.is_linear_interval(xmin, y):
2471+
next_stock.append((xmin, cov_xmin, y))
2472+
if next_stock:
2473+
yield len(next_stock)
2474+
stock = next_stock
2475+
else:
2476+
break
2477+
24362478
def is_linear_interval(self, t_min, t_max) -> bool:
24372479
"""
24382480
Return whether the interval ``[t_min, t_max]`` is linear.
24392481
24402482
This means that this interval is a total order.
24412483
24422484
EXAMPLES::
2485+
24432486
sage: # needs sage.modules
24442487
sage: P = posets.PentagonPoset()
24452488
sage: H = P._hasse_diagram

src/sage/combinat/posets/posets.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,31 +2772,7 @@ def linear_intervals_count(self) -> list[int]:
27722772
"""
27732773
if not self.cardinality():
27742774
return []
2775-
# precomputation helps for speed:
2776-
if self.cardinality() > 60:
2777-
self.lequal_matrix()
2778-
2779-
H = self._hasse_diagram
2780-
stock = [(x, x, x) for x in H]
2781-
poly = [len(stock)]
2782-
exposant = 0
2783-
while True:
2784-
exposant += 1
2785-
next_stock = []
2786-
short_stock = [(ch[0], ch[2]) for ch in stock]
2787-
for xmin, cov_xmin, xmax in stock:
2788-
for y in H.neighbor_out_iterator(xmax):
2789-
if exposant == 1:
2790-
next_stock.append((xmin, y, y))
2791-
elif (cov_xmin, y) in short_stock:
2792-
if H.is_linear_interval(xmin, y):
2793-
next_stock.append((xmin, cov_xmin, y))
2794-
if next_stock:
2795-
poly.append(len(next_stock))
2796-
stock = next_stock
2797-
else:
2798-
break
2799-
return poly
2775+
return list(self._hasse_diagram.linear_intervals_count())
28002776

28012777
def is_linear_interval(self, x, y) -> bool:
28022778
"""

0 commit comments

Comments
 (0)