#!/usr/bin/env python3 """C330 exact incidence dependency certificates for the two frozen C329 modes. python C330_Independent_Audit.py --sources DIR --out DIR Requires PREVIOUS_DATA.json (C329). Standard library only. No anchor fitting, source alteration, new endpoint/phase, or C331 calculation. """ import argparse import hashlib import itertools import json from fractions import Fraction as F from pathlib import Path def encode(x): if isinstance(x, F): return str(x) if isinstance(x, dict): return {str(k): encode(v) for k, v in x.items()} if isinstance(x, (list, tuple)): return [encode(v) for v in x] return x def rref(matrix): a = [[F(x) for x in row] for row in matrix] pivots, row = [], 0 for column in range(len(a[0])): pivot = next((k for k in range(row, len(a)) if a[k][column]), None) if pivot is None: continue a[row], a[pivot] = a[pivot], a[row] factor = a[row][column] a[row] = [x / factor for x in a[row]] for k in range(len(a)): if k != row and a[k][column]: factor = a[k][column] a[k] = [x - factor * y for x, y in zip(a[k], a[row])] pivots.append(column) row += 1 if row == len(a): break return a, pivots def rank(matrix): return len(rref(matrix)[1]) def nullspace(matrix): reduced, pivots = rref(matrix) width = len(matrix[0]) vectors = [] for free in [k for k in range(width) if k not in pivots]: v = [F(0)] * width v[free] = F(1) for row, pivot in enumerate(pivots): v[pivot] = -reduced[row][free] vectors.append(v) return vectors def dot(a, b): return sum((x * y for x, y in zip(a, b)), F(0)) def incidence(a, b, width=10): out = [F(0)] * width out[a], out[b] = F(1), F(-1) return out def combination(coeffs, rows): return [sum((coeffs[k] * rows[k][j] for k in range(len(rows))), F(0)) for j in range(len(rows[0]))] def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--sources', required=True, type=Path) parser.add_argument('--out', required=True, type=Path) args = parser.parse_args() raw = (args.sources / 'PREVIOUS_DATA.json').read_bytes() old = json.loads(raw) roles = ['Noah birth', 'Shem birth', 'Flood start', 'Noah death', 'Shem death'] node_labels = [t + ':' + role for t in ['SP', 'MT'] for role in roles] edge_labels = ['SP_adj_' + str(k) for k in range(4)] + ['MT_adj_' + str(k) for k in range(4)] + ['context_' + str(k) for k in range(5)] matrix = ([incidence(k, k + 1) for k in range(4)] + [incidence(5 + k, 6 + k) for k in range(4)] + [incidence(k, 5 + k) for k in range(5)]) basis_indices = list(range(4)) + list(range(8, 13)) basis = [matrix[k] for k in basis_indices] cycles = [] for k in range(4): v = [F(0)] * 13 v[k], v[4 + k], v[8 + k], v[9 + k] = F(1), F(-1), F(-1), F(1) cycles.append(v) checks, modes = [], [] def check(name, actual, expected): passed = actual == expected checks.append({'name': name, 'actual': encode(actual), 'expected': encode(expected), 'pass': passed}) if not passed: raise AssertionError(name) check('predecessor step', old['step'], 'C329') check('two named modes', len(old['modes']), 2) check('13x10 matrix', [len(matrix), len(matrix[0])], [13, 10]) check('rank9', rank(matrix), 9) right_null = nullspace(matrix) left_null = nullspace(list(map(list, zip(*matrix)))) check('right nullspace common translation', right_null, [[F(1)] * 10]) check('left nullspace dimension4', len(left_null), 4) check('named cycles independent', rank(cycles), 4) check('all four named row dependencies', all(combination(v, matrix) == [F(0)] * 10 for v in cycles), True) check('nine-edge selected basis independent', rank(basis), 9) for mode in old['modes']: name = mode['binding'] records = [mode['records'][t][role] for t in ['SP', 'MT'] for role in roles] centers = [F(r['center']) for r in records] values = [dot(row, centers) for row in matrix] basis_values = [values[k] for k in basis_indices] check(name + '/five context460 values', values[8:], [F(460)] * 5) check(name + '/source adjacent widths agree', values[:4], values[4:8]) check(name + '/four cycle constants close', [dot(v, values) for v in cycles], [F(0)] * 4) check(name + '/source constraint consistency', rank([row + [value] for row, value in zip(matrix, values)]), 9) cross, cross_rows, cross_values = [], [], [] inherited_rows = {(r['anchor_role'], r['terminal_role']): r for r in old['rows'] if r['binding'] == name} for a, c in itertools.product(range(5), repeat=2): edge = incidence(5 + a, c) coeffs = [F(0)] * 9 for k in range(min(a, c), max(a, c)): coeffs[k] = F(1 if a < c else -1) coeffs[4 + a] = F(-1) actual, forced = dot(edge, centers), dot(coeffs, basis_values) row = {'MT_anchor_role': roles[a], 'SP_terminal_role': roles[c], 'incidence_row': edge, 'basis_coefficients': coeffs, 'actual_residual': actual, 'forced_residual': forced, 'edge_certificate_valid': combination(coeffs, basis) == edge, 'value_certificate_valid': actual == forced, 'C329_value_preserved': actual == inherited_rows[roles[a], roles[c]]['residual'], 'individual_rank_increment': rank(matrix + [edge]) - rank(matrix)} cross.append(row) cross_rows.append(edge) cross_values.append(actual) check(name + '/all25 cross edge certificates', all(r['edge_certificate_valid'] for r in cross), True) check(name + '/all25 forced values', all(r['value_certificate_valid'] for r in cross), True) check(name + '/all25 C329 values', all(r['C329_value_preserved'] for r in cross), True) check(name + '/all25 individual rank increments0', all(r['individual_rank_increment'] == 0 for r in cross), True) combined = matrix + cross_rows combined_values = values + cross_values check(name + '/combined38-edge rank9', rank(combined), 9) check(name + '/combined38-edge consistency', rank([row + [value] for row, value in zip(combined, combined_values)]), 9) component_lookup, components_consistent = {}, True for trial in [r for r in old['phase_trials'] if r['binding'] == name]: for tradition, role, p, x in [ ('SP', trial['anchor_role'], trial['phases'][0], trial['coordinates'][0]), ('MT', trial['anchor_role'], trial['phases'][1], trial['coordinates'][1]), ('SP', trial['terminal_role'], trial['phases'][2], trial['coordinates'][2]), ]: key, value = (tradition + ':' + role, F(p)), F(x) if key in component_lookup and component_lookup[key] != value: components_consistent = False component_lookup[key] = value check(name + '/inherited component consistency', components_consistent, True) check(name + '/20 inherited endpoint components', len(component_lookup), 20) phase_menu = [F(-1, 4), F(1, 4)] check(name + '/actual inherited component offsets', all(component_lookup[label, p] == center + p for label, center in zip(node_labels, centers) for p in phase_menu), True) phase_rows = [] for phases in itertools.product(phase_menu, repeat=10): coords = [component_lookup[label, p] for label, p in zip(node_labels, phases)] residuals = [dot(row, coords) - value for row, value in zip(matrix, values)] passed = all(r == 0 for r in residuals) phase_rows.append({'phase_pattern': ''.join('-' if p < 0 else '+' for p in phases), 'edge_residuals_in_order': residuals, 'all13_constraints_exact': passed, 'common_phase_criterion': passed == (len(set(phases)) == 1)}) exact_patterns = [r['phase_pattern'] for r in phase_rows if r['all13_constraints_exact']] check(name + '/all1024 inherited phase assignments', len(phase_rows), 1024) check(name + '/only common phase assignments', exact_patterns, ['----------', '++++++++++']) check(name + '/connected phase criterion exhaustive', all(r['common_phase_criterion'] for r in phase_rows), True) birth_ids, flood_death_ids = [0, 5, 1, 6], [2, 7, 4, 9] birth, flood_death = [centers[k] for k in birth_ids], [centers[k] for k in flood_death_ids] transport = [x - y for x, y in zip(birth, flood_death)] check(name + '/four600 coordinate comparisons', transport, [F(600)] * 4) check(name + '/C329 first existing field', birth, [F(x) for x in mode['four_point_fields'][0]['coordinates']]) check(name + '/C329 second existing field', flood_death, [F(x) for x in mode['four_point_fields'][1]['coordinates']]) exact_transport_components = [] for phase in phase_menu: first = [component_lookup[node_labels[k], phase] for k in birth_ids] second = [component_lookup[node_labels[k], phase] for k in flood_death_ids] differences = [x - y for x, y in zip(first, second)] check(name + '/like-phase600 transport/' + str(phase), differences, [F(600)] * 4) exact_transport_components.append({'physical_phase': phase, 'birth_field': first, 'Flood_death_field': second, 'differences': differences}) modes.append({'binding': name, 'coherent_single_gear': mode['coherent_single_gear'], 'node_records_in_order': records, 'centers': centers, '13_edge_values': values, 'basis_values': basis_values, 'rank': rank(matrix), 'augmented_rank': 9, 'cross_residual_certificates': cross, 'combined38_edge_rank': rank(combined), 'combined_rank_increment': rank(combined) - rank(matrix), 'phase_assignments': phase_rows, 'exact_phase_patterns': exact_patterns, '600_transport': {'birth_field_node_indices': birth_ids, 'Flood_death_node_indices': flood_death_ids, 'birth_field': birth, 'Flood_death_field': flood_death, 'componentwise_differences': transport, 'exact_components': exact_transport_components, 'source_basis': 'Noah birth to Flood600 and Shem lifespan600; MT counterparts follow through same-role460 constraints.', 'ancestry_limit': 'A regular coordinate transport does not identify the distinct source roles or cumulative ancestries.'}}) result = {'step': 'C330', 'source_sha256': hashlib.sha256(raw).hexdigest(), 'node_order': node_labels, 'edge_order': edge_labels, 'incidence_matrix': matrix, 'right_nullspace': right_null, 'computed_left_nullspace': left_null, 'named_cycle_coefficients': cycles, 'named_cycle_rule': 'SP_adj_i - MT_adj_i - context_i + context_(i+1) = 0', 'selected_basis_edge_indices': basis_indices, 'selected_basis_edge_labels': [edge_labels[k] for k in basis_indices], 'modes': modes, 'checks': checks, 'check_count': len(checks), 'all_checks_passed': all(r['pass'] for r in checks), 'claim_status': 'Exact dependence of admitted duration equations. Source memberships and absolute dates remain external inputs, not consequences of rank.', 'prohibitions_observed': ['No new endpoint/context/phase', 'No fitted anchor', 'No E execution', 'No C331 calculation']} args.out.mkdir(parents=True, exist_ok=True) path = args.out / 'C330_Independent_Audit_Results.json' path.write_text(json.dumps(encode(result), indent=2, sort_keys=True) + '\n') print(json.dumps({'result': str(path), 'checks': len(checks), 'all_checks_passed': result['all_checks_passed'], 'rank': rank(matrix), 'left_nullity': len(left_null), 'right_nullity': len(right_null), 'mode_phase_success_counts': [len(m['exact_phase_patterns']) for m in modes]}, sort_keys=True)) if __name__ == '__main__': main()