Matrix Multiplication - 2
Let’s dive deeper into matrix multiplication 2 in Python! 🧮✨ We’ll cover both the step-by-step logic and a modular, function-based approach—with practice and solutions! 🚀
🟢 What is Matrix Multiplication?
- Multiply Matrix A (size m × n) with Matrix B (size n × p).
- The result is a new matrix of size m × p.
- Each element in the result is the dot product of a row from A and a column from B.
🟡 Modular Matrix Multiplication Using Functions
A clean way to multiply matrices is to use helper functions for:
- Getting a row from a matrix
- Getting a column from a matrix
- Calculating the dot product of two lists
Let’s see this in action! 👇
Step 1: Helper Functions
# Get the ith row
def row(M, i):
return M[i]
# Get the jth column
def column(M, j):
return [M[k][j] for k in range(len(M))]
# Dot product of two lists
def dot(u, v):
return sum(u[k] * v[k] for k in range(len(u)))
Step 2: Matrix Multiplication Function
def matmul(A, B):
n = len(A) # assuming square matrices of size n x n
C = []
for i in range(n):
row_result = []
for j in range(n):
rowA = row(A, i)
colB = column(B, j)
row_result.append(dot(rowA, colB))
C.append(row_result)
return C
Step 3: Example Usage
A = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
B = [
[1, 2, 1],
[3, 1, 7],
[6, 2, 3]
]
result = matmul(A, B)
for r in result:
print(r)
# Output:
# [25, 12, 28]
# [55, 27, 64]
# [85, 42, 100]
How does it work?
- For each cell in the result, we take the corresponding row from A and column from B, compute their dot product, and store it.
🟠 Practice Question
Q: Multiply the following 2x2 matrices using the modular function approach:
X = [
[2, 4],
[3, 4]
]
Y = [
[1, 2],
[1, 3]
]
Solution
def row(M, i):
return M[i]
def column(M, j):
return [M[k][j] for k in range(len(M))]
def dot(u, v):
return sum(u[k] * v[k] for k in range(len(u)))
def matmul(A, B):
n = len(A)
C = []
for i in range(n):
row_result = []
for j in range(n):
rowA = row(A, i)
colB = column(B, j)
row_result.append(dot(rowA, colB))
C.append(row_result)
return C
X = [
[2, 4],
[3, 4]
]
Y = [
[1, 2],
[1, 3]
]
result = matmul(X, Y)
for r in result:
print(r)
# Output:
# [6, 16]
# [7, 18]
🔵 Key Points
- Matrix multiplication can be broken into smaller functions: row, column, dot product.
- This modular approach makes your code clear and easy to debug! 🛠️
- Always check that the number of columns in the first matrix equals the number of rows in the second.
Try it with your own matrices and see the magic! ✨
If you want to multiply non-square matrices, just adjust the loops to use len(A)
for rows and len(B)
for columns1.
References:
- Python-IITM-Foundational-Course.pdf (modular matrix multiplication using functions)1
- Python-Cheatsheet-2024.pdf (step-by-step multiplication logic)2
⁂