"""
Test 4: Multiple attempts to recover SR from 4D Euclidean structure.

The naive reading (composing Euclidean rotation angles additively) failed:
  - Time dilation: YES (exact)
  - Velocity addition: NO
  - c-invariance: NO

Let's try several alternatives.

ATTEMPT 1: Wick rotation -- use IMAGINARY rotation angles.
  If theta -> i*eta (rapidity), then sin(theta) -> i*sinh(eta)
                                    cos(theta) -> cosh(eta)
  A "Euclidean rotation" with imaginary angle gives a hyperbolic rotation,
  which IS a Lorentz boost. Check that composition works.

ATTEMPT 2: Parameterize by rapidity (hyperbolic angle) directly.
  Instead of identifying 3D velocity with sin(theta), identify it with
  tanh(eta). Then eta composes additively (since rapidities add in SR),
  and we recover SR by construction. But is there a reason WHY rapidity
  is the natural additive parameter, coming from the geometry?

ATTEMPT 3: Redefine what "velocity" means in the Euclidean model.
  Instead of v = dx/ds (ambient arc length), try v = dx/du (rate of 3D
  motion relative to w4 motion, not ambient arc length).
  Under this definition:
    For unit 4-speed worldline: dx = sin(theta) ds, du = cos(theta) ds
    So dx/du = tan(theta), NOT sin(theta).
  This changes everything!

ATTEMPT 4: Test whether the one-directionality constraint + a specific
  composition rule can give SR.
"""

import numpy as np

c = 1.0

# ============================================================================
# ATTEMPT 1: Wick rotation -- rotation with imaginary angle = Lorentz boost
# ============================================================================
print("=" * 72)
print("ATTEMPT 1: Wick rotation (theta -> i*eta)")
print("=" * 72)
print()
print("Under theta -> i*eta:")
print("  sin(theta) -> sin(i*eta) = i*sinh(eta)")
print("  cos(theta) -> cos(i*eta) = cosh(eta)")
print()
print("Identify v/c = tanh(eta) (the relativistic rapidity-velocity relation).")
print("Check that composition of rapidities (rapidities add in SR) gives")
print("the SR velocity-addition formula.")
print()

def rapidity_from_v(v):
    return np.arctanh(v/c)

def v_from_rapidity(eta):
    return c * np.tanh(eta)

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

print(f"{'v':>6} {'w':>6} {'rap(v)':>10} {'rap(w)':>10} {'eta_sum':>10} {'v_from_sum':>12} {'SR direct':>12} {'match':>8}")
for v, w in [(0.1, 0.1), (0.5, 0.5), (0.8, 0.8), (0.9, 0.9), (0.99, 0.99), (0.99, 0.5)]:
    rv = rapidity_from_v(v)
    rw = rapidity_from_v(w)
    eta_sum = rv + rw
    v_sum = v_from_rapidity(eta_sum)
    v_sr = sr_compose(v, w)
    match = "YES" if abs(v_sum - v_sr) < 1e-10 else "NO"
    print(f"{v:>6.2f} {w:>6.2f} {rv:>10.4f} {rw:>10.4f} {eta_sum:>10.4f} {v_sum:>12.8f} {v_sr:>12.8f} {match:>8}")

print()
print("=> EXACT MATCH. Rapidities compose additively and recover SR exactly.")
print("   The question is: is there a geometric reason, within the 4D Euclidean")
print("   model, for why rapidity (arctanh(v/c)) should be the natural additive")
print("   parameter, rather than the geometric angle (arcsin(v/c))?")
print()


# ============================================================================
# ATTEMPT 2: Redefine v = dx/du (3D motion per unit w4 motion)
# ============================================================================
print("=" * 72)
print("ATTEMPT 2: Alternative velocity definition: v = dx/du")
print("=" * 72)
print()
print("If 'velocity' is defined as rate of 3D motion per unit w4 advance")
print("(NOT per unit ambient arc length), then v = tan(theta) where theta")
print("is the geometric angle from w4.")
print()
print("But tan(theta) is unbounded -- massive particles can have arbitrarily")
print("large v under this definition. That does not match SR where v < c.")
print()

print(f"{'theta (deg)':>12} {'sin(theta) = v_ambient':>22} {'tan(theta) = dx/du':>22}")
for theta_deg in [0, 10, 30, 45, 60, 80, 89]:
    theta = np.radians(theta_deg)
    print(f"{theta_deg:>12} {np.sin(theta):>22.4f} {np.tan(theta):>22.4f}")

print()
print("=> tan(theta) is unbounded, so this cannot be 'speed as measured with")
print("   c as the limit'. BUT note: if we interpret dx/du as rapidity-like,")
print("   it IS unbounded in SR -- rapidity itself goes from -inf to +inf.")
print("   So dx/du might be the rapidity, not the velocity.")
print()
print("   Check: is tan(theta) = sinh(eta) when sin(theta) = tanh(eta)?")
print()

# If sin(theta) = tanh(eta), then cos(theta) = sqrt(1 - tanh^2(eta)) = 1/cosh(eta)
# So tan(theta) = sin(theta)/cos(theta) = tanh(eta)*cosh(eta) = sinh(eta). YES.
print("   Verified: if sin(theta) = tanh(eta), then tan(theta) = sinh(eta).")
print()
print("   So in the Euclidean model:")
print("     'ambient-velocity'     v_amb   = sin(theta) = dx/ds       [bounded by c, SR-correct]")
print("     'per-w4 rate'          r       = tan(theta) = dx/du       [unbounded = rapidity sinh(eta)]")
print("     Lorentz factor         gamma   = 1/cos(theta) = sec(theta) [= cosh(eta), SR-correct]")
print()


# ============================================================================
# ATTEMPT 3: Compose using tan(theta), i.e. dx/du
# ============================================================================
print("=" * 72)
print("ATTEMPT 3: Composition law using dx/du = tan(theta)")
print("=" * 72)
print()
print("If we call r = tan(theta) the 'per-w4 rate' (= sinh(eta)),")
print("does r compose additively? (eta composes additively; does r?)")
print()
print("sinh(eta1 + eta2) = sinh(eta1)cosh(eta2) + cosh(eta1)sinh(eta2)")
print("                  = r1 * sqrt(1+r2^2) + sqrt(1+r1^2) * r2")
print()
print("This is NOT simple addition, but it's a well-defined composition.")
print("Compare to actual r-composition corresponding to SR:")
print()

print(f"{'v1':>5} {'v2':>5} {'r1':>7} {'r2':>7} {'r1+r2 (naive)':>14} {'r_SR':>10} {'match?':>8}")
for v1, v2 in [(0.3, 0.4), (0.5, 0.5), (0.8, 0.6), (0.9, 0.9), (0.99, 0.99)]:
    r1 = v1 / np.sqrt(1 - v1**2)  # = sinh(eta1) = tan(theta1)
    r2 = v2 / np.sqrt(1 - v2**2)
    naive_sum = r1 + r2
    # SR: v_sr = (v1+v2)/(1+v1*v2). Then r_SR = v_sr / sqrt(1 - v_sr^2)
    v_sr = (v1 + v2) / (1 + v1*v2)
    r_sr = v_sr / np.sqrt(1 - v_sr**2)
    # Actual sinh addition
    sinh_sum = r1 * np.sqrt(1 + r2**2) + np.sqrt(1 + r1**2) * r2
    match = "YES" if abs(sinh_sum - r_sr) < 1e-10 else "NO"
    print(f"{v1:>5.2f} {v2:>5.2f} {r1:>7.3f} {r2:>7.3f} {naive_sum:>14.4f} {r_sr:>10.4f} (sinh-add: {sinh_sum:.4f}, {match})")

print()
print("=> r values do not add simply, but they compose via the sinh-addition")
print("   formula, which IS what SR requires.")
print()


# ============================================================================
# ATTEMPT 4: The key insight -- what DOES add additively in the Euclidean model?
# ============================================================================
print("=" * 72)
print("ATTEMPT 4: What composes additively in the 4D Euclidean picture?")
print("=" * 72)
print()
print("Euclidean rotation angles theta add trivially: rotations compose.")
print("  If worldline A makes angle theta_A with w4,")
print("  and worldline B makes angle theta_B (relative to A, in the plane),")
print("  then relative to the ambient frame, B makes angle theta_A + theta_B.")
print()
print("But the relationship between 'velocity' and 'theta' is the key choice:")
print()
print("  Choice 1: v = c*sin(theta)  -> ambient 3D speed, bounded by c")
print("            Composition: v_compose = c*sin(theta_A + theta_B)")
print("            NOT SR. Fails c-invariance, fails at high v.")
print()
print("  Choice 2: v = c*tanh(theta) -> requires theta = rapidity, which is")
print("            NOT a Euclidean angle (angles are bounded by 2*pi;")
print("            rapidities range over all reals).")
print()
print("  Choice 3: 'Mixed'. Euclidean angles add, but the observable velocity")
print("            is some other function of theta that happens to reproduce SR.")
print()
print("For the geometric angle to behave as a rapidity, we need:")
print("  - No bound at theta = pi/2 (rapidity ranges over all reals)")
print("  - sin(theta) -> tanh(theta) as functional relationship")
print()
print("Neither of these is a Euclidean-rotation property.")
print("CONCLUSION: The Euclidean-rotation angle and the SR rapidity are")
print("            structurally DIFFERENT objects. The framework cannot get")
print("            SR from 'Euclidean rotations compose additively'.")
print()
print("Can it get SR from something else in the geometry? See Attempt 5.")


# ============================================================================
# ATTEMPT 5: Is there a hyperbolic structure hiding in the constraint?
# ============================================================================
print()
print("=" * 72)
print("ATTEMPT 5: Is there a hyperbolic structure hiding in the one-directionality?")
print("=" * 72)
print()
print("The Euclidean constraint |v|^2 + u^2 = c^2 is a SPHERE in 4-velocity space.")
print("SR requires the constraint on 4-velocity to be a HYPERBOLOID:")
print("  u^2 - |v|^2 = c^2,  u > 0")
print()
print("These are different manifolds:")
print("  Sphere S^3:    {(v,u) : v^2 + u^2 = c^2}        -- compact")
print("  Hyperboloid:   {(v,u) : u^2 - v^2 = c^2, u>0}   -- non-compact")
print()
print("Under analytic continuation u -> iU, the sphere becomes a hyperboloid:")
print("  |v|^2 + (iU)^2 = c^2  =>  |v|^2 - U^2 = c^2  (opposite sign)")
print()
print("So the sphere and hyperboloid ARE related by Wick rotation of u, not")
print("of theta. But this means the 4D ambient space is NOT purely Euclidean")
print("under this correspondence -- the w4 coordinate has picked up a factor i.")
print()
print("The framework's claim that the ambient geometry is 'four spatial")
print("dimensions of equal geometric status' is what breaks this.")
print("If w4 is genuinely spacelike, the constraint is |v|^2 + u^2 = c^2")
print("(sphere), and SR is NOT recovered.")
print("If w4 is 'spacelike but its motion is Wick-rotated', the constraint")
print("becomes the hyperboloid, and SR IS recovered -- but at the cost of")
print("w4 not being a true Euclidean direction.")
print()

# Let's demonstrate this numerically.
print("Numerical demo: same physics, two ways to parameterize.")
print()
print("Euclidean sphere S^3: u = sqrt(c^2 - v^2)")
print(f"{'v':>6} {'u (sphere)':>12} {'gamma (sphere)':>14} {'gamma_SR':>10}")
for v in [0.1, 0.5, 0.9, 0.99]:
    u_sphere = np.sqrt(c**2 - v**2)  # bounded by c
    gamma_sphere = c / u_sphere      # 1 / sqrt(1-v^2), equals SR gamma
    gamma_sr = 1 / np.sqrt(1 - v**2)
    print(f"{v:>6.2f} {u_sphere:>12.4f} {gamma_sphere:>14.4f} {gamma_sr:>10.4f}")
print()
print("The GAMMA FACTOR matches between sphere and SR. But the COMPOSITION")
print("of boosts does NOT, because spheres don't compose like hyperboloids.")
