Loading the problem
Project the vector v onto the line spanned by u.
proj_u(v) = ((v . u) / (u . u)) * u
The result is the point on the line through u closest to v, and v - proj
is perpendicular to u. This is the whole idea behind least squares: fitting a
linear model is projecting the target onto the span of the features.
If u is the zero vector the line is a point, so return a zero vector.
def orthogonal_projection(v, u):
Input
orthogonal_projection(np.array([3.0, 4.0]), np.array([1.0, 0.0]))
Output
array([3., 0.])
Reasoning
v . u = 3 and u . u = 1, so the projection is 3 * [1, 0] = [3, 0]. What is left, [0, 4], is perpendicular to u.
u is the zero vector, return a zero vector.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell