Solution (source code)

= Solution

For a primed inertial frame moving at speed $v$ along the positive $x$ direction relative to the unprimed frame, a <Lorentz transformation> is
$$
\boxed{x'=\gamma_v(x-vt),\qquad t'=\gamma_v\left(t-\frac{vx}{c^2}\right),\qquad
\gamma_v=(1-v^2/c^2)^{-1/2}}.
$$
The inverse has the opposite velocity. To avoid confusing the train speed with the ball speed, now use the primed frame for the train and write the inverse boost with train speed $u$:
$$
x=\gamma_u(x'+ut'),\qquad t=\gamma_u(t'+ux'/c^2).
$$
The ball has $dx'/dt'=v$ on the train, so differentiation along its worldline gives the <velocity-addition formula>
$$
\boxed{\frac{dx}{dt}=\frac{u+v}{1+uv/c^2}}.
$$
The time transformation is essential; simply adding the two speeds uses the Galilean rather than Lorentz relation. For positive subluminal $u,v$, the result remains below $c$, since $1+uv/c^2-(u+v)/c=(1-u/c)(1-v/c)>0$.