Skip to content

Commit 4ed4bce

Browse files
committed
fix(math): chip-firing lint (unused vars, formatting, variable naming)
1 parent 88047fe commit 4ed4bce

4 files changed

Lines changed: 41 additions & 47 deletions

File tree

src/jacobian/math/chip_firing/_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def _validate_divisor(
2121
) -> None:
2222
if len(divisor) != len(vertices):
2323
raise ValueError(f"{label} length must match vertex count")
24-
if any(abs(c) >= 10 ** MAX_COEFFICIENT_DIGITS for c in divisor):
24+
if any(abs(c) >= 10**MAX_COEFFICIENT_DIGITS for c in divisor):
2525
raise ValueError(f"{label} coefficients exceed the digit bound")
2626

2727

@@ -132,7 +132,7 @@ def require_valid_request(self) -> Self:
132132
raise ValueError("divisor length must match vertex count")
133133
if len(self.firing_vector) != n:
134134
raise ValueError("firing vector length must match vertex count")
135-
if any(abs(c) >= 10 ** MAX_COEFFICIENT_DIGITS for c in self.firing_vector):
135+
if any(abs(c) >= 10**MAX_COEFFICIENT_DIGITS for c in self.firing_vector):
136136
raise ValueError("firing vector coefficients exceed the digit bound")
137137
return self
138138

src/jacobian/math/chip_firing/_operations.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ def compute_reduced_laplacian(
9393
lap = full.laplacian
9494
sink_idx = vertices.index(request.sink)
9595
nonsink = [i for i in range(n) if i != sink_idx]
96-
reduced = tuple(
97-
tuple(lap[i][j] for j in nonsink) for i in nonsink
98-
)
96+
reduced = tuple(tuple(lap[i][j] for j in nonsink) for i in nonsink)
9997
return ReducedLaplacianResult(
10098
vertices=vertices,
10199
sink=request.sink,
@@ -187,7 +185,6 @@ def compute_stabilize(request: StabilizeRequest) -> StabilizeResult:
187185
"""Stabilize a sink configuration and return the odometer."""
188186
sc = request.configuration
189187
vertices = sc.graph.vertices
190-
n = len(vertices)
191188
sink_idx = vertices.index(sc.sink)
192189
adj = _adjacency(sc.graph)
193190
degrees = _degrees(sc.graph)
@@ -204,13 +201,13 @@ def compute_parallel_step(request: ParallelStepRequest) -> ParallelStepResult:
204201
"""One simultaneous legal firing step on all unstable nonsink vertices."""
205202
sc = request.configuration
206203
vertices = sc.graph.vertices
207-
n = len(vertices)
208204
sink_idx = vertices.index(sc.sink)
209205
adj = _adjacency(sc.graph)
210206
degrees = _degrees(sc.graph)
211207
config = list(sc.configuration)
212-
fired = [v for i, v in enumerate(vertices)
213-
if i != sink_idx and config[i] >= degrees[i]]
208+
fired = [
209+
v for i, v in enumerate(vertices) if i != sink_idx and config[i] >= degrees[i]
210+
]
214211
next_config = list(config)
215212
for v in fired:
216213
vi = vertices.index(v)
@@ -319,9 +316,7 @@ def _smith_normal_form_diagonal(
319316
cols = len(matrix[0]) if matrix else 0
320317
if rows == 0 or cols == 0:
321318
return ()
322-
source = sympy.Matrix(
323-
[[int(value) for value in row] for row in matrix]
324-
)
319+
source = sympy.Matrix([[int(value) for value in row] for row in matrix])
325320
diagonal, _left, _right = smith_normal_decomp(source, domain=sympy.ZZ)
326321
result = []
327322
for i in range(min(rows, cols)):
@@ -353,9 +348,7 @@ def _critical_group_factors(
353348

354349
def compute_critical_group(request: CriticalGroupRequest) -> CriticalGroupResult:
355350
"""Compute the critical group via SNF of the reduced Laplacian."""
356-
nonsink_labels, invariant = _critical_group_factors(
357-
request.graph, request.sink
358-
)
351+
nonsink_labels, invariant = _critical_group_factors(request.graph, request.sink)
359352
order = 1
360353
for d in invariant:
361354
order *= d
@@ -378,9 +371,7 @@ def compute_abel_jacobi(request: AbelJacobiRequest) -> AbelJacobiResult:
378371
n = len(vertices)
379372
sink_idx = vertices.index(request.sink)
380373
nonsink = [i for i in range(n) if i != sink_idx]
381-
nonsink_labels, invariant = _critical_group_factors(
382-
request.graph, request.sink
383-
)
374+
nonsink_labels, invariant = _critical_group_factors(request.graph, request.sink)
384375
nonsink_div = [request.divisor[i] for i in nonsink]
385376
# The coordinates: nonsink_div mod the invariant factors.
386377
# Only non-unit, non-zero factors matter for the quotient group.

src/jacobian/math/chip_firing/_tools.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ def _op[RequestT: StrictModel, ResultT: StrictModel](
112112
examples=(
113113
example(
114114
"path_graph_3_sink_a",
115-
"Compute the reduced Laplacian of a path graph with sink "
116-
"at vertex a.",
115+
"Compute the reduced Laplacian of a path graph with sink at vertex a.",
117116
{"graph": _GRAPH, "sink": "a"},
118117
),
119118
),
@@ -153,8 +152,7 @@ def _op[RequestT: StrictModel, ResultT: StrictModel](
153152
examples=(
154153
example(
155154
"fire_e_a",
156-
"Fire the unit vector e_a on a path graph; degree is "
157-
"preserved.",
155+
"Fire the unit vector e_a on a path graph; degree is preserved.",
158156
{
159157
"graph": _GRAPH,
160158
"divisor": [3, 0, 1],
@@ -178,8 +176,7 @@ def _op[RequestT: StrictModel, ResultT: StrictModel](
178176
examples=(
179177
example(
180178
"path_graph_3_sink_a",
181-
"Stabilize a path graph configuration with sink at "
182-
"vertex a.",
179+
"Stabilize a path graph configuration with sink at vertex a.",
183180
{"configuration": _SINK_CONFIG},
184181
),
185182
),
@@ -265,8 +262,7 @@ def _op[RequestT: StrictModel, ResultT: StrictModel](
265262
examples=(
266263
example(
267264
"path_graph_3",
268-
"Compute the canonical divisor of a path graph on 3 "
269-
"vertices.",
265+
"Compute the canonical divisor of a path graph on 3 vertices.",
270266
{"graph": _GRAPH},
271267
),
272268
),
@@ -286,8 +282,7 @@ def _op[RequestT: StrictModel, ResultT: StrictModel](
286282
examples=(
287283
example(
288284
"triangle_sink_a",
289-
"Compute the critical group of a triangle graph with "
290-
"sink at vertex a.",
285+
"Compute the critical group of a triangle graph with sink at vertex a.",
291286
{
292287
"graph": {
293288
"vertices": ["a", "b", "c"],

tests/math/chip_firing/test_chip_firing.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ def test_path_graph_sink_a(self) -> None:
6363
assert result.reduced_laplacian == ((2, -1), (-1, 1))
6464

6565
def test_triangle_sink_b(self) -> None:
66-
result = compute_reduced_laplacian(
67-
ReducedLaplacianRequest(graph=C3, sink="b")
68-
)
66+
result = compute_reduced_laplacian(ReducedLaplacianRequest(graph=C3, sink="b"))
6967
assert result.reduced_laplacian == ((2, -1), (-1, 2))
7068

7169
def test_invalid_sink(self) -> None:
@@ -133,7 +131,9 @@ def test_firing_vector_composition(self) -> None:
133131
)
134132
composed = compute_fire_vector(
135133
FireVectorRequest(
136-
graph=GRAPH, divisor=div, firing_vector=[f1[i] + f2[i] for i in range(3)]
134+
graph=GRAPH,
135+
divisor=div,
136+
firing_vector=[f1[i] + f2[i] for i in range(3)],
137137
)
138138
)
139139
assert r2.fired_divisor == composed.fired_divisor
@@ -162,9 +162,7 @@ def test_stabilization_is_stable(self) -> None:
162162
def test_stabilization_idempotence(self) -> None:
163163
sc = SinkConfiguration(graph=GRAPH, sink="a", configuration=[0, 5, 0])
164164
r1 = compute_stabilize(StabilizeRequest(configuration=sc))
165-
sc2 = SinkConfiguration(
166-
graph=GRAPH, sink="a", configuration=list(r1.stable)
167-
)
165+
sc2 = SinkConfiguration(graph=GRAPH, sink="a", configuration=list(r1.stable))
168166
r2 = compute_stabilize(StabilizeRequest(configuration=sc2))
169167
assert r1.stable == r2.stable
170168
assert r2.total_firings == 0
@@ -274,11 +272,11 @@ def test_triangle(self) -> None:
274272
assert result.order == 3
275273

276274
def test_cycle_c4(self) -> None:
277-
C4 = {
275+
c4 = {
278276
"vertices": ["a", "b", "c", "d"],
279277
"edges": [["a", "b"], ["b", "c"], ["c", "d"], ["d", "a"]],
280278
}
281-
result = compute_critical_group(CriticalGroupRequest(graph=C4, sink="a"))
279+
result = compute_critical_group(CriticalGroupRequest(graph=c4, sink="a"))
282280
assert result.invariant_factors == (1, 1, 4)
283281
assert result.order == 4
284282

@@ -288,14 +286,18 @@ def test_tree_is_trivial(self) -> None:
288286
assert result.order == 1
289287

290288
def test_complete_k4(self) -> None:
291-
K4 = {
289+
k4 = {
292290
"vertices": ["a", "b", "c", "d"],
293291
"edges": [
294-
["a", "b"], ["a", "c"], ["a", "d"],
295-
["b", "c"], ["b", "d"], ["c", "d"],
292+
["a", "b"],
293+
["a", "c"],
294+
["a", "d"],
295+
["b", "c"],
296+
["b", "d"],
297+
["c", "d"],
296298
],
297299
}
298-
result = compute_critical_group(CriticalGroupRequest(graph=K4, sink="a"))
300+
result = compute_critical_group(CriticalGroupRequest(graph=k4, sink="a"))
299301
assert result.order == 16
300302

301303
def test_order_matches_spanning_tree_count(self) -> None:
@@ -314,17 +316,17 @@ def count_spanning_trees(vertices, edges):
314316
minor = Matrix([row[: n - 1] for row in lap[: n - 1]])
315317
result = compute_critical_group(
316318
CriticalGroupRequest(
317-
graph={'vertices': vertices, 'edges': [list(e) for e in edges]},
319+
graph={"vertices": vertices, "edges": [list(e) for e in edges]},
318320
sink=vertices[0],
319321
)
320322
)
321323
return result, int(minor.det())
322324

323325
for vertices, edges in [
324-
(['a', 'b', 'c'], [('a', 'b'), ('b', 'c'), ('a', 'c')]),
326+
(["a", "b", "c"], [("a", "b"), ("b", "c"), ("a", "c")]),
325327
(
326-
['a', 'b', 'c', 'd'],
327-
[('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')],
328+
["a", "b", "c", "d"],
329+
[("a", "b"), ("b", "c"), ("c", "d"), ("d", "a")],
328330
),
329331
]:
330332
res, trees = count_spanning_trees(vertices, edges)
@@ -369,8 +371,14 @@ def test_laplacian_equivariance(self) -> None:
369371
assert r1.laplacian == r2.laplacian
370372

371373
def test_critical_group_relabelling(self) -> None:
372-
graph1 = {"vertices": ["a", "b", "c"], "edges": [["a", "b"], ["b", "c"], ["a", "c"]]}
373-
graph2 = {"vertices": ["x", "y", "z"], "edges": [["x", "y"], ["y", "z"], ["x", "z"]]}
374+
graph1 = {
375+
"vertices": ["a", "b", "c"],
376+
"edges": [["a", "b"], ["b", "c"], ["a", "c"]],
377+
}
378+
graph2 = {
379+
"vertices": ["x", "y", "z"],
380+
"edges": [["x", "y"], ["y", "z"], ["x", "z"]],
381+
}
374382
r1 = compute_critical_group(CriticalGroupRequest(graph=graph1, sink="a"))
375383
r2 = compute_critical_group(CriticalGroupRequest(graph=graph2, sink="x"))
376384
assert r1.invariant_factors == r2.invariant_factors

0 commit comments

Comments
 (0)