"""Exact arithmetic checks for the accompanying source audit. Run with python3.""" from fractions import Fraction as Q from itertools import product from math import comb, log2 def node(i): return 1 - Q(1, 2**i) def branch(i, t): left, right = node(i - 1), node(i) return left + (right - left) * (t if i % 2 else 1 - t) def bump(t): return 4 * t * (1 - t) def countable_examples(): half = Q(1, 2) # At the right endpoint of each cell the inverse coordinate alternates. right_preimages = [Q(i % 2) for i in range(1, 11)] assert all(branch(i, t) == node(i) for i, t in enumerate(right_preimages, 1)) heights = [t * bump(half) for t in right_preimages] assert heights == [1, 0] * 5 assert all(x * bump(y) == 0 for x, y in product((0, 1), repeat=2)) print('3.2 right-edge trace at y=1/4:', *heights) # Printed second join compares i with i+1. A row-dependent map passes it. def row_map(j, x): return bump(x) if j == 1 else 0 assert branch(1, 1) == branch(2, 1) == half assert row_map(1, half) == 1 and row_map(2, half) == 0 assert (1 - 2 * Q(3, 4)) / 2 == -Q(1, 4) assert bump(half) == 1 # distance from this peak to the horizontal zero graph # Two different constant scales at the first vertical seam, f=y(1-y). y = branch(1, half) seed = lambda t: t * (1 - t) traces = seed(y) + half * seed(half), seed(y) assert traces == (Q(5, 16), Q(3, 16)) print('4.2 scale seam:', *traces) # Taking both cell indices to infinity forces g=Lf=0 for this seed. assert seed(1) == 0 and seed(node(1)) == Q(1, 4) print('4.3 constant scale: forced height 0, interpolation height', seed(node(1))) # g=1 on the outer edges for scales supported inside a single open cell. bracket = [1 - (1 + half * (1 - half) * t) for t in right_preimages] assert bracket == [-Q(1, 4), 0] * 5 assert (1 + half) / (1 - half) == 3 print('4.3 unweighted bracket at the top edge:', *bracket[:4]) # For f_n=sqrt(x+1/n^2)-1/n, use exact x=1/k^2 to test sqrt(x). for k in (4, 16, 64, 256): assert Q(1, k) / Q(1, k**2) == k def partial_attractors(): # m=1, n=2; allow even more freedom than in the fixed rectangular word. # For x -> (x+digit)/2 the fixed point of a two-letter word is (2a+b)/3. points = {Q(2*a+b, 3) for a, b in product((0, 1), repeat=2)} assert points == {0, Q(1, 3), Q(2, 3), 1} assert Q(1, 2) not in points maps = {(i, j): int((i, j) == (1, 2)) for i, j in product((1, 2), repeat=2)} assert set(maps.values()) == {0, 1} assert {maps[1, 1], maps[2, 2]} == {0} def nonuniform_dimension(): roots = Q(3, 5), Q(4, 5) widths = [v*v for v in roots] scale = Q(5, 7) assert sum(widths) == 1 and max(widths) < scale assert scale * sum(roots) == 1 gamma = 4 * scale assert gamma**2 > 8 # equivalent to 1+log_2(gamma)>5/2 # Exact finite stopping-tree check for the mass argument in the proof. epsilon = Q(1, 64) stack = [(Q(1), Q(1))] total_mass = Q(0) while stack: root_width, vertical = stack.pop() if root_width**2 <= epsilon: assert min(widths) * epsilon < root_width**2 total_mass += vertical * root_width else: stack.extend((root_width * r, vertical * scale) for r in roots) assert total_mass == 1 print('2.4 predicted dimension:', 1 + log2(float(gamma)), 'proved upper bound: 2.5') def finite_grid_examples(): # Printed g for corner heights from f=x. x = y = 1 assert x*(1-y) + (1-x)*y == 0 and x == 1 scale, M, norm_L, norm_I_minus_L = Q(1, 3), 2, 1, Q(1, 8) assert M*scale < 1 assert scale < 1 / (M + M**2 * norm_I_minus_L) inverse_bound = (1 + M*scale) / (1 - M**2 * scale * norm_L) assert inverse_bound == -5 print('4.10(iv) claimed inverse norm upper bound:', inverse_bound) # At p=1, M=sqrt(8), q=1/4 and ||I-L||=1/8. The smallness # condition reduces to sqrt(8)+1<4, while the denominator is -1. assert 8 < 3**2 and 1 - 8 * Q(1, 4) == -1 monomials = list(product(range(2), repeat=2)) assert len(monomials) == 4 and comb(4, 2) == 6 # x^3 has uniform distance >= 1/4 to all quadratics using its # third divided difference at the four alternation points of T_3. nodes = [-1, -Q(1, 2), Q(1, 2), 1] weights = [-Q(2, 3), Q(4, 3), -Q(4, 3), Q(2, 3)] for k in range(4): assert sum(w*t**k for w, t in zip(weights, nodes)) == int(k == 3) assert sum(map(abs, weights)) == 4 # The second difference gives distance at least 1/2 from x^2 to # affine functions. The constant 1/2 attains the bound on [-1,1]. for k in (0, 1, 2): assert sum(w*t**k for w, t in zip((1, -2, 1), (-1, 0, 1))) == (2 if k == 2 else 0) # In the multivariate paper, look at the lower left branch on x=1/2: # b=0, f=x(1-x), alpha=1/2. seed = lambda t: t * (1 - t) actual = seed(Q(1, 4)) + Q(1, 2) * seed(Q(1, 2)) assert actual == Q(5, 16) and seed(Q(1, 4)) == Q(3, 16) print('2104.02950 interior-preimage identity:', actual, 'versus', seed(Q(1, 4))) if __name__ == '__main__': countable_examples() partial_attractors() nonuniform_dimension() finite_grid_examples() print('All assertions passed.')