向量的缩放是一个基本运算,它涉及将向量乘以一个数。这个数称为标量。标量在线性代数中就是一个普通数字(如 5、-2.7 或 $\pi$),与向量或矩阵不同。标量乘法改变向量的大小(长度),如果标量为负,则改变其方向。这就像拉伸、收缩或翻转向量。标量乘法的工作原理要进行标量乘法,您需要将向量的每个元素乘以标量值。在数学上,如果您有一个标量 $c$ 和一个具有 $n$ 个元素的向量 $\mathbf{v}$:$$ \mathbf{v} = \begin{bmatrix} v_1 \ v_2 \ \vdots \ v_n \end{bmatrix} $$那么标量乘法 $c\mathbf{v}$ 定义为:$$ c\mathbf{v} = c \begin{bmatrix} v_1 \ v_2 \ \vdots \ v_n \end{bmatrix} = \begin{bmatrix} c \cdot v_1 \ c \cdot v_2 \ \vdots \ c \cdot v_n \end{bmatrix} $$原始向量 $\mathbf{v}$ 的每个分量都乘以标量 $c$。结果向量与原始向量维度相同。几何意义从几何角度看,将向量 $\mathbf{v}$ 乘以标量 $c$ 会得到一个新向量,如果 $c > 0$,则方向与 $\mathbf{v}$ 相同;如果 $c < 0$,则方向相反。新向量的长度是原始向量 $\mathbf{v}$ 长度的 $|c|$ 倍。如果 $c = 0$,结果是零向量(所有元素都为零的向量)。考虑一个二维向量 $\mathbf{v} = \begin{bmatrix} 2 \ 1 \end{bmatrix}$。如果我们将 $c=2$ 乘以向量,得到 $2\mathbf{v} = \begin{bmatrix} 2 \cdot 2 \ 2 \cdot 1 \end{bmatrix} = \begin{bmatrix} 4 \ 2 \end{bmatrix}$。这个向量方向相同但长度是两倍。如果我们将 $c=-1$ 乘以向量,得到 $-1\mathbf{v} = \begin{bmatrix} -1 \cdot 2 \ -1 \cdot 1 \end{bmatrix} = \begin{bmatrix} -2 \ -1 \end{bmatrix}$。这个向量长度相同但方向相反。如果我们将 $c=0.5$ 乘以向量,得到 $0.5\mathbf{v} = \begin{bmatrix} 0.5 \cdot 2 \ 0.5 \cdot 1 \end{bmatrix} = \begin{bmatrix} 1 \ 0.5 \end{bmatrix}$。这个向量方向相同但长度是其一半。{ "layout": { "xaxis": { "range": [-5, 5], "title": "x轴", "zeroline": true, "zerolinecolor": "#adb5bd"}, "yaxis": { "range": [-3, 3], "title": "y轴", "zeroline": true, "zerolinecolor": "#adb5bd", "scaleanchor": "x", "scaleratio": 1}, "showlegend": true, "title": "标量乘法的几何影响", "margin": {"l": 40, "r": 20, "t": 40, "b": 40}, "width": 500, "height": 350, "annotations": [ {"x": 2, "y": 1, "ax": 0, "ay": 0, "xref": "x", "yref": "y", "axref": "pixel", "ayref": "pixel", "showarrow": true, "arrowhead": 2, "arrowsize": 1, "arrowwidth": 2, "arrowcolor": "#4263eb"}, {"x": 4, "y": 2, "ax": 0, "ay": 0, "xref": "x", "yref": "y", "axref": "pixel", "ayref": "pixel", "showarrow": true, "arrowhead": 2, "arrowsize": 1, "arrowwidth": 2, "arrowcolor": "#37b24d"}, {"x": -2, "y": -1, "ax": 0, "ay": 0, "xref": "x", "yref": "y", "axref": "pixel", "ayref": "pixel", "showarrow": true, "arrowhead": 2, "arrowsize": 1, "arrowwidth": 2, "arrowcolor": "#f03e3e"} ] }, "data": [ {"type": "scatter", "x": [2], "y": [1], "mode": "markers+text", "name": "v = [2, 1]", "text": ["v"], "textposition": "top right", "marker": {"color": "#4263eb", "size": 8}}, {"type": "scatter", "x": [4], "y": [2], "mode": "markers+text", "name": "2v = [4, 2]", "text": ["2v"], "textposition": "top right", "marker": {"color": "#37b24d", "size": 8}}, {"type": "scatter", "x": [-2], "y": [-1], "mode": "markers+text", "name": "-1v = [-2, -1]", "text": ["-v"], "textposition": "bottom left", "marker": {"color": "#f03e3e", "size": 8}} ] }原始向量 $\mathbf{v}$(蓝色),按 2 缩放(绿色),以及按 -1 缩放(红色)。缩放改变长度并可能改变方向。使用 NumPy 在 Python 中进行标量乘法NumPy 使用标准乘法运算符 * 使标量乘法变得简单。我们来定义向量 $\mathbf{v}$ 和标量 $c$。import numpy as np # 定义向量 v v = np.array([2, 1]) # 定义标量 c c = 3 # 执行标量乘法 scaled_v = c * v print(f"原始向量 v: {v}") print(f"标量 c: {c}") print(f"缩放后的向量 c*v: {scaled_v}")输出:Original vector v: [2 1] Scalar c: 3 Scaled vector c*v: [6 3]如预期的那样,NumPy 将向量 v 的每个元素乘以标量 c=3。我们来尝试使用负标量进行缩放:# 定义一个负标量 c_neg = -1.5 # 执行标量乘法 scaled_neg_v = c_neg * v print(f"原始向量 v: {v}") print(f"负标量 c_neg: {c_neg}") print(f"缩放后的向量 c_neg*v: {scaled_neg_v}")输出:Original vector v: [2 1] Negative scalar c_neg: -1.5 Scaled vector c_neg*v: [-3. -1.5]同样,每个元素 [2, 1] 乘以 -1.5 得到 [-3.0, -1.5]。请注意 NumPy 自动处理浮点数结果。标量乘法是线性代数和机器学习中常使用的基本组成部分,通常用于调整由向量表示的特征的影响或比例。