how to write down shortest distance between lines in R^n In $\mathbb{R}^n$ two lines pass through $A$ and $B$, respectively, and their directions $r_1$ and $r_2$ (which are non-zero and non-parallel)
I know for $n=3$ the minimal euclidean distance between points on each line can be written
$$\frac{|(r_1\times r_2)\cdot AB|}{||r_1\times r_2||_2}$$
If $n$ is larger, is it possible to write something with determinants that works for all larger $n$?
 A: Indicating by $\mathbf a$, $\mathbf b$, $\mathbf {p_1}$ and $\mathbf {p_2}$ the position vectors 
of the point $A$, $B$, generic point on line 1 and on line 2, then the parametric equations
of the two lines will be
$$
{\bf p}_{\,1}  = {\bf a} + \lambda \,{\bf r}_{\,1} \quad {\bf p}_{\,2}  = {\bf b} + \mu \,{\bf r}_{\,2} 
$$
Now impose that the difference vector  between the two generic point
be normal to each line
$$
\eqalign{
  & \left\{ \matrix{
  \left( {{\bf p}_{\,1}  - {\bf p}_{\,2} } \right) \cdot {\bf r}_{\,1}  = 0 \hfill \cr 
  \left( {{\bf p}_{\,1}  - {\bf p}_{\,2} } \right) \cdot {\bf r}_{\,2}  = 0 \hfill \cr}  \right.\quad  \Rightarrow \quad \left\{ \matrix{
  \left( {{\bf a} + \lambda {\bf r}_{\,1}  - {\bf b} - \mu {\bf r}_{\,2} } \right) \cdot {\bf r}_{\,1}  = 0 \hfill \cr 
  \left( {{\bf a} + \lambda {\bf r}_{\,1}  - {\bf b} - \mu {\bf r}_{\,2} } \right) \cdot {\bf r}_{\,2}  = 0 \hfill \cr}  \right.\quad  \Rightarrow   \cr 
  &  \Rightarrow \quad \left\{ \matrix{
  {\bf r}_{\,1}  \cdot {\bf r}_{\,1} \,\lambda  - {\bf r}_{\,2}  \cdot {\bf r}_{\,1} \,\mu  = \left( {{\bf b} - {\bf a}} \right) \cdot {\bf r}_{\,1}  \hfill \cr 
  {\bf r}_{\,2}  \cdot {\bf r}_{\,1} \,\lambda  - {\bf r}_{\,2}  \cdot {\bf r}_{\,2} \,\mu  = \left( {{\bf b} - {\bf a}} \right) \cdot {\bf r}_{\,2}  \hfill \cr}  \right. \cr} 
$$
you get a simple linear system in the unknowns $\lambda$ and $\mu$.
After solving that, it is easy to find the modulus of ${\bf p}_{\,1}  - {\bf p}_{\,2} $.
----    Addendum  -  Matrix notation    -----
Indicating
$$
\eqalign{
  & {\bf P} = \left( {{\bf p}_{\,1} |{\bf p}_{\,2} } \right)\quad {\bf R} = \left( {{\bf r}_{\,1} |{\bf r}_{\,2} } \right)\quad {\bf C} = \left( {{\bf a}|{\bf b}} \right)  \cr 
  & {\bf \Lambda } = \left( {\matrix{
   \lambda  & 0  \cr 
   0 & \mu   \cr 
 } } \right)\quad {\bf u} = \left( {\matrix{
   1  \cr 
   { - 1}  \cr 
 } } \right) \cr} 
$$
then


*

*the parametric equations become
$$
{\bf P = C + R}\;{\bf \Lambda }
$$

*the vector connecting generic points on the two lines is
$$
{\bf d} = {\bf P}\,{\bf u}
$$

*the orthogonality condition becomes
$$
\eqalign{
  & {\bf 0} = {\bf R}^{\bf T} {\bf P}\,{\bf u} = {\bf R}^{\bf T} {\bf C}\,{\bf u + R}^{\bf T} {\bf R}\;{\bf \Lambda }\,{\bf u}\quad  \Rightarrow \quad   \cr 
  &  \Rightarrow \quad \;{\bf \Lambda }\,{\bf u} =  - \;\left( {{\bf R}^{\bf T} {\bf R}} \right)^{\, - \,{\bf 1}} {\bf R}^{\bf T} {\bf C}\,{\bf u} \cr} 
$$

*which gives as the ${\bf d}$ normal to both lines
$$
\eqalign{
  & {\bf d}_ \bot   = {\bf C}\,{\bf u + R}\;{\bf \Lambda }\,{\bf u} = {\bf C}\,{\bf u} - {\bf R}\;\left( {{\bf R}^{\bf T} {\bf R}} \right)^{\, - \,{\bf 1}} {\bf R}^{\bf T} {\bf C}\,{\bf u} =   \cr 
  &  = \left( {{\bf I} - {\bf R}\;\left( {{\bf R}^{\bf T} {\bf R}} \right)^{\, - \,{\bf 1}} {\bf R}^{\bf T} } \right){\bf C}\,{\bf u} \cr} 
$$

*whose squared modulus is (the core matrix in ${\bf R}$ is idempotent)

$$
\eqalign{
  & \left| {{\bf d}_ \bot  } \right|^{\,2}  = {\bf d}_ \bot  ^{\bf T} \;{\bf d}_ \bot   = {\bf u}^{\bf T} \,{\bf C}^{\bf T} \,\left( {{\bf I} - {\bf R}\;\left( {{\bf R}^{\bf T} {\bf R}} \right)^{\, - \,{\bf 1}} {\bf R}^{\bf T} } \right)^{\,2} {\bf C}\,{\bf u} =   \cr 
  &  = {\bf u}^{\bf T} \,{\bf C}^{\bf T} \,\left( {{\bf I} - {\bf R}\;\left( {{\bf R}^{\bf T} {\bf R}} \right)^{\, - \,{\bf 1}} {\bf R}^{\bf T} } \right){\bf C}\,{\bf u} \cr} 
$$

It is interesting to note that for the said matrix we have 
$$
\left( {{\bf I} - {\bf R}\;\left( {{\bf R}^{\bf T} {\bf R}} \right)^{\, - \,{\bf 1}} {\bf R}^{\bf T} } \right){\bf R} = {\bf R}^{\bf T} \left( {{\bf I} - {\bf R}\;\left( {{\bf R}^{\bf T} {\bf R}} \right)^{\, - \,{\bf 1}} {\bf R}^{\bf T} } \right) = {\bf 0}
$$
i.e. that its columns form a basis  for the space  normal to  $ {\bf r}_{\,1} $ and $ {\bf r}_{\,2} $.
