custom
code
sovereign-compute
File size: 4,812 Bytes
e92f76f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
"""
lw_lgm.py — Latent-to-Waveform Linear Geometric Map (Reference Implementation)

Maps a latent vector z ∈ ℝ^d to an analog waveform x(t) ∈ C^0(ℝ)
using a linear expansion in a fixed dictionary of geometrically
transformed atoms (affine group acting on a mother waveform).

Usage:
    python lw_lgm.py

Output:
    First 10 samples of the generated waveform.
"""

from __future__ import annotations

import numpy as np
from typing import Tuple


def mother_gaussian(t: np.ndarray, sigma0: float) -> np.ndarray:
    """Normalized Gaussian mother waveform: φ(t) = (1/(2πσ₀²)^{1/4}) · exp(-t²/(2σ₀²))"""
    norm = 1.0 / (2.0 * np.pi * sigma0**2) ** 0.25
    return norm * np.exp(-0.5 * t**2 / (sigma0**2))


def build_dictionary(
    sigma0: float,
    a_min: float,
    a_max: float,
    b_min: float,
    b_max: float,
    m: int,
    t_start: float,
    t_end: float,
    dt: float,
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Build the dictionary matrix Ψ ∈ ℝ^{N×m} from an affine group action.

    Returns:
        psi: Dictionary matrix of shape (N, m)
        t: Time axis of length N
    """
    t = np.arange(t_start, t_end, dt)
    n = len(t)
    psi = np.zeros((n, m))

    log_a_min = np.log(a_min)
    log_a_max = np.log(a_max)
    log_a_step = (log_a_max - log_a_min) / (m // 2)

    for i in range(m):
        # Logarithmic dilation grid
        if i < m // 2:
            a = np.exp(log_a_min + i * log_a_step)
        else:
            a = -np.exp(log_a_min + (m - 1 - i) * log_a_step)

        # Uniform translation
        b = b_min + i * (b_max - b_min) / (m - 1)

        # Precompute 1/√|a|
        scale = 1.0 / np.sqrt(np.abs(a))

        # Fill column i
        arg = (t - b) / a
        phi_val = mother_gaussian(arg, sigma0)
        psi[:, i] = scale * phi_val

    return psi, t


def latent_to_waveform(
    z: np.ndarray,
    W: np.ndarray,
    psi: np.ndarray,
) -> np.ndarray:
    """
    Map a latent vector z to waveform samples x = Ψ(Wz).

    Args:
        z: Latent vector of length d
        W: Fixed matrix of shape (m, d), or identity if d == m
        psi: Dictionary matrix of shape (N, m)

    Returns:
        x: Output waveform samples of length N
    """
    c = W @ z if W.shape[1] == z.shape[0] else z
    return psi @ c


def test_linearity():
    """Verify L(αz₁ + βz₂) = αL(z₁) + βL(z₂)"""
    sigma0 = 1.0
    a_min, a_max = 0.5, 2.0
    b_min, b_max = -5.0, 5.0
    m = 32
    t_start, t_end, dt = -10.0, 10.0, 0.1

    psi, _ = build_dictionary(sigma0, a_min, a_max, b_min, b_max, m, t_start, t_end, dt)
    W = np.eye(m)

    z1 = np.random.uniform(-1.0, 1.0, m)
    z2 = np.random.uniform(-1.0, 1.0, m)

    alpha, beta = 2.5, -1.3

    lhs = latent_to_waveform(alpha * z1 + beta * z2, W, psi)
    rhs = alpha * latent_to_waveform(z1, W, psi) + beta * latent_to_waveform(z2, W, psi)

    diff = np.sum(np.abs(lhs - rhs))
    assert diff < 1e-10, f"Linearity test failed: diff = {diff}"
    print(f"✓ Linearity test passed (diff = {diff:.2e})")


def test_energy_bounds():
    """Verify energy ratio is bounded"""
    sigma0 = 1.0
    a_min, a_max = 0.5, 2.0
    b_min, b_max = -5.0, 5.0
    m = 64
    t_start, t_end, dt = -10.0, 10.0, 0.01

    psi, _ = build_dictionary(sigma0, a_min, a_max, b_min, b_max, m, t_start, t_end, dt)
    W = np.eye(m)

    z = np.random.uniform(-1.0, 1.0, m)
    x = latent_to_waveform(z, W, psi)

    energy_x = np.sum(x**2) * dt
    energy_z = np.sum(z**2)

    ratio = energy_x / energy_z
    assert 0 < ratio < np.inf, f"Energy ratio invalid: {ratio}"
    print(f"✓ Energy bounds test passed (ratio = {ratio:.4f})")


if __name__ == "__main__":
    print("LW-LGM: Latent-to-Waveform Linear Geometric Map (Python Reference)")
    print("=" * 70)

    # Parameters
    sigma0 = 1.0
    a_min, a_max = 0.5, 2.0
    b_min, b_max = -5.0, 5.0
    m = 64
    t_start, t_end, dt = -10.0, 10.0, 0.01

    print(f"Parameters:")
    print(f"  σ₀ = {sigma0}")
    print(f"  a ∈ [{a_min}, {a_max}]")
    print(f"  b ∈ [{b_min}, {b_max}]")
    print(f"  m = {m} atoms")
    print(f"  t ∈ [{t_start}, {t_end}] dt={dt}")
    print()

    # Build dictionary
    psi, t = build_dictionary(sigma0, a_min, a_max, b_min, b_max, m, t_start, t_end, dt)
    print(f"Dictionary Ψ: {psi.shape}")

    # Identity mapping
    W = np.eye(m)

    # Random latent vector
    z = np.random.uniform(-1.0, 1.0, m)
    print(f"Latent z: {z.shape}")

    # Generate waveform
    x = latent_to_waveform(z, W, psi)
    print(f"Output x: {x.shape}")
    print(f"x[0:10] = {x[:10]}")

    energy = np.sum(x**2) * dt
    print(f"Signal energy: {energy:.6f}")
    print()

    # Run tests
    test_linearity()
    test_energy_bounds()
    print()
    print("All tests passed!")