# Check of a claimed Hadamard matrix of order 668.
# Supports: https://vibemathlab.com/news/hadamard-matrix-order-668-2026
# Written for vibemathlab.com, Nov 27, 2026.
# Run: python3 hadamard-668-check.py MATRIX_FILE   (Python 3, standard library only: sys)
# MATRIX_FILE is plain text with 668 lines of 668 characters, each "+" or "-". I did not republish the matrix.
# It comes from the public collection kept by Egan and coauthors at https://math74.com/Hadamard ; I decoded
# the order-668 image there into + and - rows myself.
# Expected output for a valid matrix:
#   order 668; entries all +1/-1; non-orthogonal row pairs: 0 of 222778
#   distinct rows: 668
# The original header of the program follows unchanged.
#
# My own check of a claimed Hadamard matrix of order 668 (data file only; no third-party code).
# A Hadamard matrix of order n: entries +1/-1 and H H^T = n I (rows pairwise orthogonal).
import sys
rows = [l.strip() for l in open(sys.argv[1]) if l.strip()]
n = len(rows)
assert all(len(r) == n for r in rows), 'not square'
assert all(set(r) <= set('+-') for r in rows), 'entries not +/-'
# encode each row as two bitmasks; dot(r_i, r_j) = n - 2 * popcount(r_i XOR r_j)
masks = [int(r.replace('+', '0').replace('-', '1'), 2) for r in rows]
bad = 0
for i in range(n):
    for j in range(i + 1, n):
        if bin(masks[i] ^ masks[j]).count('1') * 2 != n: bad += 1
print(f'order {n}; entries all +1/-1; non-orthogonal row pairs: {bad} of {n*(n-1)//2}')
print('distinct rows:', len(set(rows)))
