"""Transparent five-axis tensor carrier and 32-coefficient mixed-jet algebra. This is a reference experiment, not an audio-compression breakthrough. WAV stores compressed bytes as a PCM data payload; custom psri metadata makes decoding exact. """ from __future__ import annotations import argparse from dataclasses import dataclass import hashlib import itertools import json import math from pathlib import Path import platform import struct import time import zlib import numpy as np DIM = 5 SIZE = 1 << DIM @dataclass class Jet5: c: np.ndarray def __post_init__(self): self.c = np.asarray(self.c, dtype=np.float64) if self.c.shape != (SIZE,): raise ValueError('Jet5 requires exactly 32 real coefficients') @classmethod def scalar(cls, x: float): c = np.zeros(SIZE); c[0] = x return cls(c) @classmethod def variable(cls, x: float, axis: int, speed: float = 1.0): if axis not in range(DIM): raise ValueError('axis must be 0,1,2,3,4') z = cls.scalar(x); z.c[1 << axis] = speed return z def __add__(self, other): if not isinstance(other, Jet5): other = Jet5.scalar(other) return Jet5(self.c + other.c) __radd__ = __add__ def __neg__(self): return Jet5(-self.c) def __sub__(self, other): return self + (-other if isinstance(other, Jet5) else -float(other)) def __mul__(self, other): if not isinstance(other, Jet5): return Jet5(self.c * float(other)) out = np.zeros(SIZE) for a in range(SIZE): remaining = (SIZE - 1) ^ a b = remaining while True: out[a | b] += self.c[a] * other.c[b] if b == 0: break b = (b - 1) & remaining return Jet5(out) __rmul__ = __mul__ def exp(self): n = self.c.copy(); n[0] = 0 n = Jet5(n) term = Jet5.scalar(1.0) out = Jet5.scalar(1.0) for k in range(1, DIM + 1): term = term * n * (1.0 / k) out = out + term return out * math.exp(self.c[0]) def inverse(self): if self.c[0] == 0: raise ZeroDivisionError('Jet inverse needs nonzero scalar part') q = self.c.copy(); q[0] = 0 q = Jet5(-q / self.c[0]) term = Jet5.scalar(1); out = Jet5.scalar(1) for _ in range(1, DIM + 1): term = term * q; out = out + term return out * (1.0 / self.c[0]) def _chunk(tag: bytes, body: bytes) -> bytes: if len(tag) != 4: raise ValueError('RIFF chunk identifier must be four bytes') return tag + struct.pack(' dict: """Losslessly encode an integer tensor; any prior float quantization is separate.""" tensor = np.asarray(tensor) if tensor.ndim != 5 or tensor.dtype.kind != 'i' or tensor.dtype.itemsize != 2: raise ValueError('codec domain is a five-dimensional signed-int16 tensor') if not (1 <= sample_rate <= 1_000_000): raise ValueError('unsupported sample rate') tensor = np.ascontiguousarray(tensor, dtype=' dict: blob = Path(path).read_bytes() if blob[:4] != b'RIFF' or blob[8:12] != b'WAVE': raise ValueError('not a RIFF WAVE file') if len(blob) != 8 + struct.unpack(' len(blob): raise ValueError('truncated chunk header') tag = blob[cursor:cursor+4]; length = struct.unpack(' len(blob): raise ValueError('truncated chunk body') if tag in chunks: raise ValueError('duplicate chunk identifier') chunks[tag] = blob[start:stop] cursor = stop + length % 2 return chunks def decode_wav(path: Path) -> tuple[np.ndarray, dict]: chunks = read_chunks(path) meta = json.loads(chunks[b'psri']) if meta['format'] != 'phase-shift-carrier-v1' or meta['dtype'] != ' np.ndarray: """Periodic five-axis discrete heat semigroup, with anisotropic rates.""" x = np.asarray(field, dtype=float); rates = np.asarray(rates, dtype=float) if x.ndim != 5 or rates.shape != (5,) or np.any(rates < 0) or time_value < 0: raise ValueError('heat5 needs 5D input, five nonnegative rates and nonnegative time') eigenvalue = np.zeros(x.shape) for axis, n in enumerate(x.shape): shape = [1]*5; shape[axis] = n eigenvalue += rates[axis] * (4*np.sin(np.pi*np.fft.fftfreq(n))**2).reshape(shape) return np.fft.ifftn(np.fft.fftn(x) * np.exp(-time_value*eigenvalue)).real def finite_mixed(function, x: np.ndarray, axes: list[int], h: float) -> float: answer = 0.0 for signs in itertools.product([-1,1], repeat=len(axes)): q = x.copy() for axis, sign in zip(axes, signs): q[axis] += sign*h answer += np.prod(signs) * function(q) return answer/(2*h)**len(axes) def run_demo(directory: Path, seed: int = 640291) -> dict: directory = Path(directory); directory.mkdir(parents=True, exist_ok=True) rng = np.random.default_rng(seed) x = np.array([0.2, -0.3, 0.4, 0.1, -0.2]) a = np.array([0.4, -0.7, 0.8, 0.3, -0.5]) speeds = np.array([1., .5, 2., .75, 1.25]) variables = [Jet5.variable(x[i], i, speeds[i]) for i in range(5)] expr = sum((a[i]*variables[i] for i in range(5)), Jet5.scalar(0)).exp() product = Jet5.scalar(1) for v in variables: product = product*v expr = expr + product expected = np.zeros(32) for mask in range(32): selected = [i for i in range(5) if mask & (1<