Skip to content

Commit

Permalink
sagemathgh-39380: inline an inner function in random two-sphere (for …
Browse files Browse the repository at this point in the history
…speed)

    
to avoid a function call, in order to get a little more speed

### 📝 Checklist

- [x] The title is concise and informative.
- [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 and checked the documentation
preview.
    
URL: sagemath#39380
Reported by: Frédéric Chapoton
Reviewer(s): David Coudert
  • Loading branch information
Release Manager committed Jan 27, 2025
2 parents 0d3c3f7 + 35df246 commit d64128d
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/sage/graphs/generators/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -2320,21 +2320,16 @@ def RandomTriangulation(n, set_position=False, k=3, seed=None):

pattern = ['in', 'in', 'in', 'lf', 'in'] # 'partial closures'

def rotate_word_to_next_occurrence(word):
"""
Rotate ``word`` so that the given pattern occurs at the beginning.
If the given pattern is not found, return the empty list.
"""
# We greedily perform the replacements 'in1,in2,in3,lf,in3'->'in1,in3'.
while True:
# first we rotate the word to it starts with pattern
word2 = []
N = len(word)
for i in range(N):
if all(word[(i + j) % N][0] == pattern[j] for j in range(5)):
return word[i:] + word[:i]
return []
word2 = word[i:] + word[:i]
break

# We greedily perform the replacements 'in1,in2,in3,lf,in3'->'in1,in3'.
while True:
word2 = rotate_word_to_next_occurrence(word)
if len(word2) >= 5:
word = [word2[0]] + word2[4:]
in1, in2, in3 = (u[1] for u in word2[:3])
Expand Down

0 comments on commit d64128d

Please sign in to comment.