mapFolding 0.2.0__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.
@@ -0,0 +1,153 @@
1
+ """
2
+ An unnecessarily literal translation of the original Atlas Autocode code by W. F. Lunnon to Python.
3
+ W. F. Lunnon, Multi-dimensional map-folding, The Computer Journal, Volume 14, Issue 1, 1971, Pages 75-80, https://doi.org/10.1093/comjnl/14.1.75
4
+ """# NOTE not functional yet
5
+ def foldings(p, job=None):
6
+ """An unnecessarily literal translation of the original Atlas Autocode code."""
7
+ p = list(p)
8
+ p.append(None) # NOTE mimics Atlas `array` type
9
+ p.insert(0, None) # NOTE mimics Atlas `array` type
10
+
11
+ if job is None:
12
+ global f
13
+ f = 0
14
+ def job(A, B):
15
+ global f
16
+ f = f + 1
17
+ return foldings(p, job)
18
+ # perform job (A, B) on each folding of a p[1] x ... x p[d] map,
19
+ # where A and B are the above and below vectors. p[d + 1] < 0 terminates p;
20
+
21
+ d: int
22
+ n: int
23
+ j: int
24
+ i: int
25
+ m: int
26
+ l: int
27
+ g: int
28
+ gg: int
29
+ dd: int
30
+
31
+ n = 1
32
+ i, d = 0, 0
33
+
34
+ while (i := i + 1) and p[i] is not None:
35
+ d = i
36
+ n = n * p[i]
37
+
38
+ # d dimensions and n leaves;
39
+
40
+ # A: list[int] = [None] * (n + 1) # type: ignore
41
+ # B: list[int] = [None] * (n + 1) # type: ignore
42
+ # count: list[int] = [None] * (n + 1) # type: ignore
43
+ # gapter: list[int] = [None] * (n + 1) # type: ignore
44
+ # gap: list[int] = [None] * (n * n + 1) # type: ignore
45
+ A: list[int] = [0] * (n + 1) # type: ignore
46
+ B: list[int] = [0] * (n + 1) # type: ignore
47
+ count: list[int] = [0] * (n + 1) # type: ignore
48
+ gapter: list[int] = [0] * (n + 1) # type: ignore
49
+ gap: list[int] = [0] * (n * n + 1) # type: ignore
50
+
51
+ # B[m] is the leaf below leaf m in the current folding,
52
+ # A[m] the leaf above. count[m] is the no. of sections in which
53
+ # there is a gap for the new leaf l below leaf m,
54
+ # gap[gapter[l - 1] + j] is the j-th (possible or actual) gap for leaf l,
55
+ # and later gap[gapter[l]] is the gap where leaf l is currently inserted;
56
+
57
+ P: list[int] = [0] * (d + 1) # type: ignore
58
+ C: list[list[int]] = [[0] * (n + 1) for dimension1 in range(d + 1)] # type: ignore
59
+ # D: list[list[list[int]]] = [[[None] * (n + 1) for dimension2 in range(n + 1)] for dimension1 in range(d + 1)] # type: ignore
60
+ D: list[list[list[int]]] = [[[0] * (n + 1) for dimension2 in range(n + 1)] for dimension1 in range(d + 1)]
61
+
62
+ P[0] = 1
63
+ for i in range(1, d + 1):
64
+ P[i] = P[i - 1] * p[i]
65
+
66
+ for i in range(1, d + 1):
67
+ for m in range(1, n + 1):
68
+ C[i][m] = ((m - 1) // P[i - 1]) - ((m - 1) // P[i]) * p[i] + 1
69
+
70
+ for i in range(1, d + 1):
71
+ for l in range(1, n + 1):
72
+ for m in range(1, l + 1):
73
+ D[i][l][m] = (0 if m == 0
74
+ else
75
+ ((m if C[i][m] == 1
76
+ else m - P[i - 1])
77
+ if C[i][l] - C[i][m] == (C[i][l] - C[i][m]) // 2 * 2
78
+ else
79
+ (m if C[i][m] == p[i] or m + P[i - 1] > l
80
+ else m + P[i - 1])))
81
+ # P[i] = p[1] x ... x p[i], C[i][m] = i-th co-ordinate of leaf m,
82
+ # D[i][l][m] = leaf connected to m in section i when inserting l;
83
+
84
+ for m in range(n + 1):
85
+ count[m] = 0
86
+
87
+ A[0], B[0], g, l = 0, 0, 0, 0
88
+
89
+ state = 'entry'
90
+ while True:
91
+ if state == 'entry':
92
+ gapter[l] = g
93
+ l = l + 1
94
+ if l <= n:
95
+ state = 'down'
96
+ continue
97
+ else:
98
+ job(A, B)
99
+ state = 'up'
100
+ continue
101
+
102
+ elif state == 'down':
103
+ dd = 0
104
+ gg = gapter[l - 1]
105
+ g = gg
106
+ for i in range(1, d + 1):
107
+ if D[i][l][l] == l:
108
+ dd = dd + 1
109
+ else:
110
+ m = D[i][l][l]
111
+ while m != l:
112
+ gap[gg] = m
113
+ if count[m] == 0:
114
+ gg = gg + 1
115
+ count[m] = count[m] + 1
116
+ m = D[i][l][B[m]]
117
+
118
+ if dd == d:
119
+ for m in range(l):
120
+ gap[gg] = m
121
+ gg = gg + 1
122
+
123
+ for j in range(g, gg):
124
+ gap[g] = gap[j]
125
+ if count[gap[j]] == d - dd:
126
+ g = g + 1
127
+ count[gap[j]] = 0
128
+ state = 'along'
129
+ continue
130
+
131
+ elif state == 'along':
132
+ if g == gapter[l - 1]:
133
+ state = 'up'
134
+ continue
135
+ g = g - 1
136
+ A[l] = gap[g]
137
+ B[l] = B[A[l]]
138
+ B[A[l]] = l
139
+ A[B[l]] = l
140
+ state = 'entry'
141
+ continue
142
+
143
+ elif state == 'up':
144
+ l = l - 1
145
+ B[A[l]] = B[l]
146
+ A[B[l]] = A[l]
147
+ if l > 0:
148
+ state = 'along'
149
+ continue
150
+ else:
151
+ break
152
+
153
+ return f #if job.__closure__ else None
@@ -0,0 +1,123 @@
1
+ """
2
+ A generally faithful translation of the original Atlas Autocode code by W. F. Lunnon to Python using NumPy.
3
+ W. F. Lunnon, Multi-dimensional map-folding, The Computer Journal, Volume 14, Issue 1, 1971, Pages 75-80, https://doi.org/10.1093/comjnl/14.1.75
4
+ """
5
+ from typing import List
6
+ import numpy
7
+
8
+ def foldings(p: List[int]) -> int:
9
+ """
10
+ Run loop with (A, B) on each folding of a p[1] x ... x p[d] map, where A and B are the above and below vectors.
11
+
12
+ Parameters:
13
+ p: A list of integers representing the dimensions of the map.
14
+
15
+ Returns:
16
+ f: The number of distinct foldings for the given map dimensions.
17
+
18
+ NOTE If there are fewer than two dimensions, any dimensions are not positive, or any dimensions are not integers, the output will be unreliable.
19
+ """
20
+
21
+ g: int = 0
22
+ d: int = len(p)
23
+ n: int = 1
24
+ for i in range(d):
25
+ n = n * p[i]
26
+
27
+ # d dimensions and n leaves
28
+
29
+ A = numpy.zeros(n + 1, dtype=int)
30
+ B = numpy.zeros(n + 1, dtype=int)
31
+ count = numpy.zeros(n + 1, dtype=int)
32
+ gapter = numpy.zeros(n + 1, dtype=int)
33
+ gap = numpy.zeros(n * n + 1, dtype=int)
34
+
35
+ # B[m] is the leaf below leaf m in the current folding,
36
+ # A[m] the leaf above. count[m] is the no. of sections in which
37
+ # there is a gap for the new leaf l below leaf m,
38
+ # gap[gapter[l - 1] + j] is the j-th (possible or actual) gap for leaf l,
39
+ # and later gap[gapter[l]] is the gap where leaf l is currently inserted
40
+
41
+ P = numpy.ones(d + 1, dtype=int)
42
+ C = numpy.zeros((d + 1, n + 1), dtype=int)
43
+ D = numpy.zeros((d + 1, n + 1, n + 1), dtype=int)
44
+
45
+ for i in range(1, d + 1):
46
+ P[i] = P[i - 1] * p[i - 1]
47
+
48
+ for i in range(1, d + 1):
49
+ for m in range(1, n + 1):
50
+ C[i][m] = ((m - 1) // P[i - 1]) % p[i - 1] + 1 # NOTE Because modulo is available, this statement is simpler.
51
+
52
+ for i in range(1, d + 1):
53
+ for l in range(1, n + 1):
54
+ for m in range(1, l + 1):
55
+ if C[i][l] - C[i][m] == (C[i][l] - C[i][m]) // 2 * 2:
56
+ if C[i][m] == 1:
57
+ D[i][l][m] = m
58
+ else:
59
+ D[i][l][m] = m - P[i - 1]
60
+ else:
61
+ if C[i][m] == p[i - 1] or m + P[i - 1] > l:
62
+ D[i][l][m] = m
63
+ else:
64
+ D[i][l][m] = m + P[i - 1]
65
+ # P[i] = p[1] x ... x p[i], C[i][m] = i-th co-ordinate of leaf m,
66
+ # D[i][l][m] = leaf connected to m in section i when inserting l;
67
+
68
+ f: int = 0
69
+ l: int = 1
70
+
71
+ # kick off with null folding
72
+ while l > 0:
73
+ if l <= 1 or B[0] == 1: # NOTE This statement is part of a significant divergence from the 1971 paper. As a result, this version is greater than one order of magnitude faster.
74
+ if l > n:
75
+ f = f + n # NOTE Due to `B[0] == 1`, this implementation increments the counted foldings in batches of `n`-many foldings, rather than immediately incrementing when a folding is found, i.e. `f = f + 1`
76
+ else:
77
+ dd: int = 0
78
+ gg: int = gapter[l - 1]
79
+ g = gg
80
+ # dd is the no. of sections in which l is unconstrained,
81
+ # gg the no. of possible and g the no. of actual gaps for l, + gapter[l - 1]
82
+
83
+ # find the possible gaps for leaf l in each section,
84
+ # then discard those not common to all. All possible if dd = d
85
+ for i in range(1, d + 1):
86
+ if D[i][l][l] == l:
87
+ dd = dd + 1
88
+ else:
89
+ m: int = D[i][l][l]
90
+ while m != l:
91
+ gap[gg] = m
92
+ if count[m] == 0:
93
+ gg = gg + 1
94
+ count[m] += 1
95
+ m = D[i][l][B[m]]
96
+
97
+ if dd == d:
98
+ for m in range(l):
99
+ gap[gg] = m
100
+ gg = gg + 1
101
+
102
+ for j in range(g, gg):
103
+ gap[g] = gap[j]
104
+ if count[gap[j]] == d - dd:
105
+ g = g + 1
106
+ count[gap[j]] = 0
107
+
108
+ # for each gap insert leaf l, [the main while loop shall progress],
109
+ # remove leaf l
110
+ while l > 0 and g == gapter[l - 1]:
111
+ l = l - 1
112
+ B[A[l]] = B[l]
113
+ A[B[l]] = A[l]
114
+
115
+ if l > 0:
116
+ g = g - 1
117
+ A[l] = gap[g]
118
+ B[l] = B[A[l]]
119
+ B[A[l]] = l
120
+ A[B[l]] = l
121
+ gapter[l] = g
122
+ l = l + 1
123
+ return f
@@ -0,0 +1,121 @@
1
+ """
2
+ A largely faithful translation of the original Atlas Autocode code by W. F. Lunnon to Python using `while`.
3
+ W. F. Lunnon, Multi-dimensional map-folding, The Computer Journal, Volume 14, Issue 1, 1971, Pages 75-80, https://doi.org/10.1093/comjnl/14.1.75
4
+ """
5
+ from typing import Sequence
6
+
7
+ def foldings(p: Sequence[int]) -> int:
8
+ """
9
+ Run loop with (A, B) on each folding of a p[1] x ... x p[d] map, where A and B are the above and below vectors.
10
+
11
+ Parameters:
12
+ p: An array of integers representing the dimensions of the map.
13
+
14
+ Returns:
15
+ f: The number of distinct foldings for the given map dimensions.
16
+
17
+ NOTE If there are fewer than two dimensions, any dimensions are not positive, or any dimensions are not integers, the output will be unreliable.
18
+ """
19
+
20
+ g: int = 0
21
+ d: int = len(p)
22
+ n: int = 1
23
+ for i in range(d):
24
+ n = n * p[i]
25
+
26
+ # d dimensions and n leaves
27
+
28
+ A = [0] * (n + 1)
29
+ B = [0] * (n + 1)
30
+ count = [0] * (n + 1)
31
+ gapter = [0] * (n + 1)
32
+ gap = [0] * (n * n + 1)
33
+
34
+ # B[m] is the leaf below leaf m in the current folding,
35
+ # A[m] the leaf above. count[m] is the no. of sections in which
36
+ # there is a gap for the new leaf l below leaf m,
37
+ # gap[gapter[l - 1] + j] is the j-th (possible or actual) gap for leaf l,
38
+ # and later gap[gapter[l]] is the gap where leaf l is currently inserted
39
+
40
+ P = [1] * (d + 1)
41
+ C = [[0] * (n + 1) for dimension1 in range(d + 1)]
42
+ D = [[[0] * (n + 1) for dimension2 in range(n + 1)] for dimension1 in range(d + 1)]
43
+
44
+ for i in range(1, d + 1):
45
+ P[i] = P[i - 1] * p[i - 1]
46
+
47
+ for i in range(1, d + 1):
48
+ for m in range(1, n + 1):
49
+ C[i][m] = ((m - 1) // P[i - 1]) - ((m - 1) // P[i]) * p[i - 1] + 1
50
+
51
+ for i in range(1, d + 1):
52
+ for l in range(1, n + 1):
53
+ for m in range(1, l + 1):
54
+ if C[i][l] - C[i][m] == (C[i][l] - C[i][m]) // 2 * 2: # !
55
+ if C[i][m] == 1:
56
+ D[i][l][m] = m
57
+ else:
58
+ D[i][l][m] = m - P[i - 1]
59
+ else:
60
+ if C[i][m] == p[i - 1] or m + P[i - 1] > l:
61
+ D[i][l][m] = m
62
+ else:
63
+ D[i][l][m] = m + P[i - 1]
64
+ # P[i] = p[1] x ... x p[i], C[i][m] = i-th co-ordinate of leaf m,
65
+ # D[i][l][m] = leaf connected to m in section i when inserting l;
66
+
67
+ f: int = 0
68
+ l: int = 1
69
+
70
+ # kick off with null folding
71
+ while l > 0:
72
+ if l > n:
73
+ f = f + 1
74
+ else:
75
+ dd: int = 0
76
+ gg: int = gapter[l - 1]
77
+ g = gg
78
+ # dd is the no. of sections in which l is unconstrained,
79
+ # gg the no. of possible and g the no. of actual gaps for l, + gapter[l - 1]
80
+
81
+ # find the possible gaps for leaf l in each section,
82
+ # then discard those not common to all. All possible if dd = d
83
+ for i in range(1, d + 1):
84
+ if D[i][l][l] == l:
85
+ dd = dd + 1
86
+ else:
87
+ m: int = D[i][l][l]
88
+ while m != l:
89
+ gap[gg] = m
90
+ if count[m] == 0:
91
+ gg = gg + 1
92
+ count[m] += 1
93
+ m = D[i][l][B[m]]
94
+
95
+ if dd == d:
96
+ for m in range(l):
97
+ gap[gg] = m
98
+ gg = gg + 1
99
+
100
+ for j in range(g, gg):
101
+ gap[g] = gap[j]
102
+ if count[gap[j]] == d - dd:
103
+ g = g + 1
104
+ count[gap[j]] = 0
105
+
106
+ # for each gap insert leaf l, [the main while loop shall progress],
107
+ # remove leaf l
108
+ while l > 0 and g == gapter[l - 1]:
109
+ l = l - 1
110
+ B[A[l]] = B[l]
111
+ A[B[l]] = A[l]
112
+
113
+ if l > 0:
114
+ g = g - 1
115
+ A[l] = gap[g]
116
+ B[l] = B[A[l]]
117
+ B[A[l]] = l
118
+ A[B[l]] = l
119
+ gapter[l] = g
120
+ l = l + 1
121
+ return f
@@ -0,0 +1,240 @@
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
+ (activeLeaf1ndex := 0),
25
+ (activeGap1ndex := 1),
26
+ (unconstrainedLeaf := 2),
27
+ (gap1ndexCeiling := 3),
28
+ (leaf1ndexConnectee := 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[activeLeaf1ndex] = 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[activeLeaf1ndex] <= 1 or track[leafBelow][0] == 1:
91
+ if my[activeLeaf1ndex] > the[leavesTotal]:
92
+ foldingsTotal += the[leavesTotal]
93
+ else:
94
+ my[unconstrainedLeaf] = 0
95
+ my[gap1ndexCeiling] = track[gapRangeStart][my[activeLeaf1ndex] - 1]
96
+ my[activeGap1ndex] = my[gap1ndexCeiling]
97
+
98
+ for PREPAREdimension1ndex in range(1, the[dimensionsPlus1]):
99
+ if connectionGraph[PREPAREdimension1ndex][my[activeLeaf1ndex]][my[activeLeaf1ndex]] == my[activeLeaf1ndex]:
100
+ my[unconstrainedLeaf] += 1
101
+ else:
102
+ my[leaf1ndexConnectee] = connectionGraph[PREPAREdimension1ndex][my[activeLeaf1ndex]][my[activeLeaf1ndex]]
103
+ while my[leaf1ndexConnectee] != my[activeLeaf1ndex]:
104
+
105
+ if my[leaf1ndexConnectee] != my[activeLeaf1ndex]:
106
+ my[dimension1ndex] = PREPAREdimension1ndex
107
+ return track, gapsWhere, my
108
+
109
+ if my[activeLeaf1ndex] != the[leavesTotal]:
110
+ gapsWhere[my[gap1ndexCeiling]] = my[leaf1ndexConnectee]
111
+ if track[countDimensionsGapped][my[leaf1ndexConnectee]] == 0:
112
+ my[gap1ndexCeiling] += 1
113
+ track[countDimensionsGapped][my[leaf1ndexConnectee]] += 1
114
+ else:
115
+ print("else")
116
+ my[dimension1ndex] = PREPAREdimension1ndex
117
+ return track, gapsWhere, my
118
+ # PREPAREmy[leaf1ndexConnectee] % the[leavesTotal] == PREPAREmy[taskIndex]
119
+ my[leaf1ndexConnectee] = connectionGraph[dimension1ndex][my[activeLeaf1ndex]][track[leafBelow][my[leaf1ndexConnectee]]]
120
+
121
+ if my[unconstrainedLeaf] == the[dimensionsTotal]:
122
+ for leaf1ndex in range(my[activeLeaf1ndex]):
123
+ gapsWhere[my[gap1ndexCeiling]] = leaf1ndex
124
+ my[gap1ndexCeiling] += 1
125
+
126
+ for indexMiniGap in range(my[activeGap1ndex], my[gap1ndexCeiling]):
127
+ gapsWhere[my[activeGap1ndex]] = gapsWhere[indexMiniGap]
128
+ if track[countDimensionsGapped][gapsWhere[indexMiniGap]] == the[dimensionsTotal] - my[unconstrainedLeaf]:
129
+ my[activeGap1ndex] += 1
130
+ track[countDimensionsGapped][gapsWhere[indexMiniGap]] = 0
131
+
132
+ while my[activeLeaf1ndex] > 0 and my[activeGap1ndex] == track[gapRangeStart][my[activeLeaf1ndex] - 1]:
133
+ my[activeLeaf1ndex] -= 1
134
+ track[leafBelow][track[leafAbove][my[activeLeaf1ndex]]] = track[leafBelow][my[activeLeaf1ndex]]
135
+ track[leafAbove][track[leafBelow][my[activeLeaf1ndex]]] = track[leafAbove][my[activeLeaf1ndex]]
136
+
137
+ if my[activeLeaf1ndex] > 0:
138
+ my[activeGap1ndex] -= 1
139
+ track[leafAbove][my[activeLeaf1ndex]] = gapsWhere[my[activeGap1ndex]]
140
+ track[leafBelow][my[activeLeaf1ndex]] = track[leafBelow][track[leafAbove][my[activeLeaf1ndex]]]
141
+ track[leafBelow][track[leafAbove][my[activeLeaf1ndex]]] = my[activeLeaf1ndex]
142
+ track[leafAbove][track[leafBelow][my[activeLeaf1ndex]]] = my[activeLeaf1ndex]
143
+ track[gapRangeStart][my[activeLeaf1ndex]] = my[activeGap1ndex]
144
+ my[activeLeaf1ndex] += 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[activeLeaf1ndex] <= 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[activeLeaf1ndex] > the[leavesTotal] and thisIsNotTheFirstPass:
172
+ my[foldingsSubtotal] += the[leavesTotal]
173
+ else:
174
+ if thisIsNotTheFirstPass:
175
+ my[unconstrainedLeaf] = 0
176
+ my[gap1ndexCeiling] = track[gapRangeStart][my[activeLeaf1ndex] - 1]
177
+ my[activeGap1ndex] = 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[activeLeaf1ndex]][my[activeLeaf1ndex]] == my[activeLeaf1ndex] and thisIsNotTheFirstPass:
183
+ my[unconstrainedLeaf] += 1
184
+ else:
185
+ if thisIsNotTheFirstPass:
186
+ my[leaf1ndexConnectee] = connectionGraph[my[dimension1ndex]][my[activeLeaf1ndex]][my[activeLeaf1ndex]]
187
+ if my[leaf1ndexConnectee] != my[activeLeaf1ndex]:
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[activeLeaf1ndex] != taskDivisions:
194
+ myTask = True
195
+ else:
196
+ modulo = my[leaf1ndexConnectee] % the[leavesTotal]
197
+ if modulo == my[taskIndex]: myTask = True
198
+ else:
199
+ myTask = False
200
+ if myTask:
201
+ gapsWhere[my[gap1ndexCeiling]] = my[leaf1ndexConnectee]
202
+ if track[countDimensionsGapped][my[leaf1ndexConnectee]] == 0:
203
+ my[gap1ndexCeiling] += 1
204
+ track[countDimensionsGapped][my[leaf1ndexConnectee]] += 1
205
+ my[leaf1ndexConnectee] = connectionGraph[my[dimension1ndex]][my[activeLeaf1ndex]][track[leafBelow][my[leaf1ndexConnectee]]]
206
+ if my[leaf1ndexConnectee] != my[activeLeaf1ndex]:
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[activeLeaf1ndex]):
216
+ gapsWhere[my[gap1ndexCeiling]] = leaf1ndex
217
+ my[gap1ndexCeiling] += 1
218
+
219
+ for indexMiniGap in range(my[activeGap1ndex], my[gap1ndexCeiling]):
220
+ gapsWhere[my[activeGap1ndex]] = gapsWhere[indexMiniGap]
221
+ if track[countDimensionsGapped][gapsWhere[indexMiniGap]] == the[dimensionsTotal] - my[unconstrainedLeaf]:
222
+ my[activeGap1ndex] += 1
223
+ track[countDimensionsGapped][gapsWhere[indexMiniGap]] = 0
224
+
225
+ while my[activeLeaf1ndex] > 0 and my[activeGap1ndex] == track[gapRangeStart][my[activeLeaf1ndex] - 1]:
226
+ my[activeLeaf1ndex] -= 1
227
+ track[leafBelow][track[leafAbove][my[activeLeaf1ndex]]] = track[leafBelow][my[activeLeaf1ndex]]
228
+ track[leafAbove][track[leafBelow][my[activeLeaf1ndex]]] = track[leafAbove][my[activeLeaf1ndex]]
229
+
230
+ if my[activeLeaf1ndex] > 0:
231
+ my[activeGap1ndex] -= 1
232
+ track[leafAbove][my[activeLeaf1ndex]] = gapsWhere[my[activeGap1ndex]]
233
+ track[leafBelow][my[activeLeaf1ndex]] = track[leafBelow][track[leafAbove][my[activeLeaf1ndex]]]
234
+ track[leafBelow][track[leafAbove][my[activeLeaf1ndex]]] = my[activeLeaf1ndex]
235
+ track[leafAbove][track[leafBelow][my[activeLeaf1ndex]]] = my[activeLeaf1ndex]
236
+ track[gapRangeStart][my[activeLeaf1ndex]] = my[activeGap1ndex]
237
+ my[activeLeaf1ndex] += 1
238
+
239
+ if my[activeLeaf1ndex] <= 0:
240
+ return my[foldingsSubtotal]
@@ -0,0 +1,54 @@
1
+ from numpy import integer
2
+ from numpy.typing import NDArray
3
+ from typing import Any, Tuple
4
+ import numba
5
+ import numpy
6
+ from mapFolding import outfitFoldings
7
+ # from mapFolding.benchmarks.benchmarking import recordBenchmarks
8
+ from typing import Optional, Union, Sequence, Type
9
+ import os
10
+ import pathlib
11
+
12
+ # TODO the current tests expect positional `listDimensions, computationDivisions`, so after restructuring you can arrange the parameters however you want.
13
+ def countFolds(
14
+ listDimensions: Sequence[int],
15
+ computationDivisions: Optional[Union[int, str]] = None,
16
+ CPUlimit: Optional[Union[int, float, bool]] = None,
17
+ writeFoldsTotal: Optional[Union[str, os.PathLike[str]]] = None,
18
+ **keywordArguments: Optional[Type]
19
+ ):
20
+ """keywordArguments:
21
+ dtypeDefault: Optional[Type]
22
+ dtypeLarge: Optional[Type]
23
+
24
+ writeFoldsTotal: path, filename, or pathFilename
25
+ """
26
+ stateUniversal = outfitFoldings(listDimensions, computationDivisions=computationDivisions, CPUlimit=CPUlimit, **keywordArguments)
27
+
28
+ pathFilenameFoldsTotal = None
29
+ if writeFoldsTotal is not None:
30
+ pathFilenameFoldsTotal = pathlib.Path(writeFoldsTotal)
31
+ if pathFilenameFoldsTotal.is_dir():
32
+ filenameFoldsTotalDEFAULT = str(sorted(stateUniversal['mapShape'])).replace(' ', '') + '.foldsTotal'
33
+ pathFilenameFoldsTotal = pathFilenameFoldsTotal / filenameFoldsTotalDEFAULT
34
+ pathFilenameFoldsTotal.parent.mkdir(parents=True, exist_ok=True)
35
+
36
+ from mapFolding.babbage import _countFolds
37
+ foldsTotal = _countFolds(**stateUniversal)
38
+ # foldsTotal = benchmarkSherpa(**stateUniversal)
39
+
40
+ if pathFilenameFoldsTotal is not None:
41
+ try:
42
+ pathFilenameFoldsTotal.write_text(str(foldsTotal))
43
+ except Exception as ERRORmessage:
44
+ print(ERRORmessage)
45
+ print("\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal")
46
+ print(f"{foldsTotal=}")
47
+ print("\nfoldsTotal foldsTotal foldsTotal foldsTotal foldsTotal")
48
+
49
+ return foldsTotal
50
+
51
+ # @recordBenchmarks()
52
+ # def benchmarkSherpa(connectionGraph: NDArray[integer[Any]], foldsTotal: NDArray[integer[Any]], mapShape: Tuple[int, ...], my: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]):
53
+ # from mapFolding.babbage import _countFolds
54
+ # return _countFolds(connectionGraph, foldsTotal, mapShape, my, gapsWhere, the, track)