"""Verify the finite linear algebra for the fixed abscissa obstruction.""" from fractions import Fraction from itertools import combinations from math import prod WEIGHTS = { (-5, 0, 5): 175, (-5, 1, 4): -702, (-5, 2, 3): 336, (-4, 0, 4): 512, (-4, 1, 3): -210, (-3, 0, 3): -351, (-3, 1, 2): 240, } def entry(triple, index): """Return the coefficient of the indexed value in its divided difference.""" if index not in triple: return Fraction(0) return Fraction(1, prod(index - other for other in triple if other != index)) def determinant(matrix): """Compute a small determinant by rational elimination with row pivots.""" rows = [row.copy() for row in matrix] result = Fraction(1) for col in range(len(rows)): pivot_row = next((r for r in range(col, len(rows)) if rows[r][col]), None) if pivot_row is None: return Fraction(0) if pivot_row != col: rows[col], rows[pivot_row] = rows[pivot_row], rows[col] result = -result leading = rows[col][col] result *= leading for r in range(col + 1, len(rows)): multiplier = rows[r][col] / leading for c in range(col + 1, len(rows)): rows[r][c] -= multiplier * rows[col][c] return result def main(): indices = range(-5, 6) triples = [triple for triple in combinations(indices, 3) if sum(triple) == 0] columns = [index for index in indices if index not in (0, 1, 2)] minor = [[entry(triple, index) for index in columns] for triple in triples[:8]] assert len(triples) == 13 assert determinant(minor) == Fraction(1, 149361408000) for index in indices: total = Fraction(0) for triple, weight in WEIGHTS.items(): reflected = tuple(-vertex for vertex in reversed(triple)) total += weight * (entry(triple, index) + entry(reflected, index)) / 2 assert total == 0 moments = [] for degree in range(5): moment = sum( weight * (first * first + first * second + second * second) ** degree for (first, second, _), weight in WEIGHTS.items() ) moments.append(moment) assert moments == [0, 0, 0, 0, 1451520] assert sum(weight * prod(triple) ** 2 for triple, weight in WEIGHTS.items()) == 0 assert Fraction(3, 16) * moments[4] == 272160 print("The minor, row cancellation, and moments are exact.") if __name__ == "__main__": main()