#!/usr/bin/env python3
"""N01 — 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 1 ===')
    print('Scorecard (verbatim): φ² + φ⁻² = 3 (axiom) theory 3 experiment 3 deviation 0 · d = exact Axiom exact')
    phi = (Fraction(0), Fraction(1))
    inv = (Fraction(-1), Fraction(1))
    square, inverse_square = qmul(phi, phi), qmul(inv, inv)
    result = qadd(square, inverse_square)
    print("phi_squared = " + qstr(square))
    print("phi_inverse_squared = " + qstr(inverse_square))
    print("identity = " + qstr(result))
    assert result == (Fraction(3), Fraction(0))
    print("theory = 3; internal_reference = 3; signed_percent = 0%")
    print("d = exact (identity label, not a statistical distance)")
    print()


if __name__ == "__main__":
    main()
