Lesson 1 of 15

Vector Projection

Vector Projection

The projection of v onto u is the component of v that lies in the direction of u:

projuv=vuuuu\text{proj}_{\mathbf{u}}\mathbf{v} = \frac{\mathbf{v} \cdot \mathbf{u}}{\mathbf{u} \cdot \mathbf{u}} \, \mathbf{u}

The scalar coefficient vuuu\frac{\mathbf{v} \cdot \mathbf{u}}{\mathbf{u} \cdot \mathbf{u}} scales u to exactly the "shadow" of v along u.

Geometric Intuition

Imagine shining a light perpendicular to u — the shadow of v cast onto the line through u is exactly the projection. The perpendicular component vprojuv\mathbf{v} - \text{proj}_{\mathbf{u}}\mathbf{v} is orthogonal to u.

Examples

vuproj_u(v)
[3, 4, 0][1, 0, 0][3.0000, 0.0000, 0.0000]
[1, 2, 3][1, 1, 1][2.0000, 2.0000, 2.0000]
[0, 5][3, 4][2.4000, 3.2000]

Note: For [1,2,3] onto [1,1,1]: the sum is 6, so the coefficient is 6/3=26/3 = 2, giving [2, 2, 2].

Your Task

Implement dot(a, b) returning the dot product of two vectors, and project(v, u) returning the projection of v onto u.

Python runtime loading...
Loading...
Click "Run" to execute your code.