Read the checking program (76 lines of Python)
"""Check an exact bilinear decomposition of the rational matrix product and count its terms."""
import json
import re
import sys
from itertools import product
SHAPE = (5, 5, 5)
MAX_BYTES = 131263
NUMERATOR = re.compile(rb"-?[0-9]{1,12}")
def verify(data, shape=SHAPE):
m, n, p = shape
lines = data.splitlines()
if not 2 <= len(lines) <= m * n * p + 1:
raise ValueError("The submission has the wrong number of terms.")
if re.fullmatch(rb"[1-9][0-9]{0,11}", lines[0]) is None:
raise ValueError("The first line must be a positive denominator of up to twelve digits.")
denominator = int(lines[0])
a_size, b_size, c_size = m * n, n * p, m * p
terms = []
for line in lines[1:]:
tokens = line.split()
if len(tokens) != a_size + b_size + c_size:
raise ValueError("Each term must specify all three coefficient vectors.")
if any(NUMERATOR.fullmatch(token) is None for token in tokens):
raise ValueError("A coefficient has an invalid numerator.")
coefficients = list(map(int, tokens))
term = [
[(i, value) for i, value in enumerate(vector) if value]
for vector in (
coefficients[:a_size], coefficients[a_size:a_size + b_size],
coefficients[a_size + b_size:],
)
]
if not all(term):
raise ValueError("A term has an identically zero factor.")
terms.append(term)
# Equality on pairs of standard basis matrices is equivalent to
# equality on every input by bilinearity. Scale by the denominator cubed.
tensor = [0] * (a_size * b_size * c_size)
for left, right, output in terms:
for (i, a), (j, b) in product(left, right):
offset = (i * b_size + j) * c_size
ab = a * b
for k, c in output:
tensor[offset + k] += ab * c
scale = denominator ** 3
for i, j, k in product(range(m), range(n), range(p)):
index = ((i * n + j) * b_size + j * p + k) * c_size + i * p + k
tensor[index] -= scale
if any(tensor):
raise ValueError("The terms do not sum to the matrix multiplication tensor.")
return len(terms)
def main():
data = sys.stdin.buffer.read(MAX_BYTES + 1)
try:
if len(data) > MAX_BYTES:
raise ValueError("The submission exceeds the byte limit.")
if not data.isascii() or any(c < 32 and c not in (9, 10, 13) for c in data):
raise ValueError("Use plain ASCII text.")
score = verify(data)
except ValueError as error:
result = {"valid": False, "score": None, "message": str(error)}
else:
result = {"valid": True, "score": score, "message": "The construction is valid."}
print(json.dumps(result, separators=(",", ":")))
if __name__ == "__main__":
main()