Skip to content

Commit 8455792

Browse files
author
Release Manager
committed
gh-35573: some details in posets
<!-- Please provide a concise, informative and self-explanatory title. --> <!-- Don't put issue numbers in the title. Put it in the Description below. --> <!-- For example, instead of "Fixes #12345", use "Add a new method to multiply two integers" --> ### 📚 Description Some small details in code of posets : - a few pep8 fixes - some changes to try to gain a little speed using `.monomial` <!-- Describe your changes here in detail. --> <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x ]`. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #35573 Reported by: Frédéric Chapoton Reviewer(s): Matthias Köppe
2 parents f33652c + 487bf86 commit 8455792

File tree

4 files changed

+48
-42
lines changed

4 files changed

+48
-42
lines changed

build/pkgs/configure/checksums.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
tarball=configure-VERSION.tar.gz
2-
sha1=7460b5a298a1d6e1e22816174a89526fd3baeb6f
3-
md5=adab6d06997313cc52c8da7dfbcf94b2
4-
cksum=2723491418
2+
sha1=28eb5a82094a839657b750b479cf3be623201093
3+
md5=761f7a309a7c6394e34f738461d87c30
4+
cksum=1112975644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8476c4695880ff35d9e534e916d2841a67cba0de
1+
399285a10b59972f99cbf0d0d70f81bf73705917

src/sage/combinat/posets/poset_examples.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,9 @@ def ProductOfChains(chain_lengths, facade=None):
727727
return LatticePoset(facade=facade)
728728
from sage.categories.cartesian_product import cartesian_product
729729
elements = cartesian_product([range(i) for i in l])
730-
compare = lambda a, b: all(x <= y for x, y in zip(a, b))
730+
731+
def compare(a, b):
732+
return all(x <= y for x, y in zip(a, b))
731733
return LatticePoset([elements, compare], facade=facade)
732734

733735
@staticmethod
@@ -875,7 +877,7 @@ def RandomLattice(n, p, properties=None):
875877
covers = _random_lattice(n, p)
876878
covers_dict = {i: covers[i] for i in range(n)}
877879
D = DiGraph(covers_dict)
878-
D.relabel([i-1 for i in Permutations(n).random_element()])
880+
D.relabel([i - 1 for i in Permutations(n).random_element()])
879881
return LatticePoset(D, cover_relations=True)
880882

881883
if isinstance(properties, str):
@@ -906,23 +908,23 @@ def RandomLattice(n, p, properties=None):
906908

907909
if properties == set(['planar']):
908910
D = _random_planar_lattice(n)
909-
D.relabel([i-1 for i in Permutations(n).random_element()])
911+
D.relabel([i - 1 for i in Permutations(n).random_element()])
910912
return LatticePoset(D)
911913

912914
if properties == set(['dismantlable']):
913915
D = _random_dismantlable_lattice(n)
914-
D.relabel([i-1 for i in Permutations(n).random_element()])
916+
D.relabel([i - 1 for i in Permutations(n).random_element()])
915917
return LatticePoset(D)
916918

917919
if properties == set(['stone']):
918920
D = _random_stone_lattice(n)
919-
D.relabel([i-1 for i in Permutations(n).random_element()])
921+
D.relabel([i - 1 for i in Permutations(n).random_element()])
920922
return LatticePoset(D)
921923

922924
if properties == set(['distributive']):
923925
tmp = Poset(_random_distributive_lattice(n)).order_ideals_lattice(as_ideals=False)
924926
D = copy(tmp._hasse_diagram)
925-
D.relabel([i-1 for i in Permutations(n).random_element()])
927+
D.relabel([i - 1 for i in Permutations(n).random_element()])
926928
return LatticePoset(D)
927929

928930
raise AssertionError("bug in RandomLattice()")
@@ -948,7 +950,7 @@ def SetPartitions(n):
948950

949951
def covers(x):
950952
for i, s in enumerate(x):
951-
for j in range(i+1, len(x)):
953+
for j in range(i + 1, len(x)):
952954
L = list(x)
953955
L[i] = s.union(x[j])
954956
L.pop(j)
@@ -1051,8 +1053,8 @@ def StandardExample(n, facade=None):
10511053
(False, False)
10521054
"""
10531055
n = check_int(n, 2)
1054-
return Poset((range(2*n), [[i, j+n] for i in range(n)
1055-
for j in range(n) if i != j]),
1056+
return Poset((range(2 * n), [[i, j + n] for i in range(n)
1057+
for j in range(n) if i != j]),
10561058
facade=facade)
10571059

10581060
@staticmethod
@@ -1548,20 +1550,20 @@ def YoungFibonacci(n):
15481550

15491551
covers = []
15501552
current_level = ['']
1551-
for i in range(1, n+1):
1553+
for i in range(1, n + 1):
15521554
new_level = set()
15531555
for low in current_level:
15541556
ind = low.find('1')
15551557
if ind != -1: # = found a '1' -> change first '1' to '2'
1556-
up = low[:ind]+'2'+low[ind+1:]
1558+
up = low[:ind] + '2' + low[ind + 1:]
15571559
new_level.add(up)
15581560
covers.append((low, up))
15591561
else: # no '1' in low
15601562
ind = len(low)
15611563

15621564
# add '1' to every position not after first existing '1'
1563-
for j in range(ind+1):
1564-
up = '2'*j + '1' + low[j:len(low)]
1565+
for j in range(ind + 1):
1566+
up = '2' * j + '1' + low[j:len(low)]
15651567
new_level.add(up)
15661568
covers.append((low, up))
15671569

@@ -1589,10 +1591,10 @@ def DoubleTailedDiamond(n):
15891591
"""
15901592
n = check_int(n, 1)
15911593

1592-
edges = [(i, i+1) for i in range(1, n)]
1593-
edges.extend([(n, n+1), (n, n+2), (n+1, n+3), (n+2, n+3)])
1594-
edges.extend([(i, i+1) for i in range(n+3, 2*n+2)])
1595-
p = DiGraph([list(range(1, 2*n + 3)), edges])
1594+
edges = [(i, i + 1) for i in range(1, n)]
1595+
edges.extend([(n, n + 1), (n, n + 2), (n + 1, n + 3), (n + 2, n + 3)])
1596+
edges.extend([(i, i + 1) for i in range(n + 3, 2 * n + 2)])
1597+
p = DiGraph([list(range(1, 2 * n + 3)), edges])
15961598
return DCompletePoset(p)
15971599

15981600
@staticmethod
@@ -1694,7 +1696,7 @@ def PermutationPatternInterval(bottom, top):
16941696
# Try and remove the ith element from the permutation
16951697
lower = list(upper)
16961698
j = lower.pop(i)
1697-
for k in range(len(top)-level-1): # Standardize result
1699+
for k in range(len(top) - level - 1): # Standardize result
16981700
if lower[k] > j:
16991701
lower[k] = lower[k] - 1
17001702
lower_perm = P(lower)
@@ -1749,13 +1751,13 @@ def PermutationPatternOccurrenceInterval(bottom, top, pos):
17491751
while len(top) - len(bottom) >= level + 1:
17501752
elem.append([]) # Add a new empty level
17511753
for upper in elem[level]:
1752-
for i in range(len(top)-level):
1754+
for i in range(len(top) - level):
17531755
# Try and remove the ith element from the permutation
17541756
if i in upper[1]:
17551757
continue
17561758
lower_perm = list(upper[0])
17571759
j = lower_perm.pop(i)
1758-
for e in range(len(top)-level-1):
1760+
for e in range(len(top) - level - 1):
17591761
if lower_perm[e] > j:
17601762
lower_perm[e] = lower_perm[e] - 1
17611763
lower_pos = list(upper[1])
@@ -1901,9 +1903,9 @@ def _random_lattice(n, p):
19011903
# Look for an admissible lower cover for the next element i
19021904
while True:
19031905
# Generate a random antichain
1904-
lc_list = [i-1-floor(i*sqrt(random()))]
1906+
lc_list = [i - 1 - floor(i * sqrt(random()))]
19051907
while random() < p and 0 not in lc_list:
1906-
new = i-1-floor(i*sqrt(random()))
1908+
new = i - 1 - floor(i * sqrt(random()))
19071909
if any(meets[new][lc] in [new, lc] for lc in lc_list):
19081910
continue
19091911
lc_list.append(new)

src/sage/combinat/posets/posets.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6145,7 +6145,8 @@ def relabel(self, relabeling=None):
61456145
True
61466146
"""
61476147
from sage.combinat.posets.lattices import (FiniteLatticePoset,
6148-
FiniteMeetSemilattice, FiniteJoinSemilattice)
6148+
FiniteMeetSemilattice,
6149+
FiniteJoinSemilattice)
61496150

61506151
if isinstance(self, FiniteLatticePoset):
61516152
constructor = FiniteLatticePoset
@@ -6750,7 +6751,7 @@ def interval(self, x, y):
67506751
"""
67516752
return [self._vertex_to_element(w)
67526753
for w in self._hasse_diagram.interval(
6753-
self._element_to_vertex(x), self._element_to_vertex(y))]
6754+
self._element_to_vertex(x), self._element_to_vertex(y))]
67546755

67556756
def closed_interval(self, x, y):
67566757
r"""
@@ -7191,7 +7192,7 @@ def chain_polytope(self):
71917192
"""
71927193
from sage.geometry.polyhedron.constructor import Polyhedron
71937194
ineqs = [[1] + [-ZZ(j in chain) for j in self]
7194-
for chain in self.maximal_chains_iterator()]
7195+
for chain in self.maximal_chains_iterator()]
71957196
for i in self:
71967197
ineqs += [[0] + [ZZ(j == i) for j in self]]
71977198
return Polyhedron(ieqs=ineqs, base_ring=ZZ)
@@ -7295,10 +7296,10 @@ def M_triangle(self):
72957296
rk = hasse.rank_function()
72967297
if rk is None:
72977298
raise ValueError('the poset is not graded')
7298-
x, y = polygen(ZZ, 'x,y')
7299-
p = sum(hasse.moebius_function(a, b) * x**rk(a) * y**rk(b)
7300-
for a in hasse
7301-
for b in hasse.principal_order_filter(a))
7299+
ring = PolynomialRing(ZZ, 'x,y')
7300+
p = ring.sum(hasse.moebius_function(a, b) * ring.monomial(rk(a), rk(b))
7301+
for a in hasse
7302+
for b in hasse.principal_order_filter(a))
73027303
return M_triangle(p)
73037304

73047305
def f_polynomial(self):
@@ -7407,17 +7408,19 @@ def h_polynomial(self):
74077408
:meth:`sage.topology.simplicial_complex.SimplicialComplex.h_vector`
74087409
"""
74097410
q = polygen(ZZ, 'q')
7411+
ring = q.parent()
74107412
hasse = self._hasse_diagram
74117413
if len(hasse) == 1:
74127414
return q.parent().one()
74137415
maxi = hasse.top()
74147416
mini = hasse.bottom()
74157417
if (mini is None) or (maxi is None):
74167418
raise ValueError("the poset is not bounded")
7417-
f = sum(q**(len(ch)) for ch in hasse.chains(exclude=[mini, maxi]))
7419+
f = ring.sum(ring.monomial(len(ch))
7420+
for ch in hasse.chains(exclude=[mini, maxi]))
74187421
d = f.degree()
74197422
f = (1 - q)**d * q * f(q=q / (1 - q))
7420-
return q.parent(f)
7423+
return ring(f)
74217424

74227425
def flag_f_polynomial(self):
74237426
r"""
@@ -7607,15 +7610,16 @@ def characteristic_polynomial(self):
76077610
sage: P.characteristic_polynomial()
76087611
1
76097612
"""
7610-
hasse = self._hasse_diagram
7611-
rk = hasse.rank_function()
7613+
H = self._hasse_diagram
7614+
rk = H.rank_function()
76127615
if not self.is_graded():
76137616
raise ValueError("the poset is not graded")
76147617
if not self.has_bottom():
76157618
raise ValueError("the poset does not have a bottom element")
7616-
n = rk(hasse.maximal_elements()[0])
7617-
q = polygen(ZZ, 'q')
7618-
return sum(hasse.bottom_moebius_function(x) * q**(n - rk(x)) for x in hasse)
7619+
n = rk(H.maximal_elements()[0])
7620+
ring = PolynomialRing(ZZ, 'q')
7621+
return ring.sum(H.bottom_moebius_function(x) * ring.monomial(n - rk(x))
7622+
for x in H)
76197623

76207624
def chain_polynomial(self):
76217625
"""
@@ -8953,8 +8957,8 @@ def cardinality(self, from_iterator=False):
89538957
# Obtained from The On-Line Encyclopedia of Integer Sequences;
89548958
# this is sequence number A000112.
89558959
known_values = [1, 1, 2, 5, 16, 63, 318, 2045, 16999, 183231,
8956-
2567284, 46749427, 1104891746, 33823827452, 1338193159771,
8957-
68275077901156, 4483130665195087]
8960+
2567284, 46749427, 1104891746, 33823827452, 1338193159771,
8961+
68275077901156, 4483130665195087]
89588962
if not from_iterator and self._n < len(known_values):
89598963
return Integer(known_values[self._n])
89608964
else:

0 commit comments

Comments
 (0)