Vector Dot & Cross Product Calculator

Enter two 2D or 3D vectors. This calculator computes the dot product, cross product (3D), angle between vectors, scalar and vector projections, magnitudes, and unit vectors. The SVG visualization shows both vectors, the angle arc, and the projection line with full geometric interpretation.

The Dot Product: Algebraic and Geometric Definitions

The dot product (also called the inner product or scalar product) is one of the most fundamental operations in linear algebra. Algebraically, the dot product of two vectors a = (a1, a2, ..., an) and b = (b1, b2, ..., bn) is defined as the sum of the products of corresponding components: a · b = a1b1 + a2b2 + ... + anbn. The result is always a scalar (a single number), not a vector. For the 2D case, a · b = a1b1 + a2b2. For the 3D case, a · b = a1b1 + a2b2 + a3b3.

The geometric definition provides the deeper insight: a · b = |a| |b| cos(θ), where |a| and |b| are the magnitudes (lengths) of the vectors and θ is the angle between them. This formula connects algebra and geometry in a powerful way. The dot product encodes three pieces of information simultaneously: the lengths of both vectors and the cosine of the angle between them. When both vectors are unit vectors (magnitude 1), the dot product directly equals cos(θ), making it a pure measure of directional alignment.

Geometric Interpretation: Why the Dot Product Matters

The sign of the dot product reveals the geometric relationship between two vectors. A positive dot product means the angle between the vectors is less than 90 degrees: they point in broadly the same direction. A dot product of zero means the vectors are perpendicular (orthogonal), forming a right angle. A negative dot product means the angle exceeds 90 degrees: the vectors point in broadly opposite directions. This three-way classification is used everywhere: in physics to determine whether a force aids or opposes motion, in computer graphics to determine whether a surface faces toward or away from a light source, and in machine learning to measure similarity between feature vectors.

The projection interpretation is equally important. The scalar projection of a onto b equals (a · b) / |b| = |a| cos(θ). This is the signed length of the "shadow" that vector a casts onto the line defined by b. The vector projection goes further: proj_b(a) = ((a · b) / (b · b)) b, giving the actual vector component of a in the direction of b. The remaining component, a - proj_b(a), is perpendicular to b. This decomposition of a vector into parallel and perpendicular components is the basis of Gram-Schmidt orthogonalization, least-squares regression, and signal processing.

The Cross Product: Perpendicular Vectors and Area

The cross product is defined only in three dimensions (and in seven dimensions, by a more exotic construction, but 3D is the practical case). Given vectors a = (a1, a2, a3) and b = (b1, b2, b3), the cross product a × b = (a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1). The result is a vector, not a scalar. This result vector is perpendicular to both a and b, and its direction follows the right-hand rule: curl the fingers of your right hand from a toward b, and your thumb points in the direction of a × b.

The magnitude of the cross product equals |a| |b| sin(θ), which is the area of the parallelogram formed by the two vectors. This geometric interpretation makes the cross product indispensable in physics and engineering: torque is the cross product of the position vector and force vector, the magnetic force on a moving charge is the cross product of velocity and magnetic field, and surface normals in computer graphics are computed as cross products of two edge vectors of a polygon. The cross product is anti-commutative: a × b = -(b × a), meaning the order matters.

Computing Angles Between Vectors

The angle between two vectors is one of the most commonly needed quantities in computational geometry. From the dot product formula a · b = |a| |b| cos(θ), we solve for the angle: θ = arccos((a · b) / (|a| |b|)). This formula works in any dimension. The result is always between 0 and π radians (0 to 180 degrees). To convert radians to degrees, multiply by 180/π. The formula is numerically stable for most angles, but when the vectors are nearly parallel (or anti-parallel), small floating-point errors in the ratio can push it slightly outside [-1, 1], causing arccos to return NaN. Robust implementations clamp the ratio to [-1, 1] before applying arccos.

An alternative formula using the cross product gives sin(θ) = |a × b| / (|a| |b|), which is more accurate for angles near 0 or π. The most robust approach uses atan2(|a × b|, a · b), which is accurate across the full range. For 2D vectors, the "cross product" a1b2 - a2b1 gives the signed sine of the angle, allowing you to determine whether b is clockwise or counterclockwise from a. The atan2 approach is standard in computer graphics, robotics, and navigation systems where both the magnitude and the sign of the angle matter.

Unit Vectors and Normalization

A unit vector is a vector with magnitude exactly 1. Any non-zero vector v can be converted to a unit vector by dividing each component by the magnitude: v-hat = v / |v|. This operation is called normalization. Unit vectors preserve direction while discarding magnitude information. They are the building blocks of coordinate systems: the standard basis vectors i = (1,0,0), j = (0,1,0), k = (0,0,1) are unit vectors along the three coordinate axes. Any vector can be written as a linear combination of these basis vectors: v = v1 i + v2 j + v3 k.

Normalization is critical in machine learning and information retrieval. Cosine similarity between two vectors is defined as the dot product of their unit vectors: cos(θ) = (a · b) / (|a| |b|). By pre-normalizing all vectors to unit length, cosine similarity reduces to a simple dot product, which is computationally efficient for large-scale similarity searches. Word embeddings (Word2Vec, GloVe, FastText), sentence embeddings, and document embeddings all use cosine similarity as the default similarity metric. In neural network layers, weight normalization and layer normalization involve dividing vectors by their norms to stabilize training.

Applications Across Domains

In physics, work equals the dot product of force and displacement: W = F · d = |F| |d| cos(θ). Only the component of force parallel to the displacement does work; the perpendicular component does zero work. This is why pushing a cart sideways (perpendicular to its direction of motion) requires no energy expenditure in the idealized physics model. In electrostatics, electric flux through a surface element is the dot product of the electric field vector and the surface normal vector. In quantum mechanics, the probability amplitude of transitioning from one state to another is the inner product of the state vectors in Hilbert space.

In computer graphics, the Phong lighting model uses the dot product between the surface normal and the light direction to compute diffuse lighting intensity: brighter when the surface faces the light directly (dot product near 1), darker when the surface is edge-on (dot product near 0), and completely dark when the surface faces away (dot product negative, clamped to 0). Specular highlights use the dot product between the reflection vector and the view direction. Ray tracing algorithms use dot products extensively for intersection tests, reflection direction computation, and shading calculations. The cross product computes surface normals from triangle vertices, which are then used in all subsequent lighting calculations.

Frequently Asked Questions

What is the dot product and what does it measure geometrically?

The dot product of vectors a and b is a·b = a1b1 + a2b2 (+ a3b3 in 3D). Geometrically, it equals |a| |b| cos(θ). A positive dot product means the angle is less than 90°, zero means perpendicular, and negative means the angle exceeds 90°. It measures how much two vectors point in the same direction.

How is the cross product different from the dot product?

The cross product is defined only for 3D vectors and produces a vector perpendicular to both inputs, with magnitude |a| |b| sin(θ). The dot product produces a scalar measuring parallel alignment (cosine). The cross product measures perpendicular alignment (sine), and its magnitude equals the area of the parallelogram formed by the two vectors.

What is the scalar projection of one vector onto another?

The scalar projection of a onto b is (a·b)/|b| = |a| cos(θ). It is the signed length of a's shadow onto the line defined by b. The vector projection is the actual vector component: ((a·b)/(b·b)) b. Together they decompose a vector into parallel and perpendicular components.

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

θ = arccos((a·b) / (|a| |b|)). Compute the dot product, compute both magnitudes, divide, then apply arccos. The result is between 0 and 180 degrees. Convert radians to degrees by multiplying by 180/π. This works in any dimension.

What is a unit vector and why is it useful?

A unit vector has magnitude exactly 1: v-hat = v / |v|. Unit vectors preserve direction while discarding magnitude. They simplify formulas: the dot product of two unit vectors directly gives cos(θ). In ML, cosine similarity between pre-normalized vectors is just a dot product, enabling efficient similarity search.

Related Tools

Built by Michael Lip. Try the ML3X Matrix Calculator for interactive step-by-step solutions.