Professional Documents
Culture Documents
The projection of a vector b onto a vector a is a vector p which contains the vector component of b in the
direction of a.
b
p a
The projection can be computed using the following formula
a·b
p= a, (2.4)
kak2
where kak denotes the magnitude of the vector a (and similar for b). The magnitude of the vector can be
computed using Pythagoras’ formula.
Problem definition
Create a function named computeProjection that computes the projection of the vector b = [1, 1, . . . ] onto
the vector a which is taken as an input to the function. The output must be the projection p. Note that b is
a vector with all elements equal to one and with the same dimensions as a, and that the function should work
for vectors of any dimensionality.
Solution template
import math
import numpy as np
def computeProjection(a):
#insert your code
return projection
Hint
Remember that you must import the modules your function needs before defining your function. For example,
if you need the math and numpy modules, you should write the import statements as shown above. In the rest
of the exercises we will not explicitly show the import statements in the solution templates.
Input
a Input vector a.
Output
projection Projection vector p.
Example
Consider the vectors a = [2, −1] and b = [1, 1]. The projection of b on a can be computed as
a·b 2 · 1 + (−1) · 1
projection = 2
a= [2, −1] = [0.4, −0.2]. (2.5)
kak 22 + (−1)2
Hand in on CodeJudge
The assignment must be handed in on CodeJudge.
2F