Skip to content

Commit d8794b5

Browse files
committed
fix(math): impartial games disjunctive sum invariants and lint
1 parent fec80bf commit d8794b5

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/jacobian/math/impartial_games/_models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,19 @@ class DisjunctiveSumResult(StrictModel):
221221
component_grundy_values: tuple[int, ...]
222222
is_p_position: bool
223223
component_count: int = Field(ge=1)
224+
225+
@model_validator(mode="after")
226+
def require_exact_disjunctive_invariants(self) -> Self:
227+
if self.component_count != len(self.component_grundy_values):
228+
raise ValueError("component_count must match component_grundy_values length")
229+
if any(value < 0 for value in self.component_grundy_values):
230+
raise ValueError("component Grundy values must be nonnegative")
231+
from functools import reduce
232+
from operator import xor
233+
234+
expected = reduce(xor, self.component_grundy_values, 0)
235+
if self.grundy_value != expected:
236+
raise ValueError("grundy_value must be XOR of component_grundy_values")
237+
if self.is_p_position != (expected == 0):
238+
raise ValueError("is_p_position must agree with grundy_value == 0")
239+
return self

src/jacobian/math/impartial_games/_operations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""Wire adapters for exact bounded impartial-game operations."""
22

33
from jacobian.math.impartial_games._models import (
4-
DisjunctiveSumRequest,
5-
DisjunctiveSumResult,
64
BirthdayRequest,
75
BirthdayResult,
6+
DisjunctiveSumRequest,
7+
DisjunctiveSumResult,
88
GrundyEntry,
99
GrundyTableRequest,
1010
GrundyTableResult,
@@ -67,8 +67,8 @@ def compute_subtraction_grundy_prefix(
6767

6868

6969
__all__ = [
70-
"compute_disjunctive_sum",
7170
"compute_birthday",
71+
"compute_disjunctive_sum",
7272
"compute_grundy_table",
7373
"compute_subtraction_grundy_prefix",
7474
]

tests/math/impartial_games/test_disjunctive_sum.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
from jacobian.math.impartial_games._models import (
77
DisjunctiveSumRequest,
8-
DisjunctiveSumResult,
98
)
109
from jacobian.math.impartial_games._operations import compute_disjunctive_sum
1110
from jacobian.math.impartial_games._tools import TOOLS
1211

13-
1412
# -- helpers -----------------------------------------------------------------
1513

1614
_TERMINAL = {"positions": ["start"], "moves": []}

0 commit comments

Comments
 (0)