Skip to content

Commit 47f3dd3

Browse files
author
Release Manager
committed
gh-40393: cleanup and code details in cluster_algebra_quiver some changes as suggested by pycodestyle and ruff also some further minor code enhancements, and shortening one long time doctest ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40393 Reported by: Frédéric Chapoton Reviewer(s): David Coudert, Martin Rubey
2 parents 6b2ff98 + 79a0456 commit 47f3dd3

File tree

7 files changed

+195
-212
lines changed

7 files changed

+195
-212
lines changed

src/sage/combinat/cluster_algebra_quiver/all.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
- :ref:`sage.combinat.cluster_algebra_quiver.cluster_seed`
99
"""
1010
# install the docstring of this module to the containing package
11+
from sage.misc.lazy_import import lazy_import
1112
from sage.misc.namespace_package import install_doc
12-
install_doc(__package__, __doc__)
1313

14-
from sage.misc.lazy_import import lazy_import
14+
install_doc(__package__, __doc__)
1515
lazy_import("sage.combinat.cluster_algebra_quiver.quiver_mutation_type",
1616
"QuiverMutationType")
1717
lazy_import("sage.combinat.cluster_algebra_quiver.quiver", "ClusterQuiver")

src/sage/combinat/cluster_algebra_quiver/cluster_seed.py

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,9 +2503,9 @@ def mutate(self, sequence, inplace=True, input_type=None):
25032503
sequence = self.first_green_vertex()
25042504
elif sequence == 'red':
25052505
sequence = self.first_red_vertex()
2506-
elif sequence == 'urban' or sequence == 'urban_renewal':
2506+
elif sequence in {'urban', 'urban_renewal'}:
25072507
sequence = self.first_urban_renewal()
2508-
elif sequence == 'all_urbans' or sequence == 'all_urban_renewals':
2508+
elif sequence in {'all_urbans', 'all_urban_renewals'}:
25092509
sequence = self.urban_renewals()
25102510
elif hasattr(self, sequence):
25112511
sequence = getattr(self, sequence)()
@@ -2581,33 +2581,32 @@ def mutate(self, sequence, inplace=True, input_type=None):
25812581
' "indices", or "cluster_vars"')
25822582

25832583
# Classifies the input_type. Raises warnings if the input is ambiguous, and errors if the input is not all of the same type.
2584-
else:
2585-
if is_vertices:
2586-
input_type = "vertices"
2587-
for x in seqq:
2588-
if is_indices and seed._nlist[x] != x:
2589-
print("Input can be ambiguously interpreted as both"
2590-
" vertices and indices."
2584+
elif is_vertices:
2585+
input_type = "vertices"
2586+
for x in seqq:
2587+
if is_indices and seed._nlist[x] != x:
2588+
print("Input can be ambiguously interpreted as both"
2589+
" vertices and indices."
2590+
" Mutating at vertices by default.")
2591+
break
2592+
2593+
elif is_cluster_vars:
2594+
cluster_var_index = seed.cluster_index(x)
2595+
vertex_index = seed._nlist.index(x)
2596+
if isinstance(cluster_var_index, int) and cluster_var_index != vertex_index:
2597+
print("Input can be ambiguously interpreted as"
2598+
" both vertices and cluster variables."
25912599
" Mutating at vertices by default.")
25922600
break
25932601

2594-
elif is_cluster_vars:
2595-
cluster_var_index = seed.cluster_index(x)
2596-
vertex_index = seed._nlist.index(x)
2597-
if isinstance(cluster_var_index, int) and cluster_var_index != vertex_index:
2598-
print("Input can be ambiguously interpreted as"
2599-
" both vertices and cluster variables."
2600-
" Mutating at vertices by default.")
2601-
break
2602-
2603-
# It should be impossible to interpret an index as a cluster variable.
2604-
elif is_indices:
2605-
input_type = "indices"
2606-
elif is_cluster_vars:
2607-
input_type = "cluster_vars"
2608-
else:
2609-
raise ValueError('mutation sequences must consist of exactly'
2610-
' one of vertices, indices, or cluster variables')
2602+
# It should be impossible to interpret an index as a cluster variable.
2603+
elif is_indices:
2604+
input_type = "indices"
2605+
elif is_cluster_vars:
2606+
input_type = "cluster_vars"
2607+
else:
2608+
raise ValueError('mutation sequences must consist of exactly'
2609+
' one of vertices, indices, or cluster variables')
26112610

26122611
if input_type == "cluster_vars" and len(seqq) > 1:
26132612
mutation_seed = deepcopy(seed)
@@ -3503,8 +3502,8 @@ def mutation_class_iter(self, depth=infinity, show_depth=False,
35033502
Check that :issue:`14638` is fixed::
35043503
35053504
sage: S = ClusterSeed(['E',6])
3506-
sage: MC = S.mutation_class(depth=7); len(MC) # long time
3507-
534
3505+
sage: MC = S.mutation_class(depth=6); len(MC) # long time
3506+
388
35083507
35093508
Infinite type examples::
35103509
@@ -4272,11 +4271,10 @@ def greedy(self, a1, a2, algorithm='by_recursion'):
42724271
oddT = set(T).intersection(PathSubset(a1, 0))
42734272
evenT = set(T).symmetric_difference(oddT)
42744273
ans = ans + S.x(0)**(b*len(evenT)) * S.x(1)**(c*len(oddT))
4275-
else:
4276-
if is_LeeLiZel_allowable(T, a2, a1, c, b):
4277-
oddT = set(T).intersection(PathSubset(a2, 0))
4278-
evenT = set(T).symmetric_difference(oddT)
4279-
ans = ans + S.x(0)**(b*len(oddT)) * S.x(1)**(c*len(evenT))
4274+
elif is_LeeLiZel_allowable(T, a2, a1, c, b):
4275+
oddT = set(T).intersection(PathSubset(a2, 0))
4276+
evenT = set(T).symmetric_difference(oddT)
4277+
ans = ans + S.x(0)**(b*len(oddT)) * S.x(1)**(c*len(evenT))
42804278
ans = ans*S.x(0)**(-a1)*S.x(1)**(-a2)
42814279
return ans
42824280
elif algorithm == 'just_numbers':
@@ -4431,15 +4429,15 @@ def find_upper_bound(self, verbose=False):
44314429

44324430
while True:
44334431
R = PolynomialRing(QQ, gens, order='invlex')
4434-
I = R.ideal(rels)
4435-
J = R.ideal(initial_product)
4432+
ideal_I = R.ideal(rels)
4433+
ideal_J = R.ideal(initial_product)
44364434
if verbose:
44374435
msg = 'Computing relations among {} generators'
44384436
print(msg.format(len(gens)))
44394437
start = time.time()
4440-
ISat = I.saturation(J)[0]
4441-
spend = time.time() - start
4438+
ISat = ideal_I.saturation(ideal_J)[0]
44424439
if verbose:
4440+
spend = time.time() - start
44434441
msg = 'Computed {} relations in {} seconds'
44444442
print(msg.format(len(ISat.gens()), spend))
44454443
deep_ideal = R.ideal(deep_gens) + ISat
@@ -4451,7 +4449,7 @@ def find_upper_bound(self, verbose=False):
44514449
spend = time.time() - start
44524450
if M == initial_product_ideal:
44534451
if verbose:
4454-
print('Verified that there are no new elements in', spend, 'seconds')
4452+
print(f'Verified that there are no new elements in {spend} seconds')
44554453
print('Returning a presentation for the upper bound')
44564454
return R.quotient_ring(ISat)
44574455
else:
@@ -4695,7 +4693,7 @@ def _produce_upper_cluster_algebra_element(self, vd, cList):
46954693
# Computes the Laurent Polynomial for each vector in the decomposition.
46964694
finalP = []
46974695
# Laurent polynomial for each vector in {0,1}^n
4698-
for i in range(len(vd)):
4696+
for i, vdi in enumerate(vd):
46994697
numerator = 0
47004698
if cList[i]:
47014699
# If the vector in vd is negative then it did not
@@ -4712,19 +4710,20 @@ def _produce_upper_cluster_algebra_element(self, vd, cList):
47124710
expn = 0
47134711
# The exponent is determined by the vectors a,s, and the matrix B.
47144712
for k in range(num_cols):
4715-
expn += (vd[i][0][k]-s[k])*max(0, B[j][k])+s[k]*max(0, -B[j][k])
4716-
term *= x ** expn
4713+
expn += (vdi[0][k]-s[k])*max(0, B[j][k])+s[k]*max(0, -B[j][k])
4714+
term *= x**expn
47174715
numerator += term
47184716
# Gives a numerator for the negative vector, or else the product would be zero.
47194717
else:
47204718
numerator = 1
47214719

47224720
# Uses the vectors in vd to calculates the denominator of the Laurent.
47234721
denominator = 1
4724-
for l in range(num_cols):
4725-
denominator = denominator * (R.gen(l))**vd[i][0][l]
4722+
powers = vdi[0]
4723+
for ell in range(num_cols):
4724+
denominator = denominator * R.gen(ell)**powers[ell]
47264725
# Each copy of a vector in vd contributes a factor of the Laurent polynomial calculated from it.
4727-
final = (numerator / denominator)**vd[i][1]
4726+
final = (numerator / denominator)**vdi[1]
47284727
finalP.append(final)
47294728
laurentP = 1
47304729
# The UCA element for the vector a is the product of the elements produced from the vectors in its decomposition.
@@ -4762,15 +4761,15 @@ def coeff_recurs(p, q, a1, a2, b, c):
47624761
"""
47634762
if p == 0 and q == 0:
47644763
return 1
4765-
elif p < 0 or q < 0:
4764+
if p < 0 or q < 0:
47664765
return 0
4767-
else:
4768-
if c*a1*q <= b*a2*p:
4769-
return sum((-1)**(k-1)*coeff_recurs(p-k, q, a1, a2, b, c)*_bino(a2-c*q+k-1, k)
4770-
for k in range(1, p+1))
4771-
else:
4772-
return sum((-1)**(k-1)*coeff_recurs(p, q-k, a1, a2, b, c)*_bino(a1-b*p+k-1, k)
4773-
for k in range(1, q+1))
4766+
if c * a1 * q <= b * a2 * p:
4767+
return sum((-1)**(k - 1) * coeff_recurs(p - k, q, a1, a2, b, c)
4768+
*_bino(a2 - c * q + k - 1, k)
4769+
for k in range(1, p + 1))
4770+
return sum((-1)**(k - 1) * coeff_recurs(p, q - k, a1, a2, b, c)
4771+
*_bino(a1 - b * p + k - 1, k)
4772+
for k in range(1, q + 1))
47744773

47754774

47764775
def PathSubset(n, m):

src/sage/combinat/cluster_algebra_quiver/interact.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def cluster_interact(self, fig_size=1, circular=True, kind='seed'):
4848
description="Show last mutation vertex")
4949

5050
mut_buttons = widgets.ToggleButtons(options=list(range(self._n)),
51-
style={'button_width':'initial'},
52-
description='Mutate at: ')
51+
style={'button_width': 'initial'},
52+
description='Mutate at: ')
5353

5454
which_plot = widgets.Dropdown(options=['circular', 'spring'],
5555
value='circular' if circular else "spring",

0 commit comments

Comments
 (0)