0
$\begingroup$

I am programming a molecular dynamics program with periodic boundary conditions. Here, I have a problem:

I have a list of coordinates like

list = RandomReal[{0, 1}, {5, 2}]

And now, I want to have the distances.

I do that with

EuclideanDistance @@@ Subsets[list, {2}]

But now, I do not know the direction, so if the distance not squared was positive or negative. Does someone know, how I can obtain the sign? Because I need that for computing my periodic boundary conditions

$\endgroup$
  • $\begingroup$ EuclideanDistanceis always positive. Please clarify "direction"! $\endgroup$ – Ulrich Neumann Jun 19 at 13:57
  • $\begingroup$ @UlrichNeumann So you have $\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$. For me, the "direction" is the sign of $x_1 - x_2$ and $y_1 - y_2$, which can be positive or negative, so I also want to have $x_1 - x_2$ and $y_1 - y_2$ explicitly. $\endgroup$ – Armani42 Jun 19 at 14:03
  • $\begingroup$ @ Armani42 What is the "direction" if x1-x2<0 and y1-y2>0 ? $\endgroup$ – Ulrich Neumann Jun 19 at 14:11
2
$\begingroup$

Here is a solution which gives you the distance and the difference

list = RandomReal[{0, 1}, {5, 2}]; (*points*)
sl=  Subsets[list, {2}]  (* all point pairs*)

m=Map[{Sqrt[(#[[1]] - #[[2]]).(#[[1]] - #[[2]])], #[[1]] - #[[2]]} &,\sl]
(*{{0.72911, {0.681672, -0.258698}}, {0.410144, {0.359548, 0.197341}}, ...}*)

The first element of m is the distance

m[[All, 1]] == EuclideanDistance @@@ sl
(*True*)  

the last is the difference vector.

$\endgroup$
  • $\begingroup$ Thank you, that really helped me :) $\endgroup$ – Armani42 Jun 20 at 9:24

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