Scalar Multiplication of Matrices
Multiplying a matrix by a number (a scalar) multiplies every entry by that number. Together with addition, it lets us form linear combinations of matrices, and it follows the same distributive laws as scalar multiplication of vectors.
The definition
For a scalar and an matrix ,
Special cases: , (the zero matrix), and . Dividing by a non-zero number means multiplying by its reciprocal: .
This is exactly how scalars act on vectors in Scalar Multiplication. In fact an matrix behaves, for addition and scaling, just like a vector with components written in a grid.
The laws
For scalars and matrices of the same size:
| Law | Statement |
|---|---|
| Distributive over matrix addition | |
| Distributive over scalar addition | |
| Associative | |
| Identity | |
| Zero | and |
Each follows by looking at a single entry, for example .
Together with the laws of addition, these properties are the axioms of a vector space, which is why the set of all matrices is itself a vector space; see Definition of Vector Spaces.
Linear combinations of matrices
A linear combination of matrices is . Every matrix is a linear combination of the four "unit" matrices (a single 1, zeros elsewhere):
Exact arithmetic with fractions keeps these computations clean:
from sympy import Matrix, Rational
A = Matrix([[2, -1], [0, 4]])
B = Matrix([[1, 3], [-2, 5]])
print("3A =", (3 * A).tolist())
print("A/4 =", (A / 4).tolist())
print("2A - 3B =", (2 * A - 3 * B).tolist())
print("c(A+B)==cA+cB:", Rational(2, 3) * (A + B) == Rational(2, 3) * A + Rational(2, 3) * B)
E11, E12 = Matrix([[1, 0], [0, 0]]), Matrix([[0, 1], [0, 0]])
E21, E22 = Matrix([[0, 0], [1, 0]]), Matrix([[0, 0], [0, 1]])
M = Matrix([[7, -2], [5, 9]])
print("rebuilt from E's:", 7 * E11 - 2 * E12 + 5 * E21 + 9 * E22 == M)3A = [[6, -3], [0, 12]]
A/4 = [[1/2, -1/4], [0, 1]]
2A - 3B = [[1, -11], [6, -7]]
c(A+B)==cA+cB: True
rebuilt from E's: True
Solving equations with scalars
The laws let you rearrange matrix equations like number equations. For example gives and so .
from sympy import Matrix
A = Matrix([[1, 4], [-2, 0]])
B = Matrix([[1, -2], [4, 3]])
X = (A + 2 * B) / 3
print("X =", X.tolist())
print("check 3X - A == 2B:", 3 * X - A == 2 * B)X = [[1, 0], [2, 2]]
check 3X - A == 2B: True
In software
- Gradient descent: the weight update is a scalar multiple (learning rate times the gradient matrix ) followed by a subtraction. Every optimizer step in deep learning is built from these two operations (gradient descent).
- Blending and interpolation: blends two images, two heightmaps or two sets of weights (model "soups" average network weights). It is a linear combination with weights adding to 1.
- But blending rotation matrices is wrong: the average of two rotation matrices is generally not a rotation; it shrinks the object. Games interpolate rotations with quaternions instead (Slerp).
import numpy as np
def rot(deg):
t = np.radians(deg)
return np.array([[np.cos(t), -np.sin(t)], [np.sin(t), np.cos(t)]])
M = 0.5 * rot(0) + 0.5 * rot(120) # "halfway" between 0° and 120°
v = M @ np.array([1.0, 0.0])
print("length of rotated unit vector:", round(float(np.linalg.norm(v)), 4))
print("a real rotation keeps length 1:", round(float(np.linalg.norm(rot(60) @ [1, 0])), 4))length of rotated unit vector: 0.5
a real rotation keeps length 1: 1.0
(The @ operator is matrix multiplication, the topic of the next lesson.)
Exercises
Exercise 1: Scale and combine
With and , compute and .
Answer
; .
Solution
Exercise 2: Solve a matrix equation
Solve for , where and .
Answer
.
Solution
from sympy import Matrix
A = Matrix([[3, 0], [6, -3]]); B = Matrix([[0, 3], [3, 9]])
X = (A + B) / 3
print(X.tolist(), 2*X + A == 5*X - B)[[1, 1], [3, 2]] True
Exercise 3: Find the coefficients
Find scalars with .
Answer
, .
Solution
Compare entries: : ; : ; : ; : . So , , and both remaining equations check: , .
Exercise 4: One SGD step
A weight matrix has gradient . Compute the updated weights in NumPy and print them rounded to 2 decimals.
Answer
.
Solution
, and subtracting gives .
import numpy as np
W = np.array([[0.5, -0.2], [0.1, 0.8]])
G = np.array([[1.0, 0.0], [-2.0, 0.5]])
print(np.round(W - 0.1 * G, 2).tolist())[[0.4, -0.2], [0.3, 0.75]]
Test yourself
What is ?
Which law says ?
- Distributivity over scalar addition
- Commutativity of addition
- Associativity of scalar multiplication
- The zero law
Why is a bad way to get a rotation halfway between rotations and ?
- It is undefined for matrices
- It is too slow
- It rotates by the sum of the angles
- The result is usually not a rotation; it shrinks vectors