2
$\begingroup$

I'm interested in the problem of finding the points that represents the distance between 2 lines in N-dimensional space (N >= 3).

Assuming that line 1 has 2 points $A_1$ and $X_1$, and line 2 has 2 points $A_2$ and $X_2$, each of which is represented by a N-dimensional vector, the problem can be defined as minimising the following expression:

$$\left\| t_1 \left(A_1-X_1\right)+X_1-t_2 \left(A_2-X_2\right)-X_2\right\| {}^2$$

In[25]:= f=Norm[(Subscript[t, 1](Subscript[A, 1] - Subscript[X, 1]) + Subscript[X, 1] - Subscript[t, 2](Subscript[A, 2] - Subscript[X, 2]) - Subscript[X, 2])]^2
Out[25]= Norm[Subscript[t, 1] (Subscript[A, 1]-Subscript[X, 1])+Subscript[X, 1]-Subscript[t, 2] (Subscript[A, 2]-Subscript[X, 2])-Subscript[X, 2]]^2

But Mathematica seems to be unable to solve it:

In[26]:= ArgMin[f, {Subscript[t, 1], Subscript[t, 2]}]
Out[26]= ArgMin[Norm[Subscript[t, 1] (Subscript[A, 1]-Subscript[X, 1])+Subscript[X, 1]-Subscript[t, 2] (Subscript[A, 2]-Subscript[X, 2])-Subscript[X, 2]]^2,{Subscript[t, 1],Subscript[t, 2]}]

Is it beyond Mathematica's capability? Or I made a mistake in defining the problem?

UPDATE I later explicitly defined all $A_*$ and $X_*$ as 3D vectors:

In[24]:= $Assumptions=(Subscript[A, 1]|Subscript[A, 2]|Subscript[X, 1]|Subscript[X, 2])\[Element]Vectors[3,Reals]
Out[24]= (Subscript[A, 1]|Subscript[A, 2]|Subscript[X, 1]|Subscript[X, 2])\[Element]Vectors[3,Reals]

But it doesn't solve the problem: The result is still the same :-<

$\endgroup$
  • 1
    $\begingroup$ I think you will find that Mathematica has little or no support for "abstract" N-vectors, abstract matricies, abstract sums, etc. Choose a specific concrete N (actually probably choose n because N is already defined as the name of an internal Mathematica function) and concrete points and you will instantly get an answer $\endgroup$ – Bill Aug 20 '17 at 1:34
  • $\begingroup$ I actually confined them to 3D vectors, but with no luck $\endgroup$ – tribbloid Aug 20 '17 at 1:39
  • $\begingroup$ You're still using abstract vectors. You need to put in explicit coordinates: nm = Norm[p ({x2, y2, z2} - {x1, y1, z1}) + {x1, y1, z1} - q ({x4, y4, z4} - {x3, y3, z3}) - {x3, y3, z3}]^2 /. Abs -> Identity; Solve[Thread[D[nm, {{p, q}}] == 0], {p, q}] // Simplify. The full symbolic result is a bit gnarly tho. $\endgroup$ – J. M. will be back soon Aug 20 '17 at 1:58
  • 1
    $\begingroup$ Perhaps this sections.maa.org/lams/proceedings/spring2001/bard.himel.pdf or this math.stackexchange.com/questions/2152688/… will get you where you need to go. If not then a Google search for "n dimensions minimum distance between skew lines", without the quotes, which is where I found both of those, looks promising. $\endgroup$ – Bill Aug 20 '17 at 2:59
  • 1
    $\begingroup$ Sometimes looking at how someone else was able to simply solve a problem can show you how you might let Mathematica know what you want done in a way that it might succeed. Until computers implement the "just do what I mean" button then either this or having gained expertise in guessing how to present a problem to Mathematica or asking the net to show you how to do it seems like your alternatives to possibly actually getting a solution. $\endgroup$ – Bill Aug 20 '17 at 16:43
6
$\begingroup$

Here is one possibility, using Dot instead of Norm:

dsquared = #.#&[t1(A1-X1)+X1-t2(A2-X2)-X2]

(t1 (A1 - X1) + X1 - t2 (A2 - X2) - X2).(t1 (A1 - X1) + X1 - t2 (A2 - X2) - X2)

Now, use TensorExpand with appropriate assumptions:

expand = TensorExpand[
    dsquared,
    Assumptions->Element[t1|t2, Arrays[{}]] && Element[A1|A2|X1|X2, Vectors[{d}]]
]

t1^2 A1.A1 - 2 t1 t2 A1.A2 + 2 t1 A1.X1 - 2 t1^2 A1.X1 - 2 t1 A1.X2 + 2 t1 t2 A1.X2 + t2^2 A2.A2 - 2 t2 A2.X1 + 2 t1 t2 A2.X1 + 2 t2 A2.X2 - 2 t2^2 A2.X2 + X1.X1 - 2 t1 X1.X1 + t1^2 X1.X1 - 2 X1.X2 + 2 t1 X1.X2 + 2 t2 X1.X2 - 2 t1 t2 X1.X2 + X2.X2 - 2 t2 X2.X2 + t2^2 X2.X2

Finally, take derivatives with respect to t1 and t2 and use Solve:

soln = First @ Solve[{D[expand,t1]==0, D[expand,t2]==0}, {t1, t2}]

{t1 -> -((-(-2 A1.A2 + 2 A1.X2 + 2 A2.X1 - 2 X1.X2) (-2 A2.X1 + 2 A2.X2 + 2 X1.X2 - 2 X2.X2) + (2 A1.X1 - 2 A1.X2 - 2 X1.X1 + 2 X1.X2) (2 A2.A2 - 4 A2.X2 + 2 X2.X2))/(-(-2 A1.A2 + 2 A1.X2 + 2 A2.X1 - 2 X1.X2)^2 + (2 A1.A1 - 4 A1.X1 + 2 X1.X1) (2 A2.A2 - 4 A2.X2 + 2 X2.X2))), t2 -> -((-A1.A2 A1.X1 + A1.A2 A1.X2 + A1.X1 A1.X2 - (A1.X2)^2 + A1.A1 A2.X1 - A1.X1 A2.X1 - A1.X2 A2.X1 - A1.A1 A2.X2 + 2 A1.X1 A2.X2 + A1.A2 X1.X1 - A1.X2 X1.X1 - A2.X2 X1.X1 - A1.A1 X1.X2 - A1.A2 X1.X2 + A1.X1 X1.X2 + 2 A1.X2 X1.X2 + A2.X1 X1.X2 - (X1.X2)^2 + A1.A1 X2.X2 - 2 A1.X1 X2.X2 + X1.X1 X2.X2)/((A1.A2)^2 - 2 A1.A2 A1.X2 + (A1.X2)^2 - A1.A1 A2.A2 + 2 A1.X1 A2.A2 - 2 A1.A2 A2.X1 + 2 A1.X2 A2.X1 + (A2.X1)^2 + 2 A1.A1 A2.X2 - 4 A1.X1 A2.X2 - A2.A2 X1.X1 + 2 A2.X2 X1.X1 + 2 A1.A2 X1.X2 - 2 A1.X2 X1.X2 - 2 A2.X1 X1.X2 + (X1.X2)^2 - A1.A1 X2.X2 + 2 A1.X1 X2.X2 - X1.X1 X2.X2))}

$\endgroup$
  • $\begingroup$ Thanks a lot! The answer is very intricate, ArgMin[expand] won't work in this case, but it is already very powerful $\endgroup$ – tribbloid Aug 20 '17 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.