跳轉到

1.3 向量與線性組合#

本節目標#

  • 用箭頭理解向量的加法(平行四邊形法則)與純量乘法
  • 用實驗觀察線性組合 \(\alpha\mathbf{u}+\beta\mathbf{v}\) 的「可達區域」
  • 學會單位向量、內積與夾角公式,並用 NumPy + matplotlib 畫出來

向量:有方向的一步#

數學上,二維向量是一對數字:

\[ \mathbf{v} = \begin{bmatrix} 3 \\ 2 \end{bmatrix} \]

幾何上,它是從原點出發的一支箭頭:往右 3、往上 2。 重點不是箭頭畫在哪裡,而是「往右 3、往上 2」這個位移本身。

加法:平行四邊形法則#

\[ \mathbf{u} + \mathbf{v} = \begin{bmatrix} u_1 \\ u_2 \end{bmatrix} + \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \begin{bmatrix} u_1 + v_1 \\ u_2 + v_2 \end{bmatrix} \]

幾何意義:先走 \(\mathbf{u}\) 再走 \(\mathbf{v}\) (頭接尾),或把它們當兩邊畫出平行四邊形,對角線就是和。

純量乘法:拉長或縮短#

\[ \alpha\mathbf{v} = \begin{bmatrix} \alpha v_1 \\ \alpha v_2 \end{bmatrix} \]

\(\alpha > 1\) 拉長、 \(0 < \alpha < 1\) 縮短、 \(\alpha < 0\) 反向。

畫出來看#

import numpy as np
import matplotlib.pyplot as plt

def draw_vectors(vecs, colors, labels):
    fig, ax = plt.subplots(figsize=(5, 5))
    for v, c, label in zip(vecs, colors, labels):
        ax.quiver(0, 0, v[0], v[1], angles='xy',
                  scale_units='xy', scale=1, color=c, label=label)
    ax.set_xlim(-1, 6); ax.set_ylim(-1, 6)
    ax.grid(); ax.legend(); ax.set_aspect('equal')
    plt.show()

u = np.array([3, 1])
v = np.array([1, 2])
draw_vectors([u, v, u + v], ['tab:blue', 'tab:green', 'tab:red'],
             ['u', 'v', 'u+v'])

圖中紅色箭頭 \(\mathbf{u}+\mathbf{v}\) 正好是藍、綠兩箭頭張出的平行四邊形對角線。


線性組合:你能走到哪裡?#

給定兩個向量 \(\mathbf{u}, \mathbf{v}\) ,所有

\[ \alpha\mathbf{u} + \beta\mathbf{v}, \quad \alpha, \beta \in \mathbb{R} \]

的組合,構成一個集合——在 2D 且 \(\mathbf{u},\mathbf{v}\) 不平行時,整個平面都走得到。 這組 \((\alpha, \beta)\) 就是你在新座標系(以 \(\mathbf{u}, \mathbf{v}\) 為軸)裡的地址。

實驗:畫出係數網格#

固定 \(\alpha, \beta\) 在 \(-2\) 到 \(2\) 之間取整數,畫出所有組合點:

import numpy as np
import matplotlib.pyplot as plt

u = np.array([2, 1])
v = np.array([1, 2])

alphas = np.arange(-2, 3)
betas  = np.arange(-2, 3)

fig, ax = plt.subplots(figsize=(6, 6))
for a in alphas:
    for b in betas:
        p = a * u + b * v              # 線性組合
        ax.plot(p[0], p[1], 'o', color='tab:purple', markersize=4)

# 特別標出幾個關鍵點
for a, b, c in [(1, 0, 'tab:blue'), (0, 1, 'tab:green'), (1, 1, 'tab:red')]:
    p = a * u + b * v
    ax.plot(p[0], p[1], 'o', color=c, markersize=8)

ax.quiver(0, 0, u[0], u[1], angles='xy', scale_units='xy',
          scale=1, color='tab:blue', width=0.006)
ax.quiver(0, 0, v[0], v[1], angles='xy', scale_units='xy',
          scale=1, color='tab:green', width=0.006)

ax.set_xlim(-6, 6); ax.set_ylim(-6, 6)
ax.grid(); ax.set_aspect('equal')
ax.set_title(r'$\alpha u + \beta v$,  $\alpha,\beta \in \{-2..2\}$')
plt.show()

你會看到一個傾斜的平行四邊形網格鋪滿平面——這就是 \(\mathbf{u}, \mathbf{v}\) 張成的世界。 如果 \(\mathbf{u}, \mathbf{v}\) 平行(線性相依),網格會塌縮成一條線——這正是「線性獨立」幾何意義的預告。


單位向量與內積#

單位向量:長度為 1 的方向#

向量長度(歐氏範數):

\[ \|\mathbf{v}\| = \sqrt{v_1^2 + v_2^2} \]

把它除以自己的長度就得到單位向量 \(\hat{\mathbf{v}} = \dfrac{\mathbf{v}}{\|\mathbf{v}\|}\) ,只保留方向。

內積:度量「方向有多一致」#

\[ \mathbf{u} \cdot \mathbf{v} = u_1 v_1 + u_2 v_2 = \|\mathbf{u}\|\,\|\mathbf{v}\|\cos\theta \]

反過來解出夾角:

\[ \cos\theta = \frac{\mathbf{u} \cdot \mathbf{v}}{\|\mathbf{u}\|\,\|\mathbf{v}\|} \]
內積符號 幾何意義
\(\mathbf{u}\cdot\mathbf{v} > 0\) 夾角小於 90°,方向大致相同
\(\mathbf{u}\cdot\mathbf{v} = 0\) 垂直(機器學習稱「正交」)
\(\mathbf{u}\cdot\mathbf{v} < 0\) 夾角大於 90°,方向大致相反

NumPy 實作#

u = np.array([3, 4])
v = np.array([4, 3])

length = np.linalg.norm(u)          # 5.0
u_unit = u / length                  # 單位向量

dot = np.dot(u, v)                   # 或 u @ v
cos_theta = dot / (np.linalg.norm(u) * np.linalg.norm(v))
theta = np.degrees(np.arccos(cos_theta))

print(f"|u| = {length}")             # 5.0
print(f"單位向量 = {u_unit}")        # [0.6 0.8]
print(f"夾角 = {theta:.2f} 度")      # 16.26 度

驗證: \(3\times4 + 4\times3 = 24\) , \(\|\mathbf{u}\| = 5\) , \(\|\mathbf{v}\| = 5\) , \(\cos\theta = 24/25 = 0.96\) , \(\theta \approx 16.26°\) 。✓


小結與練習#

本節重點 1. 向量加法 = 平行四邊形對角線;純量乘 = 沿同方向伸縮。 2. 線性組合 \(\alpha\mathbf{u}+\beta\mathbf{v}\) 張出的網格,預告了「向量空間」與「線性獨立」。 3. 內積連結代數(乘加)與幾何(夾角),是機器學習中「相似度」的基礎。

練習 1. 把實驗中的 u 改成 [1, 2]、v 改成 [2, 4],觀察網格發生什麼事,說明原因。 2. 用 NumPy 求 [1, 2] 與 [-2, 1] 的內積與夾角,結果有什麼特殊性? 3. 畫出 \(\mathbf{u}=[2,0]\) 、 \(\mathbf{v}=[0,3]\) 的組合網格,它和標準座標網格差在哪? 4. 證明:對任意向量 \(\mathbf{v}\) , \(\mathbf{v}\cdot\mathbf{v} = \|\mathbf{v}\|^2\) 。