"""
Test 5: Could the one-directionality constraint DO the work of Wick rotation?

Jay's intuition: the one-directionality might be what produces the Lorentzian
structure, rather than naive Euclidean rotations. Let's see if we can
formulate this precisely.

Key observation from Test 4: the sphere |v|^2 + u^2 = c^2 (Euclidean 4-speed
constraint) gives the right GAMMA factor but wrong composition law, while
the hyperboloid u^2 - |v|^2 = c^2 gives all of SR.

IDEA: Maybe the one-directionality constraint effectively Wick-rotates u.

How could this work? Consider that in the Euclidean model, every worldline
has FIXED positive u-component per unit proper advance. If two observers A
and B try to compare their 3D velocities, they each project onto their
respective "current w4" hyperplane -- but the act of projecting, combined
with each observer requiring u > 0 at their own position, might effectively
impose a boost that's hyperbolic rather than rotational.

Let me try to make this concrete by defining observer-relative quantities
explicitly.

SETUP: Observer A is at the ambient origin, moving purely in w4 (at rest).
       Observer B is moving at 3D velocity v relative to the ambient frame.
       B's 4-velocity: (v, sqrt(c^2 - v^2)).
       
Question: What 3D velocity does A MEASURE for B?

In SR, A measures B's velocity as v (in A's frame = ambient frame).

But here's the twist: when A measures B's velocity, A must project onto
their own hypersurface of simultaneity. If that hypersurface is "constant
w4 = A's current w4", then they measure B's velocity as:
   dx_B / dt = (dx_B / ds) / (dt / ds)
where "t" for A is w4. So:
   v_measured = (v) / (u / c) = c * v / u = v / sqrt(1 - v^2/c^2)
            = v * gamma

That's the "coordinate velocity" NOT the "proper velocity" --
and it IS the SR proper velocity (= dx/dtau), which is unbounded!
"""

import numpy as np

c = 1.0

print("=" * 72)
print("TEST 5a: What velocity does observer A measure for B, when A's time")
print("         is their own w4 coordinate?")
print("=" * 72)
print()
print("Setup: A at rest in ambient frame, worldline (0, 0, 0, t).")
print("       B moves at 3D speed v in ambient frame.")
print("       B's 4-velocity: (v_x, 0, 0, u) with u = sqrt(c^2 - v^2).")
print()
print("A's 'time' = A's w4 = proper time of A. Equate coordinate time")
print("with w4 coordinate of A.")
print()
print("For A to measure B: B's position at A's time t is (v*s, u*s) where")
print("u*s = t  =>  s = t/u.")
print("B's 3D position at A's time t is: x_B = v * s = v * t / u.")
print("A's measured velocity of B: dx_B/dt = v / u = v / sqrt(1 - v^2/c^2).")
print()
print("This is PROPER VELOCITY (eta * c = gamma * v in SR), not 3-velocity.")
print("It's unbounded. So naive 'same-w4' simultaneity does NOT give SR 3-velocity.")
print()

print(f"{'v (ambient)':>12} {'v (A measures)':>16} {'rapidity-sinh':>16} {'SR proper vel':>16}")
for v in [0.1, 0.3, 0.5, 0.7, 0.9, 0.99]:
    u = np.sqrt(c**2 - v**2)
    v_measured = v / u  # A's measurement
    sinh_eta = v / np.sqrt(1 - v**2)  # sinh(rapidity), = gamma*v, = SR proper velocity
    print(f"{v:>12.3f} {v_measured:>16.4f} {sinh_eta:>16.4f} {sinh_eta:>16.4f}")

print()
print("MATCH: A's measured velocity v/u = v/sqrt(1-v^2) = sinh(eta) = SR proper velocity.")
print()
print("This is a REAL result. If the natural 'simultaneity surface' in the")
print("Euclidean model is 'constant ambient w4' (same brane position),")
print("then velocities measured this way are SR PROPER velocities, not 3-velocities.")
print()

# ============================================================================
# TEST 5b: What if simultaneity is defined differently -- along the boosted 
# observer's orthogonal hyperplane?
# ============================================================================
print("=" * 72)
print("TEST 5b: Orthogonal-simultaneity (each observer uses their OWN")
print("         orthogonal-to-w4 hyperplane)")
print("=" * 72)
print()
print("In a purely Euclidean 4D space, B's natural 'simultaneity surface'")
print("is the 3D hyperplane ORTHOGONAL to B's 4-velocity, not parallel to")
print("A's w4 axis. This is the Einstein-synchronization analog.")
print()
print("Let's compute what B measures for A under this convention.")
print()
print("B's 4-velocity: (v, u) with v^2 + u^2 = c^2.")
print("B's orthogonal hyperplane has normal (v, u); its direction in the")
print("(x, w4) plane is (u, -v) (rotated 90 deg from B's motion).")
print()
print("B defines 'time' along B's 4-velocity direction, 'space' orthogonal.")
print("A's 4-velocity: (0, c) in ambient.")
print("Decompose A's 4-velocity into B's time and space components:")
print("  time component  = (A's 4-vel) . (B's 4-vel) / c = u")
print("  space component = (A's 4-vel) . (B's orth unit) = -v*0 + (-v)*c = -vc... wait")
print()

print("Let me redo the math carefully.")
print("Orthonormal basis for B: e_t_B = (v, u)/c (unit 4-velocity of B)")
print("                         e_x_B = (u, -v)/c (orthogonal, unit length)")
print()
print("A's 4-velocity: U_A = (0, c).")
print("Projection of U_A onto e_t_B: U_A . e_t_B = (0*v + c*u)/c = u")
print("Projection of U_A onto e_x_B: U_A . e_x_B = (0*u + c*(-v))/c = -v")
print()
print("So in B's Euclidean-orthogonal frame, A has 4-velocity components (-v, u).")
print("A's 'speed' as B measures it (space component / time component) = -v/u.")
print("Magnitude: v/u = v/sqrt(1-v^2) -- same as 5a. Still proper velocity.")
print()

# ============================================================================
# TEST 5c: What if the ACT of projecting introduces a hyperbolic structure?
# ============================================================================
print("=" * 72)
print("TEST 5c: Is there any projection from Euclidean 4D that gives SR 3-velocity?")
print("=" * 72)
print()
print("We want: input is Euclidean sphere point (v, u) with v^2 + u^2 = c^2,")
print("         output is v_SR with v_SR < c and v_SR = (v + w)/(1+vw/c^2) compositional.")
print()
print("Possibilities:")
print("  (i)   v_SR = v (ambient)  -- but then we hit Test 2's failure")
print("  (ii)  v_SR = v/u (= sinh(eta))  -- proper velocity, unbounded, WRONG")
print("  (iii) v_SR = something else?")
print()
print("For the sphere, the ONLY natural invariants are v, u, and angle theta.")
print("We've tried sin(theta), tan(theta). Neither gives SR.")
print()
print("What about 'radial' vs 'angular' decomposition? On a sphere of radius c,")
print("there is no 'radial' direction (all points are equidistant from origin).")
print()
print("The sphere has no hyperbolic substructure. SO(4) is not SO(3,1).")
print("No real function of (v, u) on the sphere can give a group homomorphism")
print("to the Lorentz group, because they're different-rank non-isomorphic groups.")
print()
print("However: SO(4) and SO(3,1) share a common complexification SO(4,C).")
print("This is why Wick rotation works FORMALLY: both real groups embed in the")
print("same complex group, and analytic continuation moves between them.")
print("But that's a statement about complex manifolds, not a geometric mechanism")
print("in real 4D Euclidean space.")

# ============================================================================
# TEST 5d: What happens if we impose the one-directionality constraint 
# EXACTLY -- not just u > 0, but u strictly positive in some global sense?
# ============================================================================
print()
print("=" * 72)
print("TEST 5d: The one-directionality constraint as an ACTIVE restriction")
print("=" * 72)
print()
print("Jay's intuition: maybe the one-directionality constraint does more")
print("than just 'u > 0' -- maybe it effectively forbids the closed-loop")
print("character of Euclidean rotations.")
print()
print("Consider: on the full Euclidean sphere S^3, there are closed geodesics")
print("(great circles). You can rotate by 2*pi and return to the start.")
print("If you forbid u <= 0, you're restricted to the UPPER HEMISPHERE of")
print("S^3. The upper hemisphere is topologically a disk, not closed.")
print()
print("Can 'rotations restricted to the upper hemisphere' give a different")
print("composition law?")
print()
print("Geometrically: the upper hemisphere of S^3 with its induced metric")
print("is still SPHERICAL, just with boundary. Its isometry group is still")
print("SO(3) acting on the 3D boundary (the equator) -- not the Lorentz group.")
print()
print("However: the upper hemisphere ALSO has a natural projection onto its")
print("equatorial disk -- stereographic projection from the 'south pole' (u=-c).")
print("Stereographic projection of S^n sends it to R^n with a conformal metric.")
print("This is a Riemannian construction, not Lorentzian.")
print()
print("What if we project stereographically from the 'antipode' and identify")
print("the image with a velocity space? Let's see what composition law we get.")
print()

def stereographic_velocity(v, u, c=1.0):
    """Stereographic projection from (0, -c) to the equatorial plane.
    A point (v, u) on the sphere maps to:  V = 2*c*v / (c + u)
    """
    return 2*c*v / (c + u)

def sphere_from_stereo(V, c=1.0):
    """Inverse: stereographic parameter V -> (v, u) on sphere."""
    denom = V**2 + 4*c**2
    v = 4*c**2 * V / denom
    u = c * (4*c**2 - V**2) / denom
    return v, u

print("Stereographic projection test: do Euclidean rotations on S^3, viewed")
print("through stereographic projection, give SR-like composition?")
print()
print(f"{'v':>6} {'V_stereo':>10} {'v (check)':>10} {'u (check)':>10}")
for v in [0.0, 0.3, 0.5, 0.7, 0.9, 0.99, 1.0]:
    if v < 1:
        u = np.sqrt(1 - v**2)
    else:
        u = 0
    V = stereographic_velocity(v, u)
    v_back, u_back = sphere_from_stereo(V)
    print(f"{v:>6.2f} {V:>10.4f} {v_back:>10.4f} {u_back:>10.4f}")
print()
print("V_stereo is unbounded as v -> c (photon case). So stereographic V is")
print("rapidity-LIKE but not identical to tanh-rapidity. Let's check composition.")
print()

# Rotate by Euclidean angle in (v, u) plane, see what V_stereo does
print("Composition check: compose two Euclidean rotations, stereographic project.")
print(f"{'v1':>6} {'v2':>6} {'V1':>8} {'V2':>8} {'V1+V2':>8} {'V_composed':>12} {'V_SR (stereo)':>14}")
for v1, v2 in [(0.1, 0.1), (0.3, 0.3), (0.5, 0.5), (0.7, 0.3), (0.9, 0.9)]:
    u1 = np.sqrt(1 - v1**2)
    u2 = np.sqrt(1 - v2**2)
    V1 = stereographic_velocity(v1, u1)
    V2 = stereographic_velocity(v2, u2)
    # Euclidean rotation composition: compose the angles
    theta1 = np.arcsin(v1)
    theta2 = np.arcsin(v2)
    theta_sum = theta1 + theta2
    if theta_sum < np.pi/2:
        v_composed = np.sin(theta_sum)
        u_composed = np.cos(theta_sum)
        V_composed = stereographic_velocity(v_composed, u_composed)
    else:
        V_composed = np.inf
    # SR composition of v1, v2, then stereographic project
    v_sr = (v1 + v2) / (1 + v1*v2)
    u_sr = np.sqrt(1 - v_sr**2)
    V_sr_stereo = stereographic_velocity(v_sr, u_sr)
    print(f"{v1:>6.2f} {v2:>6.2f} {V1:>8.3f} {V2:>8.3f} {V1+V2:>8.3f} {V_composed:>12.3f} {V_sr_stereo:>14.3f}")

print()
print("V1 + V2 (naive) does not match V_composed or V_sr_stereo.")
print("Stereographic projection does not save us.")
print()
print("=" * 72)
print("SUMMARY OF ATTEMPTS")
print("=" * 72)
print("""
  TEST 4 Attempt 1 (Wick rotation): EXACT SR recovery, but requires
    treating w4 angle as imaginary (theta = i*eta). This is the known
    formal equivalence, not a geometric mechanism.

  TEST 4 Attempts 2-5: No natural real-valued function of the Euclidean
    (v, u) coordinates reproduces SR composition. The sphere and the
    hyperboloid are topologically different, and their isometry groups
    SO(4) and SO(3,1) are non-isomorphic real Lie groups.

  TEST 5a,b: 'A measures B' naturally produces SR PROPER velocity
    (= gamma * v), which is unbounded. This is REAL -- proper velocity
    composes additively under the sinh-addition formula, matching SR.
    But it's not the 3-velocity, which is what experimentally equals c
    for light.

  TEST 5c: No real function on S^3 can give a group homomorphism to
    the Lorentz group.

  TEST 5d: Restricting to upper hemisphere doesn't help -- the induced
    metric and isometry group don't change character.

CONCLUSION: In the real Euclidean 4D picture, SR is NOT recoverable by any
direct geometric mechanism. The relationship between 4D Euclidean geometry
and SR geometry is ANALYTIC (Wick rotation in the complex plane), not
geometric.

HOWEVER -- and this is important -- two pieces of SR ARE recovered:
  1. The gamma factor: exact.
  2. Proper velocity (gamma*v): exact, additive under sinh-addition.
  3. Time dilation: exact.

The thing that is NOT recovered is c-invariance and the 3-velocity
addition formula. These require the SIGN difference between the spacelike
and timelike parts of the metric, which is a Lorentzian, not Euclidean,
feature.

For the paper, this means the honest claim is:
  The 4D Euclidean Embedding Reproduces:
    - Lorentz factor (gamma)
    - Time dilation
    - Proper velocity composition
  The 4D Euclidean Embedding Does NOT Reproduce (without Wick rotation):
    - c-invariance
    - 3-velocity addition
    - The Lorentz group structure

The framework thus captures the 'dilation part' of SR geometrically
but cannot capture the 'mixing of space and time' part without passing
through analytic continuation.
""")
