-
Notifications
You must be signed in to change notification settings - Fork 2
/
multidimensional_szudzik.py
208 lines (171 loc) · 6.68 KB
/
multidimensional_szudzik.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from typing import List, Iterable
from general import floored_root, three_dimensional_plot
from multidimensional_box import multidimensional_box_unpairing, multidimensional_box_pairing
def multidimensional_szudzik_product(*streams: Iterable):
n = len(streams)
if n == 0:
# The Cartesian product of no streams is a single empty item
yield []
return
# In the recursive function, it must possible to get the shell element of the final stream. This iterator is stepped
# through at each shell to produce the final_shell_element.
final_shell_iterator = iter(streams[-1])
def recursive_product(i_stream, shell_used) -> list:
if i_stream == n - 1:
if shell_used:
# A shell element was emitted earlier in the item
for i_element, element in zip(range(shell + 1), streams[i_stream]):
yield [element]
else:
# No shell element has been emitted for this item yet and the recursive function is at the last
# stream, so a shell element only must be emitted or else this item will not be on the shell.
yield [final_shell_element]
else:
for i_element, element in zip(range(shell + 1), streams[i_stream]):
next_shell_used = shell_used or i_element == shell
for remaining_elements in recursive_product(i_stream + 1, next_shell_used):
yield [element] + remaining_elements
shell = 0
while True:
final_shell_element = next(final_shell_iterator)
yield from recursive_product(0, False)
shell += 1
def multidimensional_szudzik_pairing(*indexes: int) -> int:
n = len(indexes)
if n == 0:
# The only one element of a Cartesian product of zero streams and this is it
return 0
shell = max(indexes)
def recursive_index(dim: int):
# Number of dimensions of a slice perpendicular to the current axis
slice_dims = n - dim - 1
# Number of elements in a slice (only those on the current shell)
subshell_count = (shell + 1) ** slice_dims - shell ** slice_dims
index_i = indexes[dim]
if index_i == shell:
# Once an index on the shell is encountered, the remaining indexes follow
# multidimensional box pairing
return subshell_count * shell + multidimensional_box_pairing([shell + 1] * slice_dims,
indexes[dim + 1:])
else:
# Compute the contribution from the next index the same way
# by recursing to the next dimension
return subshell_count * index_i + recursive_index(dim + 1)
# Start with the number of elements from before this shell and recursively
# find the contribution from each index to the linear index
return shell ** n + recursive_index(0)
def multidimensional_szudzik_unpairing(n: int, index: int) -> List[int]:
shell = floored_root(index, n)
def recursive_indexes(dim: int, remaining: int):
if dim == n - 1:
# If this is reached, that means that no index so far has been on the shell. By construction, this
# final index must be on the shell or else the point itself will not be on the shell.
return [shell]
else:
# Number of dimensions of a slice perpendicular to the current axis
slice_dims = n - dim - 1
# Number of elements in a slice (only those on the current shell)
subshell_count = (shell + 1) ** slice_dims - shell ** slice_dims
index_i = min(remaining // subshell_count, shell)
if index_i == shell:
# Once an index on the shell is encountered, the remaining indexes follow
# multidimensional box unpairing
return [shell] + multidimensional_box_unpairing([shell + 1] * slice_dims,
remaining - subshell_count * shell)
else:
# Compute the next index the same way by
# recursing to the next dimension
return [index_i] + recursive_indexes(dim + 1, remaining - subshell_count * index_i)
# Subtract out the elements from before this shell and recursively
# find the index at each dimension from what remains
return recursive_indexes(0, index - shell ** n)
def multidimensional_recursive_szudzik_plot():
# Plot third shell only
points = [
[0, 0, 2],
[0, 1, 2],
[1, 0, 2],
[1, 1, 2],
[0, 2, 0],
[0, 2, 1],
[1, 2, 0],
[1, 2, 1],
[0, 2, 2],
[1, 2, 2],
[2, 0, 0],
[2, 0, 1],
[2, 1, 0],
[2, 1, 1],
[2, 0, 2],
[2, 1, 2],
[2, 2, 0],
[2, 2, 1],
[2, 2, 2],
]
arrows = [
[0, 0, 2, 0, 1, 2],
[0, 1, 2, 1, 0, 2],
[1, 0, 2, 1, 1, 2],
[0, 2, 0, 0, 2, 1],
[0, 2, 1, 1, 2, 0],
[1, 2, 0, 1, 2, 1],
[1, 2, 1, 0, 2, 2],
[0, 2, 2, 1, 2, 2],
[2, 0, 0, 2, 0, 1],
[2, 0, 1, 2, 1, 0],
[2, 1, 0, 2, 1, 1],
[2, 1, 1, 2, 0, 2],
[2, 0, 2, 2, 1, 2],
[2, 1, 2, 2, 2, 0],
[2, 2, 0, 2, 2, 1],
[2, 2, 1, 2, 2, 2],
]
return three_dimensional_plot(points, arrows)
def multidimensional_sorted_szudzik_plot():
# Plot third shell only
points = [
[0, 0, 2],
[0, 1, 2],
[0, 2, 0],
[0, 2, 1],
[0, 2, 2],
[1, 0, 2],
[1, 1, 2],
[1, 2, 0],
[1, 2, 1],
[1, 2, 2],
[2, 0, 0],
[2, 0, 1],
[2, 0, 2],
[2, 1, 0],
[2, 1, 1],
[2, 1, 2],
[2, 2, 0],
[2, 2, 1],
[2, 2, 2],
]
arrows = [
[0, 0, 2, 0, 1, 2],
[0, 1, 2, 0, 2, 0],
[0, 2, 0, 0, 2, 1],
[0, 2, 1, 0, 2, 2],
[1, 0, 2, 1, 1, 2],
[1, 1, 2, 1, 2, 0],
[1, 2, 0, 1, 2, 1],
[1, 2, 1, 1, 2, 2],
[2, 0, 0, 2, 0, 1],
[2, 0, 1, 2, 0, 2],
[2, 0, 2, 2, 1, 0],
[2, 1, 0, 2, 1, 1],
[2, 1, 1, 2, 1, 2],
[2, 1, 2, 2, 2, 0],
[2, 2, 0, 2, 2, 1],
[2, 2, 1, 2, 2, 2],
]
return three_dimensional_plot(points, arrows)
if __name__ == '__main__':
from general import assert_multidimensional_consistency
multidimensional_recursive_szudzik_plot().show()
multidimensional_sorted_szudzik_plot().show()
assert_multidimensional_consistency(multidimensional_szudzik_product, multidimensional_szudzik_pairing,
multidimensional_szudzik_unpairing)