File size: 3,413 Bytes
9755170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Polynomial-jet evaluation and local diagnostics for the v2 inverse bounds.

The generic projection theorem concerns almost every exact real matrix.
A sampled/rounded matrix returned here is NOT a global injectivity certificate.
Global certified rational selection is specified by the manuscript's CAD search;
that external quantifier-elimination procedure is not implemented here.
"""
from __future__ import annotations
import math
import numpy as np


def jets_from_layers(speeds_squared, durations, layer_bound: int | None = None) -> np.ndarray:
    """Return [M11^(2)(0),M12^(2)(0),M11^(4)(0),...] by polynomial convolution.

    q_j=c_j^2 and tau_j>0. Complex perturbations are permitted for derivative
    checks. This floating-point implementation limits the layer bound to five;
    the theorem itself does not have that restriction.
    """
    q=np.asarray(speeds_squared);t=np.asarray(durations)
    if q.ndim!=1 or t.ndim!=1 or len(q)!=len(t) or not len(q):
        raise ValueError('Provide equally sized nonempty one-dimensional layer arrays.')
    if not np.all(np.isfinite(q)) or not np.all(np.isfinite(t)):
        raise ValueError('All layer parameters must be finite.')
    if np.any(np.real(q)<=0) or np.any(np.real(t)<=0):
        raise ValueError('Layer speeds squared and durations must have positive real parts.')
    r=len(q) if layer_bound is None else int(layer_bound)
    if not len(q)<=r<=5:
        raise ValueError('This float evaluator requires actual_layers <= layer_bound <= 5.')
    hmax=2**r-1
    dtype=np.result_type(q.dtype,t.dtype,np.float64)
    product=np.zeros((hmax+1,2,2),dtype=dtype);product[0]=np.eye(2)
    for speed2,tau in zip(q,t):
        layer=np.zeros_like(product)
        for h in range(hmax+1):
            co=(-speed2)**h*tau**(2*h)/float(math.factorial(2*h))
            up=(-speed2)**h*tau**(2*h+1)/float(math.factorial(2*h+1))
            layer[h,0,0]=co;layer[h,1,1]=co;layer[h,0,1]=up
            if h:layer[h,1,0]=-speed2*layer[h-1,0,1]
        new=np.zeros_like(product)
        for h in range(hmax+1):
            for k in range(h+1):new[h]+=layer[k]@product[h-k]
        product=new
    out=[]
    for h in range(1,hmax+1):
        fac=float(math.factorial(2*h))
        out.extend([fac*product[h,0,0],fac*product[h,0,1]])
    return np.asarray(out,dtype=dtype)


def draw_projection(layer_bound:int, *, known_resource:bool=False, seed:int=260925)->np.ndarray:
    """Draw a finite-precision test matrix, without claiming global certification."""
    if not 1<=layer_bound<=5:raise ValueError('Demo layer bound must lie in 1..5.')
    K=2**(layer_bound+1)-2
    if layer_bound==1:
        return np.empty((0,K)) if known_resource else np.array([[1.,0.]])
    m=min(K,4*layer_bound-(3 if known_resource else 1))
    if m==K:return np.eye(K)
    rng=np.random.default_rng(seed)
    P=rng.normal(size=(m,K));P/=np.linalg.norm(P,axis=1,keepdims=True)
    return P


def complex_step_jacobian(function, parameters, step:float=1e-25)->np.ndarray:
    """Derivative diagnostic for real polynomial/rational maps with no branching."""
    x=np.asarray(parameters,dtype=float)
    if x.ndim!=1 or step<=0:raise ValueError('Vector parameters and positive step required.')
    columns=[]
    for j in range(len(x)):
        z=x.astype(complex);z[j]+=1j*step
        columns.append(np.imag(np.asarray(function(z)))/step)
    return np.column_stack(columns)