Neural Mastery
← Practice
Vector Projection and Rejectioneasy

Vector Projection and Rejection

easy Math for ML 10-15 min

NumPy allowed · plain Python earns 10 bonus points

Linear algebra step 1/4

A vector transforms

Matrix multiplication is the first computation behind every model.
Open lesson →
01vector
02matrix
03transform
04result
[2, −1] × [[1, 2], [−.5, 1]] = [0, −2]

Computed matrix × vector transformation

Decompose vector u into orthogonal components parallel and perpendicular to v.

Task

Implement vector_projection(u, v): compute proj = (u . v / (v . v)) * v and rej = u - proj. Return {"proj": list[float], "rej": list[float]} with all numbers rounded to 4 decimals.

Function Signature

vector_projection(u: list[float], v: list[float]) -> dict[str, list[float]]

Examples

Example 1: Projection onto Axis
Input: u = [3, 4] v = [1, 0]
Output: {'proj': [3, 0], 'rej': [0, 4]}
Explanation: Projection onto horizontal axis.
Example 2: Diagonal Projection
Input: u = [2, 0] v = [1, 1]
Output: {'proj': [1, 1], 'rej': [1, -1]}
Explanation: Projection onto diagonal vector.

Constraints

  • u and v have matching lengths.
  • v is non-zero.
Python3Saved ✓
def vector_projection(u: list[float], v: list[float]) -> dict[str, list[float]]:
"""Decompose vector u into parallel projection along v and perpendicular rejection.
Return {'proj': list[float], 'rej': list[float]} with coordinates rounded to 4 decimals."""
# Your implementation here
pass

Case 1: Projection onto Axis
Input: u = [3, 4] v = [1, 0]
Expected: {'proj': [3, 0], 'rej': [0, 4]}
Case 2: Diagonal Projection
Input: u = [2, 0] v = [1, 1]
Expected: {'proj': [1, 1], 'rej': [1, -1]}