# Rotating two vector randomly keeping the relative orientation between them unchanged

Let's consider we have two vectors $A(x_1, y_1, z_1)$ and $B(x_2, y_2, z_2)$. Now want to rotate these two vectors in $3D$ space (such that the relative orientation between them is always same).

How can I do that?

PS. I have tried to rotate the two vectors independently by angles $\phi$, $\theta$, and $\psi$. But I did not get the uniform spherical distribution. But I was getting a higher intensity near the pole of the sphere.

• What do you want to accomplish, exactly? An uniformly distributed random 3D rotation transform, which you would apply to both vectors? If that's the case, you can get such a random rotation matrix in three dimensions with RandomVariate[CircularRealMatrixDistribution[3]], and rotate the vectors just by performing a matrix-vector multiplication (r . v, between a rotation matrix r and a vector v). – kirma Apr 26 '18 at 11:18

With[{
axis = RandomPoint[Sphere[]],
angle = RandomReal[{-Pi, Pi}]
},
RotationMatrix[angle, axis]
]


should give you a rotation uniformly distributed over $\operatorname{SO}(3)$.

My comment in a little more presentable form...

With[{vs = {{1, 0, 0}, {0, 2/3, 0}, {0, 0, 1/3}}},
Table[With[{r =
NestWhile[
RandomVariate[CircularRealMatrixDistribution[3]] &,
{{0}}, Det[#] <= 0 &]},
Graphics3D[{Gray, Arrow[Tube[{{0, 0, 0}, r.#}]] & /@ vs, Blue,
Opacity[1/10], Ball[{0, 0, 0}]}]], 4]] // Partition[#, 2] & // GraphicsGrid


This also works in other dimensions (although 2D is the only one I'm going to show as an example here!):

With[{vs = {{1, 0}, {0, 2/3}}},
Table[With[{r =
NestWhile[
RandomVariate[CircularRealMatrixDistribution[2]] &,
{{0}}, Det[#] <= 0 &]},
Graphics[{Arrow[{{0, 0}, r.#}] & /@ vs, Blue, Opacity[1/10],
Ball[{0, 0}]}]], 4]] // Partition[#, 2] & // GraphicsGrid


Why the NestWhile ... Det construct? @MichaelSeifert explains this in the comment, and this construct selects a random affine matrix which doesn't perform a reflection.

• Thanks for the nice representation. One more thing if I want to save this two vector as two new parameters (v1,v2). How can I do that? Could you please suggest. – Bikash Apr 26 '18 at 14:12
• @Bikash A minor variation: With[{v1 = {1, 0, 0}, v2 = {0, 2/3, 0}}, Table[With[{r = RandomVariate[CircularRealMatrixDistribution[3]]}, Graphics3D[{Gray, Arrow[Tube[{{0, 0, 0}, r.#}]] & /@ {v1, v2}, Blue, Opacity[1/10], Ball[{0, 0, 0}]}]], 4]] // Partition[#, 2] & // GraphicsGrid – kirma Apr 26 '18 at 14:23
• Thank you very much – Bikash Apr 26 '18 at 14:29
• From the documentation: "CircularRealMatrixDistribution represents a uniform distribution over the orthogonal square matrices of dimension $n$, also known as the Haar measure on the orthogonal group $O(n)$." The group $O(n)$ includes reflections as well as rotations; you'd see this effect in 3D as well if you had more than two vectors. To get "pure" rotations only (the group $SO(n)$, discard all matrices with $\det(M) = -1$ (or equivalently, select all matrices with $\det(M) = +1$.) – Michael Seifert Apr 26 '18 at 15:06
• @MichaelSeifert I must say I missed that. Probably because documentation and examples tend to make the reflection part quite well hidden. I modified my answer accordingly. – kirma Apr 26 '18 at 15:29