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

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:

v1=(2,1),v2=(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]=12+23=8[1, 2] \cdot [2, 3] = 1 \cdot 2 + 2 \cdot 3 = 8

Dot product general rule:

ab=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: v1\vec{v}_1, drawn from the origin;
  • Green: v2\vec{v}_2, starting at the head of v1\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 v1+v2\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]?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  2

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 4.  2
some-alt