Dot Product: Difference between revisions
No edit summary |
No edit summary |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Scripting Reference]] | |||
The Dot Product is a property of vectors. It is defined as the sum of the products of each component of a [[Vectors|vector]]. This is a useful property when trying to determine the angle between two vectors in n-dimensions. | |||
== Definition == | |||
The dot product of vectors u and v is written u•v and is pronounced "u dot v". This should not be confused with the multiplication dot (below marked as · as opposed to •)<br /> | |||
The dot product simply returns a number. This number is the sum of products of each component of the vector. So, if we have vectors u = <3,5,7> and v = <9,4,2>, the dot product would be | |||
u•v = <3,5,7>•<9,4,2> = 3·9 + 5·4 + 7·2 = 61 | |||
This value has special meaning with vectors. It can also be calculated by the magnitude of u multiplied by the magnitude of v multiplied by the cosine of the angle between them. | |||
|u||v|cos(θ) | |||
== Deriving the Dot Product == | |||
The dot product was merely derived from the law of cosines: | |||
c<sup>2</sup> = a<sup>2</sup> + b<sup>2</sup> - 2 · a · b · cos(θ) | |||
Imagine a triangle with three points, A, B, and C and we are trying to find the size of angle ∠BAC. In this triangle, AB = c, AC = b, and BC = a. Thus, we have | |||
(AB)<sup>2</sup> = (BC)<sup>2</sup> + (AC)<sup>2</sup> - 2 · (BC) · (AC) · cos(θ) | |||
(AB)<sup>2</sup> - (BC)<sup>2</sup> - (AC)<sup>2</sup> = -2 · (BC) · (AC) · cos(θ) | |||
[(AB)<sup>2</sup> - (BC)<sup>2</sup> - (AC)<sup>2</sup>] / [-2 · (BC) · (AC)] = cos(θ) | |||
[-(AB)<sup>2</sup> + (BC)<sup>2</sup> + (AC)<sup>2</sup>] / [2 · (BC) · (AC)] = cos(θ) | |||
At this point, we replace all sides with vectors. BC = |u|, AC = |v|, AB = |u-v|. | |||
[-|u-v|<sup>2</sup> + |u|<sup>2</sup> + |v|<sup>2</sup>] / [2 · |u| · |v|] = cos(θ) | |||
[-[(u<sub>1</sub><sup>2</sup> - 2·u<sub>1</sub>·v<sub>1</sub> + v<sub>1</sub><sup>2</sup>) + (u<sub>2</sub><sup>2</sup> - 2·u<sub>2</sub>·v<sub>2</sub> + v<sub>2</sub><sup>2</sup>) + (u<sub>3</sub><sup>2</sup> - 2·u<sub>3</sub>·v<sub>3</sub> + v<sub>1</sub><sup>3</sup>)] + u<sub>1</sub><sup>2</sup> + u<sub>2</sub><sup>2</sup> + u<sub>3</sub><sup>2</sup> + v<sub>1</sub><sup>2</sup> + v<sub>2</sub><sup>2</sup> + v<sub>3</sub><sup>2</sup>] / [2·|u|·|v|] = cos(θ) | |||
This calculation is done by "multiplying" the two vectors together. In reality, |u|<sup>2</sup> = u•u, so it is merely following the definition of the dot product above. | |||
[-(-2·u<sub>1</sub>·v<sub>1</sub> - 2·u<sub>2</sub>·v<sub>2</sub> - 2·u<sub>3</sub>·v<sub>3</sub>)] / [2·|u|·|v|] = cos(θ) | |||
[2·u<sub>1</sub>·v<sub>1</sub> + 2·u<sub>2</sub>·v<sub>2</sub> + 2·u<sub>3</sub>·v<sub>3</sub>)] / [2·|u|·|v|] = cos(θ) | |||
[u<sub>1</sub>·v<sub>1</sub> + u<sub>2</sub>·v<sub>2</sub> + u<sub>3</sub>·v<sub>3</sub>)] / [|u|·|v|] = cos(θ) | |||
And since | |||
u<sub>1</sub>·v<sub>1</sub> + u<sub>2</sub>·v<sub>2</sub> + u<sub>3</sub>·v<sub>3</sub> = u•v | |||
We have then | |||
u•v/[|u||v|] = cos(θ) | |||
u•v = |u||v|cos(θ) | |||
== The Dot Product in GraalScript == | |||
The dot product is a function of vectors in format {x,y,z}. It returns a float, as it should. This function in GraalScript is '''vectordot({{graycourier|u}},{{graycourier|v}})'''. It's return value is just simply x<sub>u</sub>x<sub>v</sub> + y<sub>u</sub>y<sub>v</sub> + z<sub>u</sub>z<sub>v</sub>. |
Latest revision as of 19:41, 5 April 2010
The Dot Product is a property of vectors. It is defined as the sum of the products of each component of a vector. This is a useful property when trying to determine the angle between two vectors in n-dimensions.
Definition
The dot product of vectors u and v is written u•v and is pronounced "u dot v". This should not be confused with the multiplication dot (below marked as · as opposed to •)
The dot product simply returns a number. This number is the sum of products of each component of the vector. So, if we have vectors u = <3,5,7> and v = <9,4,2>, the dot product would be
u•v = <3,5,7>•<9,4,2> = 3·9 + 5·4 + 7·2 = 61
This value has special meaning with vectors. It can also be calculated by the magnitude of u multiplied by the magnitude of v multiplied by the cosine of the angle between them.
|u||v|cos(θ)
Deriving the Dot Product
The dot product was merely derived from the law of cosines:
c2 = a2 + b2 - 2 · a · b · cos(θ)
Imagine a triangle with three points, A, B, and C and we are trying to find the size of angle ∠BAC. In this triangle, AB = c, AC = b, and BC = a. Thus, we have
(AB)2 = (BC)2 + (AC)2 - 2 · (BC) · (AC) · cos(θ) (AB)2 - (BC)2 - (AC)2 = -2 · (BC) · (AC) · cos(θ) [(AB)2 - (BC)2 - (AC)2] / [-2 · (BC) · (AC)] = cos(θ) [-(AB)2 + (BC)2 + (AC)2] / [2 · (BC) · (AC)] = cos(θ)
At this point, we replace all sides with vectors. BC = |u|, AC = |v|, AB = |u-v|.
[-|u-v|2 + |u|2 + |v|2] / [2 · |u| · |v|] = cos(θ) [-[(u12 - 2·u1·v1 + v12) + (u22 - 2·u2·v2 + v22) + (u32 - 2·u3·v3 + v13)] + u12 + u22 + u32 + v12 + v22 + v32] / [2·|u|·|v|] = cos(θ)
This calculation is done by "multiplying" the two vectors together. In reality, |u|2 = u•u, so it is merely following the definition of the dot product above.
[-(-2·u1·v1 - 2·u2·v2 - 2·u3·v3)] / [2·|u|·|v|] = cos(θ) [2·u1·v1 + 2·u2·v2 + 2·u3·v3)] / [2·|u|·|v|] = cos(θ) [u1·v1 + u2·v2 + u3·v3)] / [|u|·|v|] = cos(θ)
And since
u1·v1 + u2·v2 + u3·v3 = u•v
We have then
u•v/[|u||v|] = cos(θ) u•v = |u||v|cos(θ)
The Dot Product in GraalScript
The dot product is a function of vectors in format {x,y,z}. It returns a float, as it should. This function in GraalScript is vectordot(u,v). It's return value is just simply xuxv + yuyv + zuzv.