mapFolding 0.5.0__py3-none-any.whl → 0.5.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mapFolding/__init__.py +93 -58
- mapFolding/basecamp.py +5 -5
- mapFolding/beDRY.py +5 -5
- mapFolding/oeis.py +26 -25
- mapFolding/theSSOT.py +3 -15
- mapFolding/theSSOTdatatypes.py +10 -20
- {mapFolding-0.5.0.dist-info → mapFolding-0.5.1.dist-info}/METADATA +2 -1
- mapFolding-0.5.1.dist-info/RECORD +14 -0
- {mapFolding-0.5.0.dist-info → mapFolding-0.5.1.dist-info}/top_level.txt +0 -1
- mapFolding/reference/flattened.py +0 -377
- mapFolding/reference/hunterNumba.py +0 -132
- mapFolding/reference/irvineJavaPort.py +0 -120
- mapFolding/reference/jax.py +0 -208
- mapFolding/reference/lunnan.py +0 -153
- mapFolding/reference/lunnanNumpy.py +0 -123
- mapFolding/reference/lunnanWhile.py +0 -121
- mapFolding/reference/rotatedEntryPoint.py +0 -240
- mapFolding/reference/total_countPlus1vsPlusN.py +0 -211
- mapFolding/someAssemblyRequired/__init__.py +0 -5
- mapFolding/someAssemblyRequired/getLLVMforNoReason.py +0 -19
- mapFolding/someAssemblyRequired/makeJob.py +0 -56
- mapFolding/someAssemblyRequired/synthesizeModuleJAX.py +0 -27
- mapFolding/someAssemblyRequired/synthesizeNumba.py +0 -345
- mapFolding/someAssemblyRequired/synthesizeNumbaGeneralized.py +0 -397
- mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +0 -155
- mapFolding/someAssemblyRequired/synthesizeNumbaModules.py +0 -123
- mapFolding/syntheticModules/numbaCount.py +0 -158
- mapFolding/syntheticModules/numba_doTheNeedful.py +0 -13
- mapFolding-0.5.0.dist-info/RECORD +0 -39
- tests/__init__.py +0 -1
- tests/conftest.py +0 -335
- tests/test_computations.py +0 -42
- tests/test_oeis.py +0 -128
- tests/test_other.py +0 -175
- tests/test_tasks.py +0 -40
- /mapFolding/{syntheticModules/__init__.py → py.typed} +0 -0
- {mapFolding-0.5.0.dist-info → mapFolding-0.5.1.dist-info}/LICENSE +0 -0
- {mapFolding-0.5.0.dist-info → mapFolding-0.5.1.dist-info}/WHEEL +0 -0
- {mapFolding-0.5.0.dist-info → mapFolding-0.5.1.dist-info}/entry_points.txt +0 -0
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
from mapFolding import outfitFoldings
|
|
2
|
-
from numba import njit
|
|
3
|
-
from typing import List
|
|
4
|
-
import numpy
|
|
5
|
-
from numpy.typing import NDArray
|
|
6
|
-
|
|
7
|
-
"""
|
|
8
|
-
It is possible to enter the main `while` loop from an arbitrary point. This version is "rotated" to effectively enter at the modulo operator.
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
# Indices of array `track`, which is a collection of one-dimensional arrays each of length `the[leavesTotal] + 1`.
|
|
12
|
-
# The values in the array cells are dynamic, small, unsigned integers.
|
|
13
|
-
A = leafAbove = 0
|
|
14
|
-
"""Leaf above leaf m"""
|
|
15
|
-
B = leafBelow = 1
|
|
16
|
-
"""Leaf below leaf m"""
|
|
17
|
-
count = countDimensionsGapped = 2
|
|
18
|
-
"""Number of gaps available for leaf l"""
|
|
19
|
-
gapter = gapRangeStart = 3
|
|
20
|
-
"""Index of gap stack for leaf l"""
|
|
21
|
-
|
|
22
|
-
# Indices of array `my`, which holds dynamic, small, unsigned, integer values.
|
|
23
|
-
tricky = [
|
|
24
|
-
(leaf1ndex := 0),
|
|
25
|
-
(gap1ndex := 1),
|
|
26
|
-
(unconstrainedLeaf := 2),
|
|
27
|
-
(gap1ndexCeiling := 3),
|
|
28
|
-
(leafConnectee := 4),
|
|
29
|
-
(taskIndex := 5),
|
|
30
|
-
(dimension1ndex := 6),
|
|
31
|
-
(foldingsSubtotal := 7),
|
|
32
|
-
]
|
|
33
|
-
|
|
34
|
-
COUNTindicesDynamic = len(tricky)
|
|
35
|
-
|
|
36
|
-
# Indices of array `the`, which holds unchanging, small, unsigned, integer values.
|
|
37
|
-
tricky = [
|
|
38
|
-
(dimensionsPlus1 := 0),
|
|
39
|
-
(dimensionsTotal := 1),
|
|
40
|
-
(leavesTotal := 2),
|
|
41
|
-
]
|
|
42
|
-
|
|
43
|
-
COUNTindicesStatic = len(tricky)
|
|
44
|
-
|
|
45
|
-
def countFolds(listDimensions: List[int]):
|
|
46
|
-
static = numpy.zeros(COUNTindicesStatic, dtype=numpy.int64)
|
|
47
|
-
|
|
48
|
-
listDimensions, static[leavesTotal], D, track,gapsWhere = outfitFoldings(listDimensions)
|
|
49
|
-
|
|
50
|
-
static[dimensionsTotal] = len(listDimensions)
|
|
51
|
-
static[dimensionsPlus1] = static[dimensionsTotal] + 1
|
|
52
|
-
|
|
53
|
-
# Pass listDimensions and taskDivisions to _sherpa for benchmarking
|
|
54
|
-
foldingsTotal = _sherpa(track, gapsWhere, static, D, listDimensions)
|
|
55
|
-
return foldingsTotal
|
|
56
|
-
|
|
57
|
-
# @recordBenchmarks()
|
|
58
|
-
def _sherpa(track: NDArray, gap: NDArray, static: NDArray, D: NDArray, p: List[int]):
|
|
59
|
-
"""Performance critical section that counts foldings.
|
|
60
|
-
|
|
61
|
-
Parameters:
|
|
62
|
-
track: Array tracking folding state
|
|
63
|
-
gap: Array for potential gaps
|
|
64
|
-
static: Array containing static configuration values
|
|
65
|
-
D: Array of leaf connections
|
|
66
|
-
p: List of dimensions for benchmarking
|
|
67
|
-
"""
|
|
68
|
-
foldingsTotal = countFoldings(track, gap, static, D)
|
|
69
|
-
return foldingsTotal
|
|
70
|
-
|
|
71
|
-
@njit(cache=True, parallel=False, fastmath=False)
|
|
72
|
-
def countFoldings(TEMPLATEtrack: NDArray,
|
|
73
|
-
TEMPLATEgapsWhere: NDArray,
|
|
74
|
-
the: NDArray,
|
|
75
|
-
connectionGraph: NDArray
|
|
76
|
-
):
|
|
77
|
-
|
|
78
|
-
TEMPLATEmy = numpy.zeros(COUNTindicesDynamic, dtype=numpy.int64)
|
|
79
|
-
TEMPLATEmy[leaf1ndex] = 1
|
|
80
|
-
|
|
81
|
-
taskDivisions = 0
|
|
82
|
-
# taskDivisions = the[leavesTotal]
|
|
83
|
-
TEMPLATEmy[taskIndex] = taskDivisions - 1 # the first modulo is leavesTotal - 1
|
|
84
|
-
|
|
85
|
-
def prepareWork(track: NDArray,
|
|
86
|
-
gapsWhere: NDArray,
|
|
87
|
-
my: NDArray) -> tuple[NDArray, NDArray, NDArray]:
|
|
88
|
-
foldingsTotal = 0
|
|
89
|
-
while True:
|
|
90
|
-
if my[leaf1ndex] <= 1 or track[leafBelow][0] == 1:
|
|
91
|
-
if my[leaf1ndex] > the[leavesTotal]:
|
|
92
|
-
foldingsTotal += the[leavesTotal]
|
|
93
|
-
else:
|
|
94
|
-
my[unconstrainedLeaf] = 0
|
|
95
|
-
my[gap1ndexCeiling] = track[gapRangeStart][my[leaf1ndex] - 1]
|
|
96
|
-
my[gap1ndex] = my[gap1ndexCeiling]
|
|
97
|
-
|
|
98
|
-
for PREPAREdimension1ndex in range(1, the[dimensionsPlus1]):
|
|
99
|
-
if connectionGraph[PREPAREdimension1ndex][my[leaf1ndex]][my[leaf1ndex]] == my[leaf1ndex]:
|
|
100
|
-
my[unconstrainedLeaf] += 1
|
|
101
|
-
else:
|
|
102
|
-
my[leafConnectee] = connectionGraph[PREPAREdimension1ndex][my[leaf1ndex]][my[leaf1ndex]]
|
|
103
|
-
while my[leafConnectee] != my[leaf1ndex]:
|
|
104
|
-
|
|
105
|
-
if my[leafConnectee] != my[leaf1ndex]:
|
|
106
|
-
my[dimension1ndex] = PREPAREdimension1ndex
|
|
107
|
-
return track, gapsWhere, my
|
|
108
|
-
|
|
109
|
-
if my[leaf1ndex] != the[leavesTotal]:
|
|
110
|
-
gapsWhere[my[gap1ndexCeiling]] = my[leafConnectee]
|
|
111
|
-
if track[countDimensionsGapped][my[leafConnectee]] == 0:
|
|
112
|
-
my[gap1ndexCeiling] += 1
|
|
113
|
-
track[countDimensionsGapped][my[leafConnectee]] += 1
|
|
114
|
-
else:
|
|
115
|
-
print("else")
|
|
116
|
-
my[dimension1ndex] = PREPAREdimension1ndex
|
|
117
|
-
return track, gapsWhere, my
|
|
118
|
-
# PREPAREmy[leafConnectee] % the[leavesTotal] == PREPAREmy[taskIndex]
|
|
119
|
-
my[leafConnectee] = connectionGraph[dimension1ndex][my[leaf1ndex]][track[leafBelow][my[leafConnectee]]]
|
|
120
|
-
|
|
121
|
-
if my[unconstrainedLeaf] == the[dimensionsTotal]:
|
|
122
|
-
for indexLeaf in range(my[leaf1ndex]):
|
|
123
|
-
gapsWhere[my[gap1ndexCeiling]] = indexLeaf
|
|
124
|
-
my[gap1ndexCeiling] += 1
|
|
125
|
-
|
|
126
|
-
for indexMiniGap in range(my[gap1ndex], my[gap1ndexCeiling]):
|
|
127
|
-
gapsWhere[my[gap1ndex]] = gapsWhere[indexMiniGap]
|
|
128
|
-
if track[countDimensionsGapped][gapsWhere[indexMiniGap]] == the[dimensionsTotal] - my[unconstrainedLeaf]:
|
|
129
|
-
my[gap1ndex] += 1
|
|
130
|
-
track[countDimensionsGapped][gapsWhere[indexMiniGap]] = 0
|
|
131
|
-
|
|
132
|
-
while my[leaf1ndex] > 0 and my[gap1ndex] == track[gapRangeStart][my[leaf1ndex] - 1]:
|
|
133
|
-
my[leaf1ndex] -= 1
|
|
134
|
-
track[leafBelow][track[leafAbove][my[leaf1ndex]]] = track[leafBelow][my[leaf1ndex]]
|
|
135
|
-
track[leafAbove][track[leafBelow][my[leaf1ndex]]] = track[leafAbove][my[leaf1ndex]]
|
|
136
|
-
|
|
137
|
-
if my[leaf1ndex] > 0:
|
|
138
|
-
my[gap1ndex] -= 1
|
|
139
|
-
track[leafAbove][my[leaf1ndex]] = gapsWhere[my[gap1ndex]]
|
|
140
|
-
track[leafBelow][my[leaf1ndex]] = track[leafBelow][track[leafAbove][my[leaf1ndex]]]
|
|
141
|
-
track[leafBelow][track[leafAbove][my[leaf1ndex]]] = my[leaf1ndex]
|
|
142
|
-
track[leafAbove][track[leafBelow][my[leaf1ndex]]] = my[leaf1ndex]
|
|
143
|
-
track[gapRangeStart][my[leaf1ndex]] = my[gap1ndex]
|
|
144
|
-
my[leaf1ndex] += 1
|
|
145
|
-
|
|
146
|
-
RETURNtrack, RETURNgapsWhere, RETURNmy = prepareWork(TEMPLATEtrack.copy(), TEMPLATEgapsWhere.copy(), TEMPLATEmy.copy())
|
|
147
|
-
|
|
148
|
-
foldingsTotal = doWork(RETURNtrack.copy(), RETURNgapsWhere.copy(), RETURNmy.copy(), the, connectionGraph, taskDivisions)
|
|
149
|
-
|
|
150
|
-
return foldingsTotal
|
|
151
|
-
|
|
152
|
-
@njit(cache=True, parallel=False, fastmath=False)
|
|
153
|
-
def doWork(track: NDArray,
|
|
154
|
-
gapsWhere: NDArray,
|
|
155
|
-
my: NDArray,
|
|
156
|
-
the: NDArray,
|
|
157
|
-
connectionGraph: NDArray,
|
|
158
|
-
taskDivisions: int = 0
|
|
159
|
-
):
|
|
160
|
-
|
|
161
|
-
papasGotABrandNewBag = True
|
|
162
|
-
if_activeLeaf1ndex_LTE_1_or_leafBelow_index_0_equals_1 = True
|
|
163
|
-
for_dimension1ndex_in_range_1_to_dimensionsPlus1 = True
|
|
164
|
-
while_leaf1ndexConnectee_notEquals_activeLeaf1ndex = True
|
|
165
|
-
|
|
166
|
-
thisIsNotTheFirstPass = False
|
|
167
|
-
|
|
168
|
-
while papasGotABrandNewBag:
|
|
169
|
-
if my[leaf1ndex] <= 1 or track[leafBelow][0] == 1 or if_activeLeaf1ndex_LTE_1_or_leafBelow_index_0_equals_1 == True:
|
|
170
|
-
if_activeLeaf1ndex_LTE_1_or_leafBelow_index_0_equals_1 = False
|
|
171
|
-
if my[leaf1ndex] > the[leavesTotal] and thisIsNotTheFirstPass:
|
|
172
|
-
my[foldingsSubtotal] += the[leavesTotal]
|
|
173
|
-
else:
|
|
174
|
-
if thisIsNotTheFirstPass:
|
|
175
|
-
my[unconstrainedLeaf] = 0
|
|
176
|
-
my[gap1ndexCeiling] = track[gapRangeStart][my[leaf1ndex] - 1]
|
|
177
|
-
my[gap1ndex] = my[gap1ndexCeiling]
|
|
178
|
-
|
|
179
|
-
for_dimension1ndex_in_range_1_to_dimensionsPlus1 = True
|
|
180
|
-
while for_dimension1ndex_in_range_1_to_dimensionsPlus1 == True:
|
|
181
|
-
for_dimension1ndex_in_range_1_to_dimensionsPlus1 = False
|
|
182
|
-
if connectionGraph[my[dimension1ndex]][my[leaf1ndex]][my[leaf1ndex]] == my[leaf1ndex] and thisIsNotTheFirstPass:
|
|
183
|
-
my[unconstrainedLeaf] += 1
|
|
184
|
-
else:
|
|
185
|
-
if thisIsNotTheFirstPass:
|
|
186
|
-
my[leafConnectee] = connectionGraph[my[dimension1ndex]][my[leaf1ndex]][my[leaf1ndex]]
|
|
187
|
-
if my[leafConnectee] != my[leaf1ndex]:
|
|
188
|
-
while_leaf1ndexConnectee_notEquals_activeLeaf1ndex = True
|
|
189
|
-
|
|
190
|
-
while while_leaf1ndexConnectee_notEquals_activeLeaf1ndex == True:
|
|
191
|
-
while_leaf1ndexConnectee_notEquals_activeLeaf1ndex = False
|
|
192
|
-
thisIsNotTheFirstPass = True
|
|
193
|
-
if taskDivisions==0 or my[leaf1ndex] != taskDivisions:
|
|
194
|
-
myTask = True
|
|
195
|
-
else:
|
|
196
|
-
modulo = my[leafConnectee] % the[leavesTotal]
|
|
197
|
-
if modulo == my[taskIndex]: myTask = True
|
|
198
|
-
else:
|
|
199
|
-
myTask = False
|
|
200
|
-
if myTask:
|
|
201
|
-
gapsWhere[my[gap1ndexCeiling]] = my[leafConnectee]
|
|
202
|
-
if track[countDimensionsGapped][my[leafConnectee]] == 0:
|
|
203
|
-
my[gap1ndexCeiling] += 1
|
|
204
|
-
track[countDimensionsGapped][my[leafConnectee]] += 1
|
|
205
|
-
my[leafConnectee] = connectionGraph[my[dimension1ndex]][my[leaf1ndex]][track[leafBelow][my[leafConnectee]]]
|
|
206
|
-
if my[leafConnectee] != my[leaf1ndex]:
|
|
207
|
-
while_leaf1ndexConnectee_notEquals_activeLeaf1ndex = True
|
|
208
|
-
my[dimension1ndex] += 1
|
|
209
|
-
if my[dimension1ndex] < the[dimensionsPlus1]:
|
|
210
|
-
for_dimension1ndex_in_range_1_to_dimensionsPlus1 = True
|
|
211
|
-
else:
|
|
212
|
-
my[dimension1ndex] = 1
|
|
213
|
-
|
|
214
|
-
if my[unconstrainedLeaf] == the[dimensionsTotal]:
|
|
215
|
-
for leaf1ndex in range(my[leaf1ndex]):
|
|
216
|
-
gapsWhere[my[gap1ndexCeiling]] = leaf1ndex
|
|
217
|
-
my[gap1ndexCeiling] += 1
|
|
218
|
-
|
|
219
|
-
for indexMiniGap in range(my[gap1ndex], my[gap1ndexCeiling]):
|
|
220
|
-
gapsWhere[my[gap1ndex]] = gapsWhere[indexMiniGap]
|
|
221
|
-
if track[countDimensionsGapped][gapsWhere[indexMiniGap]] == the[dimensionsTotal] - my[unconstrainedLeaf]:
|
|
222
|
-
my[gap1ndex] += 1
|
|
223
|
-
track[countDimensionsGapped][gapsWhere[indexMiniGap]] = 0
|
|
224
|
-
|
|
225
|
-
while my[leaf1ndex] > 0 and my[gap1ndex] == track[gapRangeStart][my[leaf1ndex] - 1]:
|
|
226
|
-
my[leaf1ndex] -= 1
|
|
227
|
-
track[leafBelow][track[leafAbove][my[leaf1ndex]]] = track[leafBelow][my[leaf1ndex]]
|
|
228
|
-
track[leafAbove][track[leafBelow][my[leaf1ndex]]] = track[leafAbove][my[leaf1ndex]]
|
|
229
|
-
|
|
230
|
-
if my[leaf1ndex] > 0:
|
|
231
|
-
my[gap1ndex] -= 1
|
|
232
|
-
track[leafAbove][my[leaf1ndex]] = gapsWhere[my[gap1ndex]]
|
|
233
|
-
track[leafBelow][my[leaf1ndex]] = track[leafBelow][track[leafAbove][my[leaf1ndex]]]
|
|
234
|
-
track[leafBelow][track[leafAbove][my[leaf1ndex]]] = my[leaf1ndex]
|
|
235
|
-
track[leafAbove][track[leafBelow][my[leaf1ndex]]] = my[leaf1ndex]
|
|
236
|
-
track[gapRangeStart][my[leaf1ndex]] = my[gap1ndex]
|
|
237
|
-
my[leaf1ndex] += 1
|
|
238
|
-
|
|
239
|
-
if my[leaf1ndex] <= 0:
|
|
240
|
-
return my[foldingsSubtotal]
|
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
from numba import njit
|
|
2
|
-
import numpy
|
|
3
|
-
|
|
4
|
-
@njit(cache=True)
|
|
5
|
-
def foldings_plus_1(p: list[int], computationDivisions: int = 0, computationIndex: int = 0) -> int:
|
|
6
|
-
n: int = 1 # Total number of leaves
|
|
7
|
-
for dimension in p:
|
|
8
|
-
n *= dimension
|
|
9
|
-
|
|
10
|
-
d = len(p) # Number of dimensions
|
|
11
|
-
# Compute arrays P, C, D as per the algorithm
|
|
12
|
-
P = numpy.ones(d + 1, dtype=numpy.int64)
|
|
13
|
-
for i in range(1, d + 1):
|
|
14
|
-
P[i] = P[i - 1] * p[i - 1]
|
|
15
|
-
|
|
16
|
-
# C[i][m] holds the i-th coordinate of leaf m
|
|
17
|
-
C = numpy.zeros((d + 1, n + 1), dtype=numpy.int64)
|
|
18
|
-
for i in range(1, d + 1):
|
|
19
|
-
for m in range(1, n + 1):
|
|
20
|
-
C[i][m] = ((m - 1) // P[i - 1]) - ((m - 1) // P[i]) * p[i - 1] + 1
|
|
21
|
-
|
|
22
|
-
# D[i][l][m] computes the leaf connected to m in section i when inserting l
|
|
23
|
-
D = numpy.zeros((d + 1, n + 1, n + 1), dtype=numpy.int64)
|
|
24
|
-
for i in range(1, d + 1):
|
|
25
|
-
for l in range(1, n + 1):
|
|
26
|
-
for m in range(1, l + 1):
|
|
27
|
-
delta = C[i][l] - C[i][m]
|
|
28
|
-
if delta % 2 == 0:
|
|
29
|
-
# If delta is even
|
|
30
|
-
if C[i][m] == 1:
|
|
31
|
-
D[i][l][m] = m
|
|
32
|
-
else:
|
|
33
|
-
D[i][l][m] = m - P[i - 1]
|
|
34
|
-
else:
|
|
35
|
-
# If delta is odd
|
|
36
|
-
if C[i][m] == p[i - 1] or m + P[i - 1] > l:
|
|
37
|
-
D[i][l][m] = m
|
|
38
|
-
else:
|
|
39
|
-
D[i][l][m] = m + P[i - 1]
|
|
40
|
-
# Initialize arrays/lists
|
|
41
|
-
A = numpy.zeros(n + 1, dtype=numpy.int64) # Leaf above leaf m
|
|
42
|
-
B = numpy.zeros(n + 1, dtype=numpy.int64) # Leaf below leaf m
|
|
43
|
-
count = numpy.zeros(n + 1, dtype=numpy.int64) # Counts for potential gaps
|
|
44
|
-
gapter = numpy.zeros(n + 1, dtype=numpy.int64) # Indices for gap stack per leaf
|
|
45
|
-
gap = numpy.zeros(n * n + 1, dtype=numpy.int64) # Stack of potential gaps
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
# Initialize variables for backtracking
|
|
49
|
-
total_count = 0 # Total number of foldings
|
|
50
|
-
g = 0 # Gap index
|
|
51
|
-
l = 1 # Current leaf
|
|
52
|
-
|
|
53
|
-
# Start backtracking loop
|
|
54
|
-
while l > 0:
|
|
55
|
-
# If we have processed all leaves, increment total count
|
|
56
|
-
if l > n:
|
|
57
|
-
total_count += 1
|
|
58
|
-
else:
|
|
59
|
-
dd = 0 # Number of sections where leaf l is unconstrained
|
|
60
|
-
gg = g # Temporary gap index
|
|
61
|
-
g = gapter[l - 1] # Reset gap index for current leaf
|
|
62
|
-
|
|
63
|
-
# Count possible gaps for leaf l in each section
|
|
64
|
-
for i in range(1, d + 1):
|
|
65
|
-
if D[i][l][l] == l:
|
|
66
|
-
dd += 1
|
|
67
|
-
else:
|
|
68
|
-
m = D[i][l][l]
|
|
69
|
-
while m != l:
|
|
70
|
-
if computationDivisions == 0 or l != computationDivisions or m % computationDivisions == computationIndex:
|
|
71
|
-
gap[gg] = m
|
|
72
|
-
if count[m] == 0:
|
|
73
|
-
gg += 1
|
|
74
|
-
count[m] += 1
|
|
75
|
-
m = D[i][l][B[m]]
|
|
76
|
-
|
|
77
|
-
# If leaf l is unconstrained in all sections, it can be inserted anywhere
|
|
78
|
-
if dd == d:
|
|
79
|
-
for m in range(l):
|
|
80
|
-
gap[gg] = m
|
|
81
|
-
gg += 1
|
|
82
|
-
|
|
83
|
-
# Filter gaps that are common to all sections
|
|
84
|
-
for j in range(g, gg):
|
|
85
|
-
gap[g] = gap[j]
|
|
86
|
-
if count[gap[j]] == d - dd:
|
|
87
|
-
g += 1
|
|
88
|
-
count[gap[j]] = 0 # Reset count for next iteration
|
|
89
|
-
|
|
90
|
-
# Recursive backtracking steps
|
|
91
|
-
while l > 0 and g == gapter[l - 1]:
|
|
92
|
-
l -= 1
|
|
93
|
-
B[A[l]] = B[l]
|
|
94
|
-
A[B[l]] = A[l]
|
|
95
|
-
|
|
96
|
-
if l > 0:
|
|
97
|
-
g -= 1
|
|
98
|
-
A[l] = gap[g]
|
|
99
|
-
B[l] = B[A[l]]
|
|
100
|
-
B[A[l]] = l
|
|
101
|
-
A[B[l]] = l
|
|
102
|
-
gapter[l] = g # Save current gap index
|
|
103
|
-
l += 1 # Move to next leaf
|
|
104
|
-
|
|
105
|
-
return total_count
|
|
106
|
-
|
|
107
|
-
@njit(cache=True)
|
|
108
|
-
def foldings(p: list[int], computationDivisions: int = 0, computationIndex: int = 0) -> int:
|
|
109
|
-
n: int = 1 # Total number of leaves
|
|
110
|
-
for dimension in p:
|
|
111
|
-
n *= dimension
|
|
112
|
-
|
|
113
|
-
d = len(p) # Number of dimensions
|
|
114
|
-
# Compute arrays P, C, D as per the algorithm
|
|
115
|
-
P = numpy.ones(d + 1, dtype=numpy.int64)
|
|
116
|
-
for i in range(1, d + 1):
|
|
117
|
-
P[i] = P[i - 1] * p[i - 1]
|
|
118
|
-
|
|
119
|
-
# C[i][m] holds the i-th coordinate of leaf m
|
|
120
|
-
C = numpy.zeros((d + 1, n + 1), dtype=numpy.int64)
|
|
121
|
-
for i in range(1, d + 1):
|
|
122
|
-
for m in range(1, n + 1):
|
|
123
|
-
C[i][m] = ((m - 1) // P[i - 1]) - ((m - 1) // P[i]) * p[i - 1] + 1
|
|
124
|
-
# C[i][m] = ((m - 1) // P[i - 1]) % p[i - 1] + 1 # NOTE different, but either one works
|
|
125
|
-
|
|
126
|
-
# D[i][l][m] computes the leaf connected to m in section i when inserting l
|
|
127
|
-
D = numpy.zeros((d + 1, n + 1, n + 1), dtype=numpy.int64)
|
|
128
|
-
for i in range(1, d + 1):
|
|
129
|
-
for l in range(1, n + 1):
|
|
130
|
-
for m in range(1, l + 1):
|
|
131
|
-
delta = C[i][l] - C[i][m]
|
|
132
|
-
if delta % 2 == 0:
|
|
133
|
-
# If delta is even
|
|
134
|
-
if C[i][m] == 1:
|
|
135
|
-
D[i][l][m] = m
|
|
136
|
-
else:
|
|
137
|
-
D[i][l][m] = m - P[i - 1]
|
|
138
|
-
else:
|
|
139
|
-
# If delta is odd
|
|
140
|
-
if C[i][m] == p[i - 1] or m + P[i - 1] > l:
|
|
141
|
-
D[i][l][m] = m
|
|
142
|
-
else:
|
|
143
|
-
D[i][l][m] = m + P[i - 1]
|
|
144
|
-
# Initialize arrays/lists
|
|
145
|
-
A = numpy.zeros(n + 1, dtype=numpy.int64) # Leaf above leaf m
|
|
146
|
-
B = numpy.zeros(n + 1, dtype=numpy.int64) # Leaf below leaf m
|
|
147
|
-
count = numpy.zeros(n + 1, dtype=numpy.int64) # Counts for potential gaps
|
|
148
|
-
gapter = numpy.zeros(n + 1, dtype=numpy.int64) # Indices for gap stack per leaf
|
|
149
|
-
gap = numpy.zeros(n * n + 1, dtype=numpy.int64) # Stack of potential gaps
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
# Initialize variables for backtracking
|
|
153
|
-
total_count = 0 # Total number of foldings
|
|
154
|
-
g = 0 # Gap index
|
|
155
|
-
l = 1 # Current leaf
|
|
156
|
-
|
|
157
|
-
# Start backtracking loop
|
|
158
|
-
while l > 0:
|
|
159
|
-
if l <= 1 or B[0] == 1: # NOTE different
|
|
160
|
-
# NOTE the above `if` statement encloses the the if/else block below
|
|
161
|
-
# NOTE these changes increase the throughput by more than an order of magnitude
|
|
162
|
-
if l > n:
|
|
163
|
-
total_count += n
|
|
164
|
-
else:
|
|
165
|
-
dd = 0 # Number of sections where leaf l is unconstrained
|
|
166
|
-
gg = gapter[l - 1] # Track possible gaps # NOTE different, but not important
|
|
167
|
-
g = gg # NOTE different, but not important
|
|
168
|
-
|
|
169
|
-
# Count possible gaps for leaf l in each section
|
|
170
|
-
for i in range(1, d + 1):
|
|
171
|
-
if D[i][l][l] == l:
|
|
172
|
-
dd += 1
|
|
173
|
-
else:
|
|
174
|
-
m = D[i][l][l]
|
|
175
|
-
while m != l:
|
|
176
|
-
if computationDivisions == 0 or l != computationDivisions or m % computationDivisions == computationIndex:
|
|
177
|
-
gap[gg] = m
|
|
178
|
-
if count[m] == 0:
|
|
179
|
-
gg += 1
|
|
180
|
-
count[m] += 1
|
|
181
|
-
m = D[i][l][B[m]]
|
|
182
|
-
|
|
183
|
-
# If leaf l is unconstrained in all sections, it can be inserted anywhere
|
|
184
|
-
if dd == d:
|
|
185
|
-
for m in range(l):
|
|
186
|
-
gap[gg] = m
|
|
187
|
-
gg += 1
|
|
188
|
-
|
|
189
|
-
# Filter gaps that are common to all sections
|
|
190
|
-
for j in range(g, gg):
|
|
191
|
-
gap[g] = gap[j]
|
|
192
|
-
if count[gap[j]] == d - dd:
|
|
193
|
-
g += 1
|
|
194
|
-
count[gap[j]] = 0 # Reset count for next iteration
|
|
195
|
-
|
|
196
|
-
# Recursive backtracking steps
|
|
197
|
-
while l > 0 and g == gapter[l - 1]:
|
|
198
|
-
l -= 1
|
|
199
|
-
B[A[l]] = B[l]
|
|
200
|
-
A[B[l]] = A[l]
|
|
201
|
-
|
|
202
|
-
if l > 0:
|
|
203
|
-
g -= 1
|
|
204
|
-
A[l] = gap[g]
|
|
205
|
-
B[l] = B[A[l]]
|
|
206
|
-
B[A[l]] = l
|
|
207
|
-
A[B[l]] = l
|
|
208
|
-
gapter[l] = g # Save current gap index
|
|
209
|
-
l += 1 # Move to next leaf
|
|
210
|
-
|
|
211
|
-
return total_count
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
from mapFolding.someAssemblyRequired.getLLVMforNoReason import writeModuleLLVM
|
|
2
|
-
from mapFolding.someAssemblyRequired.makeJob import makeStateJob
|
|
3
|
-
from mapFolding.someAssemblyRequired.synthesizeNumbaGeneralized import youOughtaKnow
|
|
4
|
-
from mapFolding.someAssemblyRequired.synthesizeNumbaJob import writeJobNumba
|
|
5
|
-
from mapFolding.someAssemblyRequired.synthesizeNumbaModules import makeFlowNumbaOptimized
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import importlib
|
|
2
|
-
import importlib.util
|
|
3
|
-
import llvmlite.binding
|
|
4
|
-
import pathlib
|
|
5
|
-
|
|
6
|
-
def writeModuleLLVM(pathFilename: pathlib.Path, identifierCallable: str) -> pathlib.Path:
|
|
7
|
-
"""Import the generated module directly and get its LLVM IR."""
|
|
8
|
-
specTarget = importlib.util.spec_from_file_location("generatedModule", pathFilename)
|
|
9
|
-
if specTarget is None or specTarget.loader is None:
|
|
10
|
-
raise ImportError(f"Could not create module spec or loader for {pathFilename}")
|
|
11
|
-
moduleTarget = importlib.util.module_from_spec(specTarget)
|
|
12
|
-
specTarget.loader.exec_module(moduleTarget)
|
|
13
|
-
|
|
14
|
-
# Get LLVM IR and write to file
|
|
15
|
-
linesLLVM = moduleTarget.__dict__[identifierCallable].inspect_llvm()[()]
|
|
16
|
-
moduleLLVM = llvmlite.binding.module.parse_assembly(linesLLVM)
|
|
17
|
-
pathFilenameLLVM = pathFilename.with_suffix(".ll")
|
|
18
|
-
pathFilenameLLVM.write_text(str(moduleLLVM))
|
|
19
|
-
return pathFilenameLLVM
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
from collections.abc import Sequence
|
|
2
|
-
from mapFolding import getPathFilenameFoldsTotal, computationState, outfitCountFolds, getAlgorithmSource
|
|
3
|
-
from pathlib import Path
|
|
4
|
-
from types import ModuleType
|
|
5
|
-
from typing import Any, Literal, overload
|
|
6
|
-
import pickle
|
|
7
|
-
|
|
8
|
-
@overload
|
|
9
|
-
def makeStateJob(listDimensions: Sequence[int], *, writeJob: Literal[True] , **keywordArguments: str | None) -> Path: ...
|
|
10
|
-
@overload
|
|
11
|
-
def makeStateJob(listDimensions: Sequence[int], *, writeJob: Literal[False] , **keywordArguments: str | None) -> computationState: ...
|
|
12
|
-
def makeStateJob(listDimensions: Sequence[int], *, writeJob: bool = True, **keywordArguments: Any | None) -> computationState | Path:
|
|
13
|
-
"""
|
|
14
|
-
Creates a computation state job for map folding calculations and optionally saves it to disk.
|
|
15
|
-
|
|
16
|
-
This function initializes a computation state for map folding calculations based on the given dimensions,
|
|
17
|
-
sets up the initial counting configuration, and can optionally save the state to a pickle file.
|
|
18
|
-
|
|
19
|
-
Parameters
|
|
20
|
-
----------
|
|
21
|
-
listDimensions : Sequence[int]
|
|
22
|
-
The dimensions of the map to be folded, typically as [height, width].
|
|
23
|
-
writeJob : bool, optional
|
|
24
|
-
If True, saves the computation state to disk. If False, returns the state object directly.
|
|
25
|
-
Default is True.
|
|
26
|
-
**keywordArguments : Optional[str]
|
|
27
|
-
Additional keyword arguments to be passed to the outfitCountFolds function.
|
|
28
|
-
|
|
29
|
-
Returns
|
|
30
|
-
-------
|
|
31
|
-
Union[computationState, Path]
|
|
32
|
-
If writeJob is False, returns the computation state object.
|
|
33
|
-
If writeJob is True, returns the Path object pointing to the saved state file.
|
|
34
|
-
|
|
35
|
-
Notes
|
|
36
|
-
-----
|
|
37
|
-
The function creates necessary directories and saves the state as a pickle file
|
|
38
|
-
when writeJob is True. The file is saved in a directory structure based on the map shape.
|
|
39
|
-
"""
|
|
40
|
-
|
|
41
|
-
stateUniversal: computationState = outfitCountFolds(listDimensions, **keywordArguments)
|
|
42
|
-
|
|
43
|
-
moduleSource: ModuleType = getAlgorithmSource()
|
|
44
|
-
moduleSource.countInitialize(stateUniversal['connectionGraph'], stateUniversal['gapsWhere'], stateUniversal['my'], stateUniversal['track'])
|
|
45
|
-
|
|
46
|
-
if not writeJob:
|
|
47
|
-
return stateUniversal
|
|
48
|
-
|
|
49
|
-
pathFilenameChopChop = getPathFilenameFoldsTotal(stateUniversal['mapShape'])
|
|
50
|
-
suffix = pathFilenameChopChop.suffix
|
|
51
|
-
pathJob = Path(str(pathFilenameChopChop)[0:-len(suffix)])
|
|
52
|
-
pathJob.mkdir(parents=True, exist_ok=True)
|
|
53
|
-
pathFilenameJob = pathJob / 'stateJob.pkl'
|
|
54
|
-
|
|
55
|
-
pathFilenameJob.write_bytes(pickle.dumps(stateUniversal))
|
|
56
|
-
return pathFilenameJob
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
from mapFolding import getAlgorithmSource, getPathSyntheticModules
|
|
2
|
-
from mapFolding import setDatatypeModule, setDatatypeFoldsTotal, setDatatypeElephino, setDatatypeLeavesTotal
|
|
3
|
-
import ast
|
|
4
|
-
import inspect
|
|
5
|
-
import pathlib
|
|
6
|
-
|
|
7
|
-
def transformPythonToJAX(codePython: str) -> None:
|
|
8
|
-
astPython = ast.parse(codePython)
|
|
9
|
-
|
|
10
|
-
def writeJax(*, codeSource: str | None = None, pathFilenameAlgorithm: pathlib.Path | None = None, pathFilenameDestination: pathlib.Path | None = None) -> None:
|
|
11
|
-
if codeSource is None and pathFilenameAlgorithm is None:
|
|
12
|
-
algorithmSource = getAlgorithmSource()
|
|
13
|
-
codeSource = inspect.getsource(algorithmSource)
|
|
14
|
-
transformedText = transformPythonToJAX(codeSource)
|
|
15
|
-
pathFilenameAlgorithm = pathlib.Path(inspect.getfile(algorithmSource))
|
|
16
|
-
else:
|
|
17
|
-
raise NotImplementedError("You haven't written this part yet.")
|
|
18
|
-
if pathFilenameDestination is None:
|
|
19
|
-
pathFilenameDestination = getPathSyntheticModules() / "countJax.py"
|
|
20
|
-
# pathFilenameDestination.write_text(transformedText)
|
|
21
|
-
|
|
22
|
-
if __name__ == '__main__':
|
|
23
|
-
setDatatypeModule('jax.numpy', sourGrapes=True)
|
|
24
|
-
setDatatypeFoldsTotal('int64', sourGrapes=True)
|
|
25
|
-
setDatatypeElephino('uint8', sourGrapes=True)
|
|
26
|
-
setDatatypeLeavesTotal('uint8', sourGrapes=True)
|
|
27
|
-
writeJax()
|