"""Conservative state dependencies for compiler-owned expressions. This is structural bookkeeping, not C parsing or numerical Jacobian evaluation. Native multi-output operations, projections and mode-dependent writes must be registered explicitly by the lowering path. Unresolved reachable inputs disable coloring for the complete model. """ from collections import deque from dataclasses import dataclass import hashlib import json import re # Reviewed expression leaves. Unknown arrays/scalars remain unresolved instead # of silently becoming constants; the flow schedule supplies its own explicit IR. _ARRAY = re.compile(r'\b[A-Za-z_]\w*\[\d+\](?:\.[A-Za-z_]\w*)?|\bgas_\d+\.[A-Za-z_]\w*') _IDENTIFIER = re.compile(r'\b[A-Za-z_]\w*\b') _NUMBER = re.compile(r'(?> column & 1) for row in range(self.state_count)) conflicts = [set() for _ in range(self.state_count)] for entries in rows: for column in entries: conflicts[column].update(set(entries) - {column}) colors = {} while len(colors) < self.state_count: column = max((i for i in range(self.state_count) if i not in colors), key=lambda i: (len({colors[j] for j in conflicts[i] if j in colors}), len(conflicts[i]), -i)) used = {colors[j] for j in conflicts[column] if j in colors} color = 0 while color in used: color += 1 colors[column] = color ordered = tuple(colors[i] for i in range(self.state_count)) for entries in rows: if len({ordered[column] for column in entries}) != len(entries): raise AssertionError('Jacobian coloring contains a row conflict') return JacobianStructure(self.state_count, rows, ordered)