Skip to content

Commit 021835f

Browse files
committed
more uses of cython code for is_fully_comm
1 parent a3d3dbd commit 021835f

File tree

2 files changed

+29
-49
lines changed

2 files changed

+29
-49
lines changed

src/sage/combinat/affine_permutation.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def __call__(self, i):
217217
"""
218218
return self.value(i)
219219

220-
def is_i_grassmannian(self, i=0, side="right"):
220+
def is_i_grassmannian(self, i=0, side="right") -> bool:
221221
r"""
222222
Test whether ``self`` is `i`-grassmannian, i.e., either is the
223223
identity or has ``i`` as the sole descent.
@@ -277,7 +277,7 @@ def lower_covers(self,side="right"):
277277
S = self.descents(side)
278278
return [self.apply_simple_reflection(i, side) for i in S]
279279

280-
def is_one(self):
280+
def is_one(self) -> bool:
281281
r"""
282282
Tests whether the affine permutation is the identity.
283283
@@ -876,18 +876,21 @@ def to_lehmer_code(self, typ='decreasing', side='right'):
876876
code[i] += (b-i) // (self.k+1) + 1
877877
return Composition(code)
878878

879-
def is_fully_commutative(self):
879+
def is_fully_commutative(self) -> bool:
880880
r"""
881-
Determine whether ``self`` is fully commutative, i.e., has no
882-
reduced words with a braid.
881+
Determine whether ``self`` is fully commutative.
882+
883+
This means that it has no reduced word with a braid.
884+
885+
This uses a specific algorithm.
883886
884887
EXAMPLES::
885888
886889
sage: A = AffinePermutationGroup(['A',7,1])
887-
sage: p=A([3, -1, 0, 6, 5, 4, 10, 9])
890+
sage: p = A([3, -1, 0, 6, 5, 4, 10, 9])
888891
sage: p.is_fully_commutative()
889892
False
890-
sage: q=A([-3, -2, 0, 7, 9, 2, 11, 12])
893+
sage: q = A([-3, -2, 0, 7, 9, 2, 11, 12])
891894
sage: q.is_fully_commutative()
892895
True
893896
"""
@@ -900,14 +903,12 @@ def is_fully_commutative(self):
900903
if c[i] > 0:
901904
if firstnonzero is None:
902905
firstnonzero = i
903-
if m != -1 and c[i] - (i-m) >= c[m]:
906+
if m != -1 and c[i] - (i - m) >= c[m]:
904907
return False
905908
m = i
906-
#now check m (the last non-zero) against firstnonzero.
907-
d = self.n-(m-firstnonzero)
908-
if c[firstnonzero]-d >= c[m]:
909-
return False
910-
return True
909+
# now check m (the last non-zero) against first non-zero.
910+
d = self.n - (m - firstnonzero)
911+
return not c[firstnonzero] - d >= c[m]
911912

912913
def to_bounded_partition(self, typ='decreasing', side='right'):
913914
r"""

src/sage/combinat/fully_commutative_elements.py

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -153,44 +153,23 @@ def is_fully_commutative(self):
153153
sage: x = FC.element_class(FC, [1, 2, 1], check=False); x.is_fully_commutative()
154154
False
155155
"""
156-
matrix = self.parent().coxeter_group().coxeter_matrix()
157-
w = tuple(self)
158-
159-
# The following function detects 'braid' words.
160-
def contains_long_braid(w):
161-
for i in range(len(w) - 2):
162-
a = w[i]
163-
b = w[i + 1]
164-
m = matrix[a, b]
165-
if m > 2 and i + m <= len(w):
166-
ab_braid = (a, b) * (m // 2) + ((a,) if m % 2 else ())
167-
if w[i:i + m] == ab_braid:
168-
return True
169-
return False
156+
word = list(self)
157+
from sage.combinat.root_system.braid_orbit import is_fully_commutative as is_fully_comm
170158

171-
# The following function applies a commutation relation on a word.
172-
def commute_once(word, i):
173-
return word[:i] + (word[i + 1], word[i]) + word[i + 2:]
159+
group = self.parent().coxeter_group()
160+
braid_rels = group.braid_relations()
161+
I = group.index_set()
174162

175-
# A word is the reduced word of an FC element iff no sequence of
176-
# commutation relations on it yields a word with a 'braid' word:
177-
if contains_long_braid(w):
178-
return False
179-
else:
180-
l, checked, queue = len(w), {w}, deque([w])
181-
while queue:
182-
word = queue.pop()
183-
for i in range(l - 1):
184-
a, b = word[i], word[i + 1]
185-
if matrix[a, b] == 2:
186-
new_word = commute_once(word, i)
187-
if new_word not in checked:
188-
if contains_long_braid(new_word):
189-
return False
190-
else:
191-
checked.add(new_word)
192-
queue.appendleft(new_word)
193-
return True
163+
from sage.rings.integer_ring import ZZ
164+
be_careful = any(i not in ZZ for i in I)
165+
166+
if be_careful:
167+
Iinv = {i: j for j, i in enumerate(I)}
168+
word = [Iinv[i] for i in word]
169+
braid_rels = [[[Iinv[i] for i in l],
170+
[Iinv[i] for i in r]] for l, r in braid_rels]
171+
172+
return is_fully_comm(word, braid_rels)
194173

195174
# Representing FC elements: Heaps
196175
def heap(self, **kargs):

0 commit comments

Comments
 (0)