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.

Share this article

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 [4,−3]T[4,-3]^\mathsf{T}, multiplying it by 0.250.25 produces the shorter move [1,−0.75]T[1,-0.75]^\mathsf{T}. Multiplying by −1-1 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 v∈Rn\boldsymbol{v}\in\mathbb{R}^{n} be a real vector with coordinates v1,v2,…,vnv_1,v_2,\ldots,v_n, and let α∈R\alpha\in\mathbb{R} be a real scalar. The Greek letter α\alpha (alpha) is only a name for one number; symbols such as cc, ss, or λ\lambda 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 nn values through the same multiplication rule.

A negative scale reverses every nonzero coordinate

Take an update vector

u=[4−32]\boldsymbol{u}= \begin{bmatrix}4\\-3\\2\end{bmatrix}

and choose α=−1.5\alpha=-1.5. The symbol u\boldsymbol{u} 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 ∣α∣=1.5|\alpha|=1.5, 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 v\boldsymbol{v} 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 v=[v1,…,vn]T\boldsymbol{v}=[v_1,\ldots,v_n]^\mathsf{T}, define

∥v∥2=v12+⋯+vn2.\lVert\boldsymbol{v}\rVert_2 =\sqrt{v_1^2+\cdots+v_n^2}.

The subscript 2 distinguishes this length rule from other magnitude rules that a later lesson will examine. Scalar multiplication changes this magnitude by ∣α∣|\alpha|, not by α\alpha:

∥αv∥2=(αv1)2+⋯+(αvn)2=α2(v12+⋯+vn2)=∣α∣v12+⋯+vn2=∣α∣∥v∥2.\begin{aligned} \lVert\alpha\boldsymbol{v}\rVert_2 &=\sqrt{(\alpha v_1)^2+\cdots+(\alpha v_n)^2}\\ &=\sqrt{\alpha^2(v_1^2+\cdots+v_n^2)}\\ &=|\alpha|\sqrt{v_1^2+\cdots+v_n^2}\\ &=|\alpha|\lVert\boldsymbol{v}\rVert_2. \end{aligned}

The absolute value is essential because magnitude cannot be negative. The sign belongs to direction; the absolute value belongs to size.

Scalar α\alphaDirection of αv\alpha\boldsymbol{v} for v≠0\boldsymbol{v}\ne\boldsymbol{0}Magnitude
α>1\alpha>1Same directionLonger by factor α\alpha
α=1\alpha=1Same directionUnchanged
0<α<10<\alpha<1Same directionShorter by factor α\alpha
α=0\alpha=0No direction: the result is 0\boldsymbol{0}Zero
−1<α<0-1<\alpha<0Opposite directionShorter by factor ∣α∣\lvert\alpha\rvert
α=−1\alpha=-1Opposite directionUnchanged
α<−1\alpha<-1Opposite directionLonger by factor ∣α∣\lvert\alpha\rvert

Drag the scalar below across all seven cases. The fixed vector is v=[4,−3]T\boldsymbol{v}=[4,-3]^\mathsf{T}, whose magnitude is 55. The scaled arrow, status sentence, and table all begin with the server-rendered state α=1.5\alpha=1.5 and update together after the component becomes visible.

Interactive scalar multiplication: move one scalar and compare a fixed vector with its scaled result.
Choose the scalar that multiplies v

The range runs from −2 to 2 in steps of 0.25. Use the arrow keys for one step, or Page Up and Page Down for larger changes.

Current result: α = +1.5, v = (+4, -3), and αv = (+6, -4.5). Its magnitude is 7.5; its direction is the same as v.

Base vector v — solid arrowScaled vector αv — dashed arrow

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.

Numeric alternative for the current scalar and arrows
VectorScaleFirst coordinateSecond coordinateEuclidean magnitudeDirection relative to v
Base vector v — solid arrow+1+4-35reference direction
Scaled vector αv — dashed arrow+1.5+6-4.57.5the 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 ∣α∣|\alpha| 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

c=[1−12]\boldsymbol{c}=\begin{bmatrix}1\\-1\\2\end{bmatrix}

and a candidate embedding contribution

e=[21−1].\boldsymbol{e}=\begin{bmatrix}2\\1\\-1\end{bmatrix}.

With weight α=0.5\alpha=0.5, scale first and then add:

αe=0.5[21−1]=[10.5−0.5],c+αe=[1−12]+[10.5−0.5]=[2−0.51.5].\begin{aligned} \alpha\boldsymbol{e} &=0.5\begin{bmatrix}2\\1\\-1\end{bmatrix} =\begin{bmatrix}1\\0.5\\-0.5\end{bmatrix},\\ \boldsymbol{c}+\alpha\boldsymbol{e} &=\begin{bmatrix}1\\-1\\2\end{bmatrix} +\begin{bmatrix}1\\0.5\\-0.5\end{bmatrix} =\begin{bmatrix}2\\-0.5\\1.5\end{bmatrix}. \end{aligned}

The weight changes how strongly e\boldsymbol{e} contributes to this sum. A weight of 00 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 α\alpha: 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:

α(v+w)=αv+αw.\alpha(\boldsymbol{v}+\boldsymbol{w}) =\alpha\boldsymbol{v}+\alpha\boldsymbol{w}.

At coordinate jj, both sides equal α(vj+wj)=αvj+αwj\alpha(v_j+w_j)=\alpha v_j+\alpha w_j. Because every coordinate agrees, the vectors agree. The companion rule (α+β)v=αv+βv(\alpha+\beta)\boldsymbol{v}=\alpha\boldsymbol{v}+\beta\boldsymbol{v} 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 θ\boldsymbol{\theta} be a parameter vector and g\boldsymbol{g} a gradient vector that points toward increasing loss. A positive learning rate η\eta scales the direction before subtraction:

θnext=θ−ηg.\boldsymbol{\theta}_{\text{next}} =\boldsymbol{\theta}-\eta\boldsymbol{g}.

The calculus needed to compute g\boldsymbol{g} 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 θ+(−ηg)\boldsymbol{\theta}+(-\eta\boldsymbol{g}). 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

θ=[21],g=[4−3],η=0.25.\boldsymbol{\theta}=\begin{bmatrix}2\\1\end{bmatrix},\qquad \boldsymbol{g}=\begin{bmatrix}4\\-3\end{bmatrix},\qquad \eta=0.25.

Then

ηg=0.25[4−3]=[1−0.75],−ηg=[−10.75],θnext=[21]+[−10.75]=[11.75].\begin{aligned} \eta\boldsymbol{g} &=0.25\begin{bmatrix}4\\-3\end{bmatrix} =\begin{bmatrix}1\\-0.75\end{bmatrix},\\ -\eta\boldsymbol{g} &=\begin{bmatrix}-1\\0.75\end{bmatrix},\\ \boldsymbol{\theta}_{\text{next}} &=\begin{bmatrix}2\\1\end{bmatrix} +\begin{bmatrix}-1\\0.75\end{bmatrix} =\begin{bmatrix}1\\1.75\end{bmatrix}. \end{aligned}

Replacing η=0.25\eta=0.25 with η=0.5\eta=0.5 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 2v2\boldsymbol{v}. 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:

(3v)1=3(−2)=−6,(3v)2=3(5)=15,(3v)3=3(1)=3.\begin{aligned} (3\boldsymbol{v})_1&=3(-2)=-6,\\ (3\boldsymbol{v})_2&=3(5)=15,\\ (3\boldsymbol{v})_3&=3(1)=3. \end{aligned}

Therefore

3v=[−6153]∈R3.3\boldsymbol{v}=\begin{bmatrix}-6\\15\\3\end{bmatrix}\in\mathbb{R}^{3}.

The dimension stays three. Because 33 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:

−0.5v=[(−0.5)(6)(−0.5)(8)]=[−3−4].-0.5\boldsymbol{v} =\begin{bmatrix}(-0.5)(6)\\(-0.5)(8)\end{bmatrix} =\begin{bmatrix}-3\\-4\end{bmatrix}.

The original magnitude is ∥v∥2=62+82=100=10\lVert\boldsymbol{v}\rVert_2=\sqrt{6^2+8^2}=\sqrt{100}=10. The result has

∥−0.5v∥2=∣−0.5∣ 10=5.\lVert-0.5\boldsymbol{v}\rVert_2 =|-0.5|\,10=5.

Its magnitude is half as large because ∣−0.5∣=0.5|-0.5|=0.5. 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

0v=[0(7)0(−2)]=[00]=0.0\boldsymbol{v} =\begin{bmatrix}0(7)\\0(-2)\end{bmatrix} =\begin{bmatrix}0\\0\end{bmatrix} =\boldsymbol{0}.

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:

∥αv∥2=(αv1)2+(αv2)2=α2v12+α2v22=α2(v12+v22)=α2v12+v22=∣α∣∥v∥2.\begin{aligned} \lVert\alpha\boldsymbol{v}\rVert_2 &=\sqrt{(\alpha v_1)^2+(\alpha v_2)^2}\\ &=\sqrt{\alpha^2v_1^2+\alpha^2v_2^2}\\ &=\sqrt{\alpha^2(v_1^2+v_2^2)}\\ &=\sqrt{\alpha^2}\sqrt{v_1^2+v_2^2}\\ &=|\alpha|\lVert\boldsymbol{v}\rVert_2. \end{aligned}

For a real scalar, α2=∣α∣\sqrt{\alpha^2}=|\alpha|. Writing α\alpha without the absolute value would incorrectly give a negative magnitude whenever α<0\alpha<0.

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:

αe=0.25[−242]=[−0.510.5].\alpha\boldsymbol{e} =0.25\begin{bmatrix}-2\\4\\2\end{bmatrix} =\begin{bmatrix}-0.5\\1\\0.5\end{bmatrix}.

Then add matching coordinates:

c+αe=[2−0.50+1−1+0.5]=[1.51−0.5].\boldsymbol{c}+\alpha\boldsymbol{e} =\begin{bmatrix}2-0.5\\0+1\\-1+0.5\end{bmatrix} =\begin{bmatrix}1.5\\1\\-0.5\end{bmatrix}.

The scalar 0.250.25 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:

ηg=0.1[24]=[0.20.4].\eta\boldsymbol{g} =0.1\begin{bmatrix}2\\4\end{bmatrix} =\begin{bmatrix}0.2\\0.4\end{bmatrix}.

Second, reverse that scaled direction for a descent step:

−ηg=[−0.2−0.4].-\eta\boldsymbol{g}=\begin{bmatrix}-0.2\\-0.4\end{bmatrix}.

Third, add the update to the parameters:

θnext=[3−1]+[−0.2−0.4]=[2.8−1.4].\boldsymbol{\theta}_{\text{next}} =\begin{bmatrix}3\\-1\end{bmatrix} +\begin{bmatrix}-0.2\\-0.4\end{bmatrix} =\begin{bmatrix}2.8\\-1.4\end{bmatrix}.

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 [3,−6]T[3,-6]^\mathsf{T}. 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:

(2+(−0.5))v=1.5[2−1]=[3−1.5].(2+(-0.5))\boldsymbol{v} =1.5\begin{bmatrix}2\\-1\end{bmatrix} =\begin{bmatrix}3\\-1.5\end{bmatrix}.

On the right, scale twice and then add:

2v+(−0.5)v=[4−2]+[−10.5]=[3−1.5].\begin{aligned} 2\boldsymbol{v}+(-0.5)\boldsymbol{v} &=\begin{bmatrix}4\\-2\end{bmatrix} +\begin{bmatrix}-1\\0.5\end{bmatrix}\\ &=\begin{bmatrix}3\\-1.5\end{bmatrix}. \end{aligned}

Both paths produce the same vector. This is distributivity of scalar addition over scalar multiplication: (α+β)v=αv+βv(\alpha+\beta)\boldsymbol{v}=\alpha\boldsymbol{v}+\beta\boldsymbol{v}.

Sources

  1. Mathematics for Machine Learning companion website
  2. Mathematics for Machine Learning book PDF
  3. NumPy documentation: numpy.multiply
  4. NumPy documentation: broadcasting
  5. Python documentation: common sequence operations
  6. PyTorch documentation: stochastic gradient descent