Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Implementing Vectors in Python | Linear Algebra Foundations
Mathematics for Data Science

bookImplementing Vectors in Python

Defining Vectors in Python

In Python, we use NumPy arrays to define 2D vectors like this:

1234567
import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) print(f'v1 = {v1}') print(f'v2 = {v2}')
copy

These represent the vectors:

v⃗1=(2,1),v⃗2=(1,3)\vec{v}_1 = (2, 1), \quad \vec{v}_2 = (1, 3)

These can now be added, subtracted, or used in dot product and magnitude calculations.

Vector Addition

To compute vector addition:

1234567
import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) v3 = v1 + v2 print(f'v3 = v1 + v2 = {v3}')
copy

This performs:

(2,1)+(1,3)=(3,4)(2, 1) + (1, 3) = (3, 4)

This matches the rule for vector addition:

aβƒ—+bβƒ—=(a1+b1,β€…β€Ša2+b2)\vec{a} + \vec{b} = (a_1 + b_1, \; a_2 + b_2)

Vector Magnitude (Length)

To calculate magnitude in Python:

np.linalg.norm(v)

For vector [3, 4]:

123
import numpy as np print(np.linalg.norm([3, 4])) # 5.0
copy

This uses the formula:

∣aβƒ—βˆ£=a12+a22|\vec{a}| = \sqrt{a_1^2 + a_2^2}

Dot Product

To calculate the dot product:

123
import numpy as np print(np.dot([1, 2], [2, 3]))
copy

Which gives:

[1,2]β‹…[2,3]=1β‹…2+2β‹…3=8[1, 2] \cdot [2, 3] = 1 \cdot 2 + 2 \cdot 3 = 8

Dot product general rule:

a⃗⋅b⃗=a1b1+a2b2\vec{a} \cdot \vec{b} = a_1 b_1 + a_2 b_2

Visualizing 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: vβƒ—1\vec{v}_1, drawn from the origin;
  • Green: vβƒ—2\vec{v}_2, starting at the head of vβƒ—1\vec{v}_1;
  • Red: resultant vector, drawn from the origin to the final tip.

Example:

123456789101112131415161718
import 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()
copy

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 v⃗1+v⃗2\vec{v}_1 + \vec{v}_2.

question mark

Which code correctly computes the dot product of [1,2][1,2] and [2,3][2,3]?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how vector subtraction works in Python?

How do I interpret the plot generated by the code?

Can you show how to calculate the angle between two vectors?

Awesome!

Completion rate improved to 1.96

bookImplementing Vectors in Python

Swipe to show menu

Defining Vectors in Python

In Python, we use NumPy arrays to define 2D vectors like this:

1234567
import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) print(f'v1 = {v1}') print(f'v2 = {v2}')
copy

These represent the vectors:

v⃗1=(2,1),v⃗2=(1,3)\vec{v}_1 = (2, 1), \quad \vec{v}_2 = (1, 3)

These can now be added, subtracted, or used in dot product and magnitude calculations.

Vector Addition

To compute vector addition:

1234567
import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) v3 = v1 + v2 print(f'v3 = v1 + v2 = {v3}')
copy

This performs:

(2,1)+(1,3)=(3,4)(2, 1) + (1, 3) = (3, 4)

This matches the rule for vector addition:

aβƒ—+bβƒ—=(a1+b1,β€…β€Ša2+b2)\vec{a} + \vec{b} = (a_1 + b_1, \; a_2 + b_2)

Vector Magnitude (Length)

To calculate magnitude in Python:

np.linalg.norm(v)

For vector [3, 4]:

123
import numpy as np print(np.linalg.norm([3, 4])) # 5.0
copy

This uses the formula:

∣aβƒ—βˆ£=a12+a22|\vec{a}| = \sqrt{a_1^2 + a_2^2}

Dot Product

To calculate the dot product:

123
import numpy as np print(np.dot([1, 2], [2, 3]))
copy

Which gives:

[1,2]β‹…[2,3]=1β‹…2+2β‹…3=8[1, 2] \cdot [2, 3] = 1 \cdot 2 + 2 \cdot 3 = 8

Dot product general rule:

a⃗⋅b⃗=a1b1+a2b2\vec{a} \cdot \vec{b} = a_1 b_1 + a_2 b_2

Visualizing 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: vβƒ—1\vec{v}_1, drawn from the origin;
  • Green: vβƒ—2\vec{v}_2, starting at the head of vβƒ—1\vec{v}_1;
  • Red: resultant vector, drawn from the origin to the final tip.

Example:

123456789101112131415161718
import 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()
copy

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 v⃗1+v⃗2\vec{v}_1 + \vec{v}_2.

question mark

Which code correctly computes the dot product of [1,2][1,2] and [2,3][2,3]?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2
some-alt