# M4ML – Linear Algebra – 2.2 – Part 2: Cosine & Dot Product

[MUSIC] Let’s take the cosine rule from algebra, which you’ll remember
probably vaguely from school. And that said if we had
a triangle with sides a, b, and c, then what the cosine rule said, Was that c squared=
a squared + b squared- 2ab times the cos of
the angle between a and b, cos that angle theta there. Now, we can translate that
into our vector notation. If we call this vector r here,
and we call this vector s here. Then this vector will be minus s plus r,
so that vector would be r-s, minus s + r. So we could say that c squared
was the modulus of r-s squared. And that would be equal to
the modulus, the size, of r squared plus the size of s squared -2 mod r mod s cos theta. Now, here’s the cool bit. We can multiply this out using
our dot product, because we know that the size of (r- s) squared is
equal to (r- s) dotted with itself. Now, that’s just that. And we can multiply that out, and then we’ll compare it to
this right hand side here. So (r- s).(r- s), well that’s going to be, if, we need to figure out
how to multiply that out, that’s going to be equal to r.r, and then take the next one, -s.r -s.r again. If you take that -s and that r, -s.r, again, and then -s.-s. So that is, we’ve got the modulus
of r squared here and we dot r with itself, -2s.r and
then -s.-s. Well, that’s going to be
the size of -s squared, which is just the size of s squared. And then we can compare that
to the right-hand side. And when we do that comparison,
compare that to the right-hand side, the -r squareds are going to cancel,
the r squareds, even. The s squareds are going to cancel, and so we get a result which is that -2s.r=-2 modulus of r modulus of s cos theta. And then we could lose the minus sign,
right? Minus signs will cancel out. Just multiply through by minus one and
then the 2s we can cancel out again. So we can say that the dot product r.s, just to put it in a more familiar form,
=mod r mod s cos theta. So what we found here is that the dot
product really does something quite profound. It takes the size of the two vectors,
if these were both unit length vectors, those would be one, and multiplies
by cos of the angle between them. It tells us something about
the extent to which the two vectors go in the same direction. because if theta was zero,
then cos theta would be one, and r.s would just be the size of
the two vectors multiplied together. If the two vectors, on the other hand,
were at 90 degrees to each other, if they were r was like this and s was
like this, and the angle between them, theta, was equal to 90 degrees,
cos theta, cos 90, is 0. And then r.s is going to be,
we could immediately see, r.s is going to be some size of r,
some size of s times 0. So if the two vectors are pointing at 90
degrees to each other, if they’re what’s called orthogonal to each other, then
the dot product’s going to give me zero. If they’re both pointing in the same
direction, say s was like that, and the angle between them is nought,
cos 0=1. And then r.s, is equal to mod r times mod s, just the
multiplication of the two sizes together. Fun one, last fun one here, is that r and
s are in the opposite directions. So let’s say s was now going this way and the angle between them was 180 degrees. Cos 180 degrees=-1, so then r.s would be equal to minus the size
of r times the size of s. And so what the dot product here really
does with this cos, it tells us when we get the minus sign out,
that they’re going in opposite directions. So there’s some property here in
the dot product we’ve derived by looking at the cosine rule
that we’ve derived here. When the dot product’s 0, they’re at
90 degrees to each other, orthogonal. When they’re going the same way,
we get a positive answer. When they’re going more or
less in opposite directions, we get a negative answer for
the dot product. [MUSIC]