Loading content...
In linear algebra, scalar multiplication of a matrix is a fundamental operation where every element of a matrix is multiplied by a single constant value (called a scalar). This operation uniformly scales all entries of the matrix, effectively stretching or shrinking the values by the same factor.
Given a matrix A of dimensions n × m (where n is the number of rows and m is the number of columns) and a scalar k, the scaled matrix B is computed such that each element Bᵢⱼ is:
$$B_{ij} = k \cdot A_{ij}$$
This operation preserves the structure and dimensions of the original matrix while transforming its values proportionally.
Key Properties of Scalar Multiplication:
Your Task: Write a Python function that takes a matrix and a scalar value, then returns a new matrix where every element has been multiplied by the scalar. The function should handle matrices of any valid dimensions and work correctly with both positive and negative values, including integers and floating-point numbers.
matrix = [[1, 2], [3, 4]]
scalar = 2[[2, 4], [6, 8]]Each element of the 2×2 matrix is multiplied by the scalar 2:
• Position (0,0): 1 × 2 = 2 • Position (0,1): 2 × 2 = 4 • Position (1,0): 3 × 2 = 6 • Position (1,1): 4 × 2 = 8
The resulting scaled matrix is [[2, 4], [6, 8]].
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
scalar = 3[[3, 6, 9], [12, 15, 18], [21, 24, 27]]Each element of the 3×3 matrix is multiplied by the scalar 3:
• Row 0: [1×3, 2×3, 3×3] = [3, 6, 9] • Row 1: [4×3, 5×3, 6×3] = [12, 15, 18] • Row 2: [7×3, 8×3, 9×3] = [21, 24, 27]
The resulting scaled matrix maintains the 3×3 structure with all values tripled.
matrix = [[-1, 2], [3, -4]]
scalar = 5[[-5, 10], [15, -20]]Scalar multiplication works correctly with negative values, preserving their sign:
• Position (0,0): -1 × 5 = -5 (negative remains negative) • Position (0,1): 2 × 5 = 10 (positive remains positive) • Position (1,0): 3 × 5 = 15 (positive remains positive) • Position (1,1): -4 × 5 = -20 (negative remains negative)
The scaling operation uniformly magnifies all values while maintaining their original signs.
Constraints