Objective. Given two vectors with the same number of components, find their dot product by hand and in R.
Definition 1 (Dot product).
For two vectors bu and bv with the same number of components, the dot productbu⋅bv is the scalar obtained by multiplying each component of bu by the corresponding component of bv and summing the products:
The vectors (1−22) and (34) have no dot product, because the first has three components and the second has two. The definition requires the same number of components, so that each component of bu pairs with one component of bv.
Proposition 2 (Order and scalars).
Let bu and bv be vectors with the same number of components, and let m be a scalar. The dot product gives the same scalar in either order of the two vectors, and a scalar multiple of either vector can be moved outside the product:
bu⋅bv=bv⋅bu,(mbu)⋅bv=bu⋅(mbv)=m(bu⋅bv).
Example 1
Find the dot product:
1−22⋅40−1.
ShowHide solution
By the definition of the dot product, the dot product is the sum of the products of corresponding components:
(1)(4)+(−2)(0)+(2)(−1)=4+0−2=2.
Example 2
Find the dot product from MMAC 3.4 Example 2(b):
31−20⋅−2453.
ShowHide solution
The four products of corresponding components sum to
(3)(−2)+(1)(4)+(−2)(5)+(0)(3)=−6+4−10+0=−12.
Example 3
For the vectors
bu=(25),bv=(−31),
compute bu⋅bv, bv⋅bu, and (2bu)⋅bv.
ShowHide solution
Computing each dot product from the components gives
The expression a * b multiplies corresponding components of a and b, and sum() adds the products. All three lines give ba⋅bb=2, the value Example 1 finds by hand.
Vectors with different numbers of components in R. The vectors (1−22) and (34) have no dot product. R still returns a number, because dot() reuses the vector with fewer components, starting again from its first component:
> dot(c(1, -2, 2), c(3, 4))
[1] 1
Warning message:In u * v : longer object length is not a multiple of shorter object length> dot(c(1, 2), c(3, 0, -1, 5))
[1] 12
In the warning, u and v are the names dot() gives its two inputs, and “length” is R’s word for the number of components. The second call prints no warning, because 4 is a multiple of 2, yet both numbers are meaningless. Count the components of both vectors before calling dot().
By hand, the length is the square root of the sum of the squared components:
∥bu∥=(2)2+(−1)2+(3)2+(1)2=4+1+9+1=15≈3.873.
Example 7
The table gives monthly active Twitter users worldwide in millions, by quarter (MMAC 3.3 Example 4). Year counts years since 2000.
Year
11
11.25
11.5
11.75
12.75
Users
68
85
101
117
185
The line Users=mYear+b through the first two data points does not pass through the other three. Find the line’s m and b in R, subtract the line’s outputs from the observed Users, and find the length of the resulting vector.
ShowHide solution
The problem has three stages: find m and b, subtract the line’s outputs from Users, and take the length of the difference. The m and b of the line through the first two points solve the vector equation built from those two points:
The line is Users=68Year−680. The miss vector is the observed Users minus the line’s output at each Year. The line’s outputs at the last three Years are
The first two components are 0, because the line passes through the first two points. The last three are negative, because those points sit below the line. The length of the miss vector is
Objective. Use the dot product to determine whether two vectors are orthogonal.
Definition 4 (Orthogonal).
Two vectors bu and bv are orthogonal when bu⋅bv=0, where “orthogonal” is an extension of the two-dimensional idea of “perpendicular” to any number of dimensions.
The vectors (12) and (21) are not orthogonal, because their dot product is (1)(2)+(2)(1)=4, and the definition requires a dot product of 0.
Example 8
Determine whether bu and bv are orthogonal:
bu=(12),bv=(−11/2).
ShowHide solution
By the definition of orthogonal, the vectors are orthogonal when their dot product is 0. The dot product is
bu⋅bv=(1)(−1)+(2)(21)=−1+1=0.
The vectors are orthogonal, because bu⋅bv=0. Drawn as arrows from the origin, bu ends at (1,2) and bv ends at (−1,21), and the two arrows meet at a right angle.
Example 9
Determine which pairs of the vectors bu=(2−131), bv=(1111), and bw=(221−5) from MMAC 3.4 Example 4 are orthogonal, by hand and in R.
The vector bw is orthogonal to both bu and bv, because both dot products are 0. The vectors bu and bv are not orthogonal, because bu⋅bv=5. R gives the same three dot products: