Dot Product

The fundamental vector operation that measures how much two vectors point in the same direction.

8 min read Intermediate Popular

Interactive Dot Product Demo

Drag the vectors to see how the dot product changes in real-time.

Vector A
(3.0, 4.0)
|A| = 5.00
Vector B
(2.0, 1.0)
|B| = 2.24
Dot Product (A · B)
10.00
Angle Between
26.6°

Contents

Definition

The dot product (also called scalar product or inner product) is an operation that takes two vectors and returns a single number (scalar).

Algebraic Definition

For two vectors \(\vec{a} = (a_1, a_2, ..., a_n)\) and \(\vec{b} = (b_1, b_2, ..., b_n)\):

\[\vec{a} \cdot \vec{b} = a_1b_1 + a_2b_2 + ... + a_nb_n = \sum_{i=1}^{n} a_i b_i\]

Simply multiply corresponding components and add them up.

// Example: 2D vectors
(3, 4) · (2, 1) = (3×2) + (4×1) = 6 + 4 = 10

Geometric Definition

The dot product can also be defined using magnitudes and the angle between vectors:

\[\vec{a} \cdot \vec{b} = |\vec{a}| \cdot |\vec{b}| \cdot \cos(\theta)\]

This reveals the geometric meaning: the dot product measures how much the vectors "agree" directionally.

Geometric Interpretation

> 0
Acute Angle
Vectors point in similar directions (0° - 90°)
= 0
Perpendicular
Vectors are orthogonal (90°)
< 0
Obtuse Angle
Vectors point in opposite directions (90° - 180°)
Key Insight: When the dot product is zero, the vectors are perpendicular. This is fundamental to many applications, from checking orthogonality to computing projections.

Calculating the Angle

Rearranging the geometric formula, we can find the angle between any two vectors:

\[\theta = \arccos\left(\frac{\vec{a} \cdot \vec{b}}{|\vec{a}| \cdot |\vec{b}|}\right)\]

This is how the demo above calculates the angle in real-time!

Properties

  • Commutative: \(\vec{a} \cdot \vec{b} = \vec{b} \cdot \vec{a}\)
  • Distributive: \(\vec{a} \cdot (\vec{b} + \vec{c}) = \vec{a} \cdot \vec{b} + \vec{a} \cdot \vec{c}\)
  • Scalar multiplication: \((c\vec{a}) \cdot \vec{b} = c(\vec{a} \cdot \vec{b})\)
  • Self dot product: \(\vec{a} \cdot \vec{a} = |\vec{a}|^2\)

Applications

1. Testing Orthogonality

Two vectors are perpendicular if and only if their dot product is zero:

(1, 0) · (0, 1) = 0 + 0 = 0 ✓ Perpendicular

2. Vector Projection

Project vector \(\vec{a}\) onto vector \(\vec{b}\):

\[\text{proj}_{\vec{b}}\vec{a} = \frac{\vec{a} \cdot \vec{b}}{|\vec{b}|^2}\vec{b}\]

3. Cosine Similarity (AI/ML)

In machine learning, the normalized dot product measures similarity between vectors:

\[\text{similarity} = \frac{\vec{a} \cdot \vec{b}}{|\vec{a}| \cdot |\vec{b}|}\]

This is the foundation of semantic search, recommendation systems, and RAG (Retrieval Augmented Generation).

4. Physics Applications

  • Work: \(W = \vec{F} \cdot \vec{d}\) (force times displacement)
  • Power: \(P = \vec{F} \cdot \vec{v}\) (force times velocity)

Code Examples

JavaScript

function dotProduct(a, b) {
    return a.reduce((sum, val, i) => sum + val * b[i], 0);
}

// Example
const vecA = [3, 4];
const vecB = [2, 1];
console.log(dotProduct(vecA, vecB)); // 10

Python

import numpy as np

a = np.array([3, 4])
b = np.array([2, 1])
dot = np.dot(a, b)  # 10

# Or using @ operator
dot = a @ b  # 10

Common Mistakes

Watch out:
  • The dot product returns a scalar, not a vector
  • Don't confuse with the cross product (which returns a vector)
  • Vectors must have the same dimension

Next Steps

💡

The Dot Product Explained Simply

Imagine you're pushing a shopping cart. The dot product tells you how much of your pushing force actually moves the cart forward. If you push straight ahead, all your effort moves the cart. If you push at an angle, only part of your force helps. Push sideways (perpendicular), and the cart doesn't move forward at all - that's when the dot product equals zero!

Another way to think about it: the dot product measures how much two arrows "agree" with each other. When two vectors point in exactly the same direction, they're in complete agreement, and the dot product is at its maximum. When they point in opposite directions, they completely disagree, giving a negative result. When they're perpendicular (at 90 degrees), they have nothing in common - the dot product is zero.

You can also think of the dot product as a "shadow" measurement. Imagine shining a light directly above one vector so it casts a shadow onto another vector. The dot product is related to the length of that shadow multiplied by the length of the vector it falls on. When the vectors are parallel, you get the longest shadow. When perpendicular, there's no shadow at all.

In practical terms, calculating a dot product is simple: multiply matching components together and add them up. For vectors (3, 4) and (2, 1), you compute 3×2 + 4×1 = 10. This single number tells you everything about the relationship between those two directions - it's used everywhere from video game physics to AI recommendation systems to determine how "similar" or "aligned" things are.

Frequently Asked Questions

What is the dot product used for?

The dot product has many practical applications across different fields. In physics, it calculates work done by a force (W = F · d) and power (P = F · v). In computer graphics, it determines lighting and shading by measuring how directly light hits a surface. In machine learning and AI, the dot product powers cosine similarity for comparing text embeddings, enabling semantic search, recommendation systems, and RAG (Retrieval Augmented Generation). It's also used for checking if vectors are perpendicular, computing vector projections, and finding angles between directions.

How do you calculate the dot product of two vectors?

To calculate the dot product, multiply corresponding components of the two vectors and add all the products together. For 2D vectors (a₁, a₂) and (b₁, b₂), the formula is: a₁×b₁ + a₂×b₂. For example, (3, 4) · (2, 1) = 3×2 + 4×1 = 6 + 4 = 10. For 3D vectors, add a third term: a₁×b₁ + a₂×b₂ + a₃×b₃. This works for any dimension - just multiply matching components and sum them up.

What does it mean when the dot product is zero?

When the dot product equals zero, the two vectors are perpendicular (orthogonal) to each other - they form a 90-degree angle. This is because the dot product formula a·b = |a||b|cos(θ) includes cos(θ), and cosine of 90° is zero. This property is extremely useful for testing orthogonality in programming, physics, and linear algebra. For example, (1, 0) · (0, 1) = 0, confirming that the x-axis and y-axis are perpendicular.

What is the difference between dot product and cross product?

The dot product and cross product are fundamentally different operations. The dot product returns a scalar (single number) and works in any dimension. It measures how parallel two vectors are. The cross product returns a new vector that's perpendicular to both input vectors and only works in 3D. The cross product's magnitude represents the area of the parallelogram formed by the two vectors. Use dot product when you need a similarity measure or angle; use cross product when you need a perpendicular direction or area calculation.

Can the dot product be negative?

Yes, the dot product can be negative, positive, or zero. A negative dot product means the angle between the vectors is greater than 90 degrees (obtuse angle) - they point in generally opposite directions. A positive dot product means the angle is less than 90 degrees (acute angle) - they point in generally the same direction. Zero means exactly 90 degrees (perpendicular). For example, (1, 0) · (-1, 0) = -1, indicating these vectors point in opposite directions.

How do you find the angle between two vectors using the dot product?

To find the angle θ between two vectors a and b, use the formula: θ = arccos(a·b / (|a| × |b|)). First, calculate the dot product of the vectors. Then divide by the product of their magnitudes (lengths). Finally, take the inverse cosine (arccos) of the result. For example, for vectors (1, 0) and (1, 1): dot product = 1, |a| = 1, |b| = √2, so θ = arccos(1/√2) = 45 degrees.

What is cosine similarity and how does it relate to the dot product?

Cosine similarity is the dot product of two vectors divided by the product of their magnitudes: cos(θ) = (a·b) / (|a| × |b|). This normalizes the result to a range of -1 to 1, regardless of vector lengths. A value of 1 means identical direction, 0 means perpendicular, and -1 means opposite directions. Cosine similarity is essential in AI and machine learning for comparing text embeddings, measuring document similarity, building recommendation systems, and powering semantic search in vector databases.

Why is the dot product also called the scalar product or inner product?

The dot product has multiple names that reflect different aspects of the operation. It's called the "scalar product" because the result is a scalar (a single number), not a vector - this distinguishes it from the cross product which yields a vector. The term "inner product" comes from linear algebra and abstract mathematics, where it's defined as a generalization that can apply to more than just geometric vectors (like functions or matrices). The name "dot product" simply comes from the notation used: a · b with a dot between the vectors.

How is the dot product used in machine learning and neural networks?

The dot product is fundamental to machine learning. In neural networks, each neuron computes a weighted sum of inputs, which is essentially a dot product between the input vector and weight vector. Attention mechanisms in transformers (like GPT and BERT) use dot products to compute similarity scores between tokens. Vector embeddings for words, sentences, or images are compared using dot products for semantic search and retrieval. Matrix multiplication, which powers deep learning, is built from many dot products. Understanding the dot product is essential for grasping how AI models work.

What is vector projection and how does the dot product help calculate it?

Vector projection finds the component of one vector that lies along another vector's direction - like casting a shadow. To project vector a onto vector b, use the formula: proj_b(a) = (a·b / |b|²) × b. The dot product a·b measures how much of a goes in the direction of b. Dividing by |b|² normalizes for b's length, and multiplying by b gives the final vector. This is used in computer graphics for lighting calculations, in physics for decomposing forces, and in machine learning for orthogonalization in algorithms like Gram-Schmidt.