using python to calculate Vector Projection

前端 未结 1 1476
时光取名叫无心
时光取名叫无心 2021-02-20 03:35

Is there an easier command to compute vector projection? I am instead using the following:

x = np.array([ 3, -4,  0])
y = np.array([10,  5, -6])
z=float(np.dot(x         


        
1条回答
  •  迷失自我
    2021-02-20 03:46

    Maybe this is what you really want:

    np.dot(x, y) / np.linalg.norm(y)
    

    This should give the projection of vector x onto vector y - see https://en.wikipedia.org/wiki/Vector_projection. Alternatively, if you want to compute the projection of y onto x, then replace y with x in the denominator (norm) of the above equation.

    EDIT: As @VaidAbhishek commented, the above formula is for the scalar projection. To obtain vector projection multiply scalar projection by a unit vector in the direction of the vector onto which the first vector is projected. The formula then can be modified as:

    y * np.dot(x, y) / np.dot(y, y)
    

    for the vector projection of x onto y.

    0 讨论(0)
提交回复
热议问题