Vectors
How To Calculate The Dot Product Of Two Vectors
The dot product turns two vectors into a single number. Multiply matching components, add the results, and you have a scalar that records how much of one vector points along the other. The same number can be read geometrically as the product of the two lengths and the cosine of the angle between them. This page shows both views, works through three pairs of vectors and explains the related ideas of cosine similarity and scalar projection.
Last updated
Quick Answer
a · b = a1b1 + a2b2 + a3b3
- a
- The first vector, written (a1, a2, a3)
- b
- The second vector, written (b1, b2, b3)
- a · b
- The dot product, a single scalar number
Pair each component of a with the component of b in the same position, multiply the pairs and add them. For a = (1, 2, 3) and b = (4, 5, 6) the result is 4 + 10 + 18 = 32. The geometric form |a||b|cos(theta) explains why the answer is positive for acute angles, zero for perpendicular vectors and negative for obtuse ones.
What Is The Dot Product Of Two Vectors?
The dot product of two vectors is the sum of the products of their corresponding components. Write a = (a1, a2, a3) and b = (b1, b2, b3); then a · b = a1b1 + a2b2 + a3b3. With a = (1, 2, 3) and b = (4, 5, 6) that gives 1 x 4 + 2 x 5 + 3 x 6 = 4 + 10 + 18 = 32. The operation is defined for any matching number of dimensions, so two-component and four-component vectors follow exactly the same pattern. It is often called the scalar product or the inner product.
The most important structural fact is that the answer is a scalar, not a vector. Adding two vectors produces another vector with components, but the dot product collapses both inputs into one ordinary number. That number can be positive, negative or zero, and it is measured in the square of whatever unit the components carry. Because the result has no direction, it cannot be plotted as an arrow. The name product refers to the distributive algebra it satisfies, not to a component-by-component vector output.
A second formula reveals what that scalar means geometrically: a · b = |a| |b| cos(theta), where theta is the angle between the two vectors. Since lengths are positive, the sign of the dot product is exactly the sign of the cosine. Acute angles give a positive answer, a right angle gives zero and an obtuse angle gives a negative answer. The vectors (1, 0, 0) and (0, 1, 0) are perpendicular and their dot product is 0. The vectors (2, 0, 0) and (-3, 0, 0) point in opposite directions and their dot product is -6.
The two definitions are not separate facts; one follows from the other. The squared length of the difference vector a - b can be computed from components as |a| squared plus |b| squared minus 2(a · b). The cosine rule for the triangle formed by a, b and a - b gives the same quantity as |a| squared plus |b| squared minus 2|a||b|cos(theta). Comparing the two expressions forces a · b = |a||b|cos(theta). This is also why dotting a vector with itself returns its squared length: the angle is zero and the cosine is one. No angle measurement is needed to compute the product from components.
The magnitudes in the geometric form come straight from the components. In three dimensions |a| = sqrt(a1 squared + a2 squared + a3 squared) and |b| = sqrt(b1 squared + b2 squared + b3 squared), an application of the Pythagorean theorem twice over. For the default vectors these are sqrt(14), about 3.7416573867739413, and sqrt(77), about 8.774964387392123. The calculator uses exactly these square roots and never evaluates an angle. Keeping the roots unrounded until the final step preserves the precision of every derived measure.
Dividing the dot product by the product of the magnitudes gives the cosine similarity, (a · b)/(|a||b|). For the default pair that is 32 divided by (sqrt14 x sqrt77), about 0.9746318461970762. The Cauchy–Schwarz inequality guarantees the value lies between -1 and 1. A value near 1 means the directions are almost identical, a value near -1 means they are almost opposite, and zero means they are orthogonal. Unlike the raw dot product, cosine similarity is unchanged when either vector is multiplied by a positive scale factor. That scale invariance explains its widespread use in search and recommendation systems, where document lengths vary.
The scalar projection of a onto b is (a · b)/|b|. It is the signed length of the shadow that a casts on the line through b, positive when the angle is acute and negative when it is obtuse. For the default vectors the projection is 32 divided by sqrt(77), about 3.6467384467084143. Multiplying this scalar by the unit vector b/|b| would give the vector projection, which does have components. The calculator reports only the scalar length, because that is the quantity the dot product directly supplies. A zero projection means the two vectors are perpendicular.
Several algebraic properties make the dot product easy to manipulate. It is commutative, so a · b = b · a, and distributive, so a · (b + c) = a · b + a · c. Scaling either vector scales the product: doubling b doubles a · b, while doubling both quadruples it. The product of a vector with the zero vector is always zero. A vector dotted with itself is the sum of squared components and is never negative, equalling zero only for the zero vector. These rules underpin the Gram–Schmidt process and many least-squares calculations.
In mechanics the dot product turns force and displacement into work, and in computer graphics it helps decide how brightly a surface is lit. In data analysis it compares records represented as long vectors of features. A zero result is a clean orthogonality test, provided neither input is the zero vector, which has no direction. The cosine similarity and the scalar projection both divide by a magnitude, so they are undefined when b is zero. The component formula itself has no such restriction and remains valid for any two vectors of matching dimension.
Formula
a · b = a1b1 + a2b2 + a3b3
Formula page →Multiply the components that occupy the same position in each vector, then add the three products to obtain a single scalar.
| Symbol | Meaning | Unit | Notes |
|---|---|---|---|
| a1, a2, a3 | Components of the first vector | any | The three coordinates of vector a, taken in order. |
| b1, b2, b3 | Components of the second vector | any | The three coordinates of vector b; each pairs with the matching coordinate of a. |
| a · b | The scalar dot product | square of input unit | A single number, positive, negative or zero, with the square of the input unit. |
a · b = |a| |b| cos(theta)
Formula page →The product of the two magnitudes and the cosine of the angle between them; its sign reveals whether the angle is acute, right or obtuse.
| Symbol | Meaning | Unit | Notes |
|---|---|---|---|
| |a| | Magnitude of the first vector | length | The square root of the sum of the squared components of a. |
| |b| | Magnitude of the second vector | length | The square root of the sum of the squared components of b. |
| theta | Angle between the two vectors | angle | Zero for parallel vectors, a right angle for orthogonal ones and a straight angle for opposite directions. |
cos(theta) = (a · b) / (|a||b|), projection = (a · b) / |b|
Formula page →Dividing by both magnitudes removes scale and leaves a dimensionless similarity; dividing by |b| alone leaves the signed length of a's shadow on b.
| Symbol | Meaning | Unit | Notes |
|---|---|---|---|
| cos(theta) | Cosine similarity | dimensionless | A dimensionless value between -1 and 1 measuring alignment of direction. |
| |a||b| | Product of the two magnitudes | square length | The denominator that normalises the dot product in cosine similarity. |
| (a · b) / |b| | Scalar projection of a onto b | length | The signed length of the component of a lying along the direction of b. |
How To Calculate The Dot Product Of Two Vectors
- 1
Write down the components in matching order
Label the first vector (a1, a2, a3) and the second (b1, b2, b3) before doing any arithmetic. With the default inputs these are (1, 2, 3) and (4, 5, 6). Matching positions matter: a1 pairs with b1, a2 with b2 and a3 with b3. Getting the order wrong is the fastest way to a plausible but incorrect answer.
- 2
Multiply matching components
Form the three products a1b1, a2b2 and a3b3 separately. For the defaults these are 1 x 4 = 4, 2 x 5 = 10 and 3 x 6 = 18. Keep a note of the sign of each product, because a negative component multiplied by a positive one stays negative. At this stage the quantities are still ordinary numbers rather than a vector.
- 3
Add the products to obtain the scalar
Sum the three results to obtain the scalar a · b. Here 4 + 10 + 18 = 32. A positive total means the vectors are separated by an acute angle, a negative total means an obtuse angle and zero means a right angle. Check the sign against the components before moving on, because a dropped minus sign flips the interpretation completely.
- 4
Compute the two magnitudes
Square and add the components of each vector and take the square root. For a this is sqrt(1 + 4 + 9) = sqrt(14), about 3.7416573867739413, and for b it is sqrt(16 + 25 + 36) = sqrt(77), about 8.774964387392123. These lengths are needed for every derived measure and are already computed by the calculator.
- 5
Derive the similarity and the projection
Divide the dot product by both magnitudes to get cosine similarity, 32 divided by (sqrt14 x sqrt77), about 0.9746318461970762. Divide by the magnitude of b alone to get the scalar projection, 32 divided by sqrt(77), about 3.6467384467084143. The similarity is dimensionless and must lie between -1 and 1, while the projection is a signed length in the units of a. Neither step requires measuring an angle.
Examples
Example 1: The default pair: a = (1, 2, 3), b = (4, 5, 6)
- ax
- 1
- ay
- 2
- az
- 3
- bx
- 4
- by
- 5
- bz
- 6
| Step | Calculation | Result |
|---|---|---|
| Matching products | 1 x 4, 2 x 5, 3 x 6 | 4, 10, 18 |
| Sum of products | 4 + 10 + 18 | 32 |
| Magnitude of a | sqrt(1 + 4 + 9) | sqrt(14) = 3.7416573867739413 |
| Magnitude of b | sqrt(16 + 25 + 36) | sqrt(77) = 8.774964387392123 |
| Dot product | 1 x 4 + 2 x 5 + 3 x 6 | 32 |
Result: For a = (1, 2, 3) and b = (4, 5, 6) the dot product is 32, the cosine similarity is about 0.9746 and the scalar projection of a onto b is about 3.6467.
Example 2: Perpendicular unit vectors: a = (1, 0, 0), b = (0, 1, 0)
- ax
- 1
- ay
- 0
- az
- 0
- bx
- 0
- by
- 1
- bz
- 0
| Step | Calculation | Result |
|---|---|---|
| Matching products | 1 x 0, 0 x 1, 0 x 0 | 0, 0, 0 |
| Sum of products | 0 + 0 + 0 | 0 |
| Magnitude of a | sqrt(1 + 0 + 0) | 1 |
| Magnitude of b | sqrt(0 + 1 + 0) | 1 |
| Dot product | 1 x 0 + 0 x 1 + 0 x 0 | 0 |
Result: The dot product of (1, 0, 0) and (0, 1, 0) is 0, so the two vectors are perpendicular and their cosine similarity is also 0.
Example 3: Opposite directions: a = (2, 0, 0), b = (-3, 0, 0)
- ax
- 2
- ay
- 0
- az
- 0
- bx
- -3
- by
- 0
- bz
- 0
| Step | Calculation | Result |
|---|---|---|
| Matching products | 2 x -3, 0 x 0, 0 x 0 | -6, 0, 0 |
| Sum of products | -6 + 0 + 0 | -6 |
| Magnitude of a | sqrt(4 + 0 + 0) | 2 |
| Magnitude of b | sqrt(9 + 0 + 0) | 3 |
| Dot product | 2 x -3 + 0 x 0 + 0 x 0 | -6 |
Result: The dot product of (2, 0, 0) and (-3, 0, 0) is -6; the negative sign shows that the vectors point in exactly opposite directions.
Calculator
Dot product
32
- Magnitude of the first vector
- 3.7417
- Magnitude of the second vector
- 8.775
- Cosine similarity
- 0.9746
- Scalar projection of a onto b
- 3.6467
Values update as you type. This calculator covers the single scenario its formula assumes — see Common Mistakes for what it leaves out.
Prefer a full-width tool? Open the The Dot Product Of Two Vectors calculator page.
Common Mistakes
Treating the result as a vector
The dot product has no components and no direction. It is one scalar number, so writing an answer such as (32, 0, 0) is a category error. If a vector answer is required, the operation wanted is the vector projection, which multiplies the scalar projection by the unit vector b/|b|.
Pairing components from different positions
The formula uses a1b1, a2b2 and a3b3, never a1b2 or a2b1. Mixing positions is easy when vectors are written on separate lines. Keep the two triples vertically aligned and work down the columns, one product per row.
Dropping a negative sign
For a = (2, 0, 0) and b = (-3, 0, 0) the product is -6, not 6. A missing minus sign reverses the qualitative conclusion, turning an obtuse angle into an acute one. Multiply with signs attached from the start rather than taking absolute values and deciding later.
Assuming a zero dot product means a zero vector
A zero result usually means the vectors are perpendicular, as with (1, 0, 0) and (0, 1, 0). Only if one input is literally the zero vector is the zero product trivial. The zero vector has no direction, so orthogonality statements about it are meaningless.
Confusing the dot product with cosine similarity
The dot product grows when either vector is lengthened, whereas cosine similarity does not. For the default vectors the dot product is 32 but the similarity is about 0.9746. Always divide by both magnitudes if the question asks how aligned the directions are.
Dividing by a zero magnitude
Cosine similarity and scalar projection both divide by |b|, so they cannot be computed when b is the zero vector. The component formula still gives zero in that case, but the derived ratios fail. Check that the vector in the denominator has at least one nonzero component first.
FAQ
What is the dot product used for?
It measures how much two vectors point in the same direction. Physicists use it to compute work from force and displacement, graphics programmers use it for lighting, and data scientists use it to compare feature vectors. A zero value is also the standard test for perpendicularity.
Is the dot product a vector or a scalar?
It is a scalar: a single number with no direction. This is why it is also called the scalar product. Inner product is a broader term that includes this operation and its generalisations.
What does a negative dot product tell me?
It tells you the angle between the vectors is obtuse, meaning they partly oppose one another. For (2, 0, 0) and (-3, 0, 0) the answer is -6 because the vectors point in opposite directions. A positive answer indicates an acute angle and zero indicates a right angle.
How do I get the angle between two vectors?
First compute the cosine similarity as the dot product divided by the product of the two magnitudes. That ratio equals the cosine of the angle, so an inverse cosine would recover the angle itself. This calculator reports the cosine rather than the angle, so no inverse trigonometric step is involved.
How is cosine similarity different from the raw dot product?
Cosine similarity divides out the lengths of both vectors, leaving only the alignment of their directions. It always lies between -1 and 1 and does not change when one vector is scaled by a positive factor. The raw dot product does change under such scaling.
What is the scalar projection?
It is the signed length of the shadow cast by a onto the line through b, equal to (a · b)/|b|. For the default vectors it is about 3.6467. Multiplying this scalar by the unit vector along b gives the corresponding vector projection.
References
- [1], Dot product — https://en.wikipedia.org/wiki/Dot_product
- [2], Scalar projection — https://en.wikipedia.org/wiki/Scalar_projection
- [3], Cosine similarity — https://en.wikipedia.org/wiki/Cosine_similarity
