Implementing Vectors in Python
Defining Vectors in Python
In Python, we use NumPy arrays to define 2D vectors like this:
1234567import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) print(f'v1 = {v1}') print(f'v2 = {v2}')
These represent the vectors:
v1=(2,1),v2=(1,3)These can now be added, subtracted, or used in dot product and magnitude calculations.
Vector Addition
To compute vector addition:
1234567import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) v3 = v1 + v2 print(f'v3 = v1 + v2 = {v3}')
This performs:
(2,1)+(1,3)=(3,4)This matches the rule for vector addition:
a+b=(a1+b1,a2+b2)Vector Magnitude (Length)
To calculate magnitude in Python:
np.linalg.norm(v)
For vector [3, 4]:
123import numpy as np print(np.linalg.norm([3, 4])) # 5.0
This uses the formula:
∣a∣=a12+a22Dot Product
To calculate the dot product:
123import numpy as np print(np.dot([1, 2], [2, 3]))
Which gives:
[1,2]⋅[2,3]=1⋅2+2⋅3=8Dot product general rule:
a⋅b=a1b1+a2b2Visualizing Vectors with Matplotlib
You can use the quiver() function in Matplotlib to draw arrows representing vectors and their resultant. Each arrow shows the position, direction, and magnitude of a vector.
- Blue: v1, drawn from the origin;
- Green: v2, starting at the head of v1;
- Red: resultant vector, drawn from the origin to the final tip.
Example:
123456789101112131415161718import matplotlib.pyplot as plt fig, ax = plt.subplots() # v1 ax.quiver(0, 0, 2, 1, color='blue', angles='xy', scale_units='xy', scale=1) # v2 (head-to-tail) ax.quiver(2, 1, 1, 3, color='green', angles='xy', scale_units='xy', scale=1) # resultant ax.quiver(0, 0, 3, 4, color='red', angles='xy', scale_units='xy', scale=1) plt.xlim(0, 5) plt.ylim(0, 5) plt.grid(True) plt.title('Vector Addition (Head-to-Tail Method)') plt.show()
Parameters (based on the first quiver call):
ax.quiver(0, 0, 2, 1, color='blue', angles='xy', scale_units='xy', scale=1)
0, 0– starting point of the vector (origin);2, 1– vector components in the x and y directions;color='blue'– sets the arrow color to blue;angles='xy'– draws the arrow using Cartesian coordinates (x–y plane);scale_units='xy'– scales the arrow according to the same units as the axes;scale=1– keeps the arrow’s true length (no automatic scaling).
This plot shows the head-to-tail vector addition, where the red vector represents the sum v1+v2.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 1.96
Implementing Vectors in Python
Swipe to show menu
Defining Vectors in Python
In Python, we use NumPy arrays to define 2D vectors like this:
1234567import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) print(f'v1 = {v1}') print(f'v2 = {v2}')
These represent the vectors:
v1=(2,1),v2=(1,3)These can now be added, subtracted, or used in dot product and magnitude calculations.
Vector Addition
To compute vector addition:
1234567import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) v3 = v1 + v2 print(f'v3 = v1 + v2 = {v3}')
This performs:
(2,1)+(1,3)=(3,4)This matches the rule for vector addition:
a+b=(a1+b1,a2+b2)Vector Magnitude (Length)
To calculate magnitude in Python:
np.linalg.norm(v)
For vector [3, 4]:
123import numpy as np print(np.linalg.norm([3, 4])) # 5.0
This uses the formula:
∣a∣=a12+a22Dot Product
To calculate the dot product:
123import numpy as np print(np.dot([1, 2], [2, 3]))
Which gives:
[1,2]⋅[2,3]=1⋅2+2⋅3=8Dot product general rule:
a⋅b=a1b1+a2b2Visualizing Vectors with Matplotlib
You can use the quiver() function in Matplotlib to draw arrows representing vectors and their resultant. Each arrow shows the position, direction, and magnitude of a vector.
- Blue: v1, drawn from the origin;
- Green: v2, starting at the head of v1;
- Red: resultant vector, drawn from the origin to the final tip.
Example:
123456789101112131415161718import matplotlib.pyplot as plt fig, ax = plt.subplots() # v1 ax.quiver(0, 0, 2, 1, color='blue', angles='xy', scale_units='xy', scale=1) # v2 (head-to-tail) ax.quiver(2, 1, 1, 3, color='green', angles='xy', scale_units='xy', scale=1) # resultant ax.quiver(0, 0, 3, 4, color='red', angles='xy', scale_units='xy', scale=1) plt.xlim(0, 5) plt.ylim(0, 5) plt.grid(True) plt.title('Vector Addition (Head-to-Tail Method)') plt.show()
Parameters (based on the first quiver call):
ax.quiver(0, 0, 2, 1, color='blue', angles='xy', scale_units='xy', scale=1)
0, 0– starting point of the vector (origin);2, 1– vector components in the x and y directions;color='blue'– sets the arrow color to blue;angles='xy'– draws the arrow using Cartesian coordinates (x–y plane);scale_units='xy'– scales the arrow according to the same units as the axes;scale=1– keeps the arrow’s true length (no automatic scaling).
This plot shows the head-to-tail vector addition, where the red vector represents the sum v1+v2.
Thanks for your feedback!