""" logos/network.py - The Prime Topology Network """ import numpy as np import sympy from collections import Counter from typing import Dict, List, Tuple class PrimeNetwork: """ Represents the Radial Prime Topology as an instantiated graph object. Used for both visualization and signal routing validation. """ def __init__(self, max_integer: int = 1000): self.max_integer = max_integer self.positions: Dict[int, Tuple[float, float]] = {} self.gpf_map: Dict[int, int] = {} self.prime_counts = Counter() self.primes: List[int] = [] self.composites: List[int] = [] # Build the network immediately self._build() def _get_gpf(self, n: int) -> int: """Returns the Greatest Prime Factor.""" if n <= 1: return 1 i = 2 while i * i <= n: if n % i: i += 1 else: n //= i return n def _build(self): """Constructs the network nodes and edges.""" for n in range(1, self.max_integer + 1): # Radial position based on Mod 10 # 1, 3, 7, 9 are the Prime Dissolution Vectors angle = np.pi/2 - (2 * np.pi * (n % 10)) / 10 # Clockwise from Top radius = n self.positions[n] = (radius * np.cos(angle), radius * np.sin(angle)) if n > 1: if sympy.isprime(n): self.primes.append(n) else: self.composites.append(n) # Establish Gravity Link (Composite -> GPF) gpf = self._get_gpf(n) self.gpf_map[n] = gpf self.prime_counts[gpf] += 1 elif n == 1: self.primes.append(1) # Treat 1 as special unitary root def get_route(self, value: int) -> List[int]: """ Trace the dissolution path of a value back to its Prime Root. Visualization of 'Heat Dissolution'. Returns list: [value, factor, ..., gpf, ..., prime] """ path = [value] curr = value # Simple dissolution: jump to GPF repeatedly until prime while curr not in self.primes and curr > 1: if curr in self.gpf_map: next_hop = self.gpf_map[curr] if next_hop == curr: break # Should not happen if logic is correct path.append(next_hop) curr = next_hop else: # Fallback calculation if outside pre-calc range gpf = self._get_gpf(curr) path.append(gpf) curr = gpf return path def validate_wave(self, heat_code: int) -> bool: """ Check if a subset of the wave aligns with valid network topology. (Placeholder for advanced routing logic) """ # Example: Valid if heat_code modulo maps to a prime vector residue = heat_code % 10 return residue in [1, 3, 7, 9] # ========================================== # SINGLETON INSTANCE (Self-Contained Network) # ========================================== # Pre-calc network to avoid re-instantiation overhead per request SHARED_NETWORK = PrimeNetwork(max_integer=2000)