Besides combining vectors through addition and subtraction, another fundamental operation is scaling a vector, which involves multiplying it by a single number. This number is called a scalar. In linear algebra, a scalar is simply a regular number (like 5, -2.7, or π), as opposed to a vector or a matrix.
Scalar multiplication changes the magnitude (length) of the vector and, if the scalar is negative, its direction. It's like stretching, shrinking, or flipping the vector.
To perform scalar multiplication, you multiply every element of the vector by the scalar value.
Mathematically, if you have a scalar c and a vector v with n elements:
v=v1v2⋮vnThen the scalar multiplication cv is defined as:
cv=cv1v2⋮vn=c⋅v1c⋅v2⋮c⋅vnEach component of the original vector v is multiplied by the scalar c. The resulting vector has the same dimension as the original vector.
Geometrically, multiplying a vector v by a scalar c results in a new vector that points in the same direction as v if c>0, and in the opposite direction if c<0. The length of the new vector is ∣c∣ times the length of the original vector v. If c=0, the result is the zero vector (a vector with all elements equal to zero).
Consider a 2D vector v=[21].
The original vector v (blue), scaled by 2 (green), and scaled by -1 (red). Scaling changes the length and potentially the direction.
NumPy makes scalar multiplication straightforward using the standard multiplication operator *
.
Let's define our vector v and a scalar c.
import numpy as np
# Define the vector v
v = np.array([2, 1])
# Define the scalar c
c = 3
# Perform scalar multiplication
scaled_v = c * v
print(f"Original vector v: {v}")
print(f"Scalar c: {c}")
print(f"Scaled vector c*v: {scaled_v}")
Output:
Original vector v: [2 1]
Scalar c: 3
Scaled vector c*v: [6 3]
As expected, NumPy multiplied each element of the vector v
by the scalar c=3
.
Let's try scaling with a negative scalar:
# Define a negative scalar
c_neg = -1.5
# Perform scalar multiplication
scaled_neg_v = c_neg * v
print(f"Original vector v: {v}")
print(f"Negative scalar c_neg: {c_neg}")
print(f"Scaled vector c_neg*v: {scaled_neg_v}")
Output:
Original vector v: [2 1]
Negative scalar c_neg: -1.5
Scaled vector c_neg*v: [-3. -1.5]
Again, each element [2, 1]
was multiplied by -1.5
to get [-3.0, -1.5]
. Notice that NumPy automatically handles the floating-point results.
Scalar multiplication is a basic building block used frequently in linear algebra and machine learning, often for adjusting the influence or scale of features represented by vectors.
© 2025 ApX Machine Learning