1- from functools import cache
1+ from mapFolding . algorithms . matrixMeandersBeDry import walkDyckPath
22from mapFolding .dataBaskets import MatrixMeandersState
33
44def outfitDictionaryBitGroups (state : MatrixMeandersState ) -> dict [tuple [int , int ], int ]:
@@ -12,49 +12,11 @@ def outfitDictionaryBitGroups(state: MatrixMeandersState) -> dict[tuple[int, int
1212 Returns
1313 -------
1414 dictionaryBitGroups : dict[tuple[int, int], int]
15- A dictionary of `(bitsAlpha, bitsZulu)` to `distinctCrossings `.
15+ A dictionary of `(bitsAlpha, bitsZulu)` to `crossings `.
1616 """
1717 state .bitWidth = max (state .dictionaryMeanders .keys ()).bit_length ()
18- return {(arcCode & state .locatorBits , (arcCode >> 1 ) & state .locatorBits ): distinctCrossings
19- for arcCode , distinctCrossings in state .dictionaryMeanders .items ()}
20-
21- @cache
22- def walkDyckPath (intWithExtra_0b1 : int ) -> int :
23- """Find the bit position for flipping paired curve endpoints in meander transfer matrices.
24-
25- Parameters
26- ----------
27- intWithExtra_0b1 : int
28- Binary representation of curve locations with an extra bit encoding parity information.
29-
30- Returns
31- -------
32- flipExtra_0b1_Here : int
33- Bit mask indicating the position where the balance condition fails, formatted as 2^(2k).
34-
35- 3L33T H@X0R
36- ------------
37- Binary search for first negative balance in shifted bit pairs. Returns 2^(2k) mask for
38- bit position k where cumulative balance counter transitions from non-negative to negative.
39-
40- Mathematics
41- -----------
42- Implements the Dyck path balance verification algorithm from Jensen's transfer matrix
43- enumeration. Computes the position where ∑(i=0 to k) (-1)^b_i < 0 for the first time,
44- where b_i are the bits of the input at positions 2i.
45-
46- """
47- findTheExtra_0b1 : int = 0
48- flipExtra_0b1_Here : int = 1
49- while True :
50- flipExtra_0b1_Here <<= 2
51- if (intWithExtra_0b1 & flipExtra_0b1_Here ) == 0 :
52- findTheExtra_0b1 += 1
53- else :
54- findTheExtra_0b1 -= 1
55- if findTheExtra_0b1 < 0 :
56- break
57- return flipExtra_0b1_Here
18+ return {(arcCode & state .locatorBits , (arcCode >> 1 ) & state .locatorBits ): crossings
19+ for arcCode , crossings in state .dictionaryMeanders .items ()}
5820
5921def count (state : MatrixMeandersState ) -> MatrixMeandersState :
6022 """Count meanders with matrix transfer algorithm using Python `int` (*int*eger) contained in a Python `dict` (*dict*ionary).
@@ -77,25 +39,25 @@ def count(state: MatrixMeandersState) -> MatrixMeandersState:
7739 dictionaryBitGroups = outfitDictionaryBitGroups (state )
7840 state .dictionaryMeanders = {}
7941
80- for (bitsAlpha , bitsZulu ), distinctCrossings in dictionaryBitGroups .items ():
42+ for (bitsAlpha , bitsZulu ), crossings in dictionaryBitGroups .items ():
8143 bitsAlphaHasArcs : bool = bitsAlpha > 1
8244 bitsZuluHasArcs : bool = bitsZulu > 1
8345 bitsAlphaIsEven = bitsZuluIsEven = 0
8446
85- arcCurveAnalysis = ((bitsAlpha | (bitsZulu << 1 )) << 2 ) | 3
47+ arcCodeAnalysis = ((bitsAlpha | (bitsZulu << 1 )) << 2 ) | 3
8648 # simple
87- if arcCurveAnalysis < state .MAXIMUMarcCode :
88- state .dictionaryMeanders [arcCurveAnalysis ] = state .dictionaryMeanders .get (arcCurveAnalysis , 0 ) + distinctCrossings
49+ if arcCodeAnalysis < state .MAXIMUMarcCode :
50+ state .dictionaryMeanders [arcCodeAnalysis ] = state .dictionaryMeanders .get (arcCodeAnalysis , 0 ) + crossings
8951
9052 if bitsAlphaHasArcs :
91- arcCurveAnalysis = (bitsAlpha >> 2 ) | (bitsZulu << 3 ) | ((bitsAlphaIsEven := 1 - (bitsAlpha & 1 )) << 1 )
92- if arcCurveAnalysis < state .MAXIMUMarcCode :
93- state .dictionaryMeanders [arcCurveAnalysis ] = state .dictionaryMeanders .get (arcCurveAnalysis , 0 ) + distinctCrossings
53+ arcCodeAnalysis = (bitsAlpha >> 2 ) | (bitsZulu << 3 ) | ((bitsAlphaIsEven := 1 - (bitsAlpha & 1 )) << 1 )
54+ if arcCodeAnalysis < state .MAXIMUMarcCode :
55+ state .dictionaryMeanders [arcCodeAnalysis ] = state .dictionaryMeanders .get (arcCodeAnalysis , 0 ) + crossings
9456
9557 if bitsZuluHasArcs :
96- arcCurveAnalysis = (bitsZulu >> 1 ) | (bitsAlpha << 2 ) | (bitsZuluIsEven := 1 - (bitsZulu & 1 ))
97- if arcCurveAnalysis < state .MAXIMUMarcCode :
98- state .dictionaryMeanders [arcCurveAnalysis ] = state .dictionaryMeanders .get (arcCurveAnalysis , 0 ) + distinctCrossings
58+ arcCodeAnalysis = (bitsZulu >> 1 ) | (bitsAlpha << 2 ) | (bitsZuluIsEven := 1 - (bitsZulu & 1 ))
59+ if arcCodeAnalysis < state .MAXIMUMarcCode :
60+ state .dictionaryMeanders [arcCodeAnalysis ] = state .dictionaryMeanders .get (arcCodeAnalysis , 0 ) + crossings
9961
10062 if bitsAlphaHasArcs and bitsZuluHasArcs and (bitsAlphaIsEven or bitsZuluIsEven ):
10163 # aligned
@@ -104,16 +66,16 @@ def count(state: MatrixMeandersState) -> MatrixMeandersState:
10466 elif bitsZuluIsEven and not bitsAlphaIsEven :
10567 bitsZulu ^= walkDyckPath (bitsZulu ) # noqa: PLW2901
10668
107- arcCurveAnalysis : int = ((bitsZulu >> 2 ) << 1 ) | (bitsAlpha >> 2 )
108- if arcCurveAnalysis < state .MAXIMUMarcCode :
109- state .dictionaryMeanders [arcCurveAnalysis ] = state .dictionaryMeanders .get (arcCurveAnalysis , 0 ) + distinctCrossings
69+ arcCodeAnalysis : int = ((bitsZulu >> 2 ) << 1 ) | (bitsAlpha >> 2 )
70+ if arcCodeAnalysis < state .MAXIMUMarcCode :
71+ state .dictionaryMeanders [arcCodeAnalysis ] = state .dictionaryMeanders .get (arcCodeAnalysis , 0 ) + crossings
11072
11173 dictionaryBitGroups = {}
11274
11375 return state
11476
11577def doTheNeedful (state : MatrixMeandersState ) -> int :
116- """Compute `distinctCrossings ` with a transfer matrix algorithm.
78+ """Compute `crossings ` with a transfer matrix algorithm.
11779
11880 Parameters
11981 ----------
@@ -122,8 +84,8 @@ def doTheNeedful(state: MatrixMeandersState) -> int:
12284
12385 Returns
12486 -------
125- distinctCrossings : int
126- The computed value of `distinctCrossings `.
87+ crossings : int
88+ The computed value of `crossings `.
12789
12890 Notes
12991 -----
0 commit comments