Math for AI course · Lesson 16 of 180
Scalar Multiplication for AI: Turn a Vector Up, Down, or Around
Scale vectors coordinate by coordinate, predict direction and magnitude, and connect the same operation to embeddings, learning rates, and NumPy.
A direction vector can say where a model update should go without saying how large the move should be. A single number supplies that second decision. If the direction is , multiplying it by produces the shorter move . Multiplying by keeps its length but points it the other way.
That number is a scalar: one value applied to every coordinate. By the end of this lesson, you will be able to calculate scalar–vector products, predict their direction and magnitude from the scalar’s sign and absolute value, implement them correctly in NumPy, and recognize the same mechanism inside a learning-rate-scaled update or weighted embedding contribution.
Learning outcome: Scale a vector and predict changes to direction and magnitude.
Lesson 15: Vector Addition for AI established that vectors combine by adding matching coordinates. Scalar multiplication prepares one of those vectors before the addition: it controls how strongly the vector contributes. The official Mathematics for Machine Learning companion introduces multiplication by a scalar in Section 2.2 of the book. The examples, code, interactive ray, AI applications, and assessments below are original.
One number reaches every coordinate
Let be a real vector with coordinates , and let be a real scalar. The Greek letter (alpha) is only a name for one number; symbols such as , , or could serve the same role.
The operation preserves the vector’s dimension and coordinate order. A scalar does not append a coordinate, choose one coordinate, or mix neighboring coordinates. It changes all values through the same multiplication rule.
A negative scale reverses every nonzero coordinate
Take an update vector
and choose . The symbol names the whole three-coordinate update. Work from the definition rather than trying to infer the answer from the minus sign alone.
This calculation separates two effects. The absolute value—the scalar’s distance from zero—is , so the vector becomes 1.5 times as large. The negative sign reverses its direction. Neither effect changes which feature or parameter each coordinate represents.
The sign chooses the ray; the absolute value chooses the reach
In two dimensions, draw as an arrow from the origin. Every nonzero scalar multiple lies on the same infinite line through that arrow. Positive scalars occupy the same ray, negative scalars occupy the opposite ray, and zero collapses the arrow to the origin.
For this lesson, magnitude means Euclidean arrow length. For , define
The subscript 2 distinguishes this length rule from other magnitude rules that a later lesson will examine. Scalar multiplication changes this magnitude by , not by :
The absolute value is essential because magnitude cannot be negative. The sign belongs to direction; the absolute value belongs to size.
| Scalar | Direction of for | Magnitude |
|---|---|---|
| Same direction | Longer by factor | |
| Same direction | Unchanged | |
| Same direction | Shorter by factor | |
| No direction: the result is | Zero | |
| Opposite direction | Shorter by factor | |
| Opposite direction | Unchanged | |
| Opposite direction | Longer by factor |
Drag the scalar below across all seven cases. The fixed vector is , whose magnitude is . The scaled arrow, status sentence, and table all begin with the server-rendered state and update together after the component becomes visible.
Line style and text distinguish the arrows without relying on color. At α = 0, a dashed ring at the origin replaces the scaled arrow because the zero vector has no direction.
| Vector | Scale | First coordinate | Second coordinate | Euclidean magnitude | Direction relative to v |
|---|---|---|---|---|---|
| Base vector v — solid arrow | +1 | +4 | -3 | 5 | reference direction |
| Scaled vector αv — dashed arrow | +1.5 | +6 | -4.5 | 7.5 | the same as v |
The picture is only two-dimensional, but the rule is not. In a 768-coordinate embedding, one scalar still multiplies all 768 coordinates, and the magnitude still changes by under the Euclidean length rule.
Scaling before addition controls a contribution
Scalar multiplication becomes especially useful beside vector addition. Suppose a model has a context state
and a candidate embedding contribution
With weight , scale first and then add:
The weight changes how strongly contributes to this sum. A weight of removes that contribution; a negative weight contributes in the opposite direction. This is a precise arithmetic meaning of “embedding strength.” It does not guarantee that a complete neural network’s output will change in direct proportion to : later transformations, normalization, and nonlinear operations can alter the downstream effect.
The distributive rule explains why scaling a combined signal can be calculated before or after its parts are added:
At coordinate , both sides equal . Because every coordinate agrees, the vectors agree. The companion rule will let the next lesson combine several weighted vectors efficiently.
A learning rate scales an optimizer’s proposed direction
Consider the simplest stochastic gradient descent (SGD) step, without momentum or weight decay. Let be a parameter vector and a gradient vector that points toward increasing loss. A positive learning rate scales the direction before subtraction:
The calculus needed to compute comes later in the course; for now, it is a supplied vector whose scaling can be inspected directly.
This is scalar multiplication followed by the vector addition . The current PyTorch SGD documentation expresses its final parameter step in this form, after any configured weight decay, momentum, or Nesterov modifications to the update direction. The narrow claim matters: with those options enabled, the learning rate can scale a modified direction rather than the untouched raw gradient.
For a small numerical step, let
Then
Replacing with would double this one proposed update’s magnitude while preserving its direction. That arithmetic fact alone does not say whether the larger learning rate improves training or remains stable; those questions depend on the loss surface and optimizer behavior and belong to the later optimization module.
NumPy multiplies arrays; Python lists repeat
NumPy’s multiply
multiplies arguments elementwise, and * is its operator shorthand for
arrays. NumPy’s broadcasting
rules let one
scalar act across the array without constructing a full array of repeated
scalars.
import numpy as np
vector = np.array([4.0, -3.0, 2.0])
alpha = -1.5
scaled_with_operator = alpha * vector
scaled_with_function = np.multiply(alpha, vector)
print("vector shape:", vector.shape)
print("operator result:", scaled_with_operator)
print("function result:", scaled_with_function)
print("same values:", np.array_equal(
scaled_with_operator, scaled_with_function
))
The verified output is
vector shape: (3,)
operator result: [-6. 4.5 -3. ]
function result: [-6. 4.5 -3. ]
same values: True
A plain Python list changes the meaning of *. The Python sequence
documentation
defines multiplication by an integer as repetition:
coordinates = [4.0, -3.0, 2.0]
print(2 * coordinates)
print(2 * np.array(coordinates))
[4.0, -3.0, 2.0, 4.0, -3.0, 2.0]
[ 8. -6. 4.]
The first result has six entries and is not . Convert numeric
coordinates to a NumPy array before using array arithmetic. A non-integer such
as 0.5 * coordinates raises a TypeError rather than scaling the entries.
The scalar has now earned two jobs: its sign chooses whether a nonzero vector keeps or reverses direction, and its absolute value controls magnitude. Once several such scaled vectors are added, scalar multiplication and vector addition become a linear combination—the construction that will power the next lesson.
Check your understanding
Question 1
Let v = [−2, 5, 1]ᵀ and α = 3. Compute αv coordinate by coordinate, then state its dimension and direction relative to v.
Show the step-by-step solution
Apply the same scalar to each of the three coordinates:
Therefore
The dimension stays three. Because is positive, the result points in the same direction as the nonzero original vector; its magnitude is three times the original magnitude.
Question 2
For v = [6, 8]ᵀ, calculate −0.5v and compare its Euclidean magnitude and direction with v.
Show the step-by-step solution
First scale the coordinates:
The original magnitude is . The result has
Its magnitude is half as large because . Its direction is opposite because the scalar is negative.
Question 3
A student says 0v points in the same direction as v because zero is not negative. Diagnose the claim and calculate 0v for v = [7, −2]ᵀ.
Show the step-by-step solution
Coordinatewise multiplication gives
The sign rule splits into three cases, not two: positive, negative, and zero. Positive scalars preserve the direction and negative scalars reverse it. Zero collapses every coordinate to the zero vector, whose arrow has no length and therefore no direction. The student’s reasoning incorrectly treats zero as a positive scalar.
Question 4
Starting from the Euclidean magnitude formula, show why ‖αv‖₂ uses |α| rather than α for v = [v₁, v₂]ᵀ.
Show the step-by-step solution
Substitute the two scaled coordinates into the magnitude definition:
For a real scalar, . Writing without the absolute value would incorrectly give a negative magnitude whenever .
Question 5
A context is c = [2, 0, −1]ᵀ and an embedding contribution is e = [−2, 4, 2]ᵀ. With α = 0.25, compute c + αe and explain exactly what α controls.
Show the step-by-step solution
Scale the contribution before adding it:
Then add matching coordinates:
The scalar makes this embedding vector’s contribution one quarter of its original magnitude while preserving its direction. It controls the arithmetic weight in this sum; it does not by itself guarantee a proportional change in the output of every later model layer.
Question 6
For θ = [3, −1]ᵀ, gradient g = [2, 4]ᵀ, and learning rate η = 0.1, compute the simple SGD update θnext = θ − ηg in three explicit steps.
Show the step-by-step solution
First scale the gradient:
Second, reverse that scaled direction for a descent step:
Third, add the update to the parameters:
This calculation assumes the plain update shown in the prompt. Momentum, weight decay, or another optimizer rule can modify the direction before the learning-rate-scaled parameter step.
Question 7
Why does 3 * [1, −2] fail to compute the scalar multiple [3, −6] in Python, and what NumPy expression performs the intended operation?
Show the step-by-step solution
[1, -2] is a Python list, which is a sequence. Multiplication by the integer
3 repeats that sequence three times:
3 * [1, -2]
# [1, -2, 1, -2, 1, -2]The result has six entries, so it cannot equal the two-coordinate vector . Create a numeric NumPy array, then multiply:
import numpy as np
3 * np.array([1, -2])
# array([ 3, -6])NumPy applies 3 elementwise through scalar broadcasting, which matches the
mathematical definition.
Question 8
Let v = [2, −1]ᵀ. Verify (2 + (−0.5))v = 2v + (−0.5)v by calculating both sides, then name the property.
Show the step-by-step solution
On the left, add the scalars first:
On the right, scale twice and then add:
Both paths produce the same vector. This is distributivity of scalar addition over scalar multiplication: .