Runoob Tutorial ArchiveEnglish

Vector Addition and Scalar Multiplication — Geometric Perspective

Vector Addition and Scalar Multiplication -- Geometric Perspective

Vector Addition: Concatenating Two Displacements

A vector can be understood as an arrow starting from the origin—it has length and direction.

Adding two vectors is like "concatenating" two displacements.

** Triangle Rule

Connect the starting point of the second vector b to the endpoint of the first vector a.

The line connecting the start of a to the end of b is a + b.

Intuition: "Go a first, then go b"

Parallelogram Law

Draw a parallelogram with a and b as adjacent sides.

The diagonal starting from the origin is a + b.

Intuition: The results of both laws are exactly the same.

Scalar Multiplication: Stretching or shrinking a vector

Multiplying a vector by a scalar (a regular number) results in changing the vector's length while keeping its direction along the same line unchanged.

|c| > 1

Stretch

e.g., 2v: length * 2

0 < |c| < 1

Shorten

e.g., 0.5v: length / 2

c < 0

Reverse direction

e.g., -v: opposite direction

c = 0

Shrink to a point

For example, 0v = zero vector

Scalar multiplication only changes the "length" of the vector, not the direction of the line it lies on (a negative number reverses the direction but remains on the same line).

This property is calledCollinearity—the two vectors before and after scalar multiplication are always on the same line.


Real-life example

**Vector Addition: Multi-segment Journeys in Navigation

You start from home, first walk 300 meters east (vector a = (300, 0)), and then walk 400 meters north (vector b = (0, 400)).

You end up 500 meters northeast of home—this is a + b = (300, 400).

**Scalar Multiplication: Video Playback Speed

Walking 5 steps at normal speed takes 5 seconds. At 2x speed—same direction, same number of steps—it only takes 2.5 seconds.

The displacement vector's direction hasn't changed, but the "size of each step" has doubled—this is the effect of 2v.

**Reverse: Walking back the way you came

If you walked 100 meters east (v = (100, 0)), and then want to return to the origin—you need to walk 100 meters west (-v = (-100, 0)).

The negative sign is the "turn around" instruction for the vector direction.


Mathematical Definition

Vector Addition

When adding two vectors of the same dimension, you add the corresponding elements.

[ \mathbf{a} + \mathbf{b} = \begin{bmatrix} a_1 \ a_2 \ \vdots \ a_n \end{bmatrix} + \begin{bmatrix} b_1 \ b_2 \ \vdots \ b_n \end{bmatrix} = \begin{bmatrix} a_1 + b_1 \ a_2 + b_2 \ \vdots \ a_n + b_n \end{bmatrix} ]

**Prerequisite: The two vector dimensions must be the same, otherwise addition is meaningless.

Vector subtraction

Vector subtraction = adding the opposite vector: ( \mathbf{a} - \mathbf{b} = \mathbf{a} + (-\mathbf{b}) )

Scalar multiplication of a vector

Multiplying a vector by a scalar c means multiplying every element of the vector by c:

[ c \cdot \mathbf{v} = c \cdot \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} ]

Operation Properties Quick Reference

Properties Formulas Intuition
Commutative Law ( \mathbf{a} + \mathbf{b} = \mathbf{b} + \mathbf{a} ) The order of execution does not matter
Associative Law ( (\mathbf{a}+\mathbf{b})+\mathbf{c} = \mathbf{a}+(\mathbf{b}+\mathbf{c}) ) Grouping in any way yields the same result
Distributive law ( c(\mathbf{a}+\mathbf{b}) = c\mathbf{a} + c\mathbf{b} ) Add then stretch = Stretch then add

Python hands-on practice

Example

import

numpy

as

np

Create two vectors

a_runoob

=

np.

array

(

[

3

,

1

]

)

   

a = (3, 1)

b_runoob

=

np.

array

(

[

1

,

2

]

)

   

b = (1, 2)

Vector addition: corresponding positions are added

add_result

=

a_runoob + b_runoob

print

(

"a + b ="

,

add_result

)

   

[4, 3]

Vector subtraction

sub_result

=

a_runoob - b_runoob

print

(

"a - b ="

,

sub_result

)

   

[2, -1]

Scalar multiplication: multiply each element by a scalar

c

=

2.5

scale_result

=

c * a_runoob

print

(

f

"{c} * a ="

,

scale_result

)

 

[7.5, 2.5]

Negative vector: direction is completely opposite

neg

=

-a_runoob

print

(

"-a ="

,

neg

)

             

[-3, -1]

NumPy broadcasting: direct operation between scalar and vector elements

print

(

"a + 10 ="

,

a_runoob +

10

)

 

Add 10 to each element

print

(

"a * 3 = "

,

a_runoob *

3

)

   

Multiply each element by 3



a + b = [4 3]

a - b = [2 -1]

2.5 * a = [7.5 2.5]

-a = [-3 -1]

a + 10 = [13 11]

a * 3  = [9 3]

Example

import

numpy

as

np

import

matplotlib.

pyplot

as

plt

plt.

rcParams

[

'font.sans-serif'

]

=

[

'Arial Unicode MS'

,

'SimHei'

,

'DejaVu Sans'

]

plt.

rcParams

[

'axes.unicode_minus'

]

=

False

RUnoob_a

=

np.

array

(

[

3

,

1

]

)

RUnoob_b

=

np.

array

(

[

1

,

2

]

)

RUnoob_sum

=

RUnoob_a + RUnoob_b

fig

,

axes

=

plt.

subplots

(

1

,

2

,

figsize

=

(

12

,

5

)

)

Left figure: Triangle inequality

for

ax

,

title

in

zip

(

axes

,

[

'Triangle Rule'

,

'Parallelogram Rule'

]

)

:

ax.

set_xlim

(

1

,

6

)

;

ax.

set_ylim

(

1

,

5

)

    ax.

axhline

(

y

=

0

,

color

=

'gray'

,

lw

=

0.5

)

;

ax.

axvline

(

x

=

0

,

color

=

'gray'

,

lw

=

0.5

)

    ax.

set_aspect

(

'equal'

)

;

ax.

grid

(

True

,

alpha

=

0.3

)

;

ax.

set_title

(

title

)

Draw triangle rule

ax

=

axes

[

0

]

ax.

arrow

(

0

,

0

,

RUnoob_a

[

0

]

,

RUnoob_a

[

1

]

,

head_width

=

0.2

,

head_length

=

0.2

,

fc

=

'#e74c3c'

,

ec

=

'#e74c3c'

,

lw

=

2

,

label

=

'a'

)

ax.

arrow

(

RUnoob_a

[

0

]

,

RUnoob_a

[

1

]

,

RUnoob_b

[

0

]

,

RUnoob_b

[

1

]

,

head_width

=

0.2

,

head_length

=

0.2

,

fc

=

'#3498db'

,

ec

=

'#3498db'

,

lw

=

2

,

label

=

'b'

)

ax.

arrow

(

0

,

0

,

RUnoob_sum

[

0

]

,

RUnoob_sum

[

1

]

,

head_width

=

0.2

,

head_length

=

0.2

,

fc

=

'#2ecc71'

,

ec

=

'#2ecc71'

,

lw

=

2

,

ls

=

'dashed'

,

label

=

'a+b'

)

ax.

legend

(

)

Draw the parallelogram rule

ax

=

axes

[

1

]

ax.

arrow

(

0

,

0

,

RUnoob_a

[

0

]

,

RUnoob_a

[

1

]

,

head_width

=

0.2

,

head_length

=

0.2

,

fc

=

'#e74c3c'

,

ec

=

'#e74c3c'

,

lw

=

2

,

label

=

'a'

)

ax.

arrow

(

0

,

0

,

RUnoob_b

[

0

]

,

RUnoob_b

[

1

]

,

head_width

=

0.2

,

head_length

=

0.2

,

fc

=

'#3498db'

,

ec

=

'#3498db'

,

lw

=

2

,

label

=

'b'

)

ax.

arrow

(

RUnoob_a

[

0

]

,

RUnoob_a

[

1

]

,

RUnoob_b

[

0

]

,

RUnoob_b

[

1

]

,

head_width

=

0.2

,

head_length

=

0.2

,

fc

=

'#3498db'

,

ec

=

'#3498db'

,

lw

=

1.5

,

ls

=

'dotted'

,

alpha

=

0.6

)

ax.

arrow

(

RUnoob_b

[

0

]

,

RUnoob_b

[

1

]

,

RUnoob_a

[

0

]

,

RUnoob_a

[

1

]

,

head_width

=

0.2

,

head_length

=

0.2

,

fc

=

'#e74c3c'

,

ec

=

'#e74c3c'

,

lw

=

1.5

,

ls

=

'dotted'

,

alpha

=

0.6

)

ax.

arrow

(

0

,

0

,

RUnoob_sum

[

0

]

,

RUnoob_sum

[

1

]

,

head_width

=

0.2

,

head_length

=

0.2

,

fc

=

'#2ecc71'

,

ec

=

'#2ecc71'

,

lw

=

2

,

ls

=

'dashed'

,

label

=

'a+b'

)

ax.

legend

(

)

plt.

tight_layout

(

)

;

plt.

show

(

)

Interactive vector addition demonstration

Drag the endpoints of the red vector a and the blue vector b below to observe the real-time change of the green vector and vector a+b. Understand the triangle rule and the parallelogram rule:


Application scenarios in AI

Semantic operations of word vectors

The most famous discovery of word embedding models like Word2Vec and GloVe is that vector addition and subtraction have semantic meaning.

[ \text{vec}(\text{king}) - \text{vec}(\text{man}) + \text{vec}(\text{woman}) \approx \text{vec}(\text{queen}) ]

Subtraction removes the "male" feature component, and addition adds the "female" feature component. The resulting vector is closest to "queen" in the embedding space.

This semantic operation is not limited to gender—"Paris - France + Italy $\approx$ Rome" follows the same principle. Vector addition and subtraction here act as "feature editing."

Bias in neural networks

In the calculation of each neuron ($y = Wx + b$), $+b$ is vector addition—the bias shifts the result of $wx$ as a whole.

Without the bias $b$, the model's output must be 0 when $x=0$, which severely limits the model's expressive power. The bias is equivalent to giving each neuron a "default activation value."

Gradient update

The parameter update during training ($\theta_{new} = \theta_{old} - \eta \nabla J$) is a combination of scalar multiplication (learning rate $\times$ gradient) and vector subtraction.

Scalar multiplication controls the size of each step update (a larger learning rate means a larger step), and subtraction moves the parameters in the direction of the loss decrease.

Data preprocessing: Mean centering

Before training a neural network, data is usually subjected to a "mean subtraction" operation: ($x_{norm} = x - \mu$). This is vector subtraction—translating the data center to the origin, which helps gradient descent converge faster.