# relative_positions_and_velocities¶

halotools.mock_observables.relative_positions_and_velocities(x1, x2, period=None, **kwargs)[source]

Return the vector pointing from x2 towards x1, that is, x1 - x2, accounting for periodic boundary conditions.

If keyword arguments `v1` and `v2` are passed in, additionally return the velocity `v1` with respect to `v2`, with sign convention such that positive (negative) values correspond to receding (approaching) points.

Parameters:
x1array

1-d array of length Npts. If period is not None, all values must be contained in [0, Lbox)

x2array

1-d array of length Npts. If period is not None, all values must be contained in [0, Lbox)

periodfloat, optional

Size of the periodic box. Default is None for non-periodic case.

Returns:
xrelarray

1-d array of length Npts storing x1 - x2. If x1 > x2 and abs(x1 - x2) > period/2, the sign of d will be negative.

vrelarray, optional

1-d array of length Npts storing v1 relative to v2. Only returned if `v1` and `v2` are passed in.

Examples

```>>> Lbox = 250.0
>>> x1 = 1.
>>> x2 = 249.
>>> result = relative_positions_and_velocities(x1, x2, period=Lbox)
>>> assert np.isclose(result, 2)
```
```>>> result = relative_positions_and_velocities(x1, x2, period=None)
>>> assert np.isclose(result, -248)
```
```>>> npts = 100
>>> x1 = np.random.uniform(0, Lbox, npts)
>>> x2 = np.random.uniform(0, Lbox, npts)
>>> result = relative_positions_and_velocities(x1, x2, period=Lbox)
```

Now let’s frame this result in terms of a physically motivated example. Suppose we have a central galaxy with position xc and velocity vc, and a satellite galaxy with position xs and velocity vs. We can calculate the vector pointing from the central to the satellite, as well as the satellites’s host-centric velocity:

```>>> xcen, vcen = 249.9, 100
>>> xsat, vsat = 0.1, -300
>>> xrel, vrel = relative_positions_and_velocities(xsat, xcen, v1=vsat, v2=vcen, period=Lbox)
>>> assert np.isclose(xrel, +0.2)
>>> assert np.isclose(vrel, -400)
```
```>>> xcen, vcen = 0.1, 100
>>> xsat, vsat = 249.9, -300
>>> xrel, vrel = relative_positions_and_velocities(xsat, xcen, v1=vsat, v2=vcen, period=Lbox)
>>> assert np.isclose(xrel, -0.2)
>>> assert np.isclose(vrel, +400)
```