# Finding the sine of the angle between 2 rays

1. Oct 9, 2015

### Matterwave

Hello, say you are on the unit sphere and you have 2 rays intersecting it from the origin. You know the spherical coordinates of where these 2 rays intersect the sphere $(\theta_1,\phi_1),(\theta_2,\phi_2)$.

Now, because we know the dot product of two vectors, it is simple to get that the cosine of the angle between these 2 rays. You convert the spherical coordinates to Cartesian coordinates (with $r=1$) and take the dot product product to get : $$\cos\theta=\cos\theta_1\cos\theta_2+\sin\theta_1\sin\theta_2(\cos\phi_1\cos\phi_2+\sin\phi_1\sin\phi_2)$$

What I want now is the sine of the angle. Of course, it is easy to do $\sin\theta=\sin(\cos^{-1}(\cos\theta))$. However, for computational reasons, it would be much more beneficial to me if I could get this sine in terms of sines and cosines of $\theta_1,\phi_1,\theta_2,\phi_2$. I can take the cross product and do a similar thing to get this, but that formula is horrendous. Because it involves a magnitude, there's a square root of a bunch of squares of a ton of terms. Anyone know what the final result of this computation is? I'm guessing that the terms will simplify significantly, but I wrote them down and aside from a few simple simplifications, could not simplify further.

2. Oct 9, 2015

### Orodruin

Staff Emeritus
Compute the cosine and then use $\cos^2 + \sin^2 = 1$. Of course, this is not a way around the square root, you will have to live with that.

3. Oct 9, 2015

### Matterwave

This seems...equally horrendous..=/

At the end of the computation, I will have to average over all azimuthal angles of $\phi_2$ assuming spherical symmetry, and so actually I can use $\cos\theta=\cos\theta_1\cos\theta_2$ because the integrals of $\sin x,\cos x$ over 2 pi is 0. I was hoping for a similar simplicity in the sine result..

4. Oct 9, 2015

### Matterwave

I feel like I've made a little progress. I looked at the formula for $\sin(x-y)=\sin x\cos y-\cos x\sin y$. My formula should reduce to this formula if $\phi_1=\phi_2=0$, since if we are in a plane, then the angle between our rays will simply be $\theta_1-\theta_2$. So I looked at the monster that is the current, unsimpified form, of my formula and it looks like the monster formula could be some square of this. The last term in my formula is: $$-2\sin\theta_1\cos\theta_1\sin\theta_2\cos\theta_2(\sin\phi_1\sin\phi_2+\cos\phi_1\cos\phi_2+ \cos\phi_1\cos\phi_2\sin\phi_1\sin\phi_2)$$

It looks like if you took the square of $$\sin\theta_1\cos\theta_2(1+f_1(\phi_1,\phi_2))-\cos\theta_1\sin\theta_2(1+f_2(\phi_1,\phi_2))$$ you could get a term that matches the last term. I need to figure out what $f_{1,2}(\phi_1,\phi_2)$ are, but so far no luck.

5. Oct 9, 2015

### Matterwave

I put my whole gigantic formula into wolfram..and it didn't come up with any simplifications...so maybe this can't be done :(

6. Oct 9, 2015

### Jack Davies

I had a play with the expression. It comes down to writing that expression $f(\theta_i , \phi_i )$ in the form:
$$1-f^2 = g^2$$ $$\iff f^2=1-g^2$$
For some 'nice' function $g(\theta_i , \phi_i )$. I don't think it's possible here either, and see no reason why it 'should' be.

7. Oct 9, 2015

### Orodruin

Staff Emeritus
How is this different from what I already said in #2?

8. Oct 9, 2015

### Jack Davies

Exactly the same. Apologies.

9. Oct 9, 2015

### Matterwave

Ok, maybe if I post the full problem, we can perhaps come up with something.

Assume there is a sphere from which rays of stuff (in my case, neutrinos, but that's not important for this part of the problem) emanate. These rays are emitted from this sphere and they intersect. At some distance away from the sphere, we look at two rays which intersect. They both will intersect a radial ray at some polar angle and some azimuthal angle $(\theta_1,\phi_1)$ and $(\theta_2,\phi_2)$ while they make some angle $\theta$ with each other. What I want is $\sin\theta$ as some expression of $(\theta_1,\phi_1)$ and $(\theta_2,\phi_2)$.

There is an additional simplification here. We never track the azimuthal angles in our simulations (the simulations assume azimuthal symmetry), and so two rays which differ by $\theta$ are tracked and assumed different, but two rays which differ only by $\phi$ are not. So, we want to average over all the azimuthal angles at the end of the day.

Up to this point in the simulation I've been working on, we have only required use of $\cos\theta$. This was easy to find because we can exploit the dot product of 2 vectors to find $$\cos\theta=\cos\theta_1\cos\theta_2+\sin\theta_1\sin\theta_2(\cos\phi_1\cos\phi_2+\sin\phi_1\sin\phi_2)$$. We average over all the $\phi$ angles, and since $$\frac{1}{2\pi}\int_0^{2\pi}\sin\phi d\phi=0$$ $$\frac{1}{2\pi}\int_0^{2\pi}\cos\phi d\phi=0$$ the second half the expression disappears and we use $\cos\theta=\cos\theta_1\cos\theta_2$.

I want to now do this same procedure on $\sin\theta$, but as said before, the expression is horrendous.

Taking the cross product of 2 unit vectors, we know that $$\sin\theta=||\hat{k_1}\times\hat{k_2}||=\left|\left|\begin{pmatrix} \sin\theta_1\sin\phi_1\cos\theta_2-\sin\theta_2\sin\phi_2\cos\theta_1 \\ \sin\theta_2\cos\phi_2\cos\theta_1-\sin\theta_1\cos\phi_1\cos\theta_2 \\ \sin\theta_1\cos\phi_1\sin\theta_2\sin\phi_2-\sin\theta_1\sin\phi_1\sin\theta_2\cos\phi_2\end{pmatrix}\right|\right|$$ As you can probably tell, this will look horrible. So far I have gotten the expression down to: $$\sin^2\theta=\sin^2\theta_1\cos^2\theta_2+\sin^2\theta_2\cos^2\theta_1+\sin^2\theta_1\sin^2\theta_2(\cos^2\phi_1\sin^2\phi_2+\sin^2\phi_1\cos^2\phi_2)- \\ 2\sin\theta_1\cos\theta_2\sin\theta_2\cos\theta_1(\sin\phi_1\sin\phi_2+\cos\phi_1\cos\phi_2+\cos\phi_1\cos\phi_2\sin\phi_1\sin\phi_2)$$

I have not been able to simplify this further. Somehow, though if I set $\phi_1=\phi_2=0$ I should get $\sin\theta=\sin\theta_1\cos\theta_2-\sin\theta_2\cos\theta_1$ because that's the sin addition formula. I seem to be able to retrieve this result so it looks like my work is ok...Can someone check the math a bit? Any ideas on how this simplifies? How about what would happen if I took the integral of this around both $\phi$?

I looked at the expression and tried to reproduce this expression for $\sin^2\theta$ by looking at the product $(\sin\theta_1\cos\theta_2(1+\sin\phi_1\cos\phi_2)-\cos\theta_1\sin\theta_2(1+\cos\phi_1\sin\phi_2))^2$ and although I reproduce the first 2 terms and the last term in that long expression above, I can't reproduce the middle expression and there are too many terms in this. But it really seems like some sort of simplification like this should be possible...

Last edited: Oct 9, 2015
10. Oct 11, 2015

### SlowThinker

1. I'm no expert on math, but I think I can prove that the expression for $\sin\theta$ has to contain a $\sqrt{\ }$ somewhere:
The angle between the 2 rays can be seen as $2\pi-\theta$ as well as $\theta$.
The cosine of these 2 angles is the same, so you can find a formula for $\cos \theta$.
But the sine of those 2 angles has a different sign, which is a strong hint that a square root is involved.

2. I tried to do the calculation as well. I've arrived at something different, but I did all simplifications on paper, so probably made a mistake somewhere. Anyway, why don't you use the symmetry and replace $\phi_2$ with $\phi_2-\phi_1$, and $\phi_1$ with $0$?
The expression can then be simplified to the form
$$\sin(\theta)=\sqrt{a + b \sin(\phi_2-\phi_1) + c \cos(\phi_2-\phi_1) + d \sin^2(\phi_2-\phi_1)}$$
which, with some luck, can be integrated over $\phi_2-\phi_1$.

11. Oct 11, 2015

### Matterwave

So your a,b,c,d, are expressions with $\theta_1,\theta_2$ in them I am assuming?

12. Oct 11, 2015

### SlowThinker

Yes. I'm not entirely sure how the "integration" part of your problem works, but if I understand it correctly, it does not depend on the $\theta$s.

13. Oct 11, 2015

### SlowThinker

In fact, it's kind of obvious that the thing under square root will be $1-\cos^2\theta$.

14. Oct 11, 2015

### Matterwave

Well yes, but i want it in terms of $\theta_1,\theta_2$ not $\theta$

15. Oct 11, 2015

### SlowThinker

$$\cos\theta=\cos\theta_1\cos\theta_2+\sin\theta_1\sin\theta_2\cos(\phi_2-\phi_1)$$

16. Oct 11, 2015

### Matterwave

Yes, the posters above you have suggested it, but it seems this expression is still quite cumbersome when you square it and then square root it with a 1- in front).

17. Oct 12, 2015

### Staff: Mentor

Hmm.
Going back to base assumptions. You seem to be put off by calculating the arithmetic mean or some type of "average". Consider an online algorithm. This takes a steam of input, does not store very much in memory and is low overhead, and VERY simple to implement. Maybe you do not need a "gigantic formula". I cannot tell.

Here is a version from Knuth of an online algorithm. You can adapt it to most procedural programming languages that support I/O from files or the tty. There is some exposition at the top followed by simple pseudocode. This is for a variance. You alter it for your needs.

https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm