File size: 5,002 Bytes
848d4b7 | 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 | #!/usr/bin/env python3
"""
Validator for problem 106: Hadamard Matrix of Order 668 via Goethals-Seidel construction
Validates that four ±1 sequences of length 167 define circulant matrices A, B, C, D
satisfying AA^T + BB^T + CC^T + DD^T = 668·I, which yields a Hadamard matrix of
order 668 via the Goethals-Seidel array.
Expected input format:
{"rows": [[...], [...], [...], [...]]} # four sequences of length 167
"""
import argparse
from typing import Any
import numpy as np
from scipy.linalg import circulant
from . import ValidationResult, load_solution, output_result, success, failure
TARGET_ORDER = 668
BLOCK_ORDER = 167 # 668 / 4
def validate(solution: Any) -> ValidationResult:
"""
Validate a Goethals-Seidel certificate for a Hadamard matrix of order 668.
The solution must provide four ±1 sequences of length 167 (first rows of
circulant matrices A, B, C, D) such that AA^T + BB^T + CC^T + DD^T = 668·I.
The validator then assembles the full 668×668 Hadamard matrix via the
Goethals-Seidel array and verifies H·H^T = 668·I.
Args:
solution: Dict with 'rows' key containing four lists of length 167
Returns:
ValidationResult with success/failure
"""
# --- Parse input ---
try:
if isinstance(solution, dict) and 'rows' in solution:
rows = solution['rows']
elif isinstance(solution, list) and len(solution) == 4:
rows = solution
else:
return failure(
"Invalid format: expected {\"rows\": [a, b, c, d]} "
"where a, b, c, d are ±1 sequences of length 167"
)
if len(rows) != 4:
return failure(f"Expected exactly 4 sequences, got {len(rows)}")
for i, row in enumerate(rows):
if len(row) != BLOCK_ORDER:
return failure(
f"Sequence {i} has length {len(row)}, expected {BLOCK_ORDER}"
)
seqs = [np.array(row, dtype=np.int64) for row in rows]
except (ValueError, TypeError) as e:
return failure(f"Failed to parse sequences: {e}")
# --- Check entries are ±1 ---
for i, seq in enumerate(seqs):
if not np.all((seq == 1) | (seq == -1)):
invalid_count = int(np.sum((seq != 1) & (seq != -1)))
return failure(
f"Sequence {i} must have entries ±1, found {invalid_count} invalid entries"
)
# --- Build circulant matrices ---
n = BLOCK_ORDER
A, B, C, D = [circulant(seq) for seq in seqs]
# --- Check core condition: AA^T + BB^T + CC^T + DD^T = 4n·I ---
gram_sum = A @ A.T + B @ B.T + C @ C.T + D @ D.T
expected = TARGET_ORDER * np.eye(n, dtype=np.int64)
if not np.array_equal(gram_sum, expected):
diff_mask = gram_sum != expected
diff_count = int(np.sum(diff_mask))
idx = np.argwhere(diff_mask)[0]
i, j = idx
return failure(
f"AA^T + BB^T + CC^T + DD^T ≠ {TARGET_ORDER}·I. "
f"Found {diff_count} incorrect entries. "
f"Example: position ({i},{j}) has {gram_sum[i,j]}, expected {expected[i,j]}",
differences=diff_count
)
# --- Assemble full Hadamard matrix via Goethals-Seidel array ---
# R is the back-circulant (reversal) matrix: R[i,j] = delta(i+j, n-1)
R = np.fliplr(np.eye(n, dtype=np.int64))
BR = B @ R
CR = C @ R
DR = D @ R
BtR = B.T @ R
CtR = C.T @ R
DtR = D.T @ R
H = np.block([
[ A, BR, CR, DR ],
[-BR, A, DtR, -CtR],
[-CR, -DtR, A, BtR],
[-DR, CtR, -BtR, A ]
])
# --- Final verification: H·H^T = 668·I ---
HHT = H @ H.T
full_expected = TARGET_ORDER * np.eye(TARGET_ORDER, dtype=np.int64)
if not np.array_equal(HHT, full_expected):
diff_mask = HHT != full_expected
diff_count = int(np.sum(diff_mask))
idx = np.argwhere(diff_mask)[0]
i, j = idx
return failure(
f"Assembled H·H^T ≠ {TARGET_ORDER}·I. "
f"Found {diff_count} incorrect entries. "
f"Example: position ({i},{j}) has {HHT[i,j]}, expected {full_expected[i,j]}",
differences=diff_count
)
return success(
f"Verified: Goethals-Seidel construction yields {TARGET_ORDER}×{TARGET_ORDER} "
f"Hadamard matrix with H·H^T = {TARGET_ORDER}·I",
order=TARGET_ORDER,
block_order=BLOCK_ORDER
)
def main():
parser = argparse.ArgumentParser(
description='Validate Goethals-Seidel certificate for Hadamard matrix of order 668'
)
parser.add_argument('solution', help='Solution as JSON string or path to JSON file')
parser.add_argument('--verbose', '-v', action='store_true', help='Verbose output')
args = parser.parse_args()
solution = load_solution(args.solution)
result = validate(solution)
output_result(result)
if __name__ == '__main__':
main()
|