"""Cross-check the lower-threshold algorithm against two-sided principal minors.""" from fractions import Fraction from itertools import combinations, product import json from low_threshold import count_below_limit def determinant(matrix): if not matrix: return 1 return sum( (-1)**j * entry * determinant([r[:j] + r[j+1:] for r in matrix[1:]]) for j, entry in enumerate(matrix[0]) ) def brute(order, edges, threshold): subsets = [subset for size in range(1, order + 1) for subset in combinations(range(order), size)] def all_minors(signs, direction): matrix = [[threshold.numerator * (u == v) for v in range(order)] for u in range(order)] for (u, v), sign in zip(edges, signs): matrix[u][v] = matrix[v][u] = direction * sign * threshold.denominator for subset in subsets: if determinant([[matrix[i][j] for j in subset] for i in subset]) < 0: return False return True return sum( all(all_minors(signs, direction) for direction in (-1, 1)) for signs in product((-1, 1), repeat=len(edges)) ) def main(): all_pairs = list(combinations(range(4), 2)) checked = unknown = 0 for mask in range(1 << len(all_pairs)): edges = [p for j, p in enumerate(all_pairs) if mask >> j & 1] for t in map(Fraction, ('0', '1', '3/2', '2', '21/10')): value = count_below_limit(4, edges, t) if value is None: unknown += 1 else: assert value == brute(4, edges, t) checked += 1 triangle = [(0, 1), (1, 2), (2, 0)] # Every signed triangle has radius two regardless of its cycle product. assert count_below_limit(3, triangle, Fraction(3, 2)) == 0 assert count_below_limit(3, triangle, Fraction(2)) == 8 theta = [] cursor = 2 for _ in range(3): route = [0, *range(cursor, cursor+13), 1] cursor += 13 theta.extend(zip(route, route[1:])) assert count_below_limit(cursor, theta, Fraction(21, 10)) == 0 print(json.dumps(dict(exact_small_counts=checked, outside_structural_test=unknown, signed_triangle={"at_3/2": 0, "at_2": 8}, large_girth_theta=dict(girth=28, threshold="21/10", count=0)), indent=2, sort_keys=True)) if __name__ == "__main__": main()