Hey so I have a very efficient method to calculate the the components (x,y,z) of two vectors, and I want to calculate the angle between them. I am computing this for about 8 million individual entries.

My code is relatively fast, but the limiting point right now is the use of magnitudes, more specifically the sqrt function. I have explored some more efficient methods for computation of the sqrt but I wanted to see if there are more effecient methods unique to computation of the angle between two vectors. If I can be accurate to <1 degree that would be sufficient.

  • $\begingroup$ You could compute $\cos^2\theta = \langle x,\rangle y^2/(\|x\| ^2\|y \|^2)$ to avoid the square roots, and use a table of approximate $\theta=\sqrt{\arccos x}$ values. $\endgroup$ Commented Oct 15, 2019 at 0:07
  • $\begingroup$ this is truly due to my lack of knowledge on the applicable rule/identity. As I read this equation I still need to calculate the magnitude of x and y, or are you getting at that by squaring the magnitudes I can just avoid calculating the squareroot and calculate the sum of the squares? $\endgroup$
    – S moran
    Commented Oct 15, 2019 at 0:13
  • $\begingroup$ Squared magnitudes, yes. I botched the formula: $\cos^2\theta=\langle x,y\rangle^2/(\|x\|\|y\|)^2$ is what I meant. $\endgroup$ Commented Oct 15, 2019 at 0:26
  • $\begingroup$ one last clarification (and thankyou already, I can already see a good option to implement this) what does <x,y> imply? Is that dot product or difference between the vectors? (this is all based on the assumption you are treating x and y as the two 3D vectors, not x and y components. $\endgroup$
    – S moran
    Commented Oct 15, 2019 at 0:35
  • $\begingroup$ Yes, dot product. $\endgroup$ Commented Oct 15, 2019 at 0:40


You must log in to answer this question.

Browse other questions tagged .