"""
Test 1: Does the Euclidean unit-4-speed constraint reproduce SR kinematics?

Model assumption:
  Every worldline in 4D Euclidean space has fixed total 4-speed c.
  Decomposition: v_3D in visible directions, u along w4.
  Constraint: |v_3D|^2 + u^2 = c^2
  For photons: u = 0, |v_3D| = c.
  For massive particles: u > 0, |v_3D| < c.
  Proper time advance: dtau = (u/c) * ds, where ds is path length in 4D.

This is the Euclidean 4-speed constraint. The question is:
  (a) does it reproduce time dilation: dtau = dt / gamma?
  (b) does it reproduce relativistic velocity addition?

We set c = 1 throughout. One visible spatial direction x, plus w4.
Observers' worldlines are straight lines in 4D Euclidean space.
"""

import numpy as np

c = 1.0

def unit_4velocity(v_3d):
    """Given 3D velocity, return (v_3d, u) with u^2 = c^2 - v_3d^2.
    Requires |v_3d| < c for massive particles."""
    v2 = v_3d**2
    assert v2 < c**2, f"|v|={abs(v_3d)} exceeds c for massive particle"
    u = np.sqrt(c**2 - v2)
    return v_3d, u

# ----- TEST 1: Time dilation -----
# Observer A at rest: worldline is pure w4. Over proper time tau_A, A advances
#   by (0, tau_A * c) in (x, w4).
# Observer B moving at velocity v in x: worldline is (v*s, u*s) where s is
#   path length parameter. Proper time of B is tau_B = (u/c) * s.
#
# For the two observers to "meet" again (or for A to record the coordinate
# time t at which B has aged by tau_B), we need to define what "coordinate
# time" means.
#
# In SR, coordinate time for observer A is just A's own proper time, and
# time dilation says: dtau_B = dt_A / gamma = sqrt(1 - v^2/c^2) * dt_A.
#
# In our Euclidean model, if "time for A" is A's w4 position, and both A
# and B start at the origin, then at A's time t_A = c*s_A (with s_A = tau_A):
#   A is at position (0, t_A)
#   B is at (v*s_B, u*s_B) where u = sqrt(c^2 - v^2)
#
# For B's w4 to match A's w4 (i.e., B is at the same "now" from A's frame):
#   u * s_B = t_A  =>  s_B = t_A / u = t_A / sqrt(c^2 - v^2) = t_A / (c*sqrt(1 - v^2/c^2))
#
# B's proper time: tau_B = s_B = t_A / sqrt(c^2 - v^2)  (with c=1, tau_B = t_A / sqrt(1-v^2))
#
# This is NOT SR. SR says tau_B = t_A * sqrt(1 - v^2/c^2) = t_A * sqrt(1-v^2).
# The Euclidean model gives tau_B = t_A / sqrt(1-v^2) — time runs FASTER for B,
# not slower. That's the wrong sign of time dilation.
#
# Wait — let me re-examine. Maybe the identification of "proper time" with
# the w4 component (not path length) gives the right answer.

print("=" * 70)
print("TEST 1a: Identify proper time with w4 component of worldline")
print("=" * 70)

velocities = [0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 0.99]
t_A = 1.0  # A's proper time (= w4 position at event)

print(f"\nA at rest. A's time t_A = {t_A}")
print(f"{'v/c':>8} {'tau_B (Euclidean)':>20} {'tau_B (SR)':>15} {'gamma':>10}")
print(f"{'-'*8:>8} {'-'*20:>20} {'-'*15:>15} {'-'*10:>10}")

for v in velocities:
    # B's 4-velocity has |v_3d| = v, u = sqrt(c^2 - v^2)
    u = np.sqrt(c**2 - v**2)
    gamma = 1.0 / np.sqrt(1 - v**2/c**2)
    # Path length s_B such that B's w4 position = t_A
    # u * s_B = t_A  =>  s_B = t_A / u
    s_B = t_A / u
    # If proper time = w4 component, then tau_B = u * s_B = t_A
    #   That's trivial — just says they meet at the same w4, so same "proper time"
    #   by this definition.
    # If proper time = path length, then tau_B = s_B = t_A / u
    tau_B_w4 = u * s_B           # = t_A by construction
    tau_B_pathlength = s_B       # = t_A / u
    tau_B_SR = t_A / gamma       # SR: moving clock runs slow

    print(f"{v:>8.2f} {tau_B_pathlength:>20.6f} {tau_B_SR:>15.6f} {gamma:>10.4f}")

print("\nNote: tau_B by pathlength = t_A / sqrt(1-v^2) -- runs FASTER, wrong sign.")
print("      tau_B by SR           = t_A * sqrt(1-v^2) -- runs slower.")
print("      Ratio: pathlength/SR = gamma^2.\n")

# ----- TEST 1b: Try the other identification -----
# Maybe proper time should be identified with path length, and "A's time" with
# w4, and the worldline parameterization should be path length. Then:
#   A: position (0, tau_A).  A's w4 = A's proper time.
#   B: at parameter s, position (v*s, u*s).
#      B's proper time = s.
#      B's w4 = u*s.
# "Simultaneity from A's frame" = same w4 value.
# So at A's proper time t_A, B's proper time (matched by w4) is s = t_A/u.
# This is what we just did.

# Alternative identification: proper time = w4 component.
# Then B at parameter s has proper time u*s.
# And A at parameter s has proper time c*s = s (with c=1).
# For the two worldlines to reach "the same moment" (same value of the
# Euclidean parameter s), B has proper time u*s and A has proper time s.
# So tau_B = u * tau_A = sqrt(1-v^2) * tau_A.
# THIS gives the right sign. But now what is "coordinate time"?

print("=" * 70)
print("TEST 1b: Identify proper time with w4 component, common Euclidean")
print("         parameter s as 'absolute time'")
print("=" * 70)
print()
print("If the common Euclidean arc-length parameter s is the underlying")
print("'absolute time' (the brane's advance through the ambient space),")
print("and each worldline's proper time is its w4 component...")
print()
print(f"{'v/c':>8} {'tau_B (4D model)':>20} {'tau_B (SR pred)':>18} {'match?':>10}")
print(f"{'-'*8:>8} {'-'*20:>20} {'-'*18:>18} {'-'*10:>10}")

s = 1.0  # Common ambient parameter
for v in velocities:
    u = np.sqrt(c**2 - v**2)
    tau_A = c * s  # A at rest, all motion in w4
    tau_B = u * s  # B moving in x, u is w4 component
    # SR prediction: tau_B = tau_A / gamma = tau_A * sqrt(1-v^2)
    tau_B_SR = tau_A * np.sqrt(1 - v**2)
    match = "YES" if abs(tau_B - tau_B_SR) < 1e-10 else "NO"
    print(f"{v:>8.2f} {tau_B:>20.6f} {tau_B_SR:>18.6f} {match:>10}")

print()
print("=> Time dilation magnitude IS recovered under this identification.")
print("   But note: this requires an ambient 'absolute time' parameter s,")
print("   which breaks Lorentz invariance (implies a preferred slicing).")
print("   This is the 'Mansouri-Sexl test theory' situation — time dilation")
print("   alone does not distinguish SR from a preferred-frame theory.")
