"""Chapter 14 SageMath example.

Run inside the pinned SageMath Docker image:

    docker run --rm -v "<repo>:/work" -w /work \
      sagemath/sagemath:10.9 sage /work/content/examples/math/tool-chapters/ch14_sagemath_demo.sage

Illustrative teaching script. It is not an engine evidence-bundle producer.
"""

from sage.version import version as sage_version

print("sagemath", sage_version)

# --- Chapter 9 residual: gamma relation ------------------------------------
v = SR.var("v")
gamma = 1 / sqrt(1 - v^2)
residual = ((gamma^2 * (1 - v^2) - 1)).full_simplify()
print("ch14 gamma residual (symbolic v):", residual)
print("ch14 same residual at v=2 (outside |v|<1):", residual.subs(v=2).simplify_full())
print("ch14 gamma^2(1-v^2)-1 at v=2 directly:",
      ((gamma^2 * (1 - v^2) - 1).subs(v=2)).simplify_full())

# --- Algebraic boost residual with free gamma ------------------------------
t, x, g = SR.var("t x g")
boost = ((g * (t - v * x))^2
         - (g * (x - v * t))^2
         - (t^2 - x^2))
print("ch14 algebraic residual, factored:", factor(boost.expand()))

# --- False shear residual --------------------------------------------------
tt, xx, vv = SR.var("tt xx vv")
shear = (tt^2 - (xx + vv * tt)^2 - (tt^2 - xx^2))
print("ch14 shear residual, factored:", factor(shear.expand()))
print("ch14 shear residual at t=1, x=0, v=1/2:",
      shear.subs({tt: 1, xx: 0, vv: 1/2}).simplify_full())

# --- Exact rational check --------------------------------------------------
print("ch14 exact rational check:", QQ(1/2) + QQ(1/3))
