Download code/exact_inverse.py from PureOne/eve-floquet-defect-rigidity: direct link, hf CLI and curl.
- Browser
- Download file 4.27 kB
-
https://huggingface.co/datasets/PureOne/eve-floquet-defect-rigidity/resolve/main/code/exact_inverse.py
- Command line
-
hf download hf://datasets/PureOne/eve-floquet-defect-rigidity/code/exact_inverse.py
-
curl -L -o exact_inverse.py https://huggingface.co/datasets/PureOne/eve-floquet-defect-rigidity/resolve/main/code/exact_inverse.py
4.27 kB
| """Exact rational-exponent examples of finite-jet recovery and layer peeling. | |
| An exponential polynomial is {exponent: coefficient}, interpreted as | |
| sum(coefficient*exp(exponent*z)). This test implementation uses exact SymPy | |
| rational arithmetic. The manuscript's uniqueness theorem allows arbitrary | |
| positive real layer parameters; this implementation deliberately does not | |
| claim robust inversion of noisy data or arbitrary algebraic root finding. | |
| """ | |
| from __future__ import annotations | |
| import sympy as sp | |
| def clean(f): | |
| return {sp.Rational(k): sp.cancel(v) for k, v in f.items() if sp.cancel(v) != 0} | |
| def add(f, g, scale=1): | |
| out = dict(f) | |
| for x, v in g.items(): | |
| out[x] = out.get(x, 0) + scale * v | |
| return clean(out) | |
| def mul(f, g): | |
| out = {} | |
| for x, a in f.items(): | |
| for y, b in g.items(): | |
| out[x+y] = out.get(x+y, 0) + a*b | |
| return clean(out) | |
| def scaled(f, a): | |
| return clean({x: a*v for x, v in f.items()}) | |
| def cell(c, tau): | |
| c, tau = sp.Rational(c), sp.Rational(tau) | |
| if c <= 0 or tau <= 0: | |
| raise ValueError('Layer speeds and durations must be positive.') | |
| ell = c*tau | |
| co = {ell: sp.Rational(1, 2), -ell: sp.Rational(1, 2)} | |
| si = {ell: sp.Rational(1, 2), -ell: sp.Rational(-1, 2)} | |
| return [[co, scaled(si, 1/c)], [scaled(si, c), co]] | |
| def matmul(A, B): | |
| return [[add(mul(A[i][0], B[0][j]), mul(A[i][1], B[1][j])) | |
| for j in range(2)] for i in range(2)] | |
| def layer_product(layers): | |
| H = [[{sp.Rational(0): sp.Rational(1)}, {}], | |
| [{}, {sp.Rational(0): sp.Rational(1)}]] | |
| for c, tau in layers: | |
| H = matmul(cell(c, tau), H) | |
| return H | |
| def moments(f, count): | |
| return [sp.cancel(sum(a*x**m for x, a in f.items())) for m in range(count)] | |
| def recover_exponential_polynomial(mu, max_terms): | |
| """Prony recovery from 2*max_terms exact moments; handles cancellations.""" | |
| if len(mu) < 2*max_terms: | |
| raise ValueError('At least 2*max_terms moments are required.') | |
| if all(v == 0 for v in mu[:2*max_terms]): | |
| return {} | |
| d = 0 | |
| for j in range(1, max_terms+1): | |
| Hj = sp.Matrix(j, j, lambda a, b: mu[a+b]) | |
| if Hj.det() != 0: | |
| d = j | |
| if not d: | |
| raise ValueError('The supplied moments are inconsistent with the model.') | |
| H = sp.Matrix(d, d, lambda a, b: mu[a+b]) | |
| coeffs = H.inv() * -sp.Matrix(mu[d:2*d]) | |
| x = sp.Symbol('x') | |
| poly = sp.Poly(x**d + sum(coeffs[j]*x**j for j in range(d)), x) | |
| roots = sp.roots(poly) | |
| if sum(roots.values()) != d or len(roots) != d or any(not r.is_Rational for r in roots): | |
| raise NotImplementedError('This exact demonstration handles distinct rational exponents.') | |
| nodes = sorted(roots) | |
| V = sp.Matrix(d, d, lambda a, b: nodes[b]**a) | |
| amplitudes = V.inv() * sp.Matrix(mu[:d]) | |
| f = clean(dict(zip(nodes, amplitudes))) | |
| if moments(f, 2*max_terms) != list(mu[:2*max_terms]): | |
| raise ValueError('Moment consistency check failed.') | |
| return f | |
| def peel_first_row(f, g, max_layers): | |
| """Recover ordered (c, tau) by leading-exponent cancellation, not limits.""" | |
| f, g = clean(f), clean(g) | |
| layers = [] | |
| for _ in range(max_layers): | |
| if f == {sp.Rational(0): sp.Rational(1)} and not g: | |
| return layers | |
| if not f or not g or max(f) != max(g): | |
| raise ValueError('First row is inconsistent with a positive finite-layer medium.') | |
| c = sp.cancel(f[max(f)] / g[max(g)]) | |
| minus, plus = add(f, g, -c), add(f, g, c) | |
| if not minus or not plus: | |
| raise ValueError('Invalid reflection numerator or denominator.') | |
| ell = sp.cancel((max(plus) - max(minus))/2) | |
| if c <= 0 or ell <= 0: | |
| raise ValueError('Nonpositive recovered speed or optical duration.') | |
| tau = sp.cancel(ell/c) | |
| co = {ell: sp.Rational(1, 2), -ell: sp.Rational(1, 2)} | |
| si = {ell: sp.Rational(1, 2), -ell: sp.Rational(-1, 2)} | |
| new_f = add(mul(f, co), mul(g, si), -c) | |
| new_g = add(mul(g, co), mul(f, si), -1/c) | |
| f, g = new_f, new_g | |
| layers.append((c, tau)) | |
| if f == {sp.Rational(0): sp.Rational(1)} and not g: | |
| return layers | |
| raise ValueError('Residual medium remains after max_layers peeling steps.') | |