File size: 9,646 Bytes
8e19377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3
"""Exact finite tests for temperature-complete tomography and its trades.

Python standard library only. Powers of positive integer z stand for the
unnormalized measurements at inverse temperatures k*beta0. No numerical
Laplace inversion or root finder is used. Proofs are in trades_notes.md.
"""
from collections import Counter, defaultdict
from fractions import Fraction
from itertools import combinations, product
import json


def grid(shape):
    return list(product(*(range(q) for q in shape)))


def lines(shape):
    points = grid(shape)
    out = []
    for axis in range(len(shape)):
        groups = defaultdict(list)
        for p in points:
            groups[p[:axis] + p[axis + 1:]].append(p)
        out.extend(tuple(group) for _, group in sorted(groups.items()))
    return out


def signature(values, points, coordinate_lines):
    at = dict(zip(points, values))
    return tuple(tuple(sorted(at[p] for p in line)) for line in coordinate_lines)


def is_rectangular_checkerboard(h, shape):
    support = [p for p, value in h.items() if value]
    levels = [sorted({p[i] for p in support}) for i in range(len(shape))]
    if any(len(level) != 2 for level in levels):
        return False
    if set(support) != set(product(*levels)):
        return False
    reference = tuple(level[0] for level in levels)
    amplitude = h[reference]
    return all(h[p] == amplitude * (-1) ** sum(
        p[i] == levels[i][1] for i in range(len(shape))) for p in support)


def complete_line_zero(interior_values, shape):
    """Expand free interior coefficients in tensor (e_a-e_last) bases."""
    n = len(shape)
    interior_points = list(product(*(range(q - 1) for q in shape)))
    coefficients = dict(zip(interior_points, interior_values))
    h = {}
    for p in grid(shape):
        missing = [i for i in range(n) if p[i] == shape[i] - 1]
        total = 0
        choices = [range(shape[i] - 1) if i in missing else (p[i],)
                   for i in range(n)]
        for a in product(*choices):
            total += coefficients[a]
        h[p] = (-1) ** len(missing) * total
    return h


def check_line_zero_enumeration():
    shape = (3, 3, 3)
    coordinate_lines = lines(shape)
    total = nonzero = signed = minimum_support = 0
    minimum_positive = 99
    support_histogram = Counter()
    for interior in product((-1, 0, 1), repeat=8):
        h = complete_line_zero(interior, shape)
        assert all(sum(h[p] for p in line) == 0 for line in coordinate_lines)
        total += 1
        if not any(h.values()):
            continue
        nonzero += 1
        support = sum(value != 0 for value in h.values())
        positives = sum(value > 0 for value in h.values())
        negatives = sum(value < 0 for value in h.values())
        assert support >= 8 and positives >= 4 and negatives >= 4
        minimum_positive = min(minimum_positive, positives)
        support_histogram[support] += 1
        if max(map(abs, h.values())) <= 1:
            signed += 1
        if support == 8:
            assert is_rectangular_checkerboard(h, shape)
            minimum_support += 1
    return {"shape": list(shape), "interior_arrays": total,
            "nonzero_line_zero_tensors": nonzero,
            "signed_nonzero_tensors": signed,
            "minimum_support_tensors": minimum_support,
            "minimum_positive_support": minimum_positive,
            "support_histogram": dict(sorted(support_histogram.items()))}


def check_colored_fibers(shape, alphabet):
    points = grid(shape)
    coordinate_lines = lines(shape)
    fibers = defaultdict(list)
    total = 0
    for values in product(alphabet, repeat=len(points)):
        fibers[signature(values, points, coordinate_lines)].append(values)
        total += 1
    pair_checks = minimal_trade_pairs = bounded_multiplicity_fields = 0
    threshold = 2 ** (len(shape) - 1)
    for fiber in fibers.values():
        for values in fiber:
            if max(Counter(values).values()) < threshold:
                assert len(fiber) == 1
                bounded_multiplicity_fields += 1
        for a, b in combinations(fiber, 2):
            changed = [p for p, va, vb in zip(points, a, b) if va != vb]
            assert len(changed) >= 2 ** len(shape)
            moved_colors = set()
            for color in set(a) | set(b):
                h = {p: int(va == color) - int(vb == color)
                     for p, va, vb in zip(points, a, b)}
                if any(h.values()):
                    moved_colors.add(color)
                    assert sum(value > 0 for value in h.values()) >= threshold
                    assert Counter(a)[color] >= threshold
                    assert all(sum(h[p] for p in line) == 0
                               for line in coordinate_lines)
            if len(changed) == 2 ** len(shape):
                assert len(moved_colors) == 2
                color = next(iter(moved_colors))
                h = {p: int(va == color) - int(vb == color)
                     for p, va, vb in zip(points, a, b)}
                assert is_rectangular_checkerboard(h, shape)
                minimal_trade_pairs += 1
            pair_checks += 1
    return {"shape": list(shape), "alphabet": list(alphabet),
            "fields": total, "fibers": len(fibers),
            "ambiguous_fibers": sum(len(fiber) > 1 for fiber in fibers.values()),
            "equal_data_pair_checks": pair_checks,
            "minimal_trade_pair_checks": minimal_trade_pairs,
            "multiplicity_threshold": threshold,
            "bounded_multiplicity_fields_certified_unique": bounded_multiplicity_fields}


def elementary_from_powers(powers):
    e = [Fraction(1)]
    for m in range(1, len(powers) + 1):
        e.append(sum(((-1) ** (k - 1) * e[m - k] * powers[k - 1]
                      for k in range(1, m + 1)), Fraction(0)) / m)
    return e


def polynomial_of_roots(values):
    coefficients = [Fraction(1)]
    for z in values:
        following = [Fraction(0)] * (len(coefficients) + 1)
        for j, coefficient in enumerate(coefficients):
            following[j] += coefficient
            following[j + 1] -= z * coefficient
        coefficients = following
    return coefficients


def check_newton_moments():
    cases = multiset_fibers = 0
    for length in range(2, 6):
        seen = {}
        for values in product(range(1, 5), repeat=length):
            powers = tuple(sum(z ** k for z in values) for k in range(1, length + 1))
            elementary = elementary_from_powers(powers)
            polynomial = [(-1) ** k * e for k, e in enumerate(elementary)]
            assert polynomial == polynomial_of_roots(values)
            multiset = tuple(sorted(values))
            assert powers not in seen or seen[powers] == multiset
            seen[powers] = multiset
            cases += 1
        multiset_fibers += len(seen)
    return {"line_lengths": [2, 3, 4, 5],
            "ordered_line_cases": cases,
            "distinct_multisets": multiset_fibers}


def check_sharp_embedded_trades():
    cases = moment_checks = 0
    for n in range(2, 6):
        shape = (3,) * n
        points = grid(shape)
        coordinate_lines = lines(shape)
        a, b = {}, {}
        fresh = 100
        for p in points:
            if all(x < 2 for x in p):
                a[p] = 2 if sum(p) % 2 == 0 else 5
                b[p] = 5 if sum(p) % 2 == 0 else 2
            else:
                a[p] = b[p] = fresh
                fresh += 1
        assert sum(a[p] != b[p] for p in points) == 2 ** n
        assert Counter(a.values())[2] == Counter(a.values())[5] == 2 ** (n - 1)
        assert signature([a[p] for p in points], points, coordinate_lines) == signature(
            [b[p] for p in points], points, coordinate_lines)
        for line in coordinate_lines:
            for power in range(1, 7):
                assert sum(a[p] ** power for p in line) == sum(b[p] ** power for p in line)
                moment_checks += 1
        cases += 1
    # Three distinct cyclic tensors have identical line multisets and are
    # therefore invisible at all temperatures. They are not a global two-color
    # parity interchange on the ternary domain.
    cyclic_cases = 0
    for n in (2, 3, 4):
        shape = (3,) * n
        points = grid(shape)
        coordinate_lines = lines(shape)
        tensors = [[(2, 5, 11)[(sum(p) + phase) % 3] for p in points]
                   for phase in range(3)]
        signatures = [signature(v, points, coordinate_lines) for v in tensors]
        assert signatures[0] == signatures[1] == signatures[2]
        assert len(set(map(tuple, tensors))) == 3
        cyclic_cases += 1
    return {"sharp_rectangular_examples": cases,
            "exact_power_projection_checks": moment_checks,
            "cyclic_ternary_examples": cyclic_cases}


def run():
    return {"status": "PASS", "arithmetic": "exact integers and Fractions",
            "line_zero_kernel": check_line_zero_enumeration(),
            "colored_fibers": [check_colored_fibers((2, 2, 2), range(3)),
                               check_colored_fibers((3, 3), range(3))],
            "finite_temperature_moments": check_newton_moments(),
            "sharp_examples": check_sharp_embedded_trades(),
            "primary_prior_art": [
                "https://arxiv.org/abs/0704.1730",
                "https://arxiv.org/abs/1104.1295",
                "https://eccc.weizmann.ac.il/report/2008/105/download/"],
            "scope": "Finite checks support the manuscript proofs; no novelty claim follows."}


if __name__ == "__main__":
    print(json.dumps(run(), indent=2, sort_keys=True))