Scalar Multiplication
Multiplying a vector by a number, a scalar, multiplies every component by that number. Geometrically it stretches or shrinks the arrow, and a negative scalar also flips it around. Together with addition, this is the second basic operation of linear algebra.
The definition
For a scalar and a vector :
"Scalar" just means "a single number" (from the field you work over, usually ). The name comes from the fact that it scales vectors.
The geometry
| Scalar | Effect on the arrow |
|---|---|
| same direction, longer | |
| same direction, shorter | |
| unchanged | |
| the zero vector | |
| same length, opposite direction | |
| opposite direction, stretched by |
The length of is times the length of (see Length of a Vector).
Two non-zero vectors are parallel (or collinear) if one is a scalar multiple of the other. and are parallel, since ; they point in opposite directions.
from fractions import Fraction as F
def parallel_factor(u, v):
"""Return c with u = c*v, or None if u is not a multiple of v (v non-zero)."""
c = None
for a, b in zip(u, v):
if b == 0:
if a != 0:
return None
continue
r = F(a, b)
if c is None:
c = r
elif r != c:
return None
return c
print(parallel_factor((2, -6), (-1, 3)))
print(parallel_factor((4, 0, 6), (2, 0, 3)))
print(parallel_factor((1, 2, 3), (2, 4, 7)))-2
2
None
The rules
For all vectors and scalars :
| Rule | Formula |
|---|---|
| distributive over vector addition | |
| distributive over scalar addition | |
| associative | |
| identity |
Consequences: and . For example, , and subtracting from both sides gives .
from sympy import Matrix, symbols, expand
c, d = symbols("c d")
u = Matrix(symbols("u1:4"))
v = Matrix(symbols("v1:4"))
print((c * (u + v) - (c * u + c * v)).expand() == Matrix([0, 0, 0]))
print(((c + d) * v - (c * v + d * v)).expand() == Matrix([0, 0, 0]))
print(c * (d * v) == (c * d) * v)
print(list(0 * v), list(-1 * v))True
True
True
[0, 0, 0] [-v1, -v2, -v3]
In NumPy: broadcasting
NumPy multiplies an array by a number component-wise, and it can scale many vectors at once:
import numpy as np
v = np.array([2.0, -1.0, 4.0])
print("3v =", 3 * v)
print("-v/2 =", -v / 2)
# scale each row (a velocity) by its own factor
velocities = np.array([[1.0, 0.0], [0.0, 2.0], [3.0, 4.0]])
factors = np.array([10.0, 0.5, -1.0])
print(factors[:, None] * velocities)
# make a colour 40% darker (RGB values in 0..1)
orange = np.array([1.0, 0.6, 0.2])
print("darker:", np.round(0.6 * orange, 2))3v = [ 6. -3. 12.]
-v/2 = [-1. 0.5 -2. ]
[[10. 0.]
[ 0. 1.]
[-3. -4.]]
darker: [0.6 0.36 0.12]
factors[:, None] turns the factors into a column of shape (3, 1), so each row is scaled by its own number. Broadcasting is explained in NumPy Broadcasting.
In software
- Games: gives a velocity; gives the distance moved in one frame. Reversing a bouncing ball's velocity component is multiplication by (or by for a ball that loses energy).
- ML: the learning rate scales the gradient in each update, . Too large a scalar overshoots, too small barely moves.
- Graphics: scaling an RGB colour by a factor dims or brightens it; scaling a direction to length 1 is called normalizing (next lessons).
Exercises
Exercise 1: Compute
Compute .
Answer
(5, 2, 4).
Solution
Exercise 2: Parallel?
Which pairs are parallel? (a) and ; (b) and ; (c) and .
Answer
(a) and (c).
Solution
Exercise 3: Solve for the scalar
Find such that .
Answer
.
Solution
Exercise 4: Bounce with energy loss (code)
A ball has velocity . When it hits the floor, the vertical component is multiplied by . Simulate 3 bounces (ignore gravity between bounces) and print the velocity after each.
Answer
, , .
Solution
Only the y-component is scaled, which is multiplication by the vector component-wise, not a scalar multiplication of the whole vector. (Without gravity, the "bounces" just flip the sign each time, which is fine for this exercise.)
import numpy as np
v = np.array([4.0, -10.0])
for bounce in range(3):
v = v * np.array([1.0, -0.7])
print(bounce + 1, np.round(v, 2))1 [4. 7.]
2 [ 4. -4.9]
3 [4. 3.43]
Test yourself
What is ?
If and , what must be?
- 1
- -1
- 0
- Any number
What does multiplying a vector by do geometrically?
- Rotates it by 90°
- Halves its length and reverses its direction
- Doubles its length
- Leaves it unchanged