Change language

Vector projection using Python

A vector is a geometric object that has both size (i.e. length) and direction. A vector is generally represented by a line segment with a specific direction connecting the starting point A and the ending point B, as shown in the following figure, and is denoted as $overrightarrow{AB}$

Projection of a Vector on another vector

Computing vector projection onto another vector in Python:

# import numpy to perform operations on vector
import numpy as np
  
u = np.array([1, 2, 3])   # vector u
v = np.array([5, 6, 2])   # vector v:
  
# Task: Project vector u on vector v
  
# finding norm of the vector v
v_norm = np. sqrt(sum(v**2))    
  
# Apply the formula as mentioned above
# for projecting a vector onto another vector
# find dot product using np.dot()
proj_of_u_on_v = (np.dot(u, v)/v_norm**2)*v
  
print("Projection of Vector u on Vector v is: ", proj_of_u_on_v)

Output:

Projection of Vector u on Vector v is:  [1.76923077 2.12307692 0.70769231]

One liner code for projecting a vector onto another vector:

(np.dot(u, v)/np.dot(v, v))*v

Projection of a Vector onto a Plane

Computing vector projection onto a Plane in Python:

# import numpy to perform operations on vector
import numpy as np
  
# vector u 
u = np.array([2, 5, 8])       
  
# vector n: n is orthogonal vector to Plane P
n = np.array([1, 1, 7])       
   
# Task: Project vector u on Plane P
  
# finding norm of the vector n 
n_norm = np. sqrt(sum(n**2))    
   
# Apply the formula as mentioned above
# for projecting a vector onto the orthogonal vector n
# find dot product using np.dot()
proj_of_u_on_n = (np.dot(u, n)/n_norm**2)*n
  
# subtract proj_of_u_on_n from u: 
# this is the projection of u on Plane P
print("Projection of Vector u on Plane P is: ", u - proj_of_u_on_n)

Output:

Projection of Vector u on Plane P is:  [ 0.76470588  3.76470588 -0.64705]

How to use Python to calculate Vector Projection?

Question from StackOverFlow

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, y))
z1=float(np.dot(x, x))
z2=np. sqrt(z1)
z3=(z/z2**2)
x*z3

Answer:

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.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically