File size: 2,880 Bytes
5e44c61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Check selected finite examples from the TCCT manuscript using exact arithmetic."""

from fractions import Fraction
from itertools import product


def require(condition, message):
    if not condition:
        raise AssertionError(message)


def check_query_counts():
    tolerance = Fraction(1, 1024)
    geometric_tail = lambda m: Fraction(1, 2**m)
    reciprocal_tail = lambda m: Fraction(1, m + 1)
    for tail, count in ((geometric_tail, 10), (reciprocal_tail, 1023)):
        require(tail(count) <= tolerance < tail(count - 1), f"Wrong minimal count: {count}")
    print("Theorem 5.2: the optimal query counts are 10 and 1,023.")


def check_forward_differences():
    for dimension in range(1, 5):
        corners = tuple(product((0, 1), repeat=dimension))
        for side in range(1, 6):
            values = {
                point: (3 + sum((axis + 1) * x**2 for axis, x in enumerate(point))) ** (dimension + 1)
                for point in product(range(side + 1), repeat=dimension)
            }
            mixed_sum = sum(
                (-1) ** (dimension - sum(corner))
                * values[tuple(x + offset for x, offset in zip(point, corner))]
                for point in product(range(side), repeat=dimension)
                for corner in corners
            )
            boundary_sum = sum(
                (-1) ** (dimension - sum(corner)) * values[tuple(side * bit for bit in corner)]
                for corner in corners
            )
            require(mixed_sum == boundary_sum, f"Mismatch at dimension {dimension}, side {side}")
    print("Equation (13): checked exact telescoping on 20 finite grids.")


def check_transport():
    q = Fraction
    operator = ((q(1, 2), q(1, 4)), (q(1, 8), q(1, 4)))
    emission = (q(1, 4), q(5, 8))
    exact = (q(1), q(1))
    approximation = (q(61, 64), q(15, 16))
    fixed_point = tuple(emitted + sum(weight * value for weight, value in zip(row, exact))
                        for emitted, row in zip(emission, operator))
    require(fixed_point == exact, "The claimed exact radiance is not a fixed point.")
    residual = tuple(
        estimate - sum(weight * v for weight, v in zip(row, approximation)) - emitted
        for estimate, row, emitted in zip(approximation, operator, emission)
    )
    require(residual == (-q(1, 128), -q(21, 512)), "Incorrect transport residual.")
    boundary_error = abs(exact[1] - approximation[1])
    self_coupling, boundary_coupling = operator[0]
    certificate = (abs(residual[0]) + boundary_coupling * boundary_error) / (1 - self_coupling)
    require(certificate == abs(exact[0] - approximation[0]) == q(3, 64),
            "The interior error does not match the certificate.")
    print("Theorem 7.1 example: the error attains the bound 3/64.")


if __name__ == "__main__":
    check_query_counts()
    check_forward_differences()
    check_transport()