Matrix Multiplication
Implement matrix multiplication for two matrices A and B. This operation is crucial in linear algebra and computer vision, as it represents the composition of linear transformations.
Given matrices A of size m×n and B of size n×p, the product C=A×B is an m×p matrix.
- Initialize the result matrix C with zeros.
- For each element Cij​, compute the dot product of row i from A and column j from B. The resulting matrix C represents the composition of the transformations represented by A and B.
This technique is widely used in image processing and machine learning applications.
Example:
A = [[1, 2], [3, 4]] B = [[5, 6], [7, 8]]
[[19, 22], [43, 50]]
Step-by-step calculation using the matrix multiplication formula:
Cij​=∑k=1n​Aik​×Bkj​
Matrix A (2×2) × Matrix B (2×2) = Result C (2×2)
-
C00​: Row 0 of A · Column 0 of B [1,2]⋅[5,7]=(1×5)+(2×7)=5+14=19
-
C01​: Row 0 of A · Column 1 of B [1,2]⋅[6,8]=(1×6)+(2×8)=6+16=22
-
C10​: Row 1 of A · Column 0 of B [3,4]⋅[5,7]=(3×5)+(4×7)=15+28=43
-
C11​: Row 1 of A · Column 1 of B [3,4]⋅[6,8]=(3×6)+(4×8)=18+32=50
-
Final result: C=(1943​2250​)
Constraints:
- Matrix A has dimensions m×n, Matrix B has dimensions n×p
- 1 ≤ m, n, p ≤ 100
- Elements are floating-point numbers
- Round each element of the result to 4 decimal places
Matrix multiplication combines two matrices by taking dot products of rows of A with columns of B. For A of size m×n and B of size n×p, the result C is an m×p matrix where each entry Cij​ is the sum of elementwise products along the shared dimension n:
Cij​=k=1∑n​Aik​⋅Bkj​.Conceptually, each row of A represents coefficients, and each column of B represents a vector being combined by those coefficients.
In computer vision and linear algebra, matrix multiplication encodes linear transformations (like rotation, scaling, projection). Applying a transformation to a vector is just multiplying a matrix by that vector. Composing multiple transformations (e.g., rotate then translate in homogeneous coordinates) corresponds to multiplying their matrices in sequence, which is why understanding the order and shape rules of matrix multiplication is important.
1. Background Knowledge (Key Concepts)
-
Shape compatibility
-
A: m×n
-
B: n×p
-
C=AB: m×p
-
The inner dimensions must match (both equal n). The outer dimensions give the result’s shape.
-
Entry-wise definition For each output entry:
-
Index i: which row of A / C
-
Index j: which column of B / C
-
Index k: runs along the shared dimension (columns of A, rows of B).
-
Interpretation as dot products
-
Cij​=\text{dot}(\text{row }i\text{ of }A,\text{column }j\text{ of }B).
2. Algorithm / Approach
For this easy-level problem, you use the classical triple-loop algorithm:
- Initialize an m×p result matrix C with zeros.
- For each row i of A:
- For each column j of B:
- Compute Cij​ by looping over k=0…n−1 and accumulating:
This is the standard dense matrix multiplication used in many implementations when dimensions are small or moderate.
3. Step-by-Step Strategy
Assume inputs are 2D arrays (e.g., lists of lists in Python or vectors of vectors in C++).
- Read dimensions
- Let A be size m x n.
- Let B be size n x p.
- Optionally, assert/check that A’s column count equals B’s row count.
- Create result matrix
- Allocate C with size m x p.
- Initialize all entries to 0 (or the numeric zero of your type).
- Nested loops
- Outer loop over rows of A:
for i in range(m): # row index in A and C
- Middle loop over columns of B:
for j in range(p): # column index in B and C
- Inner loop over shared dimension n to accumulate the dot product:
for k in range(n): # shared dimension
C[i][j] += A[i][k] * B[k][j]
- Return or print C in the format the problem expects.
Tiny Python-like sketch (for intuition, not a full solution):
def matmul(A, B):
m, n = len(A), len(A)
n2, p = len(B), len(B)
# assume n == n2
C = [[0 for _ in range(p)] for _ in range(m)]
for i in range(m):
for j in range(p):
s = 0
for k in range(n):
s += A[i][k] * B[k][j]
C[i][j] = s
return C
4. Common Pitfalls
-
Dimension mismatch
-
Forgetting to check A_cols == B_rows before multiplying.
-
Mixing up m, n, p and indexing incorrectly.
-
Index order mistakes
-
Using B[j][k] instead of B[k][j] in the inner loop.
-
Writing to C[k][j] instead of C[i][j].
-
Incorrect initialization
-
Not resetting the accumulator s to 0 for each new (i, j).
-
Not initializing C to zeros, causing garbage values to be added.
-
Off-by-one errors
-
Using <= instead of < in loops.
-
Confusing 0-based indices (common in code) with 1-based notation in math.
5. Time & Space Complexity
-
Time complexity
-
Three nested loops over:
-
i=1…m
-
j=1…p
-
k=1…n
-
Total operations: O(mâ‹…nâ‹…p).
-
Space complexity
-
Input storage: O(mn) for A, O(np) for B.
-
Output storage: O(mp) for C.
-
Extra (auxiliary) space beyond inputs and output: O(1) (just a few scalars like i, j, k, s).