"""
Test 3: What is the Euclidean rotation composition rule, and where does it
         agree/disagree with SR?

If we compose 4D Euclidean rotations additively in angle:
  v_compose = c * sin(arcsin(v/c) + arcsin(w/c))
            = v*sqrt(1 - w^2/c^2) + w*sqrt(1 - v^2/c^2)

SR:
  v_SR = (v + w) / (1 + v*w/c^2)

Let's look at:
  (a) low-velocity limit of both
  (b) high-velocity limit
  (c) do either form a group under composition?
  (d) do either preserve the speed-of-light limit?
"""

import numpy as np

c = 1.0

def eucl_compose(v, w):
    """Euclidean rotation composition."""
    return v * np.sqrt(1 - w**2/c**2) + w * np.sqrt(1 - v**2/c**2)

def sr_compose(v, w):
    return (v + w) / (1 + v*w/c**2)

# --- (a) Low-velocity limit ---
print("=" * 70)
print("(a) Low-velocity limit")
print("=" * 70)
print(f"{'v=w':>8} {'Eucl':>12} {'SR':>12} {'Galileo':>12} {'Eucl-Gal':>12} {'SR-Gal':>12}")
for v in [1e-4, 1e-3, 1e-2, 1e-1]:
    e = eucl_compose(v, v)
    s = sr_compose(v, v)
    g = 2*v
    print(f"{v:>8.1e} {e:>12.8f} {s:>12.8f} {g:>12.8f} {e-g:>+12.2e} {s-g:>+12.2e}")
print("\nBoth reduce to Galilean addition at low velocity. But the corrections differ:")
print("  Eucl - Galileo: O(v^3) with sign TBD")
print("  SR   - Galileo: O(v^3), specifically -2v^3/c^2 at v=w")
print()

# --- Let's do the Taylor expansion explicitly ---
print("Taylor expansions at v=w, small v:")
print("  Galileo:    2v")
print("  SR:         2v/(1+v^2) = 2v - 2v^3 + 2v^5 - ...  (c=1)")
print("  Euclidean:  2v*sqrt(1-v^2) = 2v - v^3 - v^5/4 - ...  (c=1)")
print()
print("So they agree at O(v) and differ at O(v^3):")
print("  SR: -2v^3, Eucl: -v^3. Eucl has HALF the relativistic correction.")
print()

# --- (b) High-velocity limit ---
print("=" * 70)
print("(b) High-velocity limit (both v,w close to c)")
print("=" * 70)
print(f"{'v':>6} {'w':>6} {'Eucl':>12} {'SR':>12}")
for v, w in [(0.9, 0.9), (0.99, 0.99), (0.999, 0.999), (0.9999, 0.9999)]:
    e = eucl_compose(v, w)
    s = sr_compose(v, w)
    print(f"{v:>6.4f} {w:>6.4f} {e:>12.8f} {s:>12.8f}")
print()
print("SR -> 1 (c) as both v,w -> c. Euclidean does NOT approach c.")
print("This is because Euclidean rotations have period 2*pi, not infinite range.")
print("When arcsin(v/c) + arcsin(w/c) > pi/2, the formula breaks: sin exceeds 1")
print("in intent but numerical sin gives the wrong branch.")
print()

# --- (c) Group structure / associativity ---
print("=" * 70)
print("(c) Group structure: is eucl_compose associative?")
print("=" * 70)
v1, v2, v3 = 0.3, 0.4, 0.5
left = eucl_compose(eucl_compose(v1, v2), v3)
right = eucl_compose(v1, eucl_compose(v2, v3))
print(f"eucl_compose(eucl_compose({v1},{v2}),{v3})  = {left:.10f}")
print(f"eucl_compose({v1},eucl_compose({v2},{v3}))  = {right:.10f}")
print(f"Difference: {left - right:+.2e}")
print()
print("If non-zero, Euclidean rotation in (x, w4) is NOT associative as a")
print("velocity composition, because angles add but sin(sum) is nonlinear.")
print()

# SR check
print("SR associativity check:")
left_sr = sr_compose(sr_compose(v1, v2), v3)
right_sr = sr_compose(v1, sr_compose(v2, v3))
print(f"sr_compose(sr_compose({v1},{v2}),{v3})  = {left_sr:.10f}")
print(f"sr_compose({v1},sr_compose({v2},{v3}))  = {right_sr:.10f}")
print(f"Difference: {left_sr - right_sr:+.2e}")
print()

# --- (d) Speed-of-light limit preservation ---
print("=" * 70)
print("(d) Does eucl_compose preserve c as a limit?")
print("=" * 70)
print(f"{'v':>6} {'w':>6} {'eucl':>12} {'sr':>12}")
# Boost a photon
for v in [0.1, 0.5, 0.9, 0.99]:
    w = c  # photon
    # The formula breaks for w=c strictly, try it anyway
    e = eucl_compose(v, w)
    s = sr_compose(v, w)
    print(f"{v:>6.3f} {w:>6.3f} {e:>12.8f} {s:>12.8f}")
print()
print("SR: boosting a photon (w=c) from any frame still gives c. (Invariance of c.)")
print("Euclidean: boosting w=c gives v*0 + 1*sqrt(1-v^2) = sqrt(1-v^2), which is")
print("           LESS than c. The speed of light is NOT invariant under Euclidean")
print("           rotations in (x, w4).")
print()
print("=" * 70)
print("CONCLUSION")
print("=" * 70)
print("""
The Euclidean unit-4-speed model with additive rotation composition:
  - RECOVERS time dilation magnitude (sqrt(1-v^2/c^2)) exactly when w4
    component is identified with proper time and ambient path length
    is treated as 'absolute time'.
  - DOES NOT recover SR velocity addition. The composition is approximately
    relativistic at low velocities (agreeing at O(v) but differing at O(v^3))
    but diverges from SR at high velocities.
  - DOES NOT preserve the invariance of c under boosts. A photon boosted
    by a Euclidean rotation appears slower than c in the new frame.
  - Is not even associative as a velocity composition rule (though
    rotation composition itself is), because taking sin(theta) is nonlinear.

This is the Mansouri-Sexl 'preferred-frame test theory' situation:
  time dilation alone is compatible with a Euclidean ether-like picture
  with a preferred slicing, but the full Lorentz structure (invariance
  of c, relativistic velocity addition) is NOT recovered by simply
  composing Euclidean rotations.

What this means for the paper:
  The naive reading -- 'Lorentz transforms are just 4D Euclidean rotations
  restricted by one-directional evolution' -- does not work. Additional
  structure is needed to close the gap between Euclidean rotation
  composition and the Lorentz group.
""")
