Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given two vectors $u, v \in \mathbb{R}^d$ represented the spherical coordinates is there a simple formula to compute the angle between the two vectors? Without loss of generality, we can assume that the vectors $u$ and $v$ have unit norm.

I am not sure that the following notation is standard, but I assume that the vector $u$ is defined with $\rho = 1$ and the angular components $\theta_1, \ldots, \theta_{d-1}$. We can obtain the Euclidean components as follows: $$u_{x_1} = \cos \theta_1$$ $$u_{x_2} = \sin \theta_1 \cos \theta_2$$ $$\ldots$$ $$u_{x_{d-1}} = \sin \theta_1 \ldots \sin \theta_{d-2} \cos \theta_{d-1}$$ $$u_{x_d} = \sin \theta_1 \ldots \sin \theta_{d-2} \sin \theta_{d-1}.$$

One way to find the angle is to represent the two vectors in the Euclidean coordinates and compute arccos of the dot product. Is there a simpler way?

As pointed out in comments, is there a generalization of the Haversine formula?

share|cite|improve this question
Well, you've essentially given the sketch of how to derive the formula you need; set $\rho=1$ in the conversion formula for spherical coordinates; take the dot product, and get the arccosine to get the angle. – J. M. Nov 22 '10 at 13:28
...and "spherical coordinates" implies you're dealing with $\mathbb R^3$ ... – J. M. Nov 22 '10 at 13:30
@J.M. What is the proper name for the "spherical coordinates" when $d > 3$? – mkolar Nov 22 '10 at 14:16
...well, what do your vectors look like in four or more dimensions, first of all? – J. M. Nov 22 '10 at 14:45
Some authors refer to "generalized spherical coordinates". See the entry in Glossary of the mapping sciences, p. 117 (e.g. Google Books). The idea is a radius r, an azimuth $\theta$, and n-2 "latitude-like" angles $\phi_1, ... , \phi_{n-2}$, with formulas for Cartesian coordinates in terms of these that generalize the familar relations in 3D (radius times various products of sines and possibly one cosine). – hardmath Nov 22 '10 at 14:58
up vote 2 down vote accepted

Here is a recursive idea that might work. Let $s_1$, $s_2$,..., $s_{n-1}$ the sines of the angles of the first vector on $S^{n-1}$, $c_1$, ..., the cosines, and $s'_{\nu}$ and $c'_{\nu}$ those for the second one. With $p$ being the scalar product, we have $p=1$ for $n=1$, $p=c c'+s s'$ for $n=2$ and generically $$p(n)=c_1 c'_1 + s_1 s'_1 p(n-1)$$ where $p(n-1)$ is the scalar product of the vectors given by the angles $[\theta_2,...]$ and $[\theta'_2,...]$ on $S^{n-1}$(i.e. by removing the first angle of each sequence).

This, of course, still requires all sines and cosines, but my idea is to simplify $p(n-2)$ using the haversine formula and hope that this reduces the number of calls to trigoniometric functions.

share|cite|improve this answer

Perhaps a change to a different basis in spherical coordinates could make the problem simpler, or even lead to a direct solution. For example, if you want to know the angle subtended by the arc from NYC to Cleveland, you could slide the lat/long lines until the prime meridian passed through both cities, and one was on the equator.

Of course, when you start out with what seems like a different approach, you often find yourself doing the same arithmetic.

share|cite|improve this answer

Let's not overthink. We can assume that the length of each vector is 1. Figure the (x,y,z) locations of the vectors, and then the Euclidean distance, d, between them. Then the angle is one angle of an isosceles triangle with two sides = 1 and one side = d.

share|cite|improve this answer
... and this is nothing more than a re-statement of what Americo Tavares wrote in his comments dated Nov 22, 2010. :-) – Willie Wong Apr 15 '11 at 15:18

Simple, alternative derivation of Ralf's formula, without the guesswork:

Angle between the vectors: $$\alpha = \arccos\left({{u \cdot v}\over{|u| |v|}}\right)$$ $$u \cdot v = \sum_i u_i v_i $$ in hyper-spherical coordinates (n+1 dimensions, hence n angles): $$u_i(n) = |u| \cos(\theta_i) \prod_{j=1}^{i-1}{\sin(\theta_j)} $$ except when i=n: $$u_n(n) = |u| \prod_{j=1}^{n}{\sin(\theta_j)} $$ and similar for v (I will use $\phi$ for the angles of v).

With unit vectors we combine the three formulae to: $$\cos(\alpha) = \sum_{i=1}^{n-1}{\cos(\theta_i)\cos(\phi_i) \prod_{j=1}^{i-1}{\sin(\theta_j)\sin(\phi_j)}} + \prod_{i=1}^{n}{\sin(\theta_j)\sin(\phi_j)} $$ which can be written as a recursive rule: $$\cos(\alpha)_n = \cos(\theta_{n-1})\cos(\phi_{n-1}) \cos(\alpha)_{n-1} + \prod_{j=1}^{n}{\sin(\theta_j)\sin(\phi_j)} $$

Which is similar to what Ralf's answer contains (cos/sin switched).

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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