#!/usr/bin/env python3
"""N02 — arithmetic from D1287 supplied sources.
No network, external packages, fitting operations or shared runtime files.
Printed source cells and unrounded arithmetic are distinct outputs.
"""

import math
from decimal import Decimal, ROUND_HALF_UP, getcontext
from fractions import Fraction

getcontext().prec = 40
PHI = (1.0 + math.sqrt(5.0)) / 2.0

# Exact arithmetic in Q(phi), with phi^2=phi+1. A pair is a+b*phi.
def qmul(x, y):
    a, b = x
    c, d = y
    return (a*c + b*d, a*d + b*c + b*d)


def qadd(x, y):
    return (x[0]+y[0], x[1]+y[1])


def qstr(x):
    return str(x[0]) + " + (" + str(x[1]) + ")*phi"

def main():
    print('=== row 2 ===')
    print('Scorecard (verbatim): det(J vac ) φ·1·φ⁻¹ theory 1 experiment 1 deviation 0 · d = exact Theorem (AX1) exact')
    phi = (Fraction(0), Fraction(1))
    one = (Fraction(1), Fraction(0))
    inv = (Fraction(-1), Fraction(1))
    result = qmul(qmul(phi, one), inv)
    assert result == one
    print("diagonal_product = " + qstr(result))
    print("theory = 1; internal_reference = 1; signed_percent = 0%")
    print("d = exact (identity label, not a statistical distance)")
    print()


if __name__ == "__main__":
    main()
